现在的位置: 首页 > 综合 > 正文

用c++ template 算阶乘

2017年01月12日 ⁄ 综合 ⁄ 共 193字 ⁄ 字号 评论关闭
 #include <iostream>
template <int N>
    struct fact {
        enum { value = N * fact<N - 1>::value };
};
template <>
struct fact<1> {
    enum { value = 1 };
};
int main()
{     
    std::cout << "5! = " << fact<5>::value << std::endl;
}

抱歉!评论已关闭.