If constexpr
reference is initialized with just another constexpr
object as in the example:
int main() {
constexpr int a = 0;
constexpr const int & b = a;
}
then both GCC and Clang reject it saying constexpr variable 'b' must be initialized by a constant expression, address of non-static constexpr variable 'a' may differ on each invocation of the enclosing function; add 'static' to give it a constant address
.
At the same time MSVC accepts the example. Demo: https://gcc.godbolt.org/z/Whv7YeWKW
All compilers accept the code after adding static
to a:
static constexpr int a = 0;
Demo: https://gcc.godbolt.org/z/abeP4z64E
Is it really required by the standard that any constexpr
reference be initialized only with a static object?