Факториал можно вычислить на стадии компиляции
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| #include <iostream>
template<int n>
class Factorial {
public:
static const int f = Factorial<n - 1>::f * n;
};
template<>
class Factorial<0> {
public:
static const int f = 1;
};
int main() {
std::cout << Factorial<5>::f << std::endl; // 120
} |
|