递归还可以这么写,嘿嘿。。。

来源:互联网 发布:知乎 丁磊和马云 编辑:程序博客网 时间:2024/05/01 00:16

 /*********************************************
 程序功能: 利用模版在编译期间计算的
    C++阶乘实现。
*********************************************/

 

#include <iostream>

using namespace std;

 

template<int n> struct Fact {
    static const int value = n * Fact<n-1>::value;
};

 

template<> struct Fact<0> {
     static const int value = 1;
};

 

int main(int ac, char *av[])
{
     cout << "5 != " << Fact<5>::value;
 
     return 0;
}

原创粉丝点击