From Jason Turner

[dcl.init.aggr]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmptt50yoc5/{from.md → to.md} +146 -103
tmp/tmptt50yoc5/{from.md → to.md} RENAMED
@@ -8,11 +8,11 @@ An *aggregate* is an array or a class [[class]] with
8
  - no private or protected direct base classes [[class.access.base]], and
9
  - no virtual functions [[class.virtual]] or virtual base classes
10
  [[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
@@ -28,13 +28,13 @@ initialized elements* of the aggregate are determined as follows:
28
  *designated-initializer-list*, the aggregate shall be of class type,
29
  the *identifier* in each *designator* shall name a direct non-static
30
  data member of the class, and the explicitly initialized elements of
31
  the aggregate are the elements that are, or contain, those members.
32
  - If the initializer list is a brace-enclosed *initializer-list*, the
33
- explicitly initialized elements of the aggregate are the first n
34
- elements of the aggregate, where n is the number of elements in the
35
- initializer list.
36
  - Otherwise, the initializer list must be `{}`, and there are no
37
  explicitly initialized elements.
38
 
39
  For each explicitly initialized element:
40
 
@@ -54,21 +54,26 @@ For each explicitly initialized element:
54
  } c = { .a = 1, .x = 3 };
55
  ```
56
 
57
  initializes `c.a` with 1 and `c.x` with 3.
58
  — *end example*]
59
- - Otherwise, the element is copy-initialized from the corresponding
60
- *initializer-clause* or is initialized with the
61
  *brace-or-equal-initializer* of the corresponding
62
  *designated-initializer-clause*. If that initializer is of the form
63
- *assignment-expression* or `= `*assignment-expression* and a narrowing
64
- conversion [[dcl.init.list]] is required to convert the expression,
65
- the program is ill-formed.
66
- \[*Note 2*: If the initialization is by
67
- *designated-initializer-clause*, its form determines whether
68
- copy-initialization or direct-initialization is
69
  performed. — *end note*]
 
 
 
 
 
 
 
70
  \[*Note 3*: If an initializer is itself an initializer list, the
71
  element is list-initialized, which will result in a recursive
72
  application of the rules in this subclause if the element is an
73
  aggregate. — *end note*]
74
  \[*Example 2*:
@@ -127,19 +132,11 @@ struct S { int a; const char* b; int c; int d = b[a]; };
127
  S ss = { 1, "asdf" };
128
  ```
129
 
130
  initializes `ss.a` with 1, `ss.b` with `"asdf"`, `ss.c` with the value
131
  of an expression of the form `int{}` (that is, `0`), and `ss.d` with the
132
- value of `ss.b[ss.a]` (that is, `'s'`), and in
133
-
134
- ``` cpp
135
- struct X { int i, j, k = 42; };
136
- X a[] = { 1, 2, 3, 4, 5, 6 };
137
- X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
138
- ```
139
-
140
- `a` and `b` have the same value
141
 
142
  ``` cpp
143
  struct A {
144
  string a;
145
  int b = 42;
@@ -161,21 +158,21 @@ associated with a given element are sequenced before those of any
161
  element that follows it in order.
162
 
163
  An aggregate that is a class can also be initialized with a single
164
  expression not enclosed in braces, as described in  [[dcl.init]].
165
 
166
- The destructor for each element of class type is potentially invoked
167
- [[class.dtor]] from the context where the aggregate initialization
168
- occurs.
169
 
170
  [*Note 4*: This provision ensures that destructors can be called for
171
  fully-constructed subobjects in case an exception is thrown
172
  [[except.ctor]]. — *end note*]
173
 
174
- An array of unknown bound initialized with a brace-enclosed
175
- *initializer-list* containing `n` *initializer-clause*s is defined as
176
- having `n` elements [[dcl.array]].
177
 
178
  [*Example 4*:
179
 
180
  ``` cpp
181
  int x[] = { 1, 3, 5 };
@@ -184,10 +181,24 @@ int x[] = { 1, 3, 5 };
184
  declares and initializes `x` as a one-dimensional array that has three
185
  elements since no size was specified and there are three initializers.
186
 
187
  — *end example*]
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  An array of unknown bound shall not be initialized with an empty
190
  *braced-init-list* `{}`.[^6]
191
 
