Template metaprogramming

Template metaprogramming is a tricky programming technique to get some evaluations done at compile-time by abusing the facility of templates in C++. The programming is uncommon.

What 'programming at compile-time' means is best illustrated with a simple example:

Table of contents
1 Factorial with recursion
2 (4*3*2*1)
3 Factorial with template metaprogramming
4 Traditional n-dimensional vector class approach
5 Template metaprogramming enhanced n-dimensional vector class
6 See also

Factorial with recursion

int factorial(int n) {
 if (n == 1)
   return 1;
   return n * factorial(n - 1);




Factorial with template metaprogramming

struct Factorial {
 enum { value = N * Factorial::value };

template <> struct Factorial<1> {

 enum { value = 1 };

//Factorial<4>::value == 24

The template metaprogramming solution uses template specialization to provide the ending condition for the recursion.

Now here's the catch: The factorial in the latter case (Factorial<4>::value) is computed at compile time, i.e. when compiling your code. This has the natural consequence that Factorial::value can only be used for x that are known at compile time, e.g. constant numbers or the value returned by sizeof().

Template metaprogramming does have its practical uses even though its syntax looks clumsy. It may be used to create n-dimensional vector classes where n is known at compile time. The benefit over a more traditional n-dimensional vector is that the loops can be unrolled, resulting in very optimized code.

Consider this example of the addition operator:

Traditional n-dimensional vector class approach

Vector& operator+=(const Vector& rhs) {
 for (int i=0; i}
With template metaprogramming, the loop can be unrolled at compile-time, resulting in code similar to this:

Template metaprogramming enhanced n-dimensional vector class

Vector<2>& operator+=(const Vector<2>& rhs) {
 value[0] += rhs.value[0];
 value[1] += rhs.value[1];
 return *this;
Note that it results in code like that, but is not programmed like that. But it demonstrates that the loop is gone and so is the loop's overhead. For a real implementation of this technique, see
this post on flipcode.com.

Also worth noting is that the programming paradigm used in template metaprogramming is functional and that template metaprogramming in C++ is a Turing-complete technique. The former means that you should be very familiar with recursion if you plan to do template metaprogramming. The latter means that any algorithm can be implemented with template metaprogramming. The only implication is that the algorithm's input values must be known while the code is compiled.

See also

copyright 2004 FactsAbout.com