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
> In C++17 and later, the **std::variant** class is a type-safe alternative for unions.
13
+
> In C++17 and later, the `std::variant` class is a type-safe alternative for a union.
12
14
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.
15
+
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
16
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.
17
+
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:
33
+
Begin the declaration of a union by using the **`union`** keyword, and enclose the member list in curly braces:
36
34
37
35
```cpp
38
36
// declaring_a_union.cpp
@@ -45,6 +43,7 @@ union RecordType // Declare a simple union type
45
43
double d;
46
44
int *int_ptr;
47
45
};
46
+
48
47
intmain()
49
48
{
50
49
RecordType t;
@@ -53,9 +52,9 @@ int main()
53
52
}
54
53
```
55
54
56
-
## Using unions
55
+
## Use a union
57
56
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.
57
+
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:
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.
139
+
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_unions) section.
142
140
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.
141
+
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
142
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.
143
+
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
144
147
-
## Unrestricted Unions (C++11)
145
+
## Unrestricted union (C++11)
148
146
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:
147
+
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:
// must use placement new because of string member!
555
-
new(&temp) TempData(td);
556
-
}
557
-
558
-
TempData GetTemp()
559
-
{
560
-
if (type == WeatherDataType::Temperature)
561
-
return temp;
562
-
else
563
-
throw logic_error("Can't return TempData when Input holds a WindData");
564
-
}
565
-
void SetWind(WindData wd)
566
-
{
567
-
// Explicitly delete struct member that has a
568
-
// non-trivial constructor
569
-
if (type == WeatherDataType::Temperature)
570
-
{
571
-
temp.StationId.~string();
572
-
}
573
-
wind = wd; //placement new not required.
574
-
}
575
-
WindData GetWind()
576
-
{
577
-
if (type == WeatherDataType::Wind)
578
-
{
579
-
return wind;
580
-
}
581
-
else
582
-
throw logic_error("Can't return WindData when Input holds a TempData");
583
-
}
584
-
585
-
private:
586
-
587
-
union
588
-
{
589
-
TempData temp;
590
-
WindData wind;
591
-
};
592
-
};
593
505
```
594
506
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.
507
+
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
508
597
-
## Initializing unions
509
+
## Initialize a union
598
510
599
511
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
512
@@ -614,7 +526,7 @@ int main()
614
526
union NumericType Values = { 10 }; // iValue = 10
615
527
cout << Values.iValue << endl;
616
528
Values.dValue = 3.1416;
617
-
cout << Values.dValue) << endl;
529
+
cout << Values.dValue << endl;
618
530
}
619
531
/* Output:
620
532
10
@@ -625,29 +537,27 @@ int main()
625
537
The `NumericType` union is arranged in memory (conceptually) as shown in the following figure.
626
538
627
539
 <br/>
Anonymous unions are unions that are declared without a *class-name* or *declarator-list*.
544
+
An anonymous union is one declared without a *`class-name`* or *`declarator-list`*.
633
545
634
-
```cpp
635
-
union { member-list }
636
-
```
546
+
> **`union {`***`member-list`***`}`**
637
547
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.
548
+
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
549
640
-
In addition to the restrictions for named unions, anonymous unions are subject to these additional restrictions:
550
+
An anonymous union is subject to these additional restrictions:
641
551
642
-
- They must also be declared as **`static`** if declared in file or namespace scope.
552
+
-If declared in file or namespace scope, it must also be declared as **`static`**.
643
553
644
-
- They can have only **`public`** members; **`private`** and **`protected`** members in anonymous unions generate errors.
554
+
-It can have only **`public`** members; having **`private`** and **`protected`** members in an anonymous union generates errors.
645
555
646
-
- They cannot have member functions.
556
+
-It can't have member functions.
647
557
648
558
## See also
649
559
650
560
[Classes and Structs](../cpp/classes-and-structs-cpp.md)<br/>
0 commit comments