Listing 1: Fibonacci function


/* 
Fibonacci's Function as
C++ Template Metaprogram 
*/
template< unsigned long N > struct Fib
  {
  enum { value = Fib<N-1>::value +
                 Fib<N-2>::value } ;
  } ;

struct Fib< 1 >
  {
  enum { value = 1 } ;
  } ;

struct Fib< 2 >
  {
  enum { value = 1 } ;
  } ;

/* 
Corresponding ML code
fun Fib( 1 ) = 1 |
    Fib( 2 ) = 1 |
    Fib( n ) = Fib(n-1)+Fib(n-2) ;
*/
//End of File