3 ms·
>[type punning via enum] No; this works in C (since C99...?), but in C++ "it is undefined behavior to read from the member of the union that wasn't most recentl
by gpderetta 3y ago
>[type punning via enum] No; this works in C (since C99...?), but in C++ "it is undefined behavior to read from the member of the union that wasn't most recently written.
it is technically UB, but it works as a documented extension on pretty much every compiler.
- agalunar 3y agoIt looks like it's okay even technically! From a section marked "(since C99)": > If the member used to access the contents of a union is not the same as the member last used to store a value, the object representation of the value that was stored is reinterpreted as an object representation of the new type (this is known as type punning). [...] Before C99 TC3 (DR 283) this behaviour was undefined, but commonly implemented this way. https://en.cppreference.com/w/c/language/union https://en.cppreference.com/w/c/language/union