struct A{
A(){}
};
union C{
A a;
int b = 0;
};
int main(){
C c;
}
In the above code, GCC and Clang both complain that the default constructor for union C
is defined as deleted.
However, the relevant rule says that:
A defaulted default constructor for class X is defined as deleted if:
- X is a union that has a variant member with a non-trivial default constructor and no variant member of X has a default member initializer,
- X is a non-union class that has a variant member M with a non-trivial default constructor and no variant member of the anonymous union containing M has a default member initializer,
Notice the emphasized wording. In the example, IIUC, since the variant member b
has a default member initializer, the defaulted default constructor shouldn't be defined as deleted. Why do these compilers report this code as ill-formed?
If change the definition of C
to
union C{
A a{};
int b;
};
Then all compilers can compile this code. The behavior hints that the rule actually means:
X is a union that has a variant member with a non-trivial default constructor and no default member initializer is supplied for the variant member
Is this a compiler bug or the vague wording of that rule?