- Does it have a defined behaviour? If not why?
It has defined behaviour. The assignment copy the value of u2
and for me the value of an union is a designation of the active member (although that part is not represented and so can't be examined but it determines what is UB and what is not) and the value of the active member if there is one.
- What is the active member of u1 after the assignment and why?
f, see above.
Which member of u1 can be read from after the assignment without causing an UB and why?
f. In general, only the active member of an union can be read without UB in C++. There is a special rule for union of structs where those struct have a common initial sequence. Note: C is more relaxed and makes implementation defined (and perhaps completely defined) some cases which are undefined in C++ and I may have missed some changes in C++ to make it more compatible with C.
If someone want to look up the standard, I suggest starting with class.copy.assign/13.
float
part, certainly. When you perform assignment, the active member is copied along with the rest. But I'll let someone versed in the standard answer. – Schneideru1 = u2;
should makeu1
to have the same active member asu2
. – Strophanthus