Every programming language enthusiast has written programs to compute the Fibonacci numbers in every programming language. That's easy, and gets boring pretty soon.

But C++ enthusiasts can go one better. That's because C++ conforms to the slogan "buy one C++ compiler, get another interpreted language free!". So C++ can compute Fibonacci numbers at compile time!!!1!

Here's how to do it. Note that the syntax of the compile time language (aka C++ templates) is nothing like what you know of C++. This is absolutely normal in C++; every C++ program looks almost exactly nothing like any other C++ program.

template<int N> struct fib {
  static const int result = fib<N-1>::result + fib<N-2>::result;
};

struct fib<0> {
  static const int result = 0;
};

struct fib<1> {
  static const int result = 1;
};

#include <iostream>
int main(int ac, char *av[])
{
  std::cout << "Fib(10) = " << fib<10>::result << std::endl;
  return 0;
}
C++ advocates will have no problems explaining why this program has linear compilation time in N, rather than the expected exponential compilation time due to the algorithm used. Interestingly, a vastly more complicated struct with logarithmic compilation time (see Compute Fibonacci numbers FAST! for the algorithm, albeit in readable pseudocode rather than template meta-programming nonsense like the above) takes much longer to compile (presumably due to increased complexity).

Warning

You may need to increase your compiler's template depth to compile this code! The gcc incantation is -ftemplate-depth-33 or somesuch.