#include #include using std::cout; using std::endl; using std::stringstream; // Here's how you would use the compiler+templates to calculate a // compile-time constant. Note: nobody does this in real life. template struct fibonacci { static constexpr int value = fibonacci::value + fibonacci::value; }; template <> struct fibonacci<0> { static constexpr int value = 0; }; template <> struct fibonacci<1> { static constexpr int value = 1; }; int main(int argc, char **argv) { cout << "The compile-time constant fib(40) is: " << fibonacci<40>::value << endl; // Since fibonacci<> generates compile-time constants, you can create // arrays, etc, using it. stringstream s; int array[fibonacci<4>::value]; for (int i = 0; i < fibonacci<4>::value; ++i) { array[i] = i; if (i != 0) { s << ", "; } s << array[i]; } cout << "Initialized a " << fibonacci<4>::value << "-element array to: {" << s.str() << "}" << endl; return 0; }