Wikibooks article on expression patterns provides more information, especially the last part:
The above example does not show how recursive types are generated at compile time. In addition, the expression does not look like a mathematical expression at all, but it really is one. The following code shows how types are recursively composed using a repeating instance of the next overloaded + operator.
template< class A, class B >
DExpression<DBinaryExpression<DExpression<A>, DExpression<B>, Add> >
operator + (DExpression<A> a, DExpression<B> b)
{
typedef DBinaryExpression <DExpression<A>, DExpression<B>, Add> ExprT;
return DExpression<ExprT>(ExprT(a,b));
}
The operator + overloaded above does two things: it adds syntactic sugar and allows a recursive composition type limited to the compiler limits. Therefore, it can be used to replace the call for evaluation as follows:
evaluate (a.begin(), a.end(), x + l + x);
source
share