During constant expression evaluation in C++17, shall the compiler consider any pointer addressing a valid object unequal to any pointer addressing an object after the end of its lifetime?
For example:
constexpr auto f(int * x = nullptr) {
int c = 0;
auto p = &c;
if ( x == p )
throw "unexpected";
return p;
};
int main() {
static_assert( f( f() ) );
}
Here the inner call of function f()
returns a dangling pointer, which is passed to f
again. I believed that the condition x == p
must be false
since x
is a dangling pointer and p
is a valid pointer, and it is indeed so in Clang. But in GCC the condition is satisfied and the constant evaluation fails due to throw
. Demo: https://gcc.godbolt.org/z/ehcMro17q
Is it an undefined or implementation-defined behavior, or one of the compilers is wrong?