What is template metaprogramming?
The ability of C++ to perform compile-time computations to create a more generic and optimized code is known as template metaprogramming (TMP).
TMP can be thought of as an embedded language that sits on a layer above the standard C++, is interpreted by a C++ compiler’s template system, and outputs the standard C++ code that is compiled normally. It allows the performance of general-purpose computations with types, templates, functions, constant variables, and literals as objects.
Runtime and compile-time variables
Before diving into the details of TMP, it is essential to understand the difference between runtime and compile-time variables:
/* Runtime variables. */char c = 'x';int *ptr = new int(10);/* Compile-time values and types. */const int num = 10;typedef float t;
The variables c and ptr are dynamic runtime variables that can be assigned any value during the execution of the program. The variables num and t are compile-time variables that can only be assigned once during a compilation. They are immutable throughout compilation and in any instantiation that they are used.
Now, let’s consider a runtime function that adds two numbers. The function is only compiled once and can be called multiple times with different values.
// A runtime function that returns the sum of two integers.int add(int num1, int num2) {return num1 + num2;}
However, as shown below, the same function can be made into a compile-time function using TMP:
template <int num1, int num2>struct add {static const int value = (num1 + num2);};
The add function is mapped to a compile-time computation using a class template instantiation where:
- A class or struct template body is the function body.
- The template parameters are the function parameters.
- A static const member is the return value.
The struct template, add, is compiled for each unique instantiation of the template and gathers a unique type each time. The evaluation of the static const member, ‘value’, during instantiation of the struct performs the exact same computation as the runtime function shown above, only this computation is performed during instantiation and is, therefore, performed entirely at compile-time.
Code
The following code snippet executes the compile-time function that was made using TMP:
#include <iostream>using namespace std;template <int num1, int num2>struct add {static const int value = (num1 + num2);};int main() {const int a = 10, b = 20;const int r1 = add<a, b>::value;cout << "Sum is: " << r1 << endl;return 0;}
Free Resources