Executing the code in compile-time
As we discussed earlier, template metaprogramming will run the code in compile-time by creating a new code. Now, let's see how we can get the compile-time constant and generate a compile-time class in this section.
Getting a compile-time constant
To retrieve a compile-time constant, let's create a code that has the template for a Fibonacci algorithm in it. We will consume the template so the compiler will provide the value in compile time. The code should be as follows:
/* fibonaccimeta.cpp */
#include <iostream>
using namespace std;
// Defining Fibonacci template
// to calculate the Fibonacci sequence
template <int number>
struct Fibonacci
{
enum
{
value =
Fibonacci<number - 1>::value +
Fibonacci<number - 2>::value
};
};
// Defining template for
// specific input value
// 'number' = 1
template <>
struct Fibonacci<1...