Aggregate initialization of a union in C++ with `{}`

In the following program the union U has two fields a and b, each with distinct default value. If one creates a variable of type U using aggregate initialization {} what are the value and the active member of the union?

#include <iostream>

struct A { int x = 1; };
struct B { int x = 0; };

union U {
    A a;
    B b;
};

int main() {
    U u{};
    std::cout << u.a.x;
}

Surprisingly the compilers diverge here: Clang prints 1 and GCC prints 0, demo: https://gcc.godbolt.org/z/8Tj4Y1Pv1

Is there a bug in one of the compilers or the behavior here is not defined by the standard?

Source: Windows Questions C++

LEAVE A COMMENT