From Jason Turner

[dcl.init.aggr]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp8e1e4vce/{from.md → to.md} +181 -55
tmp/tmp8e1e4vce/{from.md → to.md} RENAMED
@@ -1,23 +1,40 @@
1
  ### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
2
 
3
- An *aggregate* is an array or a class (Clause  [[class]]) with no
4
- user-provided constructors ([[class.ctor]]), no private or protected
5
- non-static data members (Clause  [[class.access]]), no base classes
6
- (Clause  [[class.derived]]), and no virtual functions (
7
- [[class.virtual]]).
8
 
9
- When an aggregate is initialized by an initializer list, as specified
10
- in  [[dcl.init.list]], the elements of the initializer list are taken as
11
- initializers for the members of the aggregate, in increasing subscript
12
- or member order. Each member is copy-initialized from the corresponding
13
- *initializer-clause*. If the *initializer-clause* is an expression and a
14
- narrowing conversion ([[dcl.init.list]]) is required to convert the
15
- expression, the program is ill-formed. If an *initializer-clause* is
16
- itself an initializer list, the member is list-initialized, which will
17
- result in a recursive application of the rules in this section if the
18
- member is an aggregate.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  ``` cpp
21
  struct A {
22
  int x;
23
  struct B {
@@ -27,29 +44,79 @@ struct A {
27
  } a = { 1, { 2, 3 } };
28
  ```
29
 
30
  initializes `a.x` with 1, `a.b.i` with 2, `a.b.j` with 3.
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  An aggregate that is a class can also be initialized with a single
33
  expression not enclosed in braces, as described in  [[dcl.init]].
34
 
35
- An array of unknown size initialized with a brace-enclosed
36
  *initializer-list* containing `n` *initializer-clause*s, where `n` shall
37
- be greater than zero, is defined as having `n` elements (
38
  [[dcl.array]]).
39
 
 
 
40
  ``` cpp
41
  int x[] = { 1, 3, 5 };
42
  ```
43
 
44
  declares and initializes `x` as a one-dimensional array that has three
45
  elements since no size was specified and there are three initializers.
 
 
 
46
  An empty initializer list `{}` shall not be used as the
47
- *initializer-clause * for an array of unknown bound.[^15]
48
 
49
- Static data members and anonymous bit-fields are not considered members
50
- of the class for purposes of aggregate initialization.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  ``` cpp
53
  struct A {
54
  int i;
55
  static int s;
@@ -59,27 +126,47 @@ struct A {
59
  } a = { 1, 2, 3 };
60
  ```
61
 
62
  Here, the second initializer 2 initializes `a.j` and not the static data
63
  member `A::s`, and the third initializer 3 initializes `a.k` and not the
64
- anonymous bit-field before it.
 
 
 
 
65
 
66
  An *initializer-list* is ill-formed if the number of
67
- *initializer-clause*s exceeds the number of members or elements to
68
- initialize.
 
69
 
70
  ``` cpp
71
  char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
72
  ```
73
 
74
  is ill-formed.
75
 
 
 
76
  If there are fewer *initializer-clause*s in the list than there are
77
- members in the aggregate, then each member not explicitly initialized
78
- shall be initialized from its *brace-or-equal-initializer* or, if there
79
- is no *brace-or-equal-initializer*, from an empty initializer list (
80
- [[dcl.init.list]]).
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  ``` cpp
83
  struct S { int a; const char* b; int c; int d = b[a]; };
84
  S ss = { 1, "asdf" };
85
  ```
@@ -94,17 +181,39 @@ X a[] = { 1, 2, 3, 4, 5, 6 };
94
  X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
95
  ```
96
 
97
  `a` and `b` have the same value
98
 
99
- If an aggregate class `C` contains a subaggregate member `m` that has no
100
- members for purposes of aggregate initialization, the
101
- *initializer-clause* for `m` shall not be omitted from an
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  *initializer-list* for an object of type `C` unless the
103
- *initializer-clause*s for all members of `C` following `m` are also
104
  omitted.
105
 
 
 
106
  ``` cpp
107
  struct S { } s;
108
  struct A {
109
  S s1;
110
  int i1;
@@ -118,17 +227,18 @@ struct A {
118
  s, // Required initialization
119
  0
120
  }; // Initialization not required for A::s3 because A::i3 is also not initialized
121
  ```
122
 
123
- If an incomplete or empty *initializer-list* leaves a member of
124
- reference type uninitialized, the program is ill-formed.
125
 
126
  When initializing a multi-dimensional array, the *initializer-clause*s
127
  initialize the elements with the last (rightmost) index of the array
128
  varying the fastest ([[dcl.array]]).
129
 
 
 
130
  ``` cpp
131
  int x[2][2] = { 3, 1, 4, 2 };
132
  ```
133
 
134
  initializes `x[0][0]` to `3`, `x[0][1]` to `1`, `x[1][0]` to `4`, and
@@ -141,20 +251,24 @@ float y[4][3] = {
141
  ```
142
 
143
  initializes the first column of `y` (regarded as a two-dimensional
144
  array) and leaves the rest zero.
145
 
 
 
146
  Braces can be elided in an *initializer-list* as follows. If the
147
  *initializer-list* begins with a left brace, then the succeeding
148
- comma-separated list of *initializer-clause*s initializes the members of
149
- a subaggregate; it is erroneous for there to be more
150
- *initializer-clause*s than members. If, however, the *initializer-list*
151
  for a subaggregate does not begin with a left brace, then only enough
152
- *initializer-clause*s from the list are taken to initialize the members
153
  of the subaggregate; any remaining *initializer-clause*s are left to
154
- initialize the next member of the aggregate of which the current
155
- subaggregate is a member.
 
 
156
 
157
  ``` cpp
158
  float y[4][3] = {
159
  { 1, 3, 5 },
160
  { 2, 4, 6 },
@@ -180,19 +294,24 @@ float y[4][3] = {
180
 
181
  The initializer for `y` begins with a left brace, but the one for `y[0]`
182
  does not, therefore three elements from the list are used. Likewise the
183
  next three are taken successively for `y[1]` and `y[2]`.
184
 
 
 
185
  All implicit type conversions (Clause  [[conv]]) are considered when
186
- initializing the aggregate member with an *assignment-expression*. If
187
- the *assignment-expression* can initialize a member, the member is
188
- initialized. Otherwise, if the member is itself a subaggregate, brace
189
  elision is assumed and the *assignment-expression* is considered for the
190
- initialization of the first member of the subaggregate. As specified
191
- above, brace elision cannot apply to subaggregates with no members for
192
- purposes of aggregate initialization; an *initializer-clause* for the
193
- entire subobject is required.
 
 
 
194
 
195
  ``` cpp
196
  struct A {
197
  int i;
198
  operator int();
@@ -207,30 +326,37 @@ B b = { 4, a, a };
207
 
208
  Braces are elided around the *initializer-clause* for `b.a1.i`. `b.a1.i`
209
  is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
210
  initialized with whatever `a.operator int()` returns.
211
 
212
- An aggregate array or an aggregate class may contain members of a class
213
- type with a user-provided constructor ([[class.ctor]]). Initialization
214
- of these aggregate objects is described in  [[class.expl.init]].
215
 
216
- Whether the initialization of aggregates with static storage duration is
217
- static or dynamic is specified in  [[basic.start.init]] and 
218
- [[stmt.dcl]].
 
 
 
 
 
219
 
220
  When a union is initialized with a brace-enclosed initializer, the
221
  braces shall only contain an *initializer-clause* for the first
222
  non-static data member of the union.
223
 
 
 
224
  ``` cpp
225
  union u { int a; const char* b; };
226
  u a = { 1 };
227
  u b = a;
228
  u c = 1; // error
229
  u d = { 0, "asdf" }; // error
230
  u e = { "asdf" }; // error
231
  ```
232
 
233
- As described above, the braces around the *initializer-clause* for a
234
- union member can be omitted if the union is a member of another
235
- aggregate.
 
 
236
 
 
1
  ### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
2
 
3
+ An *aggregate* is an array or a class (Clause  [[class]]) with
 
 
 
 
4
 
5
+ - no user-provided, `explicit`, or inherited constructors (
6
+ [[class.ctor]]),
7
+ - no private or protected non-static data members (Clause 
8
+ [[class.access]]),
9
+ - no virtual functions ([[class.virtual]]), and
10
+ - no virtual, private, or protected base classes ([[class.mi]]).
11
+
12
+ [*Note 1*: Aggregate initialization does not allow accessing protected
13
+ and private base class’ members or constructors. *end note*]
14
+
15
+ The *elements* of an aggregate are:
16
+
17
+ - for an array, the array elements in increasing subscript order, or
18
+ - for a class, the direct base classes in declaration order, followed by
19
+ the direct non-static data members ([[class.mem]]) that are not
20
+ members of an anonymous union, in declaration order.
21
+
22
+ When an aggregate is initialized by an initializer list as specified in 
23
+ [[dcl.init.list]], the elements of the initializer list are taken as
24
+ initializers for the elements of the aggregate, in order. Each element
25
+ is copy-initialized from the corresponding *initializer-clause*. If the
26
+ *initializer-clause* is an expression and a narrowing conversion (
27
+ [[dcl.init.list]]) is required to convert the expression, the program is
28
+ ill-formed.
29
+
30
+ [*Note 2*: If an *initializer-clause* is itself an initializer list,
31
+ the element is list-initialized, which will result in a recursive
32
+ application of the rules in this section if the element is an
33
+ aggregate. — *end note*]
34
+
35
+ [*Example 1*:
36
 
37
  ``` cpp
38
  struct A {
39
  int x;
40
  struct B {
 
44
  } a = { 1, { 2, 3 } };
45
  ```
46
 
47
  initializes `a.x` with 1, `a.b.i` with 2, `a.b.j` with 3.
48
 
49
+ ``` cpp
50
+ struct base1 { int b1, b2 = 42; };
51
+ struct base2 {
52
+ base2() {
53
+ b3 = 42;
54
+ }
55
+ int b3;
56
+ };
57
+ struct derived : base1, base2 {
58
+ int d;
59
+ };
60
+
61
+ derived d1{{1, 2}, {}, 4};
62
+ derived d2{{}, {}, 4};
63
+ ```
64
+
65
+ initializes `d1.b1` with 1, `d1.b2` with 2, `d1.b3` with 42, `d1.d` with
66
+ 4, and `d2.b1` with 0, `d2.b2` with 42, `d2.b3` with 42, `d2.d` with 4.
67
+
68
+ — *end example*]
69
+
70
  An aggregate that is a class can also be initialized with a single
71
  expression not enclosed in braces, as described in  [[dcl.init]].
72
 
73
+ An array of unknown bound initialized with a brace-enclosed
74
  *initializer-list* containing `n` *initializer-clause*s, where `n` shall
75
+ be greater than zero, is defined as having `n` elements (
76
  [[dcl.array]]).
77
 
78
+ [*Example 2*:
79
+
80
  ``` cpp
81
  int x[] = { 1, 3, 5 };
82
  ```
83
 
84
  declares and initializes `x` as a one-dimensional array that has three
85
  elements since no size was specified and there are three initializers.
86
+
87
+ — *end example*]
88
+
89
  An empty initializer list `{}` shall not be used as the
90
+ *initializer-clause* for an array of unknown bound.[^13]
91
 
92
+ [*Note 3*:
93
+
94
+ A default member initializer does not determine the bound for a member
95
+ array of unknown bound. Since the default member initializer is ignored
96
+ if a suitable *mem-initializer* is present ([[class.base.init]]), the
97
+ default member initializer is not considered to initialize the array of
98
+ unknown bound.
99
+
100
+ [*Example 3*:
101
+
102
+ ``` cpp
103
+ struct S {
104
+ int y[] = { 0 }; // error: non-static data member of incomplete type
105
+ };
106
+ ```
107
+
108
+ — *end example*]
109
+
110
+ — *end note*]
111
+
112
+ [*Note 4*:
113
+
114
+ Static data members and unnamed bit-fields are not considered elements
115
+ of the aggregate.
116
+
117
+ [*Example 4*:
118
 
119
  ``` cpp
120
  struct A {
121
  int i;
122
  static int s;
 
126
  } a = { 1, 2, 3 };
127
  ```
128
 
129
  Here, the second initializer 2 initializes `a.j` and not the static data
130
  member `A::s`, and the third initializer 3 initializes `a.k` and not the
131
+ unnamed bit-field before it.
132
+
133
+ — *end example*]
134
+
135
+ — *end note*]
136
 
137
  An *initializer-list* is ill-formed if the number of
138
+ *initializer-clause*s exceeds the number of elements to initialize.
139
+
140
+ [*Example 5*:
141
 
142
  ``` cpp
143
  char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
144
  ```
145
 
146
  is ill-formed.
147
 
148
+ — *end example*]
149
+
150
  If there are fewer *initializer-clause*s in the list than there are
151
+ elements in a non-union aggregate, then each element not explicitly
152
+ initialized is initialized as follows:
153
+
154
+ - If the element has a default member initializer ([[class.mem]]), the
155
+ element is initialized from that initializer.
156
+ - Otherwise, if the element is not a reference, the element is
157
+ copy-initialized from an empty initializer list ([[dcl.init.list]]).
158
+ - Otherwise, the program is ill-formed.
159
+
160
+ If the aggregate is a union and the initializer list is empty, then
161
+
162
+ - if any variant member has a default member initializer, that member is
163
+ initialized from its default member initializer;
164
+ - otherwise, the first member of the union (if any) is copy-initialized
165
+ from an empty initializer list.
166
+
167
+ [*Example 6*:
168
 
169
  ``` cpp
170
  struct S { int a; const char* b; int c; int d = b[a]; };
171
  S ss = { 1, "asdf" };
172
  ```
 
181
  X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
182
  ```
183
 
184
  `a` and `b` have the same value
185
 
186
+ *end example*]
187
+
188
+ If a reference member is initialized from its default member initializer
189
+ and a potentially-evaluated subexpression thereof is an aggregate
190
+ initialization that would use that default member initializer, the
191
+ program is ill-formed.
192
+
193
+ [*Example 7*:
194
+
195
+ ``` cpp
196
+ struct A;
197
+ extern A a;
198
+ struct A {
199
+ const A& a1 { A{a,a} }; // OK
200
+ const A& a2 { A{} }; // error
201
+ };
202
+ A a{a,a}; // OK
203
+ ```
204
+
205
+ — *end example*]
206
+
207
+ If an aggregate class `C` contains a subaggregate element `e` with no
208
+ elements, the *initializer-clause* for `e` shall not be omitted from an
209
  *initializer-list* for an object of type `C` unless the
210
+ *initializer-clause*s for all elements of `C` following `e` are also
211
  omitted.
212
 
213
+ [*Example 8*:
214
+
215
  ``` cpp
216
  struct S { } s;
217
  struct A {
218
  S s1;
219
  int i1;
 
227
  s, // Required initialization
228
  0
229
  }; // Initialization not required for A::s3 because A::i3 is also not initialized
230
  ```
231
 
232
+ *end example*]
 
233
 
234
  When initializing a multi-dimensional array, the *initializer-clause*s
235
  initialize the elements with the last (rightmost) index of the array
236
  varying the fastest ([[dcl.array]]).
237
 
238
+ [*Example 9*:
239
+
240
  ``` cpp
241
  int x[2][2] = { 3, 1, 4, 2 };
242
  ```
243
 
244
  initializes `x[0][0]` to `3`, `x[0][1]` to `1`, `x[1][0]` to `4`, and
 
251
  ```
252
 
253
  initializes the first column of `y` (regarded as a two-dimensional
254
  array) and leaves the rest zero.
255
 
256
+ — *end example*]
257
+
258
  Braces can be elided in an *initializer-list* as follows. If the
259
  *initializer-list* begins with a left brace, then the succeeding
260
+ comma-separated list of *initializer-clause*s initializes the elements
261
+ of a subaggregate; it is erroneous for there to be more
262
+ *initializer-clause*s than elements. If, however, the *initializer-list*
263
  for a subaggregate does not begin with a left brace, then only enough
264
+ *initializer-clause*s from the list are taken to initialize the elements
265
  of the subaggregate; any remaining *initializer-clause*s are left to
266
+ initialize the next element of the aggregate of which the current
267
+ subaggregate is an element.
268
+
269
+ [*Example 10*:
270
 
271
  ``` cpp
272
  float y[4][3] = {
273
  { 1, 3, 5 },
274
  { 2, 4, 6 },
 
294
 
295
  The initializer for `y` begins with a left brace, but the one for `y[0]`
296
  does not, therefore three elements from the list are used. Likewise the
297
  next three are taken successively for `y[1]` and `y[2]`.
298
 
299
+ — *end example*]
300
+
301
  All implicit type conversions (Clause  [[conv]]) are considered when
302
+ initializing the element with an *assignment-expression*. If the
303
+ *assignment-expression* can initialize an element, the element is
304
+ initialized. Otherwise, if the element is itself a subaggregate, brace
305
  elision is assumed and the *assignment-expression* is considered for the
306
+ initialization of the first element of the subaggregate.
307
+
308
+ [*Note 5*: As specified above, brace elision cannot apply to
309
+ subaggregates with no elements; an *initializer-clause* for the entire
310
+ subobject is required. — *end note*]
311
+
312
+ [*Example 11*:
313
 
314
  ``` cpp
315
  struct A {
316
  int i;
317
  operator int();
 
326
 
327
  Braces are elided around the *initializer-clause* for `b.a1.i`. `b.a1.i`
328
  is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
329
  initialized with whatever `a.operator int()` returns.
330
 
331
+ *end example*]
 
 
332
 
333
+ [*Note 6*: An aggregate array or an aggregate class may contain
334
+ elements of a class type with a user-provided constructor (
335
+ [[class.ctor]]). Initialization of these aggregate objects is described
336
+ in  [[class.expl.init]]. — *end note*]
337
+
338
+ [*Note 7*: Whether the initialization of aggregates with static storage
339
+ duration is static or dynamic is specified in  [[basic.start.static]],
340
+ [[basic.start.dynamic]], and  [[stmt.dcl]]. — *end note*]
341
 
342
  When a union is initialized with a brace-enclosed initializer, the
343
  braces shall only contain an *initializer-clause* for the first
344
  non-static data member of the union.
345
 
346
+ [*Example 12*:
347
+
348
  ``` cpp
349
  union u { int a; const char* b; };
350
  u a = { 1 };
351
  u b = a;
352
  u c = 1; // error
353
  u d = { 0, "asdf" }; // error
354
  u e = { "asdf" }; // error
355
  ```
356
 
357
+ *end example*]
358
+
359
+ [*Note 8*: As described above, the braces around the
360
+ *initializer-clause* for a union member can be omitted if the union is a
361
+ member of another aggregate. — *end note*]
362