What makes a union member active?
I've read chapter 9.5 of the C++14 standard (the one about unions), but I haven't found a clear answer to what makes a union member active.
There is a note:
In general, one must use explicit destructor calls and placement new operators to change the active member of a union.
So for example,
union U {
int i;
short s;
} u;
new(&u.i) int(42);
Okay, placement new changes the active member, it's clear. But we usually don't use placement new when working with types with trivial constructors.
Does operator= change the active member without UB?
u.i = 42;
Here, operator= called on an unconstructed object. Is it well defined?
What about this?
struct A {
int i0;
int i1;
};
union U {
A a;
short s;
} u;
What makes a to be the active member of u? Is setting both i0 & i1 enough?
u.a.i0 = 42;
u.a.i1 = 99;
What if I write:
u.a.i0 = 42; // supposedly this doesn't change the active member to a, as i1 isn't set
int x = u.a.i0; // is it fine to read from a.i0? a is not an active member supposedly
After u.a.i0 = 42;, the active member isn't changed to a (I think), so is it UB to do int x = u.a.i0;?
Does C++17 improve on the description of active members?