https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105059
Bug ID: 105059
Summary: Inconsistency between paren- and brace-initialization
of a union with anonymous struct
Product: gcc
Version: 11.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: barry.revzin at gmail dot com
Target Milestone: ---
union U {
struct {
int x;
int y;
};
long all;
};
constexpr long parens() {
U u(42);
return u.all;
}
static_assert(parens() == 42);
constexpr int braces() {
U u{42};
return u.all;
}
static_assert(braces() == 42);
gcc currently considers parens() to be a valid constant expression with value
42 (i.e. U(42) initializes U::long) but considers braces() to be invalid
because of reading the wrong member of the union (i.e. U{42} initializes U::x).
clang currently considers parens() ill-formed by rejecting U(42) entirely (and
then rejects braces() for the same reason as gcc does, because it's u.x that
was initialized).
The standard doesn't even talk about anonymous structs so it's hard to say what
the "right answer" here is, but it seems to me that either U(42) should do the
same thing as U{42} here or at the very least be ill-formed, but definitely not
do some different valid thing.