Created
December 30, 2008 00:34
-
-
Save benjamn/41454 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
// Recursive case: | |
template <int N> struct Fib { | |
enum { val = Fib<N-1>::val | |
+ Fib<N-2>::val }; | |
}; | |
// Base cases: | |
template <> struct Fib<0> { enum { val = 0 }; }; | |
template <> struct Fib<1> { enum { val = 1 }; }; | |
int main() | |
{ | |
std::cout << "first ten fibonacci numbers: " | |
<< Fib<0>::val << ", " | |
<< Fib<1>::val << ", " | |
<< Fib<2>::val << ", " | |
<< Fib<3>::val << ", " | |
<< Fib<4>::val << ", " | |
<< Fib<5>::val << ", " | |
<< Fib<6>::val << ", " | |
<< Fib<7>::val << ", " | |
<< Fib<8>::val << ", " | |
<< Fib<9>::val << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment