<<< More on template specialization | Index | >>> |
A cute template specialization trick: factorial calculation made at compile time:
// factorial.cpp template < int N > struct Factorial { enum { value = N * Factorial< N-1 >::value }; }; template < > struct Factorial< 1 > { enum { value = 1 }; }; // main.cpp #include <iostream> int main() { const int fact5 = Factorial< 5 >::value; std::cout << fact5 << '\n'; return 0; } // Program prints 120
<<< More on template specialization | Index | >>> |