Sobes.tech
Junior — Senior

Compilation-based factorial calculation

livecode

Task condition

Rewrite the recursive constexpr function responsible for calculating the factorial so that its result is obtained at compile time, regardless of how convoluted the function call is.

constexpr int computeFactorial(unsigned n) {
    return n == 0 ? 1 : n * computeFactorial(n - 1);
}

constexpr int result = computeFactorial(5);
static_assert(result == 120); // compile-time check