# 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.

## Motivation and example

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:

```class Vec {
std::vector<double> elems;

public:
Vec(size_t n) : elems(n) {}

double &operator[](size_t i)      { return elems[i]; }
double operator[](size_t i) const { return elems[i]; }
size_t size()               const { return elems.size(); }
};

Vec operator+(Vec const &u, Vec const &v) {
assert(u.size() == v.size());
Vec sum(u.size());
for (size_t i = 0; i < u.size(); i++) {
sum[i] = u[i] + v[i];
}
return sum;
}
```

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

A problem with this approach is that more complicated expressions such as Vec x = a + b + c are implemented inefficiently. The implementation first produces a temporary vector to hold a + b, then produces another vector 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 a custom type, say VecSum, that represents the unevaluated sum of two vectors, or a vector with a VecSum, etc. Larger expressions then effectively build expression trees that are evaluated only when assigned to an actual Vec 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 Vec, such as Vec x = a + b + c, generates a new Vec constructor if needed by template instantiation. This constructor operates on three Vec; 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 VecExpression represents any vector-valued expression. It is templated on the actual expression type E to be implemented, per the curiously recurring template pattern.

```1 template <typename E>
2 class VecExpression {
3   public:
4     double operator[](size_t i) const { return static_cast<E const&>(*this)[i];     }
5     size_t size()               const { return static_cast<E const&>(*this).size(); }
6 };
```

The Vec class still stores the coordinates of a fully evaluated vector expression, and becomes a subclass of VecExpression.

```class Vec : public VecExpression<Vec> {
std::vector<double> elems;

public:
double operator[](size_t i) const { return elems[i]; }
double &operator[](size_t i)      { return elems[i]; }
size_t size() const               { return elems.size(); }

Vec(size_t n) : elems(n) {}

// construct vector using initializer list
Vec(std::initializer_list<double>init){
for(auto i:init)
elems.push_back(i);
}

// A Vec can be constructed from any VecExpression, forcing its evaluation.
template <typename E>
Vec(VecExpression<E> const& expr) : elems(expr.size()) {
for (size_t i = 0; i != expr.size(); ++i) {
elems[i] = expr[i];
}
}
};
```

The sum of two vectors is represented by a new type, VecSum, that is templated on the types of the left- and right-hand sides of the sum so that it can be applied to arbitrary pairs of vector expressions. An overloaded operator+ serves as syntactic sugar for the VecSum constructor.

``` 1 template <typename E1, typename E2>
2 class VecSum : public VecExpression<VecSum<E1, E2> > {
3     E1 const& _u;
4     E2 const& _v;
5
6 public:
7     VecSum(E1 const& u, E2 const& v) : _u(u), _v(v) {
8         assert(u.size() == v.size());
9     }
10
11     double operator[](size_t i) const { return _u[i] + _v[i]; }
12     size_t size()               const { return _v.size(); }
13 };
14
15 template <typename E1, typename E2>
16 VecSum<E1,E2>
17 operator+(VecExpression<E1> const& u, VecExpression<E2> const& v) {
18    return VecSum<E1, E2>(*static_cast<const E1*>(&u), *static_cast<const E2*>(&v));
19 }
```

With the above definitions, the expression a + b + c is of type

```VecSum<VecSum<Vec, Vec>, Vec>
```

so Vec x = a + b + c invokes the templated Vec constructor with this type as its E template argument. Inside this constructor, the loop body

```elems[i] = expr[i];
```

is effectively expanded (following the recursive definitions of operator+ and operator[] on this type) to

```elems[i] = a.elems[i] + b.elems[i] + c.elems[i];
```

with no temporary vectors needed and only one pass through each memory block.

Basic Usage :

``` 1 int main(){
2
3     Vec v0 = {23.4,12.5,144.56,90.56};
4     Vec v1 = {67.12,34.8,90.34,89.30};
5     Vec v2 = {34.90,111.9,45.12,90.5};
6
7     // Following assignment will call the ctor of Vec which accept type of
8     // `VecExpression<E> const&`. Then expand the loop body to
9     // a.elems[i] + b.elems[i] + c.elems[i]
10     Vec sum_of_vec_type = v0+v1+v2;
11
12     for(size_t i=0;i<sum_of_vec_type.size();++i)
13         std::cout << sum_of_vec_type[i] << std::endl;
14
15 }
```

## Applications

Expression templates have been found especially useful by the authors of libraries for linear algebra, i.e., for dealing with vectors and matrices of numbers. Among libraries employing expression template are Armadillo, Blaze, Blitz++, Boost uBLAS, Eigen, POOMA, Stan Math Library, and xtensor. Expression templates can also accelerate C++ automatic differentiation implementations, as demonstrated in the Adept library.

Outside of vector math, the Spirit parser framework uses expression templates to represent formal grammars and compile these into parsers.