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
博客介绍了模板元编程,以计算 N*N - 1*N - 2*......1 (即阶乘)为需求,给出了相关代码,并展示运行结果,如 5! = 120 。

被折叠的 条评论
为什么被折叠?



