template <unsigned int n>
struct D {
enum { value = n * D<n - 1>::value };
};
template <>
struct D<0> {
enum { value = 1 };
};
计算阶乘的模板递归,使用:
D<10>;
D<100>;
该博客介绍了使用模板递归的方式计算阶乘,通过D<n>模板结构体,实现了从n到0的递归计算,最终得到n的阶乘值。示例中展示了计算10和100的阶乘结果。
template <unsigned int n>
struct D {
enum { value = n * D<n - 1>::value };
};
template <>
struct D<0> {
enum { value = 1 };
};
计算阶乘的模板递归,使用:
D<10>;
D<100>;
2153
2341
3190

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