: Expression Templates is a new C++ technique for passing expressions as function arguments. The expression can be inlined into the function body, which results in faster and more convenient code than C-style callback functions. This technique can also be used to evaluate vector and matrix expressions in a single pass without temporaries. In preliminary benchmark results, one compiler evaluates vector expressions at 95-99.5% efficiency of hand-coded C using this technique (for long vectors). The speed is 2-15 times that of a conventional C++ vector class. Introduction Passing an expression to a function is a common occurrence. In C, expressions are usually passed using a pointer to a callback function containing the expression. For example, the standard C library routines qsort(), lsearch(), and bsearch() accept an argument int (*cmp)(void*, void*) which points to a user-defined function to compare two elements. Another common example is passing mathematical expressions to functions. ...

...ssion) are preserved. The idea of applicative template classes has been borrowed from the Standard Template Library (STL) developed by Alexander Stepanov and Meng Lee, of Hewlett Packard Laboratories =-=[2]-=-. The STL has been accepted as part of the ISO/ANSI Standard C++ Library. In the STL, an applicative template class provides an inline operator() which applies an operation to its arguments and return...

...), a(1000), b(1000), c(1000), d(1000); y = (a+b)/(c-d); Until now, this level of abstraction has come at a high cost, since vector and matrix operators are usually implemented using temporary vectors =-=[3]-=-. Evaluating the above expression using a conventional class library generates code equivalent to: DoubleVec __t1 = a+b; DoubleVec __t2 = c-d; DoubleVec __t3 = __t1/__t2; y = __t3; Each line in the co...

... eqn in terms of element # In each of these examples, the compiler produces a function instance which contains the expression inline. The technique has a similar effect to Jensen's Device in ALGOL 60 =-=[1]-=-. Expression templates also solve an important problem in the design of vector and matrix class libraries. The technique allows developers to write code such as Vector y(100), a(100), b(100), c(100), ...