| The factorial function - from Wikipedia. | Lecture 5 - slide 13 : 39 Program 1 |
// Factorial template function from Wikipedia.
#include <iostream>
#include <string>
// The general case:
template <int N> struct Factorial {
static const int value = N * Factorial<N - 1>::value;
};
// The base case of the recursion, via template specialization:
template <>
struct Factorial<0> {
static const int value = 1;
};
int main(){
Factorial<5> facBox;
std::cout << facBox.value << std::endl; // 120
std::cout << Factorial<10>::value << std::endl; // 3628800
}