1.概要
//模板元编程
/* 需求:N*N-1*N-2*......1 */
2.代码
- #include <iostream>
-
- //模板元编程
- /* 需求:N*N-1*N-2*......1 */
- template <int N>
- struct Factorial {
- enum { value = N * Factorial<N - 1>::value };
- };
-
- template <>
- struct Factorial<0> {
- enum { value = 1 };
- };
-
- int main() {
- std::cout << "5! = " << Factorial<5>::value << std::endl;
- return 0;
- }
3.运行结果
5! = 120