Unexpected compile-time evaluation of recursive constexpr function

Viewed 80

I have a recursive constexpr function that produces unexpected results in Visual Studio 2019.

After program simplification it took the following form:

constexpr int f(int * x) {
    int c = 0;
    auto p = &c;
    if ( !x )
        return f( p );
    if ( x == p )
        *x = 1;
    return c;
};

int main() {
    static_assert( f(nullptr) == 0 ); //fails in MSVC
}

I would expect that the function call f(nullptr) returns 0, but in MSVC it returns 1. Demo: https://gcc.godbolt.org/z/n4Kndon71

The only possibility for it that I see is the execution of if ( x == p ) *x = 1. But this means that the pointer on local variable c in both caller-f and callee-f is the same, which shall not be.

Is it a bug in MSVC?

0 Answers
Related