您的位置:首页 > 其它

计算阶乘,你见过这样的么?

2011-12-09 11:41 232 查看
//TMP(模板元编程),计算阶乘
#include <iostream>
using namespace std;
template <unsigned n>
struct Factorial
{
enum
{
value = n * Factorial<n - 1>::value
};
};
//偏特化
template <>
struct Factorial<0>
{
enum
{
value = 1
};
};
int main()
{
cout << Factorial<5>::value << endl;
cout << Factorial<10>::value << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: