Type punning via unions is not UB in C in general, but it is in C++ IIRC.
I write "in general" because, as with other forms of memory reinterpretation (memcpy or copy through a character type), evaluating a trap representation triggers UB.
The short version is that it's fine in C++ as long as you only read the member that was last written to or a char type.
The short version is that it's fine in C++ as long as you only read the member that was last written to or a char type.