Skip to content

Commit 5a2816c

Browse files
author
Colin Robertson
committed
Fix for cpp-docs issue 2390 stray char in sample
1 parent e5ea538 commit 5a2816c

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

docs/cpp/unions.md

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
11
---
2-
title: "Unions"
3-
ms.date: "05/06/2019"
2+
title: "union"
3+
ms.date: "08/18/2020"
44
f1_keywords: ["union_cpp"]
5-
helpviewer_keywords: ["class types [C++], unions as", "union keyword [C++]"]
5+
helpviewer_keywords: ["class type [C++], union as", "union keyword [C++]"]
66
ms.assetid: 25c4e219-fcbb-4b7b-9b64-83f3252a92ca
7+
no-loc: ["union", "struct", "enum", "class"]
78
---
8-
# Unions
9+
# `union`
910

1011
> [!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.
1213
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.
1415

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.
1617

1718
## Syntax
1819

19-
```cpp
20-
union [name] { member-list };
21-
```
20+
> **`union`** *`class-name`*<sub>opt</sub> **`{`** *`member-list`* **`};`**
2221
2322
### Parameters
2423

25-
*name*<br/>
24+
*`class-name`*<br/>
2625
The type name given to the union.
2726

28-
*member-list*<br/>
29-
Members that the union can contain. See Remarks.
30-
31-
## Remarks
27+
*`member-list`*<br/>
28+
Members that the union can contain.
3229

33-
## Declaring a Union
30+
## Declare a union
3431

35-
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:
3633

3734
```cpp
3835
// declaring_a_union.cpp
@@ -53,9 +50,9 @@ int main()
5350
}
5451
```
5552

56-
## Using unions
53+
## Use a union
5754

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:
5956

6057
```cpp
6158
#include <queue>
@@ -138,15 +135,15 @@ void Initialize()
138135
}
139136
```
140137
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.
142139
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.
144141
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.
146143
147-
## Unrestricted Unions (C++11)
144+
## Unrestricted union (C++11)
148145
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:
150147
151148
```cpp
152149
// for MyVariant
@@ -504,6 +501,9 @@ int main()
504501
char c;
505502
cin >> c;
506503
}
504+
```
505+
506+
```cpp
507507
#include <queue>
508508
#include <iostream>
509509
using namespace std;
@@ -592,9 +592,9 @@ private:
592592
};
593593
```
594594
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.
596596
597-
## Initializing unions
597+
## Initialize a union
598598
599599
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.
600600
@@ -614,7 +614,7 @@ int main()
614614
union NumericType Values = { 10 }; // iValue = 10
615615
cout << Values.iValue << endl;
616616
Values.dValue = 3.1416;
617-
cout << Values.dValue) << endl;
617+
cout << Values.dValue << endl;
618618
}
619619
/* Output:
620620
10
@@ -625,25 +625,25 @@ int main()
625625
The `NumericType` union is arranged in memory (conceptually) as shown in the following figure.
626626

627627
![Storage of data in a numeric type union](../cpp/media/vc38ul1.png "Storage of data in a NumericType union") <br/>
628-
Storage of Data in NumericType Union
628+
Storage of data in a `NumericType` union
629629

630-
## <a name="anonymous_unions"></a> Anonymous unions
630+
## <a name="anonymous_union"></a> Anonymous union
631631

632-
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`*.
633633

634634
```cpp
635635
union { member-list }
636636
```
637637
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.
639639
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:
641641
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`**.
643643
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.
645645
646-
- They cannot have member functions.
646+
- It can't have member functions.
647647
648648
## See also
649649

0 commit comments

Comments
 (0)