cpp/language/union

A union is a special class type that can hold only one of its non-static at a time.

Syntax
The class specifier for a union declaration is similar to declaration:

A union can have member functions (including constructors and destructors), but not virtual functions.

A union cannot have base classes and cannot be used as a base class.

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

Just like in declaration, the default member access in a union is.

Explanation
The union is at least as big as necessary to hold its largest data member, but is usually not larger. The other data members are intended to be allocated in the same bytes as part of that largest member. The details of that allocation are implementation-defined,. It is undefined behavior to read from the member of the union that wasn't most recently written. Many compilers implement, as a non-standard language extension, the ability to read inactive members of a union.

Each member is allocated as if it is the only member of the class.

If two union members are standard-layout types, it's well-defined to examine their common subsequence on any compiler.

Member lifetime
The of a union member begins when the member is made active. If another member was active previously, its lifetime ends.

When active member of a union is switched by an assignment expression of the form that uses either the built-in assignment operator or a trivial assignment operator, for each union member X that appears in the member access and array subscript subexpressions of  that is not a class with non-trivial or deleted default constructors, if modification of X would have undefined behavior under type aliasing rules, an object of the type of X is implicitly created in the nominated storage; no initialization is performed and the beginning of its lifetime is sequenced after the value computation of the left and right operands and before the assignment.

Trivial copy constructor and copy assignment operator of union types copy object representations. If the source and the destination are not the same object, these special member functions start lifetime of every object (except for objects that are neither subobjects of the destination nor of ) nested in the destination corresponding to the one nested in the source before the copy is performed. Otherwise, they do nothing. Two union objects have the same corresponding active member (if any) after construction or assignment via trivial special functions.

Anonymous unions
An anonymous union is an unnamed union definition that does not simultaneously define any variables (including objects of the union type, references, or pointers to the union).

Anonymous unions have further restrictions: they cannot have member functions, cannot have static data members, and all their data members must be public. The only declarations allowed are non-static data members.

Members of an anonymous union are injected in the enclosing scope (and must not conflict with other names declared there).

Namespace-scope anonymous unions must be declared static unless they appear in an unnamed namespace.

Union-like classes
A union-like class is either a union, or a (non-union) class that has at least one anonymous union as a member. A union-like class has a set of variant members:
 * the non-static data members of its member anonymous unions;
 * in addition, if the union-like class is a union, its non-static data members that are not anonymous unions.

Union-like classes can be used to implement.