In this program ``` struct A { char u, v, w; }; struct X { char x; }; struct B : X, A { using A::operator=; }; constexpr A f() { union U { A a{ 1, 2, 3 }; B b; } u; u.b = u.a; return u.b; } // ok in GCC and MSVC, fails in Clang static_assert( f().w == 3 ); int main() { return f().w; //2 in GCC with -O0 } ``` constant evaluation of `f().w == 3` succeeds in GCC, but in runtime with -O0 switch `f().w` is evaluated as 2. Clang here finds that `u.b = u.a;` is not a constant expression because it simultaneously accesses two members of a union during copying. Online demo: https://gcc.godbolt.org/z/Ks7oaoaz9 Related discussion: https://stackoverflow.com/q/79196052/7325599
I am not sure if this is related to https://cplusplus.github.io/CWG/issues/2721.html at all. because
``` struct A { char u, v, w; }; struct X { char x; }; struct B : X { A y; }; constexpr A f() { union U { A a{ 1, 2, 3 }; B b; } u; u.b.y = u.a; // when does the life time of u.b start, during the statement? return u.b.y; } // ok in GCC and MSVC, fails in Clang static_assert( f().w == 3 ); int main() { return f().w; //2 in GCC with -O0 } ``` So I think this is similar as the above. This produces similar results as GCC, clang, and MSVC for the original testcase. EDG also accepts this code and produces 3 for main. Note I think only GCC -O0 code generation is wrong for main. basic.life/1 says: ``` The lifetime of an object o of type T ends when: ... the storage which the object occupies is released, or is reused by an object that is not nested within o (6.7.2 [intro.object]). ``` But it is not obvious.
Here is a better example. It looks valid (unlike above one) because of creating of copy `A( u.a )`: ``` #include <iostream> struct A { int c[7]; }; struct X { int x; }; struct B : X, A { using A::operator=; }; int main() { union U { A a{ 0, 1, 2, 3, 4, 5, 6 }; B b; } u; u.b = A( u.a ); for ( int i = 0; i < 7; ++i ) std::cout << (int)u.b.c[i]; } ``` It prints `0123456` in Clang and MSVC. But in GCC 14 it prints `0122356` with any optimization level (-O0 through -O3). Online demo: https://gcc.godbolt.org/z/GKo8YcqqM