Introduction and motivation
Expression templates (denoted as ETs in the following) are a powerful template meta-programming technique, used to speed-up calculations of sometimes quite expensive expressions. It is widely used in different domains, for example in implementation of linear algebra libraries.
For this example, consider the context of linear algebraic computations. More specifically, computations involving only element-wise operations. This kind of computations are the most basic applications of ETs, and they serve as a good introduction to how ETs work internally.
Let’s look at a motivating example. Consider the computation of the expression:
Vector vec_1, vec_2, vec_3;
// Initializing vec_1, vec_2 and vec_3.
Vector result = vec_1 + vec_2*vec_3;
Here for the sake of simplicity, I’ll assume that the class Vector
and operation + (vector plus: element-wise plus operation) and operation * (here means vector inner product: also element-wise operation) are both correctly implemented, as how they should be, mathematically.
In a conventional implementation without using ETs (or other similar techniques), at least five constructions of Vector
instances take place in order to obtain the final result
:
vec_1
, vec_2
and vec_3
.Vector
instance _tmp
, representing the result of _tmp = vec_2*vec_3;
.result
in result = vec_1 + _tmp;
.Implementation using ETs can eliminate the creation of temporary Vector _tmp
in 2, thus leaving only four constructions of Vector
instances. More interestingly, consider the following expression which is more complex:
Vector result = vec_1 + (vec_2*vec3 + vec_1)*(vec_2 + vec_3*vec_1);
There will also be four constructions of Vector
instances in total: vec_1, vec_2, vec_3
and result
. In other words, in this example, where only element-wise operations are involved, it is guaranteed that no temporary objects will be created from intermediate calculations.
How do ETs work
Basically speaking, ETs for any algebraic computations consist of two building blocks: