A union is a user-defined data type in which all members share the same memory location. Unlike aDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
struct or class where each member has its own distinct memory address, a union allocates only enough memory to hold its largest member, and its overall alignment is determined by the member with the strictest alignment requirement. Consequently, a union can store a valid value for only one of its non-static data members at any given time.
Memory Layout
Thesizeof a union is equal to the size of its largest data member, potentially padded to satisfy the alignment requirements of the overall union.
The Active Member and Undefined Behavior
In C++, the member that was most recently written to is known as the active member. Reading from a member other than the active member results in Undefined Behavior (UB). C++ strictly forbids using unions for type punning (interpreting the bit representation of one type as another type), which is a notable divergence from C.Anonymous Unions
An anonymous union is a union defined without a type name and without declaring an object of that type. The members of an anonymous union are injected directly into the enclosing scope and are accessed as if they were direct variables of that scope. If an anonymous union is declared at namespace scope (global scope), it must be explicitly declaredstatic.
Unrestricted Unions (C++11)
Prior to C++11, unions were restricted to Plain Old Data (POD) types. They could not contain members with non-trivial constructors, destructors, copy constructors, or assignment operators (e.g.,std::string or std::vector).
C++11 introduced unrestricted unions, allowing members with non-trivial special member functions. However, if a union contains a non-trivial member, the compiler implicitly deletes the union’s corresponding default special member functions (constructor, destructor, etc.). The developer assumes full responsibility for managing the object lifecycle using placement new to construct the active member and explicit destructor calls to destroy it.
std::variant (C++17)
Because managing the lifecycle of non-trivial members in unrestricted unions is highly error-prone, C++17 introduced std::variant. It is the standard, type-safe alternative to raw unions. std::variant automatically manages the construction and destruction of its active member, tracks which type is currently active, and prevents undefined behavior by throwing an exception (std::bad_variant_access) on invalid accesses.
Access Control and Member Functions
Like classes and structs, unions can have access specifiers (public, private, protected), member functions, constructors, and destructors. By default, all members of a union are public. However, unions have several fundamental restrictions:
- They cannot contain reference members.
- They cannot inherit from other classes.
- They cannot be used as base classes.
- They cannot contain
virtualfunctions.
Master C++ with Deep Grasping Methodology!Learn More





