As far as I know, undefined behavior shall be a compile error during constant evaluation.
But if one takes an example of undefined behavior from C++20 standard class.union#6.3 with minor modification to activate constant evaluation:
struct X { const int a; int b; };
union Y { X x; int k; };
constexpr bool g() {
Y y = { { 1, 2 } }; // OK, y.x is active union member ([class.mem])
int n = y.x.a;
y.k = 4; // OK: ends lifetime of y.x, y.k is active member of union
y.x.b = n; // undefined behavior: y.x.b modified outside its lifetime,
// S(y.x.b) is empty because X's default constructor is deleted,
// so union member y.x's lifetime does not implicitly start
return y.x.b > 0;
}
int main() {
static_assert( g() );
}
then it is accepted by all compilers without any warnings. Demo: https://gcc.godbolt.org/z/W7o4n5KrG
Are all compilers wrong here, or there is no undefined behavior in the example, or no diagnostic is required?