5

In the wikipedia article, it provides some template classes. I want to use it in actual code. How can I do that? I found there is almost no way for me to instantiate a Vec object.

Xeo
  • 129,499
  • 52
  • 291
  • 397

1 Answers1

2

The Wikibooks article on Expression Templates provides more insight, especially the last part:

The above example does not show how recursive types are generated at compile-time. Also, expr does not look like a mathematical expression at all, but it is indeed one. The code that follows show how types are recursively composed using repetitive instantiation of the following 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 above overloaded operator+ does two things - it adds syntactic sugar and enables recursive type composition, bounded by the compiler's limits. It can therefore be used to replace the call to evaluate as follows:

evaluate (a.begin(), a.end(), x + l + x); 
/// It is (2*x + 50.00), which does look like a mathematical expression.
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • is it possible for you provide a running example for the code in the http://en.wikipedia.org/wiki/Expression_templates? How to make it work in this case? Thank you. –  Dec 13 '11 at 23:53