192
  [*Note 5*:
193
 
@@ -195,11 +206,11 @@ A default member initializer does not determine the bound for a member
195
  array of unknown bound. Since the default member initializer is ignored
196
  if a suitable *mem-initializer* is present [[class.base.init]], the
197
  default member initializer is not considered to initialize the array of
198
  unknown bound.
199
 
200
- [*Example 5*:
201
 
202
  ``` cpp
203
  struct S {
204
  int y[] = { 0 }; // error: non-static data member of incomplete type
205
  };
@@ -212,11 +223,11 @@ struct S {
212
  [*Note 6*:
213
 
214
  Static data members, non-static data members of anonymous union members,
215
  and unnamed bit-fields are not considered elements of the aggregate.
216
 
217
- [*Example 6*:
218
 
219
  ``` cpp
220
  struct A {
221
  int i;
222
  static int s;
@@ -232,23 +243,10 @@ unnamed bit-field before it.
232
 
233
  — *end example*]
234
 
235
  — *end note*]
236
 
237
- An *initializer-list* is ill-formed if the number of
238
- *initializer-clause*s exceeds the number of elements of the aggregate.
239
-
240
- [*Example 7*:
241
-
242
- ``` cpp
243
- char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
244
- ```
245
-
246
- is ill-formed.
247
-
248
- — *end example*]
249
-
250
  If a member has a default member initializer and a potentially-evaluated
251
  subexpression thereof is an aggregate initialization that would use that
252
  default member initializer, the program is ill-formed.
253
 
254
  [*Example 8*:
@@ -267,42 +265,15 @@ struct B {
267
  };
268
  ```
269
 
270
  — *end example*]
271
 
272
- If an aggregate class `C` contains a subaggregate element `e` with no
273
- elements, the *initializer-clause* for `e` shall not be omitted from an
274
- *initializer-list* for an object of type `C` unless the
275
- *initializer-clause*s for all elements of `C` following `e` are also
276
- omitted.
277
-
278
- [*Example 9*:
279
-
280
- ``` cpp
281
- struct S { } s;
282
- struct A {
283
- S s1;
284
- int i1;
285
- S s2;
286
- int i2;
287
- S s3;
288
- int i3;
289
- } a = {
290
- { }, // Required initialization
291
- 0,
292
- s, // Required initialization
293
- 0
294
- }; // Initialization not required for A::s3 because A::i3 is also not initialized
295
- ```
296
-
297
- — *end example*]
298
-
299
  When initializing a multidimensional array, the *initializer-clause*s
300
  initialize the elements with the last (rightmost) index of the array
301
  varying the fastest [[dcl.array]].
302
 
303
- [*Example 10*:
304
 
305
  ``` cpp
306
  int x[2][2] = { 3, 1, 4, 2 };
307
  ```
308
 
@@ -318,23 +289,77 @@ float y[4][3] = {
318
  initializes the first column of `y` (regarded as a two-dimensional
319
  array) and leaves the rest zero.
320
 
321
  — *end example*]
322
 
323
- Braces can be elided in an *initializer-list* as follows. If the
324
- *initializer-list* begins with a left brace, then the succeeding
325
- comma-separated list of *initializer-clause*s initializes the elements
326
- of a subaggregate; it is erroneous for there to be more
327
- *initializer-clause*s than elements. If, however, the *initializer-list*
328
- for a subaggregate does not begin with a left brace, then only enough
329
- *initializer-clause*s from the list are taken to initialize the elements
330
- of the subaggregate; any remaining *initializer-clause*s are left to
331
- initialize the next element of the aggregate of which the current
332
- subaggregate is an element.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
 
334
  [*Example 11*:
335
 
 
 
 
 
 
 
 
 
336
  ``` cpp
337
  float y[4][3] = {
338
  { 1, 3, 5 },
339
  { 2, 4, 6 },
340
  { 3, 5, 7 },
@@ -342,15 +367,15 @@ float y[4][3] = {
342
  ```
343
 
344
  is a completely-braced initialization: 1, 3, and 5 initialize the first
345
  row of the array `y[0]`, namely `y[0][0]`, `y[0][1]`, and `y[0][2]`.
346
  Likewise the next two lines initialize `y[1]` and `y[2]`. The
347
- initializer ends early and therefore `y[3]`s elements are initialized as
348
- if explicitly initialized with an expression of the form `float()`, that
349
- is, are initialized with `0.0`. In the following example, braces in the
350
- *initializer-list* are elided; however the *initializer-list* has the
351
- same effect as the completely-braced *initializer-list* of the above
352
  example,
353
 
354
  ``` cpp
355
  float y[4][3] = {
356
  1, 3, 5, 2, 4, 6, 3, 5, 7
@@ -361,22 +386,39 @@ The initializer for `y` begins with a left brace, but the one for `y[0]`
361
  does not, therefore three elements from the list are used. Likewise the
362
  next three are taken successively for `y[1]` and `y[2]`.
363
 
364
  — *end example*]
365
 
366
- All implicit type conversions [[conv]] are considered when initializing
367
- the element with an *assignment-expression*. If the
368
- *assignment-expression* can initialize an element, the element is
369
- initialized. Otherwise, if the element is itself a subaggregate, brace
370
- elision is assumed and the *assignment-expression* is considered for the
371
- initialization of the first element of the subaggregate.
372
-
373
- [*Note 7*: As specified above, brace elision cannot apply to
374
- subaggregates with no elements; an *initializer-clause* for the entire
375
- subobject is required. — *end note*]
376
-
377
- [*Example 12*:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378
 
379
  ``` cpp
380
  struct A {
381
  int i;
382
  operator int();
@@ -393,23 +435,24 @@ Braces are elided around the *initializer-clause* for `b.a1.i`. `b.a1.i`
393
  is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
394
  initialized with whatever `a.operator int()` returns.
395
 
396
  — *end example*]
397
 
398
- [*Note 8*: An aggregate array or an aggregate class can contain
399
  elements of a class type with a user-declared constructor
400
  [[class.ctor]]. Initialization of these aggregate objects is described
401
  in  [[class.expl.init]]. — *end note*]
402
 
403
- [*Note 9*: Whether the initialization of aggregates with static storage
404
- duration is static or dynamic is specified in  [[basic.start.static]],
405
- [[basic.start.dynamic]], and  [[stmt.dcl]]. — *end note*]
 
406
 
407
  When a union is initialized with an initializer list, there shall not be
408
  more than one explicitly initialized element.
409
 
410
- [*Example 13*:
411
 
412
  ``` cpp
413
  union u { int a; const char* b; };
414
  u a = { 1 };
415
  u b = a;
@@ -420,9 +463,9 @@ u f = { .b = "asdf" };
420
  u g = { .a = 1, .b = "asdf" }; // error
421
  ```
422
 
423
  — *end example*]
424
 
425
- [*Note 10*: As described above, the braces around the
426
  *initializer-clause* for a union member can be omitted if the union is a
427
  member of another aggregate. — *end note*]
428
 
 
8
  - no private or protected direct base classes [[class.access.base]], and
9
  - no virtual functions [[class.virtual]] or virtual base classes
10
  [[class.mi]].
11
 
12
  [*Note 1*: Aggregate initialization does not allow accessing protected
13
+ and private base class’ members, including 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
 
28
  *designated-initializer-list*, the aggregate shall be of class type,
29
  the *identifier* in each *designator* shall name a direct non-static
30
  data member of the class, and the explicitly initialized elements of
31
  the aggregate are the elements that are, or contain, those members.
32
  - If the initializer list is a brace-enclosed *initializer-list*, the
33
+ explicitly initialized elements of the aggregate are those for which
34
+ an element of the initializer list appertains to the aggregate element
35
+ or to a subobject thereof (see below).
36
  - Otherwise, the initializer list must be `{}`, and there are no
37
  explicitly initialized elements.
38
 
39
  For each explicitly initialized element:
40
 
 
54
  } c = { .a = 1, .x = 3 };
55
  ```
56
 
57
  initializes `c.a` with 1 and `c.x` with 3.
58
  — *end example*]
59
+ - Otherwise, if the initializer list is a brace-enclosed
60
+ *designated-initializer-list*, the element is initialized with the
61
  *brace-or-equal-initializer* of the corresponding
62
  *designated-initializer-clause*. If that initializer is of the form
63
+ `= `*assignment-expression* and a narrowing conversion
64
+ [[dcl.init.list]] is required to convert the expression, the program
65
+ is ill-formed. \[*Note 2*: The form of the initializer determines
66
+ whether copy-initialization or direct-initialization is
 
 
67
  performed. — *end note*]
68
+ - Otherwise, the initializer list is a brace-enclosed
69
+ *initializer-list*. If an *initializer-clause* appertains to the
70
+ aggregate element, then the aggregate element is copy-initialized from
71
+ the *initializer-clause*. Otherwise, the aggregate element is
72
+ copy-initialized from a brace-enclosed *initializer-list* consisting
73
+ of all of the *initializer-clause*s that appertain to subobjects of
74
+ the aggregate element, in the order of appearance.
75
  \[*Note 3*: If an initializer is itself an initializer list, the
76
  element is list-initialized, which will result in a recursive
77
  application of the rules in this subclause if the element is an
78
  aggregate. — *end note*]
79
  \[*Example 2*:
 
132
  S ss = { 1, "asdf" };
133
  ```
134
 
135
  initializes `ss.a` with 1, `ss.b` with `"asdf"`, `ss.c` with the value
136
  of an expression of the form `int{}` (that is, `0`), and `ss.d` with the
137
+ value of `ss.b[ss.a]` (that is, `'s'`).
 
 
 
 
 
 
 
 
138
 
139
  ``` cpp
140
  struct A {
141
  string a;
142
  int b = 42;
 
158
  element that follows it in order.
159
 
160
  An aggregate that is a class can also be initialized with a single
161
  expression not enclosed in braces, as described in  [[dcl.init]].
162
 
163
+ The destructor for each element of class type other than an anonymous
164
+ union member is potentially invoked [[class.dtor]] from the context
165
+ where the aggregate initialization occurs.
166
 
167
  [*Note 4*: This provision ensures that destructors can be called for
168
  fully-constructed subobjects in case an exception is thrown
169
  [[except.ctor]]. — *end note*]
170
 
171
+ The number of elements [[dcl.array]] in an array of unknown bound
172
+ initialized with a brace-enclosed *initializer-list* is the number of
173
+ explicitly initialized elements of the array.
174
 
175
  [*Example 4*:
176
 
177
  ``` cpp
178
  int x[] = { 1, 3, 5 };
 
181
  declares and initializes `x` as a one-dimensional array that has three
182
  elements since no size was specified and there are three initializers.
183
 
184
  — *end example*]
185
 
186
+ [*Example 5*:
187
+
188
+ In
189
+
190
+ ``` cpp
191
+ struct X { int i, j, k; };
192
+ X a[] = { 1, 2, 3, 4, 5, 6 };
193
+ X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
194
+ ```
195
+
196
+ `a` and `b` have the same value.
197
+
198
+ — *end example*]
199
+
200
  An array of unknown bound shall not be initialized with an empty
201
  *braced-init-list* `{}`.[^6]
202
 
203
  [*Note 5*:
204
 
 
206
  array of unknown bound. Since the default member initializer is ignored
207
  if a suitable *mem-initializer* is present [[class.base.init]], the
208
  default member initializer is not considered to initialize the array of
209
  unknown bound.
210
 
211
+ [*Example 6*:
212
 
213
  ``` cpp
214
  struct S {
215
  int y[] = { 0 }; // error: non-static data member of incomplete type
216
  };
 
223
  [*Note 6*:
224
 
225
  Static data members, non-static data members of anonymous union members,
226
  and unnamed bit-fields are not considered elements of the aggregate.
227
 
228
+ [*Example 7*:
229
 
230
  ``` cpp
231
  struct A {
232
  int i;
233
  static int s;
 
243
 
244
  — *end example*]
245
 
246
  — *end note*]
247
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  If a member has a default member initializer and a potentially-evaluated
249
  subexpression thereof is an aggregate initialization that would use that
250
  default member initializer, the program is ill-formed.
251
 
252
  [*Example 8*:
 
265
  };
266
  ```
267
 
268
  — *end example*]
269
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  When initializing a multidimensional array, the *initializer-clause*s
271
  initialize the elements with the last (rightmost) index of the array
272
  varying the fastest [[dcl.array]].
273
 
274
+ [*Example 9*:
275
 
276
  ``` cpp
277
  int x[2][2] = { 3, 1, 4, 2 };
278
  ```
279
 
 
289
  initializes the first column of `y` (regarded as a two-dimensional
290
  array) and leaves the rest zero.
291
 
292
  — *end example*]
293
 
294
+ Each *initializer-clause* in a brace-enclosed *initializer-list* is said
295
+ to *appertain* to an element of the aggregate being initialized or to an
296
+ element of one of its subaggregates. Considering the sequence of
297
+ *initializer-clause*s, and the sequence of aggregate elements initially
298
+ formed as the sequence of elements of the aggregate being initialized
299
+ and potentially modified as described below, each *initializer-clause*
300
+ appertains to the corresponding aggregate element if
301
+
302
+ - the aggregate element is not an aggregate, or
303
+ - the *initializer-clause* begins with a left brace, or
304
+ - the *initializer-clause* is an expression and an implicit conversion
305
+ sequence can be formed that converts the expression to the type of the
306
+ aggregate element, or
307
+ - the aggregate element is an aggregate that itself has no aggregate
308
+ elements.
309
+
310
+ Otherwise, the aggregate element is an aggregate and that subaggregate
311
+ is replaced in the list of aggregate elements by the sequence of its own
312
+ aggregate elements, and the appertainment analysis resumes with the
313
+ first such element and the same *initializer-clause*.
314
+
315
+ [*Note 7*:
316
+
317
+ These rules apply recursively to the aggregate’s subaggregates.
318
+
319
+ [*Example 10*:
320
+
321
+ In
322
+
323
+ ``` cpp
324
+ struct S1 { int a, b; };
325
+ struct S2 { S1 s, t; };
326
+
327
+ S2 x[2] = { 1, 2, 3, 4, 5, 6, 7, 8 };
328
+ S2 y[2] = {
329
+ {
330
+ { 1, 2 },
331
+ { 3, 4 }
332
+ },
333
+ {
334
+ { 5, 6 },
335
+ { 7, 8 }
336
+ }
337
+ };
338
+ ```
339
+
340
+ `x` and `y` have the same value.
341
+
342
+ — *end example*]
343
+
344
+ — *end note*]
345
+
346
+ This process continues until all *initializer-clause*s have been
347
+ exhausted. If any *initializer-clause* remains that does not appertain
348
+ to an element of the aggregate or one of its subaggregates, the program
349
+ is ill-formed.
350
 
351
  [*Example 11*:
352
 
353
+ ``` cpp
354
+ char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error: too many initializers
355
+ ```
356
+
357
+ — *end example*]
358
+
359
+ [*Example 12*:
360
+
361
  ``` cpp
362
  float y[4][3] = {
363
  { 1, 3, 5 },
364
  { 2, 4, 6 },
365
  { 3, 5, 7 },
 
367
  ```
368
 
369
  is a completely-braced initialization: 1, 3, and 5 initialize the first
370
  row of the array `y[0]`, namely `y[0][0]`, `y[0][1]`, and `y[0][2]`.
371
  Likewise the next two lines initialize `y[1]` and `y[2]`. The
372
+ initializer ends early and therefore `y[3]`s elements are initialized
373
+ as if explicitly initialized with an expression of the form `float()`,
374
+ that is, are initialized with `0.0`. In the following example, braces in
375
+ the *initializer-list* are elided; however the *initializer-list* has
376
+ the same effect as the completely-braced *initializer-list* of the above
377
  example,
378
 
379
  ``` cpp
380
  float y[4][3] = {
381
  1, 3, 5, 2, 4, 6, 3, 5, 7
 
386
  does not, therefore three elements from the list are used. Likewise the
387
  next three are taken successively for `y[1]` and `y[2]`.
388
 
389
  — *end example*]
390
 
391
+ [*Note 8*:
392
+
393
+ The initializer for an empty subaggregate is needed if any initializers
394
+ are provided for subsequent elements.
395
+
396
+ [*Example 13*:
397
+
398
+ ``` cpp
399
+ struct S { } s;
400
+ struct A {
401
+ S s1;
402
+ int i1;
403
+ S s2;
404
+ int i2;
405
+ S s3;
406
+ int i3;
407
+ } a = {
408
+ { }, // Required initialization
409
+ 0,
410
+ s, // Required initialization
411
+ 0
412
+ }; // Initialization not required for A::s3 because A::i3 is also not initialized
413
+ ```
414
+
415
+ — *end example*]
416
+
417
+ — *end note*]
418
+
419
+ [*Example 14*:
420
 
421
  ``` cpp
422
  struct A {
423
  int i;
424
  operator int();
 
435
  is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
436
  initialized with whatever `a.operator int()` returns.
437
 
438
  — *end example*]
439
 
440
+ [*Note 9*: An aggregate array or an aggregate class can contain
441
  elements of a class type with a user-declared constructor
442
  [[class.ctor]]. Initialization of these aggregate objects is described
443
  in  [[class.expl.init]]. — *end note*]
444
 
445
+ [*Note 10*: Whether the initialization of aggregates with static
446
+ storage duration is static or dynamic is specified in 
447
+ [[basic.start.static]], [[basic.start.dynamic]], and 
448
+ [[stmt.dcl]]. — *end note*]
449
 
450
  When a union is initialized with an initializer list, there shall not be
451
  more than one explicitly initialized element.
452
 
453
+ [*Example 15*:
454
 
455
  ``` cpp
456
  union u { int a; const char* b; };
457
  u a = { 1 };
458
  u b = a;
 
463
  u g = { .a = 1, .b = "asdf" }; // error
464
  ```
465
 
466
  — *end example*]
467
 
468
+ [*Note 11*: As described above, the braces around the
469
  *initializer-clause* for a union member can be omitted if the union is a
470
  member of another aggregate. — *end note*]
471