Middle — Senior
Compiler calculation of factorial in a template without static context
livecode
Task condition
A template metaclass Fac<N> has been created, which allows calculating the factorial at compile time. The task is to eliminate the use of static constexpr and implement the calculation through a constexpr method of the class instance.
template <unsigned N>
struct Fac {
constexpr int get() const {
return N * Fac<N - 1>().get();
}
};
template <>
struct Fac<0> {
constexpr int get() const {
return 1;
}
};
static_assert(Fac<5>().get() == 120);