How different would be these two compile-time Factorial functions in C++20

121 views Asked by At

Following This link, I came up to this question.

I'm trying to understand how different would be the targeting the following two compile-time functions by a C++20 compiler.

consteval int Factorial(int n) 
{
    int result = 1;
    while (n > 1) 
        result *= n--;
    return result;
}

and what we had in C++11 in case being used by C++20

template <int N>
struct Factorial {
     static constexpr int value = N * Factorial<N - 1>::value;
};

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

Is it similar when compiler tries to, for instance, do Factorial(10) and Factorial<10>::value in terms of using compiler depth, creating stack frame (if there would be any for consteval int Factorial(int n)) and anything else?

0

There are 0 answers