From Jason Turner

[dcl.init.aggr]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpl55ewl3w/{from.md → to.md} +160 -99
tmp/tmpl55ewl3w/{from.md → to.md} RENAMED
@@ -1,41 +1,74 @@
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 {
41
  int i;
@@ -43,11 +76,10 @@ struct A {
43
  } 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;
@@ -60,46 +92,109 @@ struct derived : base1, base2 {
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
  };
@@ -107,16 +202,16 @@ struct S {
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;
@@ -133,86 +228,51 @@ unnamed bit-field before it.
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
- ```
173
-
174
- initializes `ss.a` with 1, `ss.b` with `"asdf"`, `ss.c` with the value
175
- of an expression of the form `int{}` (that is, `0`), and `ss.d` with the
176
- value of `ss.b[ss.a]` (that is, `'s'`), and in
177
-
178
- ``` cpp
179
- struct X { int i, j, k = 42; };
180
- X a[] = { 1, 2, 3, 4, 5, 6 };
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;
@@ -231,13 +291,13 @@ struct A {
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
 
@@ -264,11 +324,11 @@ 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 },
@@ -296,22 +356,22 @@ 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();
@@ -328,35 +388,36 @@ 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
 
 
1
  ### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
2
 
3
+ An *aggregate* is an array or a class [[class]] with
4
 
5
+ - no user-declared or inherited constructors [[class.ctor]],
6
+ - no private or protected direct non-static data members
7
+ [[class.access]],
8
+ - no virtual functions [[class.virtual]], and
9
+ - no virtual, private, or protected base classes [[class.mi]].
 
10
 
11
  [*Note 1*: Aggregate initialization does not allow accessing protected
12
  and private base class’ members or constructors. — *end note*]
13
 
14
  The *elements* of an aggregate are:
15
 
16
  - for an array, the array elements in increasing subscript order, or
17
  - for a class, the direct base classes in declaration order, followed by
18
+ the direct non-static data members [[class.mem]] that are not members
19
+ of an anonymous union, in declaration order.
20
 
21
  When an aggregate is initialized by an initializer list as specified in 
22
  [[dcl.init.list]], the elements of the initializer list are taken as
23
+ initializers for the elements of the aggregate. The *explicitly
24
+ initialized elements* of the aggregate are determined as follows:
25
+
26
+ - If the initializer list is a *designated-initializer-list*, the
27
+ aggregate shall be of class type, the *identifier* in each
28
+ *designator* shall name a direct non-static data member of the class,
29
+ and the explicitly initialized elements of the aggregate are the
30
+ elements that are, or contain, those members.
31
+ - If the initializer list is an *initializer-list*, the explicitly
32
+ initialized elements of the aggregate are the first n elements of the
33
+ aggregate, where n is the number of elements in the initializer list.
34
+ - Otherwise, the initializer list must be `{}`, and there are no
35
+ explicitly initialized elements.
36
+
37
+ For each explicitly initialized element:
38
+
39
+ - If the element is an anonymous union object and the initializer list
40
+ is a *designated-initializer-list*, the anonymous union object is
41
+ initialized by the *designated-initializer-list* `{ `*D*` }`, where
42
+ *D* is the *designated-initializer-clause* naming a member of the
43
+ anonymous union object. There shall be only one such
44
+ *designated-initializer-clause*.
45
+ \[*Example 1*:
46
+ ``` cpp
47
+ struct C {
48
+ union {
49
+ int a;
50
+ const char* p;
51
+ };
52
+ int x;
53
+ } c = { .a = 1, .x = 3 };
54
+ ```
55
+
56
+ initializes `c.a` with 1 and `c.x` with 3.
57
+ — *end example*]
58
+ - Otherwise, the element is copy-initialized from the corresponding
59
+ *initializer-clause* or is initialized with the
60
+ *brace-or-equal-initializer* of the corresponding
61
+ *designated-initializer-clause*. If that initializer is of the form
62
+ *assignment-expression* or `= `*assignment-expression* and a narrowing
63
+ conversion [[dcl.init.list]] is required to convert the expression,
64
+ the program is ill-formed.
65
+ \[*Note 2*: If an initializer is itself an initializer list, the
66
+ element is list-initialized, which will result in a recursive
67
+ application of the rules in this subclause if the element is an
68
  aggregate. — *end note*]
69
+ \[*Example 2*:
 
 
70
  ``` cpp
71
  struct A {
72
  int x;
73
  struct B {
74
  int i;
 
76
  } b;
77
  } a = { 1, { 2, 3 } };
78
  ```
79
 
80
  initializes `a.x` with 1, `a.b.i` with 2, `a.b.j` with 3.
 
81
  ``` cpp
82
  struct base1 { int b1, b2 = 42; };
83
  struct base2 {
84
  base2() {
85
  b3 = 42;
 
92
 
93
  derived d1{{1, 2}, {}, 4};
94
  derived d2{{}, {}, 4};
95
  ```
96
 
97
+ initializes `d1.b1` with 1, `d1.b2` with 2, `d1.b3` with 42, `d1.d`
98
+ with 4, and `d2.b1` with 0, `d2.b2` with 42, `d2.b3` with 42, `d2.d`
99
+ with 4.
100
+ — *end example*]
101
+
102
+ For a non-union aggregate, each element that is not an explicitly
103
+ initialized element is initialized as follows:
104
+
105
+ - If the element has a default member initializer [[class.mem]], the
106
+ element is initialized from that initializer.
107
+ - Otherwise, if the element is not a reference, the element is
108
+ copy-initialized from an empty initializer list [[dcl.init.list]].
109
+ - Otherwise, the program is ill-formed.
110
+
111
+ If the aggregate is a union and the initializer list is empty, then
112
+
113
+ - if any variant member has a default member initializer, that member is
114
+ initialized from its default member initializer;
115
+ - otherwise, the first member of the union (if any) is copy-initialized
116
+ from an empty initializer list.
117
+
118
+ [*Example 3*:
119
+
120
+ ``` cpp
121
+ struct S { int a; const char* b; int c; int d = b[a]; };
122
+ S ss = { 1, "asdf" };
123
+ ```
124
+
125
+ initializes `ss.a` with 1, `ss.b` with `"asdf"`, `ss.c` with the value
126
+ of an expression of the form `int{}` (that is, `0`), and `ss.d` with the
127
+ value of `ss.b[ss.a]` (that is, `'s'`), and in
128
+
129
+ ``` cpp
130
+ struct X { int i, j, k = 42; };
131
+ X a[] = { 1, 2, 3, 4, 5, 6 };
132
+ X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
133
+ ```
134
+
135
+ `a` and `b` have the same value
136
+
137
+ ``` cpp
138
+ struct A {
139
+ string a;
140
+ int b = 42;
141
+ int c = -1;
142
+ };
143
+ ```
144
+
145
+ `A{.c=21}` has the following steps:
146
+
147
+ - Initialize `a` with `{}`
148
+ - Initialize `b` with `= 42`
149
+ - Initialize `c` with `= 21`
150
 
151
  — *end example*]
152
 
153
+ The initializations of the elements of the aggregate are evaluated in
154
+ the element order. That is, all value computations and side effects
155
+ associated with a given element are sequenced before those of any
156
+ element that follows it in order.
157
+
158
  An aggregate that is a class can also be initialized with a single
159
  expression not enclosed in braces, as described in  [[dcl.init]].
160
 
161
+ The destructor for each element of class type is potentially invoked
162
+ [[class.dtor]] from the context where the aggregate initialization
163
+ occurs.
164
+
165
+ [*Note 3*: This provision ensures that destructors can be called for
166
+ fully-constructed subobjects in case an exception is thrown
167
+ [[except.ctor]]. — *end note*]
168
+
169
  An array of unknown bound initialized with a brace-enclosed
170
+ *initializer-list* containing `n` *initializer-clause*s is defined as
171
+ having `n` elements [[dcl.array]].
 
172
 
173
+ [*Example 4*:
174
 
175
  ``` cpp
176
  int x[] = { 1, 3, 5 };
177
  ```
178
 
179
  declares and initializes `x` as a one-dimensional array that has three
180
  elements since no size was specified and there are three initializers.
181
 
182
  — *end example*]
183
 
184
+ An array of unknown bound shall not be initialized with an empty
185
+ *braced-init-list* `{}`. [^6]
186
 
187
+ [*Note 4*:
188
 
189
  A default member initializer does not determine the bound for a member
190
  array of unknown bound. Since the default member initializer is ignored
191
+ if a suitable *mem-initializer* is present [[class.base.init]], the
192
  default member initializer is not considered to initialize the array of
193
  unknown bound.
194
 
195
+ [*Example 5*:
196
 
197
  ``` cpp
198
  struct S {
199
  int y[] = { 0 }; // error: non-static data member of incomplete type
200
  };
 
202
 
203
  — *end example*]
204
 
205
  — *end note*]
206
 
207
+ [*Note 5*:
208
 
209
+ Static data members, non-static data members of anonymous union members,
210
+ and unnamed bit-fields are not considered elements of the aggregate.
211
 
212
+ [*Example 6*:
213
 
214
  ``` cpp
215
  struct A {
216
  int i;
217
  static int s;
 
228
  — *end example*]
229
 
230
  — *end note*]
231
 
232
  An *initializer-list* is ill-formed if the number of
233
+ *initializer-clause*s exceeds the number of elements of the aggregate.
234
 
235
+ [*Example 7*:
236
 
237
  ``` cpp
238
  char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
239
  ```
240
 
241
  is ill-formed.
242
 
243
  — *end example*]
244
 
245
+ If a member has a default member initializer and a potentially-evaluated
246
+ subexpression thereof is an aggregate initialization that would use that
247
+ default member initializer, the program is ill-formed.
248
 
249
+ [*Example 8*:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
 
251
  ``` cpp
252
  struct A;
253
  extern A a;
254
  struct A {
255
  const A& a1 { A{a,a} }; // OK
256
  const A& a2 { A{} }; // error
257
  };
258
  A a{a,a}; // OK
259
+
260
+ struct B {
261
+ int n = B{}.n; // error
262
+ };
263
  ```
264
 
265
  — *end example*]
266
 
267
  If an aggregate class `C` contains a subaggregate element `e` with no
268
  elements, the *initializer-clause* for `e` shall not be omitted from an
269
  *initializer-list* for an object of type `C` unless the
270
  *initializer-clause*s for all elements of `C` following `e` are also
271
  omitted.
272
 
273
+ [*Example 9*:
274
 
275
  ``` cpp
276
  struct S { } s;
277
  struct A {
278
  S s1;
 
291
 
292
  — *end example*]
293
 
294
  When initializing a multi-dimensional array, the *initializer-clause*s
295
  initialize the elements with the last (rightmost) index of the array
296
+ varying the fastest [[dcl.array]].
297
 
298
+ [*Example 10*:
299
 
300
  ``` cpp
301
  int x[2][2] = { 3, 1, 4, 2 };
302
  ```
303
 
 
324
  *initializer-clause*s from the list are taken to initialize the elements
325
  of the subaggregate; any remaining *initializer-clause*s are left to
326
  initialize the next element of the aggregate of which the current
327
  subaggregate is an element.
328
 
329
+ [*Example 11*:
330
 
331
  ``` cpp
332
  float y[4][3] = {
333
  { 1, 3, 5 },
334
  { 2, 4, 6 },
 
356
  does not, therefore three elements from the list are used. Likewise the
357
  next three are taken successively for `y[1]` and `y[2]`.
358
 
359
  — *end example*]
360
 
361
+ All implicit type conversions [[conv]] are considered when initializing
362
+ the element with an *assignment-expression*. If the
363
  *assignment-expression* can initialize an element, the element is
364
  initialized. Otherwise, if the element is itself a subaggregate, brace
365
  elision is assumed and the *assignment-expression* is considered for the
366
  initialization of the first element of the subaggregate.
367
 
368
+ [*Note 6*: As specified above, brace elision cannot apply to
369
  subaggregates with no elements; an *initializer-clause* for the entire
370
  subobject is required. — *end note*]
371
 
372
+ [*Example 12*:
373
 
374
  ``` cpp
375
  struct A {
376
  int i;
377
  operator int();
 
388
  is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
389
  initialized with whatever `a.operator int()` returns.
390
 
391
  — *end example*]
392
 
393
+ [*Note 7*: An aggregate array or an aggregate class may contain
394
+ elements of a class type with a user-declared constructor
395
+ [[class.ctor]]. Initialization of these aggregate objects is described
396
  in  [[class.expl.init]]. — *end note*]
397
 
398
+ [*Note 8*: Whether the initialization of aggregates with static storage
399
  duration is static or dynamic is specified in  [[basic.start.static]],
400
  [[basic.start.dynamic]], and  [[stmt.dcl]]. — *end note*]
401
 
402
+ When a union is initialized with an initializer list, there shall not be
403
+ more than one explicitly initialized element.
 
404
 
405
+ [*Example 13*:
406
 
407
  ``` cpp
408
  union u { int a; const char* b; };
409
  u a = { 1 };
410
  u b = a;
411
  u c = 1; // error
412
  u d = { 0, "asdf" }; // error
413
  u e = { "asdf" }; // error
414
+ u f = { .b = "asdf" };
415
+ u g = { .a = 1, .b = "asdf" }; // error
416
  ```
417
 
418
  — *end example*]
419
 
420
+ [*Note 9*: As described above, the braces around the
421
  *initializer-clause* for a union member can be omitted if the union is a
422
  member of another aggregate. — *end note*]
423