MST
星途 面试题库

面试题:C++ 模板元编程之递归模板

使用模板元编程实现一个编译期计算阶乘的功能。要求通过递归模板的方式实现,例如 `Factorial<5>::value` 应该在编译期计算出结果 120 。
21.3万 热度难度
编程语言C++

知识考点

AI 面试

面试题答案

一键面试
template <int N>
struct Factorial {
    static const int value = N * Factorial<N - 1>::value;
};

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