N3797::9.5/2 [class.union]
says:
If any non-static data member of a union has a non-trivial default
constructor (12.1), copy constructor (12.8), move constructor (12.8),
copy assignment operator (12.8), move assignment operator (12.8), or
destructor (12.4), the corresponding member function of the union must
be user-provided or it will be implicitly deleted (8.4.3) for the
union
I was trying to understand that note by example:
#include <iostream>
#include <limits>
struct A
{
A(const A&){ std::cout << "~A()" << std::endl; } //A has no default constructor
};
union U
{
A a;
};
U u; //error: call to implicitly-deleted default constructor of 'U'
int main()
{
}
That behavior isn't quite clear to me. struct A
doesn't have implicitly-declared default constructor, because 12.1/4: [class.ctor]
says:
If there is no user-declared constructor for class X, a constructor
having no parameters is implicitly declared as defaulted (8.4).
Which means struct A
doesn't have a non-trivial default constructor (There is no default constructor at all, in particular non-trivial). That's union U
doesn't have to have a deleted default constructor. What's wrong?
Best Answer
The relevant wording is in C++11 [class.ctor]p5 (emphasis mine):
Your class
A
has no default constructor, so a defaulted default constructor (whether implicit or explicit) for a classX
(whether union or non-union) containing a non-static data member of typeA
without an initialiser leads to the default constructor forX
being deleted. It has to: there's simply no way for the compiler to generate any other default constructor.As for your follow-up question in the comments:
If instead of
A
not having a default constructor, it has a non-trivial default constructor, then there is a difference between using that in a union and in a non-union class, and that is also part of [class.ctor]p5: it is the first bullet point that I included, without emphasis, in my earlier quote.