(I just realized I first need to solve a much more basic issue with copying unions: When a union object is copied, is a member subobject created?. Please see that other question first.)
The implicitly generated copy operations (constructor and assignment) of a class perform member by member copy (initialization or assignment). (For a trivial type these are the same.)
So a class with some members not initialized cannot be copied, as accessing uninitialized objects is illegal.
struct C {
int m1, m2;
};
void f() {
C c1, c2;
c1.m1 = 1;
c2 = c1; // not initialized
}
But a union can always be copied, even if it contains class members, some of which aren't initialized (because... by definition not two members of a unions are initialized).
Does that mean that copying a union of a class with uninitialized members is legal:
union U {
C m;
};
void g() {
U u1, u2;
u1.m.m1 = 1;
u2 = u1;
}
and if so, can classes be copied by casting to such union?
void f2() {
C c1, c2;
c1.m1 = 1;
(U&)c2 = (U&)c1; // not initialized?
}