Namespaces
Variants
Actions

Talk:cpp/language/union

From cppreference.com

[edit] Undefined behavior in the example

Doesn't the example invoke undefined behavior? As far as I remember, one can read the value of the union only through the same member of the union which was used to write the value into the union type. That is, a union can 'contain' only one type at a time, not all at once. -- P12 07:06, 24 March 2012 (PDT)

Yes, it is definitely UB, although it deserves a note that many compilers support such use through extensions. A standards-compliant example use case for unions would be a tagged union. --Cubbi 12:39, 24 March 2012 (PDT)
I've seen this kind of example used often (for example, detecting platform endianness), which is why I included it. I hadn't realised that it was UB, although, I agree that it should be mentioned. --- Undeterminant 15:18, 24 March 2012 (PDT)

[edit] A union cannot have data members of reference types.

Johannkokos (talk) 07:13, 29 September 2018 (PDT)

It should be

A union cannot have non-static data members of reference types.

Done. (It's allowed to add missing pieces yourself.) --Fruderica (talk) 03:52, 30 September 2018 (PDT)