The following code compiles in GCC, CLANG, and MSVC. It initializes the int ir
to refer to the non-existent a[1]
which is one beyond the end of a
.
consteval int foo()
{
int a[1]{ 42 };
int& ir = a[1];
return *(&ir-1);
}
int main()
{
constexpr int ci=foo();
return ci;
}
This compiles in spite of the core constant expression evaluation seeming to contain UB. See this which contains “A reference shall be initialized to refer to a valid object or function.”
A more recent n4950 draft removes this language.
Is this no longer UB?