Hubbry Logo
search
logo

Expression templates

logo
Community Hub0 Subscribers
Write something...
Be the first to start a discussion here.
Be the first to start a discussion here.
See all
Expression templates

Expression templates are a C++ template metaprogramming technique that builds structures representing a computation at compile time, where expressions are evaluated only as needed to produce efficient code for the entire computation. Expression templates thus allow programmers to bypass the normal order of evaluation of the C++ language and achieve optimizations such as loop fusion.

Expression templates were invented independently by Todd Veldhuizen and David Vandevoorde; it was Veldhuizen who gave them their name. They are a popular technique for the implementation of linear algebra software.

Consider a library representing vectors and operations on them. One common mathematical operation is to add two vectors u and v, element-wise, to produce a new vector. The obvious C++ implementation of this operation would be an overloaded operator+ that returns a new vector object:

Users of this class can now write Vec3 x = a + b; where a and b are both instances of Vec3.

A problem with this approach is that more complicated expressions such as Vec3 x = a + b + c are implemented inefficiently. The implementation first produces a temporary Vec3 to hold a + b, then produces another Vec3 with the elements of c added in. Even with return value optimization this will allocate memory at least twice and require two loops.

Delayed evaluation solves this problem, and can be implemented in C++ by letting operator+ return an object of an auxiliary type, say Vec3Sum, that represents the unevaluated sum of two Vec3s, or a vector with a Vec3Sum, etc. Larger expressions then effectively build expression trees that are evaluated only when assigned to an actual Vec3 variable. But this requires traversing such trees to do the evaluation, which is in itself costly.

Expression templates implement delayed evaluation using expression trees that only exist at compile time. Each assignment to a Vec3, such as Vec3 x = a + b + c, generates a new Vec3 constructor if needed by template instantiation. This constructor operates on three Vec3; it allocates the necessary memory and then performs the computation. Thus only one memory allocation is performed.

An example implementation of expression templates looks like the following. A base class Vec3Expression represents any vector-valued expression. It is templated on the actual expression type E to be implemented, per the curiously recurring template pattern. The existence of a base class like VecExpression is not strictly necessary for expression templates to work. It will merely serve as a function argument type to distinguish the expressions from other types (note the definition of a Vec3 constructor and operator+ below).

See all
User Avatar
No comments yet.