#include <stdio.h> // The following code prints out the first five elements of the // fibonacci series without calculating the values at runtime. template <int param> struct CFibonacci { static const value; }; template <int param> const CFibonacci<param>::value = CFibonacci<param - 2>::value + CFibonacci<param - 1>::value; const CFibonacci<0>::value = 0; const CFibonacci<1>::value = 1; void main( void ) { printf( "%d\n", CFibonacci<1>::value ); printf( "%d\n", CFibonacci<2>::value ); printf( "%d\n", CFibonacci<3>::value ); printf( "%d\n", CFibonacci<4>::value ); printf( "%d\n", CFibonacci<5>::value ); }
Saturday, August 15, 1998
Calculating fibonacci series at compile-time
Subscribe to:
Posts (Atom)