From Jason Turner

[class.union.anon]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp3v2q1np_/{from.md → to.md} +89 -0
tmp/tmp3v2q1np_/{from.md → to.md} RENAMED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Anonymous unions <a id="class.union.anon">[[class.union.anon]]</a>
2
+
3
+ A union of the form
4
+
5
+ is called an *anonymous union*; it defines an unnamed type and an
6
+ unnamed object of that type called an *anonymous union object*. Each
7
+ *member-declaration* in the *member-specification* of an anonymous union
8
+ shall either define a non-static data member or be a
9
+ *static_assert-declaration*.
10
+
11
+ [*Note 1*: Nested types, anonymous unions, and functions cannot be
12
+ declared within an anonymous union. — *end note*]
13
+
14
+ The names of the members of an anonymous union shall be distinct from
15
+ the names of any other entity in the scope in which the anonymous union
16
+ is declared. For the purpose of name lookup, after the anonymous union
17
+ definition, the members of the anonymous union are considered to have
18
+ been defined in the scope in which the anonymous union is declared.
19
+
20
+ [*Example 1*:
21
+
22
+ ``` cpp
23
+ void f() {
24
+ union { int a; const char* p; };
25
+ a = 1;
26
+ p = "Jennifer";
27
+ }
28
+ ```
29
+
30
+ Here `a` and `p` are used like ordinary (non-member) variables, but
31
+ since they are union members they have the same address.
32
+
33
+ — *end example*]
34
+
35
+ Anonymous unions declared in a named namespace or in the global
36
+ namespace shall be declared `static`. Anonymous unions declared at block
37
+ scope shall be declared with any storage class allowed for a block-scope
38
+ variable, or with no storage class. A storage class is not allowed in a
39
+ declaration of an anonymous union in a class scope. An anonymous union
40
+ shall not have `private` or `protected` members (Clause 
41
+ [[class.access]]). An anonymous union shall not have member functions.
42
+
43
+ A union for which objects, pointers, or references are declared is not
44
+ an anonymous union.
45
+
46
+ [*Example 2*:
47
+
48
+ ``` cpp
49
+ void f() {
50
+ union { int aa; char* p; } obj, *ptr = &obj;
51
+ aa = 1; // error
52
+ ptr->aa = 1; // OK
53
+ }
54
+ ```
55
+
56
+ The assignment to plain `aa` is ill-formed since the member name is not
57
+ visible outside the union, and even if it were visible, it is not
58
+ associated with any particular object.
59
+
60
+ — *end example*]
61
+
62
+ [*Note 2*: Initialization of unions with no user-declared constructors
63
+ is described in  [[dcl.init.aggr]]. — *end note*]
64
+
65
+ A *union-like class* is a union or a class that has an anonymous union
66
+ as a direct member. A union-like class `X` has a set of *variant
67
+ members*. If `X` is a union, a non-static data member of `X` that is not
68
+ an anonymous union is a variant member of `X`. In addition, a non-static
69
+ data member of an anonymous union that is a member of `X` is also a
70
+ variant member of `X`. At most one variant member of a union may have a
71
+ default member initializer.
72
+
73
+ [*Example 3*:
74
+
75
+ ``` cpp
76
+ union U {
77
+ int x = 0;
78
+ union {
79
+ int k;
80
+ };
81
+ union {
82
+ int z;
83
+ int y = 1; // error: initialization for second variant member of U
84
+ };
85
+ };
86
+ ```
87
+
88
+ — *end example*]
89
+