// This program demos the use of a template metaprogram // to construct fibonacci numbers. // Template metaprograms were introduced by Todd Veldhuizen, // in C++ Report, Vol. 7, No. 4. // Note that template instantiation requires the equivalent // of a pattern-matching based interpreter to be included // in the compiler, making template implementation tricky! #include template< unsigned long N > class Fib { public: enum { value = Fib::value + Fib::value }; }; // Explicit specialization of first fibonacci number: class Fib<1> { public: enum { value = 1 }; }; // Explicit specialization of second fibonacci number: class Fib<2> { public: enum { value = 1 }; }; main() { Fib<5> five; Fib<6> six; std::cout << five.value << std::endl; std::cout << six.value << std::endl; std::cout << Fib<1000>::value << std::endl; }