https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99192
Bug ID: 99192 Summary: A wrong Aggregate initialization for a union with a variant member of non-aggregate class type Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: xmh970252187 at gmail dot com Target Milestone: --- struct X { const int a; int b; X(int):a(0){}}; union Y { X x;int k; }; int main(){ Y y{ }; } Such an example is accepted by GCC(https://godbolt.org/z/hTn8zc) but rejected by Clang. GCC has a wrong interpretation for this example. Union `Y` is an aggregate class type as per [dcl.init.aggr#1], So aggregate initialization is applied to this declaration `Y y{ };`. According to > If the aggregate is a union and the initializer list is empty, then >> if any variant member has a default member initializer, that member is >> initialized from its default member initializer; >> otherwise, the first member of the union (if any) is copy-initialized from >> an empty initializer list. Since there's no default-initializer for a member of Y, hence the second bullet applies here. That means, the first member will be copy-initialized from an empty initializer list. However, the class `X` is not an aggregate class type and it has a user-defined constructor `X(int)`, Hence the following rule will be applied to initialize `x`, that is: >Otherwise, if T is a class type, constructors are considered. The applicable >constructors are enumerated and the best one is chosen through overload >resolution ([over.match], [over.match.list]). If a narrowing conversion (see >below) is required to convert any of the arguments, the program is ill-formed. The only candidate function here is `X(int)` and the corresponding argument list is empty. So, there's no viable function that exists, Hence the invocation should be ill-formed here as per: > If a best viable function exists and is unique, overload resolution succeeds > and produces it as the result. Otherwise overload resolution fails and the > invocation is ill-formed.