The following program was reduced to demonstrate the question. struct A
has a constexpr
member function that compares this
against some pointer, which can be nullptr
. Then this member function is called for a temporary object (taking address of rvalue) inside a constant expression:
struct A {
A * a = nullptr; // in real program this pointer can be changed
constexpr bool f() {
return a != this;
}
};
static_assert( A{}.f() );
Clang and MSVC are ok with it, but GCC does not like this code, complaining:
<source>:8:21: error: non-constant condition for static assertion
8 | static_assert( A{}.f() );
| ~~~~~^~
<source>:4:18: error: '((&<anonymous>) != 0)' is not a constant expression
4 | return a != this;
Online demo: https://gcc.godbolt.org/z/evK1Y1r8e
Is it a well formed program? Or is it always necessary to check that pointers are not nullptr
before comparison in a constant expression?
This is just a GCC bug: of course the (symbolic) address of a temporary constructed during constant evaluation is known to that evaluation.