mardi 2 mai 2017

Constexpr Factorial Compilation Results in VS2015 and GCC 5.4.0

Wondering if the following surprises anyone, as it did me? Alex Allain's article here on using constexpr shows the following factorial example:

constexpr factorial (int n)
{
    return n > 0 ? n * factorial( n - 1 ) : 1;
}

And states:

Now you can use factorial(2) and when the compiler sees it, it can optimize away the call and make the calculation entirely at compile time.

I tried this in VS2015 in Release mode with full optimizations on (/Ox) and stepped through the code in the debugger viewing the assembly and saw that the factorial calculation was not done at compilation.

Using GCC v5.4.0 with --std=C++14, I must use /O2 or /O3 before the calculation is performed at compile time. I was surprised thought that using just /O the calculation did not occur at compilation time.

Main main question is: Why is VS2015 not performing this calculation at compilation time?

Aucun commentaire:

Enregistrer un commentaire