You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
helpviewer_keywords: ["class type [C++], union as", "union keyword [C++]"]
6
6
ms.assetid: 25c4e219-fcbb-4b7b-9b64-83f3252a92ca
7
+
no-loc: ["union", "struct", "enum", "class"]
7
8
---
8
-
# Unions
9
+
# `union`
9
10
10
11
> [!NOTE]
11
-
> In C++17 and later, the **std::variant** class is a type-safe alternative for unions.
12
+
> In C++17 and later, the `std::variant` class is a type-safe alternative for a union.
12
13
13
-
A **`union`** is a user-defined type in which all members share the same memory location. This means that at any given time a union can contain no more than one object from its list of members. It also means that no matter how many members a union has, it always uses only enough memory to store the largest member.
14
+
A **`union`** is a user-defined type in which all members share the same memory location. This definition means that at any given time, a union can contain no more than one object from its list of members. It also means that no matter how many members a union has, it always uses only enough memory to store the largest member.
14
15
15
-
Unions can be useful for conserving memory when you have lots of objects and/or limited memory. However they require extra care to use correctly because you are responsible for ensuring that you always access the last member that was written to. If any member types have a non-trivial constructor, then you must write additional code to explicitly construct and destroy that member. Before using a union, consider whether the problem you are trying to solve could be better expressed by using a base class and derived classes.
16
+
A union can be useful for conserving memory when you have lots of objects and limited memory. However, a union requires extra care to use correctly. You're responsible for ensuring that you always access the same member you assigned. If any member types have a non-trivial constructor, then you must write additional code to explicitly construct and destroy that member. Before you use a union, consider whether the problem you're trying to solve could be better expressed by using a base class and derived class types.
Begin the declaration of a union with the **`union`** keyword, and enclose the member list in curly braces:
32
+
Begin the declaration of a union by using the **`union`** keyword, and enclose the member list in curly braces:
36
33
37
34
```cpp
38
35
// declaring_a_union.cpp
@@ -53,9 +50,9 @@ int main()
53
50
}
54
51
```
55
52
56
-
## Using unions
53
+
## Use a union
57
54
58
-
In the previous example, any code that accesses the union needs to know which member is holding the data. The most common solution to this problem is to enclose the union in a struct along with an additional enum member that indicates the type of the data currently being stored in the union. This is called a *discriminated union* and the following example shows the basic pattern.
55
+
In the previous example, any code that accesses the union needs to know which member holds the data. The most common solution to this problem is called a *discriminated union*. It encloses the union in a struct, and includes an enum member that indicates the member type currently stored in the union. The following example shows the basic pattern:
59
56
60
57
```cpp
61
58
#include<queue>
@@ -138,15 +135,15 @@ void Initialize()
138
135
}
139
136
```
140
137
141
-
In the previous example, note that the union in the Input struct has no name. This is an anonymous union and its members can be accessed as if they were direct members of the struct. For more information about anonymous unions, see the section below.
138
+
In the previous example, the union in the `Input` struct has no name, so it's called an *anonymous* union. Its members can be accessed directly as if they're members of the struct. For more information about how to use an anonymous union, see the [Anonymous union](#anonymous_union) section.
142
139
143
-
Of course, the previous example shows a problem that could also be solved by using classes that derive from a common base class, and branching your code based on the runtime type of each object in the container. This may result in code that easier to maintain and understand, but it might also be slower than using unions. Also, with a union, you can store completely unrelated types, and dynamically change the type of the value that is stored without changing the type of the union variable itself. Thus you can create a heterogeneous array of MyUnionType whose elements store different values of different types.
140
+
The previous example shows a problem that you could also solve by using class types that derive from a common base class. You could branch your code based on the runtime type of each object in the container. Your code might be easier to maintain and understand, but it might also be slower than using a union. Also, with a union, you can store unrelated types. A union lets you dynamically change the type of the stored value without changing the type of the union variable itself. For example, you could create a heterogeneous array of `MyUnionType`, whose elements store different values of different types.
144
141
145
-
Note that the `Input` struct in the preceding example can be easily misused. It is completely up to the user to use the discriminator correctly to access the member that holds the data. You can protect against misuse by making the union private and providing special access functions, as shown in the next example.
142
+
It's easy to misuse the `Input` struct in the example. It's up to the user to use the discriminator correctly to access the member that holds the data. You can protect against misuse by making the union **`private`** and providing special access functions, as shown in the next example.
146
143
147
-
## Unrestricted Unions (C++11)
144
+
## Unrestricted union (C++11)
148
145
149
-
In C++03 and earlier a union can contain non-static data members with class type as long as the type has no user provided constructors, destructors or assignment operators. In C++11, these restrictions are removed. If you include such a member in your union then the compiler will automatically mark any special member functions that are not user provided as deleted. If the union is an anonymous union inside a class or struct, then any special member functions of the class or struct that are not user provided are marked as deleted. The following example shows how to handle the case where one of the members of the union has a member that requires this special treatment:
146
+
In C++03 and earlier, a union can contain non-static data members that have a class type, as long as the type has no user provided constructors, destructors, or assignment operators. In C++11, these restrictions are removed. If you include such a member in your union, the compiler automatically marks any special member functions that aren't user provided as **`deleted`**. If the union is an anonymous union inside a class or struct, then any special member functions of the class or struct that aren't user provided are marked as **`deleted`**. The following example shows how to handle this case. One of the members of the union has a member that requires this special treatment:
150
147
151
148
```cpp
152
149
// for MyVariant
@@ -504,6 +501,9 @@ int main()
504
501
char c;
505
502
cin >> c;
506
503
}
504
+
```
505
+
506
+
```cpp
507
507
#include<queue>
508
508
#include<iostream>
509
509
usingnamespacestd;
@@ -592,9 +592,9 @@ private:
592
592
};
593
593
```
594
594
595
-
Unions cannot store references. Unions don’t support inheritance, therefore a union itself cannot be used as a base class, or inherit from another class, or have virtual functions.
595
+
A union can't store a reference. A union also doesn’t support inheritance. That means you can't use a union as a base class, or inherit from another class, or have virtual functions.
596
596
597
-
## Initializing unions
597
+
## Initialize a union
598
598
599
599
You can declare and initialize a union in the same statement by assigning an expression enclosed in braces. The expression is evaluated and assigned to the first field of the union.
600
600
@@ -614,7 +614,7 @@ int main()
614
614
union NumericType Values = { 10 }; // iValue = 10
615
615
cout << Values.iValue << endl;
616
616
Values.dValue = 3.1416;
617
-
cout << Values.dValue) << endl;
617
+
cout << Values.dValue << endl;
618
618
}
619
619
/* Output:
620
620
10
@@ -625,25 +625,25 @@ int main()
625
625
The `NumericType` union is arranged in memory (conceptually) as shown in the following figure.
626
626
627
627
 <br/>
Anonymous unions are unions that are declared without a *class-name* or *declarator-list*.
632
+
An anonymous union is one declared without a *`class-name`* or *`declarator-list`*.
633
633
634
634
```cpp
635
635
union { member-list }
636
636
```
637
637
638
-
Names declared in an anonymous union are used directly, like nonmember variables. Therefore, the names declared in an anonymous union must be unique in the surrounding scope.
638
+
Names declared in an anonymous union are used directly, like nonmember variables. It implies that the names declared in an anonymous union must be unique in the surrounding scope.
639
639
640
-
In addition to the restrictions for named unions, anonymous unions are subject to these additional restrictions:
640
+
An anonymous union is subject to these additional restrictions:
641
641
642
-
- They must also be declared as **`static`** if declared in file or namespace scope.
642
+
- If declared in file or namespace scope, it must also be declared as **`static`**.
643
643
644
-
- They can have only **`public`** members; **`private`** and **`protected`** members in anonymous unions generate errors.
644
+
- It can have only **`public`** members; **`private`** and **`protected`** members in an anonymous union generates errors.
0 commit comments