From Jason Turner

[class.union.general]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpvnawhlb9/{from.md → to.md} +22 -21
tmp/tmpvnawhlb9/{from.md → to.md} RENAMED
@@ -10,13 +10,13 @@ non-static data members can be stored in a union at any time.
10
 
11
  [*Note 1*: One special guarantee is made in order to simplify the use
12
  of unions: If a standard-layout union contains several standard-layout
13
  structs that share a common initial sequence [[class.mem]], and if a
14
  non-static data member of an object of this standard-layout union type
15
- is active and is one of the standard-layout structs, it is permitted to
16
- inspect the common initial sequence of any of the standard-layout struct
17
- members; see  [[class.mem]]. — *end note*]
18
 
19
  The size of a union is sufficient to contain the largest of its
20
  non-static data members. Each non-static data member is allocated as if
21
  it were the sole member of a non-union class.
22
 
@@ -26,22 +26,20 @@ consequence, all non-static data members of a union object have the same
26
  address. — *end note*]
27
 
28
  A union can have member functions (including constructors and
29
  destructors), but it shall not have virtual [[class.virtual]] functions.
30
  A union shall not have base classes. A union shall not be used as a base
31
- class. If a union contains a non-static data member of reference type
32
  the program is ill-formed.
33
 
34
  [*Note 3*:
35
 
36
- Absent default member initializers [[class.mem]], if any non-static data
37
- member of a union has a non-trivial default constructor
38
- [[class.default.ctor]], copy constructor, move constructor
39
- [[class.copy.ctor]], copy assignment operator, move assignment operator
40
- [[class.copy.assign]], or destructor [[class.dtor]], the corresponding
41
- member function of the union must be user-provided or it will be
42
- implicitly deleted [[dcl.fct.def.delete]] for the union.
43
 
44
  [*Example 1*:
45
 
46
  Consider the following union:
47
 
@@ -53,13 +51,14 @@ union U {
53
  };
54
  ```
55
 
56
  Since `std::string` [[string.classes]] declares non-trivial versions of
57
  all of the special member functions, `U` will have an implicitly deleted
58
- default constructor, copy/move constructor, copy/move assignment
59
- operator, and destructor. To use `U`, some or all of these member
60
- functions must be user-provided.
 
61
 
62
  — *end example*]
63
 
64
  — *end note*]
65
 
@@ -76,17 +75,19 @@ lifetime of that union member, as described below. For an expression
76
  subscripting operator, S(E) is S(A) if `A` is of array type, S(B) if
77
  `B` is of array type, and empty otherwise.
78
  - Otherwise, S(E) is empty.
79
 
80
  In an assignment expression of the form `E1 = E2` that uses either the
81
- built-in assignment operator [[expr.ass]] or a trivial assignment
82
- operator [[class.copy.assign]], for each element `X` of S(`E1`), if
83
- modification of `X` would have undefined behavior under  [[basic.life]],
84
- an object of the type of `X` is implicitly created in the nominated
85
- storage; no initialization is performed and the beginning of its
86
- lifetime is sequenced after the value computation of the left and right
87
- operands and before the assignment.
 
 
88
 
89
  [*Note 4*: This ends the lifetime of the previously-active member of
90
  the union, if any [[basic.life]]. — *end note*]
91
 
92
  [*Example 2*:
 
10
 
11
  [*Note 1*: One special guarantee is made in order to simplify the use
12
  of unions: If a standard-layout union contains several standard-layout
13
  structs that share a common initial sequence [[class.mem]], and if a
14
  non-static data member of an object of this standard-layout union type
15
+ is active and is one of the standard-layout structs, the common initial
16
+ sequence of any of the standard-layout struct members can be inspected;
17
+ see  [[class.mem]]. — *end note*]
18
 
19
  The size of a union is sufficient to contain the largest of its
20
  non-static data members. Each non-static data member is allocated as if
21
  it were the sole member of a non-union class.
22
 
 
26
  address. — *end note*]
27
 
28
  A union can have member functions (including constructors and
29
  destructors), but it shall not have virtual [[class.virtual]] functions.
30
  A union shall not have base classes. A union shall not be used as a base
31
+ class. If a union contains a non-static data member of reference type,
32
  the program is ill-formed.
33
 
34
  [*Note 3*:
35
 
36
+ If any non-static data member of a union has a non-trivial copy
37
+ constructor, move constructor [[class.copy.ctor]], copy assignment
38
+ operator, or move assignment operator [[class.copy.assign]], the
39
+ corresponding member function of the union must be user-provided or it
40
+ will be implicitly deleted [[dcl.fct.def.delete]] for the union.
 
 
41
 
42
  [*Example 1*:
43
 
44
  Consider the following union:
45
 
 
51
  };
52
  ```
53
 
54
  Since `std::string` [[string.classes]] declares non-trivial versions of
55
  all of the special member functions, `U` will have an implicitly deleted
56
+ copy/move constructor and copy/move assignment operator. The default
57
+ constructor and destructor of `U` are both trivial even though
58
+ `std::string` has a non-trivial default constructor and a non-trivial
59
+ destructor.
60
 
61
  — *end example*]
62
 
63
  — *end note*]
64
 
 
75
  subscripting operator, S(E) is S(A) if `A` is of array type, S(B) if
76
  `B` is of array type, and empty otherwise.
77
  - Otherwise, S(E) is empty.
78
 
79
  In an assignment expression of the form `E1 = E2` that uses either the
80
+ built-in assignment operator [[expr.assign]] or a trivial assignment
81
+ operator [[class.copy.assign]], for each element `X` of S(`E1`) and each
82
+ anonymous union member `X` [[class.union.anon]] that is a member of a
83
+ union and has such an element as an immediate subobject (recursively),
84
+ if modification of `X` would have undefined behavior under 
85
+ [[basic.life]], an object of the type of `X` is implicitly created in
86
+ the nominated storage; no initialization is performed and the beginning
87
+ of its lifetime is sequenced after the value computation of the left and
88
+ right operands and before the assignment.
89
 
90
  [*Note 4*: This ends the lifetime of the previously-active member of
91
  the union, if any [[basic.life]]. — *end note*]
92
 
93
  [*Example 2*: