Expression Templates

Learn the runtime lazy evaluation process for compile-time computations.

Expression templates are a metaprogramming technique that enables lazy evaluation of a computation at compile-time. This helps to avoid inefficient operations that occur at runtime. However, this does not come for free, as expression templates require more code and can be cumbersome to read or understand. They are often used in the implementation of linear algebra libraries.

Problem solved by expression templates

Before seeing how expression templates are implemented, let’s look at what problem they solve. For this, let’s suppose we want to do some operations with matrixes, for which we implemented the basic operations, addition, subtraction, and multiplication (either of two matrixes or of a scalar and a matrix). We can have the following expressions:

Get hands-on with 1200+ tech skills courses.