From Jason Turner

[class.union]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpcoub7sfz/{from.md → to.md} +37 -22
tmp/tmpcoub7sfz/{from.md → to.md} RENAMED
@@ -8,24 +8,24 @@ contains several standard-layout structs that share a common initial
8
  sequence ([[class.mem]]), and if an object of this standard-layout
9
  union type contains one of the standard-layout structs, it is permitted
10
  to inspect the common initial sequence of any of standard-layout struct
11
  members; see  [[class.mem]]. The size of a union is sufficient to
12
  contain the largest of its non-static data members. Each non-static data
13
- member is allocated as if it were the sole member of a struct.
 
14
 
15
  A union can have member functions (including constructors and
16
  destructors), but not virtual ([[class.virtual]]) functions. A union
17
  shall not have base classes. A union shall not be used as a base class.
18
  If a union contains a non-static data member of reference type the
19
- program is ill-formed. At most one non-static data member of a union may
20
- have a *brace-or-equal-initializer*. If any non-static data member of a
21
- union has a non-trivial default constructor ([[class.ctor]]), copy
22
- constructor ([[class.copy]]), move constructor ([[class.copy]]), copy
23
- assignment operator ([[class.copy]]), move assignment operator (
24
- [[class.copy]]), or destructor ([[class.dtor]]), the corresponding
25
- member function of the union must be user-provided or it will be
26
- implicitly deleted ([[dcl.fct.def.delete]]) for the union.
27
 
28
  Consider the following union:
29
 
30
  ``` cpp
31
  union U {
@@ -54,19 +54,19 @@ u.m.~M();
54
  new (&u.n) N;
55
  ```
56
 
57
  A union of the form
58
 
59
- is called an anonymous union; it defines an unnamed object of unnamed
60
  type. The *member-specification* of an anonymous union shall only define
61
- non-static data members. Nested types and functions cannot be declared
62
- within an anonymous union. The names of the members of an anonymous
63
- union shall be distinct from the names of any other entity in the scope
64
- in which the anonymous union is declared. For the purpose of name
65
- lookup, after the anonymous union definition, the members of the
66
- anonymous union are considered to have been defined in the scope in
67
- which the anonymous union is declared.
68
 
69
  ``` cpp
70
  void f() {
71
  union { int a; const char* p; };
72
  a = 1;
@@ -83,25 +83,40 @@ scope shall be declared with any storage class allowed for a block-scope
83
  variable, or with no storage class. A storage class is not allowed in a
84
  declaration of an anonymous union in a class scope. An anonymous union
85
  shall not have `private` or `protected` members (Clause 
86
  [[class.access]]). An anonymous union shall not have function members.
87
 
88
- A union for which objects or pointers are declared is not an anonymous
89
- union.
90
 
91
  ``` cpp
 
92
  union { int aa; char* p; } obj, *ptr = &obj;
93
  aa = 1; // error
94
  ptr->aa = 1; // OK
 
95
  ```
96
 
97
  The assignment to plain `aa` is ill-formed since the member name is not
98
  visible outside the union, and even if it were visible, it is not
99
  associated with any particular object. Initialization of unions with no
100
  user-declared constructors is described in ([[dcl.init.aggr]]).
101
 
102
  A *union-like class* is a union or a class that has an anonymous union
103
  as a direct member. A union-like class `X` has a set of *variant
104
- members*. If `X` is a union its variant members are the non-static data
105
- members; otherwise, its variant members are the non-static data members
106
- of all anonymous unions that are members of `X`.
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
 
8
  sequence ([[class.mem]]), and if an object of this standard-layout
9
  union type contains one of the standard-layout structs, it is permitted
10
  to inspect the common initial sequence of any of standard-layout struct
11
  members; see  [[class.mem]]. The size of a union is sufficient to
12
  contain the largest of its non-static data members. Each non-static data
13
+ member is allocated as if it were the sole member of a struct. All
14
+ non-static data members of a union object have the same address.
15
 
16
  A union can have member functions (including constructors and
17
  destructors), but not virtual ([[class.virtual]]) functions. A union
18
  shall not have base classes. A union shall not be used as a base class.
19
  If a union contains a non-static data member of reference type the
20
+ program is ill-formed. If any non-static data member of a union has a
21
+ non-trivial default constructor ([[class.ctor]]), copy constructor (
22
+ [[class.copy]]), move constructor ([[class.copy]]), copy assignment
23
+ operator ([[class.copy]]), move assignment operator ([[class.copy]]),
24
+ or destructor ([[class.dtor]]), the corresponding member function of
25
+ the union must be user-provided or it will be implicitly deleted (
26
+ [[dcl.fct.def.delete]]) for the union.
 
27
 
28
  Consider the following union:
29
 
30
  ``` cpp
31
  union U {
 
54
  new (&u.n) N;
55
  ```
56
 
57
  A union of the form
58
 
59
+ is called an *anonymous union*; it defines an unnamed object of unnamed
60
  type. The *member-specification* of an anonymous union shall only define
61
+ non-static data members. Nested types, anonymous unions, and functions
62
+ cannot be declared within an anonymous union. The names of the members
63
+ of an anonymous union shall be distinct from the names of any other
64
+ entity in the scope in which the anonymous union is declared. For the
65
+ purpose of name lookup, after the anonymous union definition, the
66
+ members of the anonymous union are considered to have been defined in
67
+ the scope in which the anonymous union is declared.
68
 
69
  ``` cpp
70
  void f() {
71
  union { int a; const char* p; };
72
  a = 1;
 
83
  variable, or with no storage class. A storage class is not allowed in a
84
  declaration of an anonymous union in a class scope. An anonymous union
85
  shall not have `private` or `protected` members (Clause 
86
  [[class.access]]). An anonymous union shall not have function members.
87
 
88
+ A union for which objects, pointers, or references are declared is not
89
+ an anonymous union.
90
 
91
  ``` cpp
92
+ void f() {
93
  union { int aa; char* p; } obj, *ptr = &obj;
94
  aa = 1; // error
95
  ptr->aa = 1; // OK
96
+ }
97
  ```
98
 
99
  The assignment to plain `aa` is ill-formed since the member name is not
100
  visible outside the union, and even if it were visible, it is not
101
  associated with any particular object. Initialization of unions with no
102
  user-declared constructors is described in ([[dcl.init.aggr]]).
103
 
104
  A *union-like class* is a union or a class that has an anonymous union
105
  as a direct member. A union-like class `X` has a set of *variant
106
+ members*. If `X` is a union, a non-static data member of `X` that is not
107
+ an anonymous union is a variant member of `X`. In addition, a non-static
108
+ data member of an anonymous union that is a member of `X` is also a
109
+ variant member of `X`. At most one variant member of a union may have a
110
+ *brace-or-equal-initializer*.
111
+
112
+ ``` cpp
113
+ union U {
114
+ int x = 0;
115
+ union { };
116
+ union {
117
+ int z;
118
+ int y = 1; // error: initialization for second variant member of U
119
+ };
120
+ };
121
+ ```
122