How to use the Expression template

In a wikipedia article , it provides some template classes. I want to use it in real code. How can i do this? I found that there is almost no way to create an object Vec.

+5
source share
1 answer

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); 
/// It is (2*x + 50.00), which does look like a mathematical expression.
+2
source

All Articles