From Jason Turner

[dcl.init]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpt7fp76bl/{from.md → to.md} +480 -627
tmp/tmpt7fp76bl/{from.md → to.md} RENAMED
@@ -1,13 +1,12 @@
1
  ## Initializers <a id="dcl.init">[[dcl.init]]</a>
2
 
3
- A declarator can specify an initial value for the identifier being
4
- declared. The identifier designates a variable being initialized. The
5
- process of initialization described in the remainder of  [[dcl.init]]
6
- applies also to initializations specified by other syntactic contexts,
7
- such as the initialization of function parameters ([[expr.call]]) or
8
- the initialization of return values ([[stmt.return]]).
9
 
10
  ``` bnf
11
  initializer:
12
  brace-or-equal-initializer
13
  '(' expression-list ')'
@@ -23,28 +22,49 @@ brace-or-equal-initializer:
23
  initializer-clause:
24
  assignment-expression
25
  braced-init-list
26
  ```
27
 
28
- ``` bnf
29
- initializer-list:
30
- initializer-clause '...'ₒₚₜ
31
- initializer-list ',' initializer-clause '...'ₒₚₜ
32
- ```
33
-
34
  ``` bnf
35
  braced-init-list:
36
  '{' initializer-list ','ₒₚₜ '}'
 
37
  '{' '}'
38
  ```
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  ``` bnf
41
  expr-or-braced-init-list:
42
  expression
43
  braced-init-list
44
  ```
45
 
 
 
 
 
46
  Except for objects declared with the `constexpr` specifier, for which
47
  see  [[dcl.constexpr]], an *initializer* in the definition of a variable
48
  can consist of arbitrary expressions involving literals and previously
49
  declared variables and functions, regardless of the variable’s storage
50
  duration.
@@ -58,43 +78,44 @@ int b = f(a);
58
  int c(b);
59
  ```
60
 
61
  — *end example*]
62
 
63
- [*Note 1*: Default arguments are more restricted; see 
64
  [[dcl.fct.default]]. — *end note*]
65
 
66
- [*Note 2*: The order of initialization of variables with static storage
67
  duration is described in  [[basic.start]] and 
68
  [[stmt.dcl]]. — *end note*]
69
 
70
  A declaration of a block-scope variable with external or internal
71
  linkage that has an *initializer* is ill-formed.
72
 
73
  To *zero-initialize* an object or reference of type `T` means:
74
 
75
- - if `T` is a scalar type ([[basic.types]]), the object is initialized
76
- to the value obtained by converting the integer literal `0` (zero) to
77
- `T`;[^12]
78
- - if `T` is a (possibly cv-qualified) non-union class type, each
79
- non-static data member, each non-virtual base class subobject, and, if
80
- the object is not a base class subobject, each virtual base class
81
- subobject is zero-initialized and padding is initialized to zero bits;
82
- - if `T` is a (possibly cv-qualified) union type, the object’s first
83
- non-static named data member is zero-initialized and padding is
84
- initialized to zero bits;
 
85
  - if `T` is an array type, each element is zero-initialized;
86
  - if `T` is a reference type, no initialization is performed.
87
 
88
  To *default-initialize* an object of type `T` means:
89
 
90
- - If `T` is a (possibly cv-qualified) class type (Clause  [[class]]),
91
- constructors are considered. The applicable constructors are
92
- enumerated ([[over.match.ctor]]), and the best one for the
93
- *initializer* `()` is chosen through overload resolution (
94
- [[over.match]]). The constructor thus selected is called, with an
95
- empty argument list, to initialize the object.
96
  - If `T` is an array type, each element is default-initialized.
97
  - Otherwise, no initialization is performed.
98
 
99
  A class type `T` is *const-default-constructible* if
100
  default-initialization of `T` would invoke a user-provided constructor
@@ -115,105 +136,53 @@ If a program calls for the default-initialization of an object of a
115
  const-qualified type `T`, `T` shall be a const-default-constructible
116
  class type or array thereof.
117
 
118
  To *value-initialize* an object of type `T` means:
119
 
120
- - if `T` is a (possibly cv-qualified) class type (Clause  [[class]])
121
- with either no default constructor ([[class.ctor]]) or a default
122
- constructor that is user-provided or deleted, then the object is
123
- default-initialized;
124
- - if `T` is a (possibly cv-qualified) class type without a user-provided
125
- or deleted default constructor, then the object is zero-initialized
126
- and the semantic constraints for default-initialization are checked,
127
- and if `T` has a non-trivial default constructor, the object is
128
- default-initialized;
129
  - if `T` is an array type, then each element is value-initialized;
130
  - otherwise, the object is zero-initialized.
131
 
132
  A program that calls for default-initialization or value-initialization
133
  of an entity of reference type is ill-formed.
134
 
135
- [*Note 3*: Every object of static storage duration is zero-initialized
136
- at program startup before any other initialization takes place. In some
137
- cases, additional initialization is done later. *end note*]
 
138
 
139
  An object whose initializer is an empty set of parentheses, i.e., `()`,
140
  shall be value-initialized.
141
 
142
- [*Note 4*:
143
 
144
  Since `()` is not permitted by the syntax for *initializer*,
145
 
146
  ``` cpp
147
  X a();
148
  ```
149
 
150
  is not the declaration of an object of class `X`, but the declaration of
151
  a function taking no argument and returning an `X`. The form `()` is
152
- permitted in certain other initialization contexts ([[expr.new]],
153
  [[expr.type.conv]], [[class.base.init]]).
154
 
155
  — *end note*]
156
 
157
  If no initializer is specified for an object, the object is
158
- default-initialized. When storage for an object with automatic or
159
- dynamic storage duration is obtained, the object has an *indeterminate
160
- value*, and if no initialization is performed for the object, that
161
- object retains an indeterminate value until that value is replaced (
162
- [[expr.ass]]).
163
-
164
- [*Note 5*: Objects with static or thread storage duration are
165
- zero-initialized, see  [[basic.start.static]]. — *end note*]
166
-
167
- If an indeterminate value is produced by an evaluation, the behavior is
168
- undefined except in the following cases:
169
-
170
- - If an indeterminate value of unsigned narrow character type (
171
- [[basic.fundamental]]) or `std::byte` type ([[cstddef.syn]]) is
172
- produced by the evaluation of:
173
- - the second or third operand of a conditional expression (
174
- [[expr.cond]]),
175
- - the right operand of a comma expression ([[expr.comma]]),
176
- - the operand of a cast or conversion ([[conv.integral]],
177
- [[expr.type.conv]], [[expr.static.cast]], [[expr.cast]]) to an
178
- unsigned narrow character type or `std::byte` type (
179
- [[cstddef.syn]]), or
180
- - a discarded-value expression (Clause  [[expr]]),
181
-
182
- then the result of the operation is an indeterminate value.
183
- - If an indeterminate value of unsigned narrow character type or
184
- `std::byte` type is produced by the evaluation of the right operand of
185
- a simple assignment operator ([[expr.ass]]) whose first operand is an
186
- lvalue of unsigned narrow character type or `std::byte` type, an
187
- indeterminate value replaces the value of the object referred to by
188
- the left operand.
189
- - If an indeterminate value of unsigned narrow character type is
190
- produced by the evaluation of the initialization expression when
191
- initializing an object of unsigned narrow character type, that object
192
- is initialized to an indeterminate value.
193
- - If an indeterminate value of unsigned narrow character type or
194
- `std::byte` type is produced by the evaluation of the initialization
195
- expression when initializing an object of `std::byte` type, that
196
- object is initialized to an indeterminate value.
197
-
198
- [*Example 2*:
199
-
200
- ``` cpp
201
- int f(bool b) {
202
- unsigned char c;
203
- unsigned char d = c; // OK, d has an indeterminate value
204
- int e = d; // undefined behavior
205
- return b ? d : 0; // undefined behavior if b is true
206
- }
207
- ```
208
-
209
- — *end example*]
210
 
211
  An initializer for a static member is in the scope of the member’s
212
  class.
213
 
214
- [*Example 3*:
215
 
216
  ``` cpp
217
  int a;
218
 
219
  struct X {
@@ -230,164 +199,252 @@ int X::b = a; // X::b = X::a
230
  If the entity being initialized does not have class type, the
231
  *expression-list* in a parenthesized initializer shall be a single
232
  expression.
233
 
234
  The initialization that occurs in the `=` form of a
235
- *brace-or-equal-initializer* or *condition* ([[stmt.select]]), as well
236
- as in argument passing, function return, throwing an exception (
237
- [[except.throw]]), handling an exception ([[except.handle]]), and
238
- aggregate member initialization ([[dcl.init.aggr]]), is called
239
  *copy-initialization*.
240
 
241
- [*Note 6*: Copy-initialization may invoke a move (
242
- [[class.copy]]). — *end note*]
243
 
244
- The initialization that occurs in the forms
245
 
246
- ``` cpp
247
- T x(a);
248
- T x{a};
249
- ```
 
 
250
 
251
- as well as in `new` expressions ([[expr.new]]), `static_cast`
252
- expressions ([[expr.static.cast]]), functional notation type
253
- conversions ([[expr.type.conv]]), *mem-initializer*s (
254
- [[class.base.init]]), and the *braced-init-list* form of a *condition*
255
  is called *direct-initialization*.
256
 
257
  The semantics of initializers are as follows. The *destination type* is
258
  the type of the object or reference being initialized and the *source
259
  type* is the type of the initializer expression. If the initializer is
260
  not a single (possibly parenthesized) expression, the source type is not
261
  defined.
262
 
263
  - If the initializer is a (non-parenthesized) *braced-init-list* or is
264
- `=` *braced-init-list*, the object or reference is list-initialized (
265
- [[dcl.init.list]]).
266
  - If the destination type is a reference type, see  [[dcl.init.ref]].
267
  - If the destination type is an array of characters, an array of
268
- `char16_t`, an array of `char32_t`, or an array of `wchar_t`, and the
269
- initializer is a string literal, see  [[dcl.init.string]].
 
270
  - If the initializer is `()`, the object is value-initialized.
271
- - Otherwise, if the destination type is an array, the program is
272
- ill-formed.
273
- - If the destination type is a (possibly cv-qualified) class type:
 
 
 
 
 
 
 
 
 
 
274
  - If the initializer expression is a prvalue and the cv-unqualified
275
  version of the source type is the same class as the class of the
276
  destination, the initializer expression is used to initialize the
277
- destination object. \[*Example 4*: `T x = T(T(T()));` calls the `T`
278
  default constructor to initialize `x`. — *end example*]
279
  - Otherwise, if the initialization is direct-initialization, or if it
280
  is copy-initialization where the cv-unqualified version of the
281
  source type is the same class as, or a derived class of, the class
282
  of the destination, constructors are considered. The applicable
283
- constructors are enumerated ([[over.match.ctor]]), and the best one
284
- is chosen through overload resolution ([[over.match]]). The
285
- constructor so selected is called to initialize the object, with the
286
- initializer expression or *expression-list* as its argument(s). If
287
- no constructor applies, or the overload resolution is ambiguous, the
288
- initialization is ill-formed.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  - Otherwise (i.e., for the remaining copy-initialization cases),
290
- user-defined conversion sequences that can convert from the source
291
- type to the destination type or (when a conversion function is used)
292
- to a derived class thereof are enumerated as described in 
293
  [[over.match.copy]], and the best one is chosen through overload
294
- resolution ([[over.match]]). If the conversion cannot be done or is
295
  ambiguous, the initialization is ill-formed. The function selected
296
  is called with the initializer expression as its argument; if the
297
  function is a constructor, the call is a prvalue of the
298
  cv-unqualified version of the destination type whose result object
299
  is initialized by the constructor. The call is used to
300
  direct-initialize, according to the rules above, the object that is
301
  the destination of the copy-initialization.
302
  - Otherwise, if the source type is a (possibly cv-qualified) class type,
303
  conversion functions are considered. The applicable conversion
304
- functions are enumerated ([[over.match.conv]]), and the best one is
305
- chosen through overload resolution ([[over.match]]). The user-defined
306
  conversion so selected is called to convert the initializer expression
307
  into the object being initialized. If the conversion cannot be done or
308
  is ambiguous, the initialization is ill-formed.
 
 
 
309
  - Otherwise, the initial value of the object being initialized is the
310
- (possibly converted) value of the initializer expression. Standard
311
- conversions (Clause  [[conv]]) will be used, if necessary, to convert
312
  the initializer expression to the cv-unqualified version of the
313
  destination type; no user-defined conversions are considered. If the
314
  conversion cannot be done, the initialization is ill-formed. When
315
  initializing a bit-field with a value that it cannot represent, the
316
  resulting value of the bit-field is *implementation-defined*.
317
- \[*Note 7*:
318
  An expression of type “*cv1* `T`” can initialize an object of type
319
  “*cv2* `T`” independently of the cv-qualifiers *cv1* and *cv2*.
320
  ``` cpp
321
  int a;
322
  const int b = a;
323
  int c = b;
324
  ```
325
 
326
  — *end note*]
327
 
328
- An *initializer-clause* followed by an ellipsis is a pack expansion (
329
- [[temp.variadic]]).
330
 
331
  If the initializer is a parenthesized *expression-list*, the expressions
332
- are evaluated in the order specified for function calls (
333
- [[expr.call]]).
 
 
334
 
335
  An object whose initialization has completed is deemed to be
336
- constructed, even if no constructor of the object’s class is invoked for
337
- the initialization.
338
 
339
- [*Note 8*: Such an object might have been value-initialized or
340
- initialized by aggregate initialization ([[dcl.init.aggr]]) or by an
341
- inherited constructor ([[class.inhctor.init]]). — *end note*]
 
 
 
 
 
342
 
343
  A declaration that specifies the initialization of a variable, whether
344
  from an explicit initializer or by default-initialization, is called the
345
  *initializing declaration* of that variable.
346
 
347
- [*Note 9*: In most cases this is the defining declaration (
348
- [[basic.def]]) of the variable, but the initializing declaration of a
349
- non-inline static data member ([[class.static.data]]) might be the
350
  declaration within the class definition and not the definition at
351
  namespace scope. — *end note*]
352
 
353
  ### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
354
 
355
- An *aggregate* is an array or a class (Clause  [[class]]) with
356
 
357
- - no user-provided, `explicit`, or inherited constructors (
358
- [[class.ctor]]),
359
- - no private or protected non-static data members (Clause 
360
- [[class.access]]),
361
- - no virtual functions ([[class.virtual]]), and
362
- - no virtual, private, or protected base classes ([[class.mi]]).
363
 
364
  [*Note 1*: Aggregate initialization does not allow accessing protected
365
  and private base class’ members or constructors. — *end note*]
366
 
367
  The *elements* of an aggregate are:
368
 
369
  - for an array, the array elements in increasing subscript order, or
370
  - for a class, the direct base classes in declaration order, followed by
371
- the direct non-static data members ([[class.mem]]) that are not
372
- members of an anonymous union, in declaration order.
373
 
374
  When an aggregate is initialized by an initializer list as specified in 
375
  [[dcl.init.list]], the elements of the initializer list are taken as
376
- initializers for the elements of the aggregate, in order. Each element
377
- is copy-initialized from the corresponding *initializer-clause*. If the
378
- *initializer-clause* is an expression and a narrowing conversion (
379
- [[dcl.init.list]]) is required to convert the expression, the program is
380
- ill-formed.
381
-
382
- [*Note 2*: If an *initializer-clause* is itself an initializer list,
383
- the element is list-initialized, which will result in a recursive
384
- application of the rules in this section if the element is an
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385
  aggregate. — *end note*]
386
-
387
- [*Example 1*:
388
-
389
  ``` cpp
390
  struct A {
391
  int x;
392
  struct B {
393
  int i;
@@ -395,11 +452,10 @@ struct A {
395
  } b;
396
  } a = { 1, { 2, 3 } };
397
  ```
398
 
399
  initializes `a.x` with 1, `a.b.i` with 2, `a.b.j` with 3.
400
-
401
  ``` cpp
402
  struct base1 { int b1, b2 = 42; };
403
  struct base2 {
404
  base2() {
405
  b3 = 42;
@@ -412,46 +468,109 @@ struct derived : base1, base2 {
412
 
413
  derived d1{{1, 2}, {}, 4};
414
  derived d2{{}, {}, 4};
415
  ```
416
 
417
- initializes `d1.b1` with 1, `d1.b2` with 2, `d1.b3` with 42, `d1.d` with
418
- 4, and `d2.b1` with 0, `d2.b2` with 42, `d2.b3` with 42, `d2.d` with 4.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
419
 
420
  — *end example*]
421
 
 
 
 
 
 
422
  An aggregate that is a class can also be initialized with a single
423
  expression not enclosed in braces, as described in  [[dcl.init]].
424
 
 
 
 
 
 
 
 
 
425
  An array of unknown bound initialized with a brace-enclosed
426
- *initializer-list* containing `n` *initializer-clause*s, where `n` shall
427
- be greater than zero, is defined as having `n` elements (
428
- [[dcl.array]]).
429
 
430
- [*Example 2*:
431
 
432
  ``` cpp
433
  int x[] = { 1, 3, 5 };
434
  ```
435
 
436
  declares and initializes `x` as a one-dimensional array that has three
437
  elements since no size was specified and there are three initializers.
438
 
439
  — *end example*]
440
 
441
- An empty initializer list `{}` shall not be used as the
442
- *initializer-clause* for an array of unknown bound.[^13]
443
 
444
- [*Note 3*:
445
 
446
  A default member initializer does not determine the bound for a member
447
  array of unknown bound. Since the default member initializer is ignored
448
- if a suitable *mem-initializer* is present ([[class.base.init]]), the
449
  default member initializer is not considered to initialize the array of
450
  unknown bound.
451
 
452
- [*Example 3*:
453
 
454
  ``` cpp
455
  struct S {
456
  int y[] = { 0 }; // error: non-static data member of incomplete type
457
  };
@@ -459,16 +578,16 @@ struct S {
459
 
460
  — *end example*]
461
 
462
  — *end note*]
463
 
464
- [*Note 4*:
465
 
466
- Static data members and unnamed bit-fields are not considered elements
467
- of the aggregate.
468
 
469
- [*Example 4*:
470
 
471
  ``` cpp
472
  struct A {
473
  int i;
474
  static int s;
@@ -485,86 +604,51 @@ unnamed bit-field before it.
485
  — *end example*]
486
 
487
  — *end note*]
488
 
489
  An *initializer-list* is ill-formed if the number of
490
- *initializer-clause*s exceeds the number of elements to initialize.
491
 
492
- [*Example 5*:
493
 
494
  ``` cpp
495
  char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
496
  ```
497
 
498
  is ill-formed.
499
 
500
  — *end example*]
501
 
502
- If there are fewer *initializer-clause*s in the list than there are
503
- elements in a non-union aggregate, then each element not explicitly
504
- initialized is initialized as follows:
505
 
506
- - If the element has a default member initializer ([[class.mem]]), the
507
- element is initialized from that initializer.
508
- - Otherwise, if the element is not a reference, the element is
509
- copy-initialized from an empty initializer list ([[dcl.init.list]]).
510
- - Otherwise, the program is ill-formed.
511
-
512
- If the aggregate is a union and the initializer list is empty, then
513
-
514
- - if any variant member has a default member initializer, that member is
515
- initialized from its default member initializer;
516
- - otherwise, the first member of the union (if any) is copy-initialized
517
- from an empty initializer list.
518
-
519
- [*Example 6*:
520
-
521
- ``` cpp
522
- struct S { int a; const char* b; int c; int d = b[a]; };
523
- S ss = { 1, "asdf" };
524
- ```
525
-
526
- initializes `ss.a` with 1, `ss.b` with `"asdf"`, `ss.c` with the value
527
- of an expression of the form `int{}` (that is, `0`), and `ss.d` with the
528
- value of `ss.b[ss.a]` (that is, `'s'`), and in
529
-
530
- ``` cpp
531
- struct X { int i, j, k = 42; };
532
- X a[] = { 1, 2, 3, 4, 5, 6 };
533
- X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
534
- ```
535
-
536
- `a` and `b` have the same value
537
-
538
- — *end example*]
539
-
540
- If a reference member is initialized from its default member initializer
541
- and a potentially-evaluated subexpression thereof is an aggregate
542
- initialization that would use that default member initializer, the
543
- program is ill-formed.
544
-
545
- [*Example 7*:
546
 
547
  ``` cpp
548
  struct A;
549
  extern A a;
550
  struct A {
551
  const A& a1 { A{a,a} }; // OK
552
  const A& a2 { A{} }; // error
553
  };
554
  A a{a,a}; // OK
 
 
 
 
555
  ```
556
 
557
  — *end example*]
558
 
559
  If an aggregate class `C` contains a subaggregate element `e` with no
560
  elements, the *initializer-clause* for `e` shall not be omitted from an
561
  *initializer-list* for an object of type `C` unless the
562
  *initializer-clause*s for all elements of `C` following `e` are also
563
  omitted.
564
 
565
- [*Example 8*:
566
 
567
  ``` cpp
568
  struct S { } s;
569
  struct A {
570
  S s1;
@@ -583,13 +667,13 @@ struct A {
583
 
584
  — *end example*]
585
 
586
  When initializing a multi-dimensional array, the *initializer-clause*s
587
  initialize the elements with the last (rightmost) index of the array
588
- varying the fastest ([[dcl.array]]).
589
 
590
- [*Example 9*:
591
 
592
  ``` cpp
593
  int x[2][2] = { 3, 1, 4, 2 };
594
  ```
595
 
@@ -616,11 +700,11 @@ for a subaggregate does not begin with a left brace, then only enough
616
  *initializer-clause*s from the list are taken to initialize the elements
617
  of the subaggregate; any remaining *initializer-clause*s are left to
618
  initialize the next element of the aggregate of which the current
619
  subaggregate is an element.
620
 
621
- [*Example 10*:
622
 
623
  ``` cpp
624
  float y[4][3] = {
625
  { 1, 3, 5 },
626
  { 2, 4, 6 },
@@ -648,22 +732,22 @@ The initializer for `y` begins with a left brace, but the one for `y[0]`
648
  does not, therefore three elements from the list are used. Likewise the
649
  next three are taken successively for `y[1]` and `y[2]`.
650
 
651
  — *end example*]
652
 
653
- All implicit type conversions (Clause  [[conv]]) are considered when
654
- initializing the element with an *assignment-expression*. If the
655
  *assignment-expression* can initialize an element, the element is
656
  initialized. Otherwise, if the element is itself a subaggregate, brace
657
  elision is assumed and the *assignment-expression* is considered for the
658
  initialization of the first element of the subaggregate.
659
 
660
- [*Note 5*: As specified above, brace elision cannot apply to
661
  subaggregates with no elements; an *initializer-clause* for the entire
662
  subobject is required. — *end note*]
663
 
664
- [*Example 11*:
665
 
666
  ``` cpp
667
  struct A {
668
  int i;
669
  operator int();
@@ -680,49 +764,50 @@ Braces are elided around the *initializer-clause* for `b.a1.i`. `b.a1.i`
680
  is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
681
  initialized with whatever `a.operator int()` returns.
682
 
683
  — *end example*]
684
 
685
- [*Note 6*: An aggregate array or an aggregate class may contain
686
- elements of a class type with a user-provided constructor (
687
- [[class.ctor]]). Initialization of these aggregate objects is described
688
  in  [[class.expl.init]]. — *end note*]
689
 
690
- [*Note 7*: Whether the initialization of aggregates with static storage
691
  duration is static or dynamic is specified in  [[basic.start.static]],
692
  [[basic.start.dynamic]], and  [[stmt.dcl]]. — *end note*]
693
 
694
- When a union is initialized with a brace-enclosed initializer, the
695
- braces shall only contain an *initializer-clause* for the first
696
- non-static data member of the union.
697
 
698
- [*Example 12*:
699
 
700
  ``` cpp
701
  union u { int a; const char* b; };
702
  u a = { 1 };
703
  u b = a;
704
  u c = 1; // error
705
  u d = { 0, "asdf" }; // error
706
  u e = { "asdf" }; // error
 
 
707
  ```
708
 
709
  — *end example*]
710
 
711
- [*Note 8*: As described above, the braces around the
712
  *initializer-clause* for a union member can be omitted if the union is a
713
  member of another aggregate. — *end note*]
714
 
715
  ### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
716
 
717
- An array of narrow character type ([[basic.fundamental]]), `char16_t`
718
- array, `char32_t` array, or `wchar_t` array can be initialized by a
719
- narrow string literal, `char16_t` string literal, `char32_t` string
720
- literal, or wide string literal, respectively, or by an
721
- appropriately-typed string literal enclosed in braces ([[lex.string]]).
722
- Successive characters of the value of the string literal initialize the
723
- elements of the array.
724
 
725
  [*Example 1*:
726
 
727
  ``` cpp
728
  char msg[] = "Syntax error on line %s\n";
@@ -745,16 +830,16 @@ char cv[4] = "asdf"; // error
745
  is ill-formed since there is no space for the implied trailing `'\0'`.
746
 
747
  — *end example*]
748
 
749
  If there are fewer initializers than there are array elements, each
750
- element not explicitly initialized shall be zero-initialized (
751
- [[dcl.init]]).
752
 
753
  ### References <a id="dcl.init.ref">[[dcl.init.ref]]</a>
754
 
755
- A variable whose declared type is “reference to type `T`” ([[dcl.ref]])
756
  shall be initialized.
757
 
758
  [*Example 1*:
759
 
760
  ``` cpp
@@ -777,19 +862,19 @@ void f() {
777
 
778
  A reference cannot be changed to refer to another object after
779
  initialization.
780
 
781
  [*Note 1*: Assignment to a reference assigns to the object referred to
782
- by the reference ([[expr.ass]]). — *end note*]
783
 
784
- Argument passing ([[expr.call]]) and function value return (
785
- [[stmt.return]]) are initializations.
786
 
787
  The initializer can be omitted for a reference only in a parameter
788
- declaration ([[dcl.fct]]), in the declaration of a function return
789
- type, in the declaration of a class member within its class definition (
790
- [[class.mem]]), and where the `extern` specifier is explicitly used.
791
 
792
  [*Example 2*:
793
 
794
  ``` cpp
795
  int& r1; // error: initializer missing
@@ -797,48 +882,41 @@ extern int& r2; // OK
797
  ```
798
 
799
  — *end example*]
800
 
801
  Given types “*cv1* `T1`” and “*cv2* `T2`”, “*cv1* `T1`” is
802
- *reference-related* to “*cv2* `T2`” if `T1` is the same type as `T2`, or
803
- `T1` is a base class of `T2`. “*cv1* `T1`” is *reference-compatible*
804
- with “*cv2* `T2`” if
805
-
806
- - `T1` is reference-related to `T2`, or
807
- - `T2` is “`noexcept` function” and `T1` is “function”, where the
808
- function types are otherwise the same,
809
-
810
- and *cv1* is the same cv-qualification as, or greater cv-qualification
811
- than, *cv2*. In all cases where the reference-related or
812
  reference-compatible relationship of two types is used to establish the
813
- validity of a reference binding, and `T1` is a base class of `T2`, a
814
- program that necessitates such a binding is ill-formed if `T1` is an
815
- inaccessible (Clause  [[class.access]]) or ambiguous (
816
- [[class.member.lookup]]) base class of `T2`.
817
 
818
  A reference to type “*cv1* `T1`” is initialized by an expression of type
819
  “*cv2* `T2`” as follows:
820
 
821
  - If the reference is an lvalue reference and the initializer expression
822
  - is an lvalue (but is not a bit-field), and “*cv1* `T1`” is
823
  reference-compatible with “*cv2* `T2`”, or
824
  - has a class type (i.e., `T2` is a class type), where `T1` is not
825
  reference-related to `T2`, and can be converted to an lvalue of type
826
  “*cv3* `T3`”, where “*cv1* `T1`” is reference-compatible with “*cv3*
827
- `T3`”[^14] (this conversion is selected by enumerating the
828
- applicable conversion functions ([[over.match.ref]]) and choosing
829
- the best one through overload resolution ([[over.match]])),
830
 
831
  then the reference is bound to the initializer expression lvalue in
832
  the first case and to the lvalue result of the conversion in the
833
  second case (or, in either case, to the appropriate base class
834
  subobject of the object).
835
- \[*Note 2*: The usual lvalue-to-rvalue ([[conv.lval]]),
836
- array-to-pointer ([[conv.array]]), and function-to-pointer (
837
- [[conv.func]]) standard conversions are not needed, and therefore are
838
- suppressed, when such direct bindings to lvalues are
839
- done. — *end note*]
840
  \[*Example 3*:
841
  ``` cpp
842
  double d = 2.0;
843
  double& rd = d; // rd refers to d
844
  const double& rcd = d; // rcd refers to d
@@ -849,36 +927,36 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
849
  const A& rca = b; // rca refers to A subobject in b
850
  int& ir = B(); // ir refers to the result of B::operator int&
851
  ```
852
 
853
  — *end example*]
854
- - Otherwise, the reference shall be an lvalue reference to a
855
- non-volatile const type (i.e., *cv1* shall be `const`), or the
856
- reference shall be an rvalue reference.
857
  \[*Example 4*:
858
  ``` cpp
859
  double& rd2 = 2.0; // error: not an lvalue and reference not const
860
  int i = 2;
861
  double& rd3 = i; // error: type mismatch and reference not const
862
  ```
863
 
864
  — *end example*]
865
- - If the initializer expression
866
  - is an rvalue (but not a bit-field) or function lvalue and “*cv1*
867
  `T1`” is reference-compatible with “*cv2* `T2`”, or
868
  - has a class type (i.e., `T2` is a class type), where `T1` is not
869
  reference-related to `T2`, and can be converted to an rvalue or
870
  function lvalue of type “*cv3* `T3`”, where “*cv1* `T1`” is
871
  reference-compatible with “*cv3* `T3`” (see  [[over.match.ref]]),
872
 
873
- then the value of the initializer expression in the first case and
874
- the result of the conversion in the second case is called the
875
- converted initializer. If the converted initializer is a prvalue,
876
- its type `T4` is adjusted to type “*cv1* `T4`” ([[conv.qual]]) and
877
- the temporary materialization conversion ([[conv.rval]]) is
878
- applied. In any case, the reference is bound to the resulting
879
- glvalue (or to an appropriate base class subobject).
880
  \[*Example 5*:
881
  ``` cpp
882
  struct A { };
883
  struct B : A { } b;
884
  extern B f();
@@ -894,41 +972,40 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
894
  B&& rrb = x; // bound directly to the result of operator B
895
  ```
896
 
897
  — *end example*]
898
  - Otherwise:
899
- - If `T1` or `T2` is a class type and `T1` is not reference-related
900
- to `T2`, user-defined conversions are considered using the rules
901
- for copy-initialization of an object of type “*cv1* `T1`” by
902
  user-defined conversion ([[dcl.init]], [[over.match.copy]],
903
- [[over.match.conv]]); the program is ill-formed if the
904
- corresponding non-reference copy-initialization would be
905
- ill-formed. The result of the call to the conversion function, as
906
- described for the non-reference copy-initialization, is then used
907
- to direct-initialize the reference. For this
908
- direct-initialization, user-defined conversions are not
909
- considered.
910
  - Otherwise, the initializer expression is implicitly converted to a
911
  prvalue of type “*cv1* `T1`”. The temporary materialization
912
  conversion is applied and the reference is bound to the result.
913
 
914
  If `T1` is reference-related to `T2`:
915
  - *cv1* shall be the same cv-qualification as, or greater
916
  cv-qualification than, *cv2*; and
917
- - if the reference is an rvalue reference, the initializer
918
- expression shall not be an lvalue.
919
 
920
  \[*Example 6*:
921
  ``` cpp
922
  struct Banana { };
923
  struct Enigma { operator const Banana(); };
924
  struct Alaska { operator Banana&(); };
925
  void enigmatic() {
926
  typedef const Banana ConstBanana;
927
- Banana &&banana1 = ConstBanana(); // ill-formed
928
- Banana &&banana2 = Enigma(); // ill-formed
929
- Banana &&banana3 = Alaska(); // ill-formed
930
  }
931
 
932
  const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
933
  double&& rrd = 2; // rrd refers to temporary with value 2.0
934
  const volatile int cvi = 1;
@@ -945,43 +1022,44 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
945
  ```
946
 
947
  — *end example*]
948
 
949
  In all cases except the last (i.e., implicitly converting the
950
- initializer expression to the underlying type of the reference), the
951
- reference is said to *bind directly* to the initializer expression.
952
 
953
  [*Note 3*: [[class.temporary]] describes the lifetime of temporaries
954
  bound to references. — *end note*]
955
 
956
  ### List-initialization <a id="dcl.init.list">[[dcl.init.list]]</a>
957
 
958
  *List-initialization* is initialization of an object or reference from a
959
  *braced-init-list*. Such an initializer is called an *initializer list*,
960
- and the comma-separated *initializer-clause*s of the list are called the
961
- *elements* of the initializer list. An initializer list may be empty.
962
- List-initialization can occur in direct-initialization or
 
963
  copy-initialization contexts; list-initialization in a
964
  direct-initialization context is called *direct-list-initialization* and
965
  list-initialization in a copy-initialization context is called
966
  *copy-list-initialization*.
967
 
968
  [*Note 1*:
969
 
970
  List-initialization can be used
971
 
972
- - as the initializer in a variable definition ([[dcl.init]])
973
- - as the initializer in a *new-expression* ([[expr.new]])
974
- - in a return statement ([[stmt.return]])
975
- - as a *for-range-initializer* ([[stmt.iter]])
976
- - as a function argument ([[expr.call]])
977
- - as a subscript ([[expr.sub]])
978
- - as an argument to a constructor invocation ([[dcl.init]], 
979
  [[expr.type.conv]])
980
- - as an initializer for a non-static data member ([[class.mem]])
981
- - in a *mem-initializer* ([[class.base.init]])
982
- - on the right-hand side of an assignment ([[expr.ass]])
983
 
984
  [*Example 1*:
985
 
986
  ``` cpp
987
  int a = {1};
@@ -997,43 +1075,57 @@ std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
997
  — *end example*]
998
 
999
  — *end note*]
1000
 
1001
  A constructor is an *initializer-list constructor* if its first
1002
- parameter is of type `std::initializer_list<E>` or reference to possibly
1003
- cv-qualified `std::initializer_list<E>` for some type `E`, and either
1004
- there are no other parameters or else all other parameters have default
1005
- arguments ([[dcl.fct.default]]).
1006
 
1007
  [*Note 2*: Initializer-list constructors are favored over other
1008
- constructors in list-initialization ([[over.match.list]]). Passing an
1009
  initializer list as the argument to the constructor template
1010
  `template<class T> C(T)` of a class `C` does not create an
1011
  initializer-list constructor, because an initializer list argument
1012
- causes the corresponding parameter to be a non-deduced context (
1013
- [[temp.deduct.call]]). — *end note*]
1014
 
1015
  The template `std::initializer_list` is not predefined; if the header
1016
- `<initializer_list>` is not included prior to a use of
1017
  `std::initializer_list` — even an implicit use in which the type is not
1018
- named ([[dcl.spec.auto]]) — the program is ill-formed.
1019
 
1020
  List-initialization of an object or reference of type `T` is defined as
1021
  follows:
1022
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1023
  - If `T` is an aggregate class and the initializer list has a single
1024
  element of type *cv* `U`, where `U` is `T` or a class derived from
1025
  `T`, the object is initialized from that element (by
1026
  copy-initialization for copy-list-initialization, or by
1027
  direct-initialization for direct-list-initialization).
1028
  - Otherwise, if `T` is a character array and the initializer list has a
1029
- single element that is an appropriately-typed string literal (
1030
- [[dcl.init.string]]), initialization is performed as described in that
1031
- section.
1032
  - Otherwise, if `T` is an aggregate, aggregate initialization is
1033
- performed ([[dcl.init.aggr]]).
1034
- \[*Example 2*:
1035
  ``` cpp
1036
  double ad[] = { 1, 2.0 }; // OK
1037
  int ai[] = { 1, 2.0 }; // error: narrowing
1038
 
1039
  struct S2 {
@@ -1050,14 +1142,14 @@ follows:
1050
  type with a default constructor, the object is value-initialized.
1051
  - Otherwise, if `T` is a specialization of `std::initializer_list<E>`,
1052
  the object is constructed as described below.
1053
  - Otherwise, if `T` is a class type, constructors are considered. The
1054
  applicable constructors are enumerated and the best one is chosen
1055
- through overload resolution ([[over.match]],  [[over.match.list]]).
1056
- If a narrowing conversion (see below) is required to convert any of
1057
- the arguments, the program is ill-formed.
1058
- \[*Example 3*:
1059
  ``` cpp
1060
  struct S {
1061
  S(std::initializer_list<double>); // #1
1062
  S(std::initializer_list<int>); // #2
1063
  S(); // #3
@@ -1067,20 +1159,20 @@ follows:
1067
  S s2 = { 1, 2, 3 }; // invoke #2
1068
  S s3 = { }; // invoke #3
1069
  ```
1070
 
1071
  — *end example*]
1072
- \[*Example 4*:
1073
  ``` cpp
1074
  struct Map {
1075
  Map(std::initializer_list<std::pair<std::string,int>>);
1076
  };
1077
  Map ship = {{"Sophie",14}, {"Surprise",28}};
1078
  ```
1079
 
1080
  — *end example*]
1081
- \[*Example 5*:
1082
  ``` cpp
1083
  struct S {
1084
  // no initializer-list constructors
1085
  S(int, double, double); // #1
1086
  S(); // #2
@@ -1090,17 +1182,17 @@ follows:
1090
  S s2 { 1.0, 2, 3 }; // error: narrowing
1091
  S s3 { }; // OK: invoke #2
1092
  ```
1093
 
1094
  — *end example*]
1095
- - Otherwise, if `T` is an enumeration with a fixed underlying type (
1096
- [[dcl.enum]]), the *initializer-list* has a single element `v`, and
1097
- the initialization is direct-list-initialization, the object is
1098
- initialized with the value `T(v)` ([[expr.type.conv]]); if a
1099
- narrowing conversion is required to convert `v` to the underlying type
1100
- of `T`, the program is ill-formed.
1101
- \[*Example 6*:
1102
  ``` cpp
1103
  enum byte : unsigned char { };
1104
  byte b { 42 }; // OK
1105
  byte c = { 42 }; // error
1106
  byte d = byte{ 42 }; // OK; same value as b
@@ -1123,26 +1215,28 @@ follows:
1123
  reference-related to `E`, the object or reference is initialized from
1124
  that element (by copy-initialization for copy-list-initialization, or
1125
  by direct-initialization for direct-list-initialization); if a
1126
  narrowing conversion (see below) is required to convert the element to
1127
  `T`, the program is ill-formed.
1128
- \[*Example 7*:
1129
  ``` cpp
1130
  int x1 {2}; // OK
1131
  int x2 {2.0}; // error: narrowing
1132
  ```
1133
 
1134
  — *end example*]
1135
- - Otherwise, if `T` is a reference type, a prvalue of the type
1136
- referenced by `T` is generated. The prvalue initializes its result
1137
- object by copy-list-initialization or direct-list-initialization,
1138
- depending on the kind of initialization for the reference. The prvalue
1139
- is then used to direct-initialize the reference.
 
 
1140
  \[*Note 3*: As usual, the binding will fail and the program is
1141
  ill-formed if the reference type is an lvalue reference to a non-const
1142
  type. — *end note*]
1143
- \[*Example 8*:
1144
  ``` cpp
1145
  struct S {
1146
  S(std::initializer_list<double>); // #1
1147
  S(const std::string&); // #2
1148
  // ...
@@ -1151,24 +1245,28 @@ follows:
1151
  const S& r2 { "Spinach" }; // OK: invoke #2
1152
  S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
1153
  const int& i1 = { 1 }; // OK
1154
  const int& i2 = { 1.1 }; // error: narrowing
1155
  const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
 
 
 
 
1156
  ```
1157
 
1158
  — *end example*]
1159
  - Otherwise, if the initializer list has no elements, the object is
1160
  value-initialized.
1161
- \[*Example 9*:
1162
- ``` cpp
1163
- int** pp {}; // initialized to null pointer
1164
- ```
1165
-
1166
- — *end example*]
1167
- - Otherwise, the program is ill-formed.
1168
  \[*Example 10*:
1169
  ``` cpp
 
 
 
 
 
 
 
1170
  struct A { int i; int j; };
1171
  A a1 { 1, 2 }; // aggregate initialization
1172
  A a2 { 1.2 }; // error: narrowing
1173
  struct B {
1174
  B(std::initializer_list<int>);
@@ -1186,13 +1284,13 @@ follows:
1186
  ```
1187
 
1188
  — *end example*]
1189
 
1190
  Within the *initializer-list* of a *braced-init-list*, the
1191
- *initializer-clause*s, including any that result from pack expansions (
1192
- [[temp.variadic]]), are evaluated in the order in which they appear.
1193
- That is, every value computation and side effect associated with a given
1194
  *initializer-clause* is sequenced before every value computation and
1195
  side effect associated with any *initializer-clause* that follows it in
1196
  the comma-separated list of the *initializer-list*.
1197
 
1198
  [*Note 4*: This evaluation ordering holds regardless of the semantics
@@ -1200,25 +1298,25 @@ of the initialization; for example, it applies when the elements of the
1200
  *initializer-list* are interpreted as arguments of a constructor call,
1201
  even though ordinarily there are no sequencing constraints on the
1202
  arguments of a call. — *end note*]
1203
 
1204
  An object of type `std::initializer_list<E>` is constructed from an
1205
- initializer list as if the implementation generated and materialized (
1206
- [[conv.rval]]) a prvalue of type “array of N `const E`”, where N is the
1207
  number of elements in the initializer list. Each element of that array
1208
  is copy-initialized with the corresponding element of the initializer
1209
  list, and the `std::initializer_list<E>` object is constructed to refer
1210
  to that array.
1211
 
1212
  [*Note 5*: A constructor or conversion function selected for the copy
1213
- shall be accessible (Clause  [[class.access]]) in the context of the
1214
  initializer list. — *end note*]
1215
 
1216
  If a narrowing conversion is required to initialize any of the elements,
1217
  the program is ill-formed.
1218
 
1219
- [*Example 11*:
1220
 
1221
  ``` cpp
1222
  struct X {
1223
  X(std::initializer_list<double> v);
1224
  };
@@ -1236,16 +1334,16 @@ X x(std::initializer_list<double>(__a, __a+3));
1236
  assuming that the implementation can construct an `initializer_list`
1237
  object with a pair of pointers.
1238
 
1239
  — *end example*]
1240
 
1241
- The array has the same lifetime as any other temporary object (
1242
- [[class.temporary]]), except that initializing an `initializer_list`
1243
  object from the array extends the lifetime of the array exactly like
1244
  binding a reference to a temporary.
1245
 
1246
- [*Example 12*:
1247
 
1248
  ``` cpp
1249
  typedef std::complex<double> cmplx;
1250
  std::vector<cmplx> v1 = { 1, 2, 3 };
1251
 
@@ -1264,11 +1362,11 @@ For `v1` and `v2`, the `initializer_list` object is a parameter in a
1264
  function call, so the array created for `{ 1, 2, 3 }` has
1265
  full-expression lifetime. For `i3`, the `initializer_list` object is a
1266
  variable, so the array persists for the lifetime of the variable. For
1267
  `i4`, the `initializer_list` object is initialized in the constructor’s
1268
  *ctor-initializer* as if by binding a temporary array to a reference
1269
- member, so the program is ill-formed ([[class.base.init]]).
1270
 
1271
  — *end example*]
1272
 
1273
  [*Note 6*: The implementation is free to allocate the array in
1274
  read-only memory if an explicit array with the same initializer could be
@@ -1286,16 +1384,17 @@ A *narrowing conversion* is an implicit conversion
1286
  value after conversion will fit into the target type and will produce
1287
  the original value when converted back to the original type, or
1288
  - from an integer type or unscoped enumeration type to an integer type
1289
  that cannot represent all the values of the original type, except
1290
  where the source is a constant expression whose value after integral
1291
- promotions will fit into the target type.
 
1292
 
1293
  [*Note 7*: As indicated above, such conversions are not allowed at the
1294
  top level in list-initializations. — *end note*]
1295
 
1296
- [*Example 13*:
1297
 
1298
  ``` cpp
1299
  int x = 999; // x is not a constant expression
1300
  const int y = 999;
1301
  const int z = 99;
@@ -1309,258 +1408,12 @@ unsigned int ui1 = {-1}; // error: narrows
1309
  signed int si1 =
1310
  { (unsigned int)-1 }; // error: narrows
1311
  int ii = {2.0}; // error: narrows
1312
  float f1 { x }; // error: might narrow
1313
  float f2 { 7 }; // OK: 7 can be exactly represented as a float
 
1314
  int f(int);
1315
- int a[] =
1316
- { 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level
1317
  ```
1318
 
1319
  — *end example*]
1320
 
1321
- <!-- Link reference definitions -->
1322
- [basic.align]: basic.md#basic.align
1323
- [basic.compound]: basic.md#basic.compound
1324
- [basic.def]: basic.md#basic.def
1325
- [basic.def.odr]: basic.md#basic.def.odr
1326
- [basic.fundamental]: basic.md#basic.fundamental
1327
- [basic.life]: basic.md#basic.life
1328
- [basic.link]: basic.md#basic.link
1329
- [basic.lookup]: basic.md#basic.lookup
1330
- [basic.lookup.argdep]: basic.md#basic.lookup.argdep
1331
- [basic.lookup.classref]: basic.md#basic.lookup.classref
1332
- [basic.lookup.elab]: basic.md#basic.lookup.elab
1333
- [basic.lookup.qual]: basic.md#basic.lookup.qual
1334
- [basic.lookup.udir]: basic.md#basic.lookup.udir
1335
- [basic.lookup.unqual]: basic.md#basic.lookup.unqual
1336
- [basic.lval]: basic.md#basic.lval
1337
- [basic.namespace]: #basic.namespace
1338
- [basic.scope]: basic.md#basic.scope
1339
- [basic.scope.block]: basic.md#basic.scope.block
1340
- [basic.scope.declarative]: basic.md#basic.scope.declarative
1341
- [basic.scope.namespace]: basic.md#basic.scope.namespace
1342
- [basic.scope.pdecl]: basic.md#basic.scope.pdecl
1343
- [basic.scope.proto]: basic.md#basic.scope.proto
1344
- [basic.start]: basic.md#basic.start
1345
- [basic.start.dynamic]: basic.md#basic.start.dynamic
1346
- [basic.start.static]: basic.md#basic.start.static
1347
- [basic.stc]: basic.md#basic.stc
1348
- [basic.stc.auto]: basic.md#basic.stc.auto
1349
- [basic.stc.dynamic]: basic.md#basic.stc.dynamic
1350
- [basic.stc.static]: basic.md#basic.stc.static
1351
- [basic.stc.thread]: basic.md#basic.stc.thread
1352
- [basic.type.qualifier]: basic.md#basic.type.qualifier
1353
- [basic.types]: basic.md#basic.types
1354
- [class]: class.md#class
1355
- [class.access]: class.md#class.access
1356
- [class.base.init]: special.md#class.base.init
1357
- [class.bit]: class.md#class.bit
1358
- [class.conv]: special.md#class.conv
1359
- [class.conv.ctor]: special.md#class.conv.ctor
1360
- [class.conv.fct]: special.md#class.conv.fct
1361
- [class.copy]: special.md#class.copy
1362
- [class.ctor]: special.md#class.ctor
1363
- [class.dtor]: special.md#class.dtor
1364
- [class.expl.init]: special.md#class.expl.init
1365
- [class.friend]: class.md#class.friend
1366
- [class.inhctor.init]: special.md#class.inhctor.init
1367
- [class.init]: special.md#class.init
1368
- [class.mem]: class.md#class.mem
1369
- [class.member.lookup]: class.md#class.member.lookup
1370
- [class.mfct]: class.md#class.mfct
1371
- [class.mi]: class.md#class.mi
1372
- [class.name]: class.md#class.name
1373
- [class.qual]: basic.md#class.qual
1374
- [class.static]: class.md#class.static
1375
- [class.static.data]: class.md#class.static.data
1376
- [class.temporary]: special.md#class.temporary
1377
- [class.union]: class.md#class.union
1378
- [class.union.anon]: class.md#class.union.anon
1379
- [class.virtual]: class.md#class.virtual
1380
- [conv]: conv.md#conv
1381
- [conv.array]: conv.md#conv.array
1382
- [conv.func]: conv.md#conv.func
1383
- [conv.integral]: conv.md#conv.integral
1384
- [conv.lval]: conv.md#conv.lval
1385
- [conv.prom]: conv.md#conv.prom
1386
- [conv.ptr]: conv.md#conv.ptr
1387
- [conv.qual]: conv.md#conv.qual
1388
- [conv.rval]: conv.md#conv.rval
1389
- [cstddef.syn]: language.md#cstddef.syn
1390
- [dcl.align]: #dcl.align
1391
- [dcl.ambig.res]: #dcl.ambig.res
1392
- [dcl.array]: #dcl.array
1393
- [dcl.asm]: #dcl.asm
1394
- [dcl.attr]: #dcl.attr
1395
- [dcl.attr.depend]: #dcl.attr.depend
1396
- [dcl.attr.deprecated]: #dcl.attr.deprecated
1397
- [dcl.attr.fallthrough]: #dcl.attr.fallthrough
1398
- [dcl.attr.grammar]: #dcl.attr.grammar
1399
- [dcl.attr.nodiscard]: #dcl.attr.nodiscard
1400
- [dcl.attr.noreturn]: #dcl.attr.noreturn
1401
- [dcl.attr.unused]: #dcl.attr.unused
1402
- [dcl.constexpr]: #dcl.constexpr
1403
- [dcl.dcl]: #dcl.dcl
1404
- [dcl.decl]: #dcl.decl
1405
- [dcl.enum]: #dcl.enum
1406
- [dcl.fct]: #dcl.fct
1407
- [dcl.fct.def]: #dcl.fct.def
1408
- [dcl.fct.def.default]: #dcl.fct.def.default
1409
- [dcl.fct.def.delete]: #dcl.fct.def.delete
1410
- [dcl.fct.def.general]: #dcl.fct.def.general
1411
- [dcl.fct.default]: #dcl.fct.default
1412
- [dcl.fct.spec]: #dcl.fct.spec
1413
- [dcl.friend]: #dcl.friend
1414
- [dcl.init]: #dcl.init
1415
- [dcl.init.aggr]: #dcl.init.aggr
1416
- [dcl.init.list]: #dcl.init.list
1417
- [dcl.init.ref]: #dcl.init.ref
1418
- [dcl.init.string]: #dcl.init.string
1419
- [dcl.inline]: #dcl.inline
1420
- [dcl.link]: #dcl.link
1421
- [dcl.meaning]: #dcl.meaning
1422
- [dcl.mptr]: #dcl.mptr
1423
- [dcl.name]: #dcl.name
1424
- [dcl.ptr]: #dcl.ptr
1425
- [dcl.ref]: #dcl.ref
1426
- [dcl.spec]: #dcl.spec
1427
- [dcl.spec.auto]: #dcl.spec.auto
1428
- [dcl.stc]: #dcl.stc
1429
- [dcl.struct.bind]: #dcl.struct.bind
1430
- [dcl.type]: #dcl.type
1431
- [dcl.type.auto.deduct]: #dcl.type.auto.deduct
1432
- [dcl.type.class.deduct]: #dcl.type.class.deduct
1433
- [dcl.type.cv]: #dcl.type.cv
1434
- [dcl.type.elab]: #dcl.type.elab
1435
- [dcl.type.simple]: #dcl.type.simple
1436
- [dcl.typedef]: #dcl.typedef
1437
- [except.handle]: except.md#except.handle
1438
- [except.spec]: except.md#except.spec
1439
- [except.throw]: except.md#except.throw
1440
- [expr]: expr.md#expr
1441
- [expr.alignof]: expr.md#expr.alignof
1442
- [expr.ass]: expr.md#expr.ass
1443
- [expr.call]: expr.md#expr.call
1444
- [expr.cast]: expr.md#expr.cast
1445
- [expr.comma]: expr.md#expr.comma
1446
- [expr.cond]: expr.md#expr.cond
1447
- [expr.const]: expr.md#expr.const
1448
- [expr.const.cast]: expr.md#expr.const.cast
1449
- [expr.mptr.oper]: expr.md#expr.mptr.oper
1450
- [expr.new]: expr.md#expr.new
1451
- [expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
1452
- [expr.prim.this]: expr.md#expr.prim.this
1453
- [expr.ref]: expr.md#expr.ref
1454
- [expr.static.cast]: expr.md#expr.static.cast
1455
- [expr.sub]: expr.md#expr.sub
1456
- [expr.type.conv]: expr.md#expr.type.conv
1457
- [expr.unary]: expr.md#expr.unary
1458
- [expr.unary.op]: expr.md#expr.unary.op
1459
- [intro.compliance]: intro.md#intro.compliance
1460
- [intro.execution]: intro.md#intro.execution
1461
- [intro.multithread]: intro.md#intro.multithread
1462
- [lex.charset]: lex.md#lex.charset
1463
- [lex.digraph]: lex.md#lex.digraph
1464
- [lex.key]: lex.md#lex.key
1465
- [lex.name]: lex.md#lex.name
1466
- [lex.string]: lex.md#lex.string
1467
- [namespace.alias]: #namespace.alias
1468
- [namespace.def]: #namespace.def
1469
- [namespace.memdef]: #namespace.memdef
1470
- [namespace.qual]: basic.md#namespace.qual
1471
- [namespace.udecl]: #namespace.udecl
1472
- [namespace.udir]: #namespace.udir
1473
- [namespace.unnamed]: #namespace.unnamed
1474
- [over]: over.md#over
1475
- [over.match]: over.md#over.match
1476
- [over.match.class.deduct]: over.md#over.match.class.deduct
1477
- [over.match.conv]: over.md#over.match.conv
1478
- [over.match.copy]: over.md#over.match.copy
1479
- [over.match.ctor]: over.md#over.match.ctor
1480
- [over.match.list]: over.md#over.match.list
1481
- [over.match.ref]: over.md#over.match.ref
1482
- [over.oper]: over.md#over.oper
1483
- [over.sub]: over.md#over.sub
1484
- [stmt.ambig]: stmt.md#stmt.ambig
1485
- [stmt.dcl]: stmt.md#stmt.dcl
1486
- [stmt.expr]: stmt.md#stmt.expr
1487
- [stmt.if]: stmt.md#stmt.if
1488
- [stmt.iter]: stmt.md#stmt.iter
1489
- [stmt.label]: stmt.md#stmt.label
1490
- [stmt.return]: stmt.md#stmt.return
1491
- [stmt.select]: stmt.md#stmt.select
1492
- [stmt.stmt]: stmt.md#stmt.stmt
1493
- [stmt.switch]: stmt.md#stmt.switch
1494
- [support.runtime]: language.md#support.runtime
1495
- [tab:simple.type.specifiers]: #tab:simple.type.specifiers
1496
- [temp]: temp.md#temp
1497
- [temp.arg.type]: temp.md#temp.arg.type
1498
- [temp.class.spec]: temp.md#temp.class.spec
1499
- [temp.deduct]: temp.md#temp.deduct
1500
- [temp.deduct.call]: temp.md#temp.deduct.call
1501
- [temp.dep]: temp.md#temp.dep
1502
- [temp.expl.spec]: temp.md#temp.expl.spec
1503
- [temp.explicit]: temp.md#temp.explicit
1504
- [temp.inst]: temp.md#temp.inst
1505
- [temp.mem]: temp.md#temp.mem
1506
- [temp.names]: temp.md#temp.names
1507
- [temp.param]: temp.md#temp.param
1508
- [temp.res]: temp.md#temp.res
1509
- [temp.spec]: temp.md#temp.spec
1510
- [temp.variadic]: temp.md#temp.variadic
1511
-
1512
- [^1]: The “implicit int” rule of C is no longer supported.
1513
-
1514
- [^2]: The `inline` keyword has no effect on the linkage of a function.
1515
-
1516
- [^3]: There is no special provision for a *decl-specifier-seq* that
1517
- lacks a *type-specifier* or that has a *type-specifier* that only
1518
- specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
1519
- supported.
1520
-
1521
- [^4]: This set of values is used to define promotion and conversion
1522
- semantics for the enumeration type. It does not preclude an
1523
- expression of enumeration type from having a value that falls
1524
- outside this range.
1525
-
1526
- [^5]: this implies that the name of the class or function is
1527
- unqualified.
1528
-
1529
- [^6]: A *using-declaration* with more than one *using-declarator* is
1530
- equivalent to a corresponding sequence of *using-declaration*s with
1531
- one *using-declarator* each.
1532
-
1533
- [^7]: During name lookup in a class hierarchy, some ambiguities may be
1534
- resolved by considering whether one member hides the other along
1535
- some paths ([[class.member.lookup]]). There is no such
1536
- disambiguation when considering the set of names found as a result
1537
- of following *using-directive*s.
1538
-
1539
- [^8]: As indicated by syntax, cv-qualifiers are a significant component
1540
- in function return types.
1541
-
1542
- [^9]: One can explicitly disambiguate the parse either by introducing a
1543
- comma (so the ellipsis will be parsed as part of the
1544
- *parameter-declaration-clause*) or by introducing a name for the
1545
- parameter (so the ellipsis will be parsed as part of the
1546
- *declarator-id*).
1547
-
1548
- [^10]: This means that default arguments cannot appear, for example, in
1549
- declarations of pointers to functions, references to functions, or
1550
- `typedef` declarations.
1551
-
1552
- [^11]: Implementations are permitted to provide additional predefined
1553
- variables with names that are reserved to the implementation (
1554
- [[lex.name]]). If a predefined variable is not odr-used (
1555
- [[basic.def.odr]]), its string value need not be present in the
1556
- program image.
1557
-
1558
- [^12]: As specified in  [[conv.ptr]], converting an integer literal
1559
- whose value is `0` to a pointer type results in a null pointer
1560
- value.
1561
-
1562
- [^13]: The syntax provides for empty *initializer-list*s, but
1563
- nonetheless C++does not have zero length arrays.
1564
-
1565
- [^14]: This requires a conversion function ([[class.conv.fct]])
1566
- returning a reference type.
 
1
  ## Initializers <a id="dcl.init">[[dcl.init]]</a>
2
 
3
+ The process of initialization described in this subclause applies to all
4
+ initializations regardless of syntactic context, including the
5
+ initialization of a function parameter [[expr.call]], the initialization
6
+ of a return value [[stmt.return]], or when an initializer follows a
7
+ declarator.
 
8
 
9
  ``` bnf
10
  initializer:
11
  brace-or-equal-initializer
12
  '(' expression-list ')'
 
22
  initializer-clause:
23
  assignment-expression
24
  braced-init-list
25
  ```
26
 
 
 
 
 
 
 
27
  ``` bnf
28
  braced-init-list:
29
  '{' initializer-list ','ₒₚₜ '}'
30
+ '{' designated-initializer-list ','ₒₚₜ '}'
31
  '{' '}'
32
  ```
33
 
34
+ ``` bnf
35
+ initializer-list:
36
+ initializer-clause '...'ₒₚₜ
37
+ initializer-list ',' initializer-clause '...'ₒₚₜ
38
+ ```
39
+
40
+ ``` bnf
41
+ designated-initializer-list:
42
+ designated-initializer-clause
43
+ designated-initializer-list ',' designated-initializer-clause
44
+ ```
45
+
46
+ ``` bnf
47
+ designated-initializer-clause:
48
+ designator brace-or-equal-initializer
49
+ ```
50
+
51
+ ``` bnf
52
+ designator:
53
+ '.' identifier
54
+ ```
55
+
56
  ``` bnf
57
  expr-or-braced-init-list:
58
  expression
59
  braced-init-list
60
  ```
61
 
62
+ [*Note 1*: The rules in this subclause apply even if the grammar
63
+ permits only the *brace-or-equal-initializer* form of *initializer* in a
64
+ given context. — *end note*]
65
+
66
  Except for objects declared with the `constexpr` specifier, for which
67
  see  [[dcl.constexpr]], an *initializer* in the definition of a variable
68
  can consist of arbitrary expressions involving literals and previously
69
  declared variables and functions, regardless of the variable’s storage
70
  duration.
 
78
  int c(b);
79
  ```
80
 
81
  — *end example*]
82
 
83
+ [*Note 2*: Default arguments are more restricted; see 
84
  [[dcl.fct.default]]. — *end note*]
85
 
86
+ [*Note 3*: The order of initialization of variables with static storage
87
  duration is described in  [[basic.start]] and 
88
  [[stmt.dcl]]. — *end note*]
89
 
90
  A declaration of a block-scope variable with external or internal
91
  linkage that has an *initializer* is ill-formed.
92
 
93
  To *zero-initialize* an object or reference of type `T` means:
94
 
95
+ - if `T` is a scalar type [[basic.types]], the object is initialized to
96
+ the value obtained by converting the integer literal `0` (zero) to
97
+ `T`;[^5]
98
+ - if `T` is a (possibly cv-qualified) non-union class type, its padding
99
+ bits [[basic.types]] are initialized to zero bits and each non-static
100
+ data member, each non-virtual base class subobject, and, if the object
101
+ is not a base class subobject, each virtual base class subobject is
102
+ zero-initialized;
103
+ - if `T` is a (possibly cv-qualified) union type, its padding bits
104
+ [[basic.types]] are initialized to zero bits and the object’s first
105
+ non-static named data member is zero-initialized;
106
  - if `T` is an array type, each element is zero-initialized;
107
  - if `T` is a reference type, no initialization is performed.
108
 
109
  To *default-initialize* an object of type `T` means:
110
 
111
+ - If `T` is a (possibly cv-qualified) class type [[class]], constructors
112
+ are considered. The applicable constructors are enumerated
113
+ [[over.match.ctor]], and the best one for the *initializer* `()` is
114
+ chosen through overload resolution [[over.match]]. The constructor
115
+ thus selected is called, with an empty argument list, to initialize
116
+ the object.
117
  - If `T` is an array type, each element is default-initialized.
118
  - Otherwise, no initialization is performed.
119
 
120
  A class type `T` is *const-default-constructible* if
121
  default-initialization of `T` would invoke a user-provided constructor
 
136
  const-qualified type `T`, `T` shall be a const-default-constructible
137
  class type or array thereof.
138
 
139
  To *value-initialize* an object of type `T` means:
140
 
141
+ - if `T` is a (possibly cv-qualified) class type [[class]], then
142
+ - if `T` has either no default constructor [[class.default.ctor]] or a
143
+ default constructor that is user-provided or deleted, then the
144
+ object is default-initialized;
145
+ - otherwise, the object is zero-initialized and the semantic
146
+ constraints for default-initialization are checked, and if `T` has a
147
+ non-trivial default constructor, the object is default-initialized;
 
 
148
  - if `T` is an array type, then each element is value-initialized;
149
  - otherwise, the object is zero-initialized.
150
 
151
  A program that calls for default-initialization or value-initialization
152
  of an entity of reference type is ill-formed.
153
 
154
+ [*Note 4*: For every object of static storage duration, static
155
+ initialization [[basic.start.static]] is performed at program startup
156
+ before any other initialization takes place. In some cases, additional
157
+ initialization is done later. — *end note*]
158
 
159
  An object whose initializer is an empty set of parentheses, i.e., `()`,
160
  shall be value-initialized.
161
 
162
+ [*Note 5*:
163
 
164
  Since `()` is not permitted by the syntax for *initializer*,
165
 
166
  ``` cpp
167
  X a();
168
  ```
169
 
170
  is not the declaration of an object of class `X`, but the declaration of
171
  a function taking no argument and returning an `X`. The form `()` is
172
+ permitted in certain other initialization contexts ([[expr.new]],
173
  [[expr.type.conv]], [[class.base.init]]).
174
 
175
  — *end note*]
176
 
177
  If no initializer is specified for an object, the object is
178
+ default-initialized.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
  An initializer for a static member is in the scope of the member’s
181
  class.
182
 
183
+ [*Example 2*:
184
 
185
  ``` cpp
186
  int a;
187
 
188
  struct X {
 
199
  If the entity being initialized does not have class type, the
200
  *expression-list* in a parenthesized initializer shall be a single
201
  expression.
202
 
203
  The initialization that occurs in the `=` form of a
204
+ *brace-or-equal-initializer* or *condition* [[stmt.select]], as well as
205
+ in argument passing, function return, throwing an exception
206
+ [[except.throw]], handling an exception [[except.handle]], and aggregate
207
+ member initialization [[dcl.init.aggr]], is called
208
  *copy-initialization*.
209
 
210
+ [*Note 6*: Copy-initialization may invoke a move
211
+ [[class.copy.ctor]]. — *end note*]
212
 
213
+ The initialization that occurs
214
 
215
+ - for an *initializer* that is a parenthesized *expression-list* or a
216
+ *braced-init-list*,
217
+ - for a *new-initializer* [[expr.new]],
218
+ - in a `static_cast` expression [[expr.static.cast]],
219
+ - in a functional notation type conversion [[expr.type.conv]], and
220
+ - in the *braced-init-list* form of a *condition*
221
 
 
 
 
 
222
  is called *direct-initialization*.
223
 
224
  The semantics of initializers are as follows. The *destination type* is
225
  the type of the object or reference being initialized and the *source
226
  type* is the type of the initializer expression. If the initializer is
227
  not a single (possibly parenthesized) expression, the source type is not
228
  defined.
229
 
230
  - If the initializer is a (non-parenthesized) *braced-init-list* or is
231
+ `=` *braced-init-list*, the object or reference is list-initialized
232
+ [[dcl.init.list]].
233
  - If the destination type is a reference type, see  [[dcl.init.ref]].
234
  - If the destination type is an array of characters, an array of
235
+ `char8_t`, an array of `char16_t`, an array of `char32_t`, or an array
236
+ of `wchar_t`, and the initializer is a *string-literal*, see 
237
+ [[dcl.init.string]].
238
  - If the initializer is `()`, the object is value-initialized.
239
+ - Otherwise, if the destination type is an array, the object is
240
+ initialized as follows. Let x₁, …, xₖ be the elements of the
241
+ *expression-list*. If the destination type is an array of unknown
242
+ bound, it is defined as having k elements. Let n denote the array size
243
+ after this potential adjustment. If k is greater than n, the program
244
+ is ill-formed. Otherwise, the iᵗʰ array element is copy-initialized
245
+ with xᵢ for each 1 ≤ i ≤ k, and value-initialized for each k < i ≤ n.
246
+ For each 1 ≤ i < j ≤ n, every value computation and side effect
247
+ associated with the initialization of the iᵗʰ element of the array is
248
+ sequenced before those associated with the initialization of the jᵗʰ
249
+ element.
250
+ - Otherwise, if the destination type is a (possibly cv-qualified) class
251
+ type:
252
  - If the initializer expression is a prvalue and the cv-unqualified
253
  version of the source type is the same class as the class of the
254
  destination, the initializer expression is used to initialize the
255
+ destination object. \[*Example 3*: `T x = T(T(T()));` calls the `T`
256
  default constructor to initialize `x`. — *end example*]
257
  - Otherwise, if the initialization is direct-initialization, or if it
258
  is copy-initialization where the cv-unqualified version of the
259
  source type is the same class as, or a derived class of, the class
260
  of the destination, constructors are considered. The applicable
261
+ constructors are enumerated [[over.match.ctor]], and the best one is
262
+ chosen through overload resolution [[over.match]]. Then:
263
+ - If overload resolution is successful, the selected constructor is
264
+ called to initialize the object, with the initializer expression
265
+ or *expression-list* as its argument(s).
266
+ - Otherwise, if no constructor is viable, the destination type is an
267
+ aggregate class, and the initializer is a parenthesized
268
+ *expression-list*, the object is initialized as follows. Let e₁,
269
+ …, eₙ be the elements of the aggregate [[dcl.init.aggr]]. Let x₁,
270
+ …, xₖ be the elements of the *expression-list*. If k is greater
271
+ than n, the program is ill-formed. The element eᵢ is
272
+ copy-initialized with xᵢ for 1 ≤ i ≤ k. The remaining elements are
273
+ initialized with their default member initializers, if any, and
274
+ otherwise are value-initialized. For each 1 ≤ i < j ≤ n, every
275
+ value computation and side effect associated with the
276
+ initialization of eᵢ is sequenced before those associated with the
277
+ initialization of eⱼ.
278
+ \[*Note 7*:
279
+ By contrast with direct-list-initialization, narrowing conversions
280
+ [[dcl.init.list]] are permitted, designators are not permitted, a
281
+ temporary object bound to a reference does not have its lifetime
282
+ extended [[class.temporary]], and there is no brace elision.
283
+ \[*Example 4*:
284
+ ``` cpp
285
+ struct A {
286
+ int a;
287
+ int&& r;
288
+ };
289
+
290
+ int f();
291
+ int n = 10;
292
+
293
+ A a1{1, f()}; // OK, lifetime is extended
294
+ A a2(1, f()); // well-formed, but dangling reference
295
+ A a3{1.0, 1}; // error: narrowing conversion
296
+ A a4(1.0, 1); // well-formed, but dangling reference
297
+ A a5(1.0, std::move(n)); // OK
298
+ ```
299
+
300
+ — *end example*]
301
+ — *end note*]
302
+ - Otherwise, the initialization is ill-formed.
303
  - Otherwise (i.e., for the remaining copy-initialization cases),
304
+ user-defined conversions that can convert from the source type to
305
+ the destination type or (when a conversion function is used) to a
306
+ derived class thereof are enumerated as described in 
307
  [[over.match.copy]], and the best one is chosen through overload
308
+ resolution [[over.match]]. If the conversion cannot be done or is
309
  ambiguous, the initialization is ill-formed. The function selected
310
  is called with the initializer expression as its argument; if the
311
  function is a constructor, the call is a prvalue of the
312
  cv-unqualified version of the destination type whose result object
313
  is initialized by the constructor. The call is used to
314
  direct-initialize, according to the rules above, the object that is
315
  the destination of the copy-initialization.
316
  - Otherwise, if the source type is a (possibly cv-qualified) class type,
317
  conversion functions are considered. The applicable conversion
318
+ functions are enumerated [[over.match.conv]], and the best one is
319
+ chosen through overload resolution [[over.match]]. The user-defined
320
  conversion so selected is called to convert the initializer expression
321
  into the object being initialized. If the conversion cannot be done or
322
  is ambiguous, the initialization is ill-formed.
323
+ - Otherwise, if the initialization is direct-initialization, the source
324
+ type is `std::nullptr_t`, and the destination type is `bool`, the
325
+ initial value of the object being initialized is `false`.
326
  - Otherwise, the initial value of the object being initialized is the
327
+ (possibly converted) value of the initializer expression. A standard
328
+ conversion sequence [[conv]] will be used, if necessary, to convert
329
  the initializer expression to the cv-unqualified version of the
330
  destination type; no user-defined conversions are considered. If the
331
  conversion cannot be done, the initialization is ill-formed. When
332
  initializing a bit-field with a value that it cannot represent, the
333
  resulting value of the bit-field is *implementation-defined*.
334
+ \[*Note 8*:
335
  An expression of type “*cv1* `T`” can initialize an object of type
336
  “*cv2* `T`” independently of the cv-qualifiers *cv1* and *cv2*.
337
  ``` cpp
338
  int a;
339
  const int b = a;
340
  int c = b;
341
  ```
342
 
343
  — *end note*]
344
 
345
+ An *initializer-clause* followed by an ellipsis is a pack expansion
346
+ [[temp.variadic]].
347
 
348
  If the initializer is a parenthesized *expression-list*, the expressions
349
+ are evaluated in the order specified for function calls [[expr.call]].
350
+
351
+ The same *identifier* shall not appear in multiple *designator*s of a
352
+ *designated-initializer-list*.
353
 
354
  An object whose initialization has completed is deemed to be
355
+ constructed, even if the object is of non-class type or no constructor
356
+ of the object’s class is invoked for the initialization.
357
 
358
+ [*Note 9*: Such an object might have been value-initialized or
359
+ initialized by aggregate initialization [[dcl.init.aggr]] or by an
360
+ inherited constructor [[class.inhctor.init]]. — *end note*]
361
+
362
+ Destroying an object of class type invokes the destructor of the class.
363
+ Destroying a scalar type has no effect other than ending the lifetime of
364
+ the object [[basic.life]]. Destroying an array destroys each element in
365
+ reverse subscript order.
366
 
367
  A declaration that specifies the initialization of a variable, whether
368
  from an explicit initializer or by default-initialization, is called the
369
  *initializing declaration* of that variable.
370
 
371
+ [*Note 10*: In most cases this is the defining declaration
372
+ [[basic.def]] of the variable, but the initializing declaration of a
373
+ non-inline static data member [[class.static.data]] might be the
374
  declaration within the class definition and not the definition at
375
  namespace scope. — *end note*]
376
 
377
  ### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
378
 
379
+ An *aggregate* is an array or a class [[class]] with
380
 
381
+ - no user-declared or inherited constructors [[class.ctor]],
382
+ - no private or protected direct non-static data members
383
+ [[class.access]],
384
+ - no virtual functions [[class.virtual]], and
385
+ - no virtual, private, or protected base classes [[class.mi]].
 
386
 
387
  [*Note 1*: Aggregate initialization does not allow accessing protected
388
  and private base class’ members or constructors. — *end note*]
389
 
390
  The *elements* of an aggregate are:
391
 
392
  - for an array, the array elements in increasing subscript order, or
393
  - for a class, the direct base classes in declaration order, followed by
394
+ the direct non-static data members [[class.mem]] that are not members
395
+ of an anonymous union, in declaration order.
396
 
397
  When an aggregate is initialized by an initializer list as specified in 
398
  [[dcl.init.list]], the elements of the initializer list are taken as
399
+ initializers for the elements of the aggregate. The *explicitly
400
+ initialized elements* of the aggregate are determined as follows:
401
+
402
+ - If the initializer list is a *designated-initializer-list*, the
403
+ aggregate shall be of class type, the *identifier* in each
404
+ *designator* shall name a direct non-static data member of the class,
405
+ and the explicitly initialized elements of the aggregate are the
406
+ elements that are, or contain, those members.
407
+ - If the initializer list is an *initializer-list*, the explicitly
408
+ initialized elements of the aggregate are the first n elements of the
409
+ aggregate, where n is the number of elements in the initializer list.
410
+ - Otherwise, the initializer list must be `{}`, and there are no
411
+ explicitly initialized elements.
412
+
413
+ For each explicitly initialized element:
414
+
415
+ - If the element is an anonymous union object and the initializer list
416
+ is a *designated-initializer-list*, the anonymous union object is
417
+ initialized by the *designated-initializer-list* `{ `*D*` }`, where
418
+ *D* is the *designated-initializer-clause* naming a member of the
419
+ anonymous union object. There shall be only one such
420
+ *designated-initializer-clause*.
421
+ \[*Example 1*:
422
+ ``` cpp
423
+ struct C {
424
+ union {
425
+ int a;
426
+ const char* p;
427
+ };
428
+ int x;
429
+ } c = { .a = 1, .x = 3 };
430
+ ```
431
+
432
+ initializes `c.a` with 1 and `c.x` with 3.
433
+ — *end example*]
434
+ - Otherwise, the element is copy-initialized from the corresponding
435
+ *initializer-clause* or is initialized with the
436
+ *brace-or-equal-initializer* of the corresponding
437
+ *designated-initializer-clause*. If that initializer is of the form
438
+ *assignment-expression* or `= `*assignment-expression* and a narrowing
439
+ conversion [[dcl.init.list]] is required to convert the expression,
440
+ the program is ill-formed.
441
+ \[*Note 2*: If an initializer is itself an initializer list, the
442
+ element is list-initialized, which will result in a recursive
443
+ application of the rules in this subclause if the element is an
444
  aggregate. — *end note*]
445
+ \[*Example 2*:
 
 
446
  ``` cpp
447
  struct A {
448
  int x;
449
  struct B {
450
  int i;
 
452
  } b;
453
  } a = { 1, { 2, 3 } };
454
  ```
455
 
456
  initializes `a.x` with 1, `a.b.i` with 2, `a.b.j` with 3.
 
457
  ``` cpp
458
  struct base1 { int b1, b2 = 42; };
459
  struct base2 {
460
  base2() {
461
  b3 = 42;
 
468
 
469
  derived d1{{1, 2}, {}, 4};
470
  derived d2{{}, {}, 4};
471
  ```
472
 
473
+ initializes `d1.b1` with 1, `d1.b2` with 2, `d1.b3` with 42, `d1.d`
474
+ with 4, and `d2.b1` with 0, `d2.b2` with 42, `d2.b3` with 42, `d2.d`
475
+ with 4.
476
+ — *end example*]
477
+
478
+ For a non-union aggregate, each element that is not an explicitly
479
+ initialized element is initialized as follows:
480
+
481
+ - If the element has a default member initializer [[class.mem]], the
482
+ element is initialized from that initializer.
483
+ - Otherwise, if the element is not a reference, the element is
484
+ copy-initialized from an empty initializer list [[dcl.init.list]].
485
+ - Otherwise, the program is ill-formed.
486
+
487
+ If the aggregate is a union and the initializer list is empty, then
488
+
489
+ - if any variant member has a default member initializer, that member is
490
+ initialized from its default member initializer;
491
+ - otherwise, the first member of the union (if any) is copy-initialized
492
+ from an empty initializer list.
493
+
494
+ [*Example 3*:
495
+
496
+ ``` cpp
497
+ struct S { int a; const char* b; int c; int d = b[a]; };
498
+ S ss = { 1, "asdf" };
499
+ ```
500
+
501
+ initializes `ss.a` with 1, `ss.b` with `"asdf"`, `ss.c` with the value
502
+ of an expression of the form `int{}` (that is, `0`), and `ss.d` with the
503
+ value of `ss.b[ss.a]` (that is, `'s'`), and in
504
+
505
+ ``` cpp
506
+ struct X { int i, j, k = 42; };
507
+ X a[] = { 1, 2, 3, 4, 5, 6 };
508
+ X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
509
+ ```
510
+
511
+ `a` and `b` have the same value
512
+
513
+ ``` cpp
514
+ struct A {
515
+ string a;
516
+ int b = 42;
517
+ int c = -1;
518
+ };
519
+ ```
520
+
521
+ `A{.c=21}` has the following steps:
522
+
523
+ - Initialize `a` with `{}`
524
+ - Initialize `b` with `= 42`
525
+ - Initialize `c` with `= 21`
526
 
527
  — *end example*]
528
 
529
+ The initializations of the elements of the aggregate are evaluated in
530
+ the element order. That is, all value computations and side effects
531
+ associated with a given element are sequenced before those of any
532
+ element that follows it in order.
533
+
534
  An aggregate that is a class can also be initialized with a single
535
  expression not enclosed in braces, as described in  [[dcl.init]].
536
 
537
+ The destructor for each element of class type is potentially invoked
538
+ [[class.dtor]] from the context where the aggregate initialization
539
+ occurs.
540
+
541
+ [*Note 3*: This provision ensures that destructors can be called for
542
+ fully-constructed subobjects in case an exception is thrown
543
+ [[except.ctor]]. — *end note*]
544
+
545
  An array of unknown bound initialized with a brace-enclosed
546
+ *initializer-list* containing `n` *initializer-clause*s is defined as
547
+ having `n` elements [[dcl.array]].
 
548
 
549
+ [*Example 4*:
550
 
551
  ``` cpp
552
  int x[] = { 1, 3, 5 };
553
  ```
554
 
555
  declares and initializes `x` as a one-dimensional array that has three
556
  elements since no size was specified and there are three initializers.
557
 
558
  — *end example*]
559
 
560
+ An array of unknown bound shall not be initialized with an empty
561
+ *braced-init-list* `{}`. [^6]
562
 
563
+ [*Note 4*:
564
 
565
  A default member initializer does not determine the bound for a member
566
  array of unknown bound. Since the default member initializer is ignored
567
+ if a suitable *mem-initializer* is present [[class.base.init]], the
568
  default member initializer is not considered to initialize the array of
569
  unknown bound.
570
 
571
+ [*Example 5*:
572
 
573
  ``` cpp
574
  struct S {
575
  int y[] = { 0 }; // error: non-static data member of incomplete type
576
  };
 
578
 
579
  — *end example*]
580
 
581
  — *end note*]
582
 
583
+ [*Note 5*:
584
 
585
+ Static data members, non-static data members of anonymous union members,
586
+ and unnamed bit-fields are not considered elements of the aggregate.
587
 
588
+ [*Example 6*:
589
 
590
  ``` cpp
591
  struct A {
592
  int i;
593
  static int s;
 
604
  — *end example*]
605
 
606
  — *end note*]
607
 
608
  An *initializer-list* is ill-formed if the number of
609
+ *initializer-clause*s exceeds the number of elements of the aggregate.
610
 
611
+ [*Example 7*:
612
 
613
  ``` cpp
614
  char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
615
  ```
616
 
617
  is ill-formed.
618
 
619
  — *end example*]
620
 
621
+ If a member has a default member initializer and a potentially-evaluated
622
+ subexpression thereof is an aggregate initialization that would use that
623
+ default member initializer, the program is ill-formed.
624
 
625
+ [*Example 8*:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
626
 
627
  ``` cpp
628
  struct A;
629
  extern A a;
630
  struct A {
631
  const A& a1 { A{a,a} }; // OK
632
  const A& a2 { A{} }; // error
633
  };
634
  A a{a,a}; // OK
635
+
636
+ struct B {
637
+ int n = B{}.n; // error
638
+ };
639
  ```
640
 
641
  — *end example*]
642
 
643
  If an aggregate class `C` contains a subaggregate element `e` with no
644
  elements, the *initializer-clause* for `e` shall not be omitted from an
645
  *initializer-list* for an object of type `C` unless the
646
  *initializer-clause*s for all elements of `C` following `e` are also
647
  omitted.
648
 
649
+ [*Example 9*:
650
 
651
  ``` cpp
652
  struct S { } s;
653
  struct A {
654
  S s1;
 
667
 
668
  — *end example*]
669
 
670
  When initializing a multi-dimensional array, the *initializer-clause*s
671
  initialize the elements with the last (rightmost) index of the array
672
+ varying the fastest [[dcl.array]].
673
 
674
+ [*Example 10*:
675
 
676
  ``` cpp
677
  int x[2][2] = { 3, 1, 4, 2 };
678
  ```
679
 
 
700
  *initializer-clause*s from the list are taken to initialize the elements
701
  of the subaggregate; any remaining *initializer-clause*s are left to
702
  initialize the next element of the aggregate of which the current
703
  subaggregate is an element.
704
 
705
+ [*Example 11*:
706
 
707
  ``` cpp
708
  float y[4][3] = {
709
  { 1, 3, 5 },
710
  { 2, 4, 6 },
 
732
  does not, therefore three elements from the list are used. Likewise the
733
  next three are taken successively for `y[1]` and `y[2]`.
734
 
735
  — *end example*]
736
 
737
+ All implicit type conversions [[conv]] are considered when initializing
738
+ the element with an *assignment-expression*. If the
739
  *assignment-expression* can initialize an element, the element is
740
  initialized. Otherwise, if the element is itself a subaggregate, brace
741
  elision is assumed and the *assignment-expression* is considered for the
742
  initialization of the first element of the subaggregate.
743
 
744
+ [*Note 6*: As specified above, brace elision cannot apply to
745
  subaggregates with no elements; an *initializer-clause* for the entire
746
  subobject is required. — *end note*]
747
 
748
+ [*Example 12*:
749
 
750
  ``` cpp
751
  struct A {
752
  int i;
753
  operator int();
 
764
  is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
765
  initialized with whatever `a.operator int()` returns.
766
 
767
  — *end example*]
768
 
769
+ [*Note 7*: An aggregate array or an aggregate class may contain
770
+ elements of a class type with a user-declared constructor
771
+ [[class.ctor]]. Initialization of these aggregate objects is described
772
  in  [[class.expl.init]]. — *end note*]
773
 
774
+ [*Note 8*: Whether the initialization of aggregates with static storage
775
  duration is static or dynamic is specified in  [[basic.start.static]],
776
  [[basic.start.dynamic]], and  [[stmt.dcl]]. — *end note*]
777
 
778
+ When a union is initialized with an initializer list, there shall not be
779
+ more than one explicitly initialized element.
 
780
 
781
+ [*Example 13*:
782
 
783
  ``` cpp
784
  union u { int a; const char* b; };
785
  u a = { 1 };
786
  u b = a;
787
  u c = 1; // error
788
  u d = { 0, "asdf" }; // error
789
  u e = { "asdf" }; // error
790
+ u f = { .b = "asdf" };
791
+ u g = { .a = 1, .b = "asdf" }; // error
792
  ```
793
 
794
  — *end example*]
795
 
796
+ [*Note 9*: As described above, the braces around the
797
  *initializer-clause* for a union member can be omitted if the union is a
798
  member of another aggregate. — *end note*]
799
 
800
  ### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
801
 
802
+ An array of ordinary character type [[basic.fundamental]], `char8_t`
803
+ array, `char16_t` array, `char32_t` array, or `wchar_t` array can be
804
+ initialized by an ordinary string literal, UTF-8 string literal, UTF-16
805
+ string literal, UTF-32 string literal, or wide string literal,
806
+ respectively, or by an appropriately-typed *string-literal* enclosed in
807
+ braces [[lex.string]]. Successive characters of the value of the
808
+ *string-literal* initialize the elements of the array.
809
 
810
  [*Example 1*:
811
 
812
  ``` cpp
813
  char msg[] = "Syntax error on line %s\n";
 
830
  is ill-formed since there is no space for the implied trailing `'\0'`.
831
 
832
  — *end example*]
833
 
834
  If there are fewer initializers than there are array elements, each
835
+ element not explicitly initialized shall be zero-initialized
836
+ [[dcl.init]].
837
 
838
  ### References <a id="dcl.init.ref">[[dcl.init.ref]]</a>
839
 
840
+ A variable whose declared type is “reference to type `T`” [[dcl.ref]]
841
  shall be initialized.
842
 
843
  [*Example 1*:
844
 
845
  ``` cpp
 
862
 
863
  A reference cannot be changed to refer to another object after
864
  initialization.
865
 
866
  [*Note 1*: Assignment to a reference assigns to the object referred to
867
+ by the reference [[expr.ass]]. — *end note*]
868
 
869
+ Argument passing [[expr.call]] and function value return [[stmt.return]]
870
+ are initializations.
871
 
872
  The initializer can be omitted for a reference only in a parameter
873
+ declaration [[dcl.fct]], in the declaration of a function return type,
874
+ in the declaration of a class member within its class definition
875
+ [[class.mem]], and where the `extern` specifier is explicitly used.
876
 
877
  [*Example 2*:
878
 
879
  ``` cpp
880
  int& r1; // error: initializer missing
 
882
  ```
883
 
884
  — *end example*]
885
 
886
  Given types “*cv1* `T1`” and “*cv2* `T2`”, “*cv1* `T1`” is
887
+ *reference-related* to “*cv2* `T2`” if `T1` is similar [[conv.qual]] to
888
+ `T2`, or `T1` is a base class of `T2`. “*cv1* `T1`” is
889
+ *reference-compatible* with “*cv2* `T2`” if a prvalue of type “pointer
890
+ to *cv2* `T2`” can be converted to the type “pointer to *cv1* `T1`” via
891
+ a standard conversion sequence [[conv]]. In all cases where the
 
 
 
 
 
892
  reference-compatible relationship of two types is used to establish the
893
+ validity of a reference binding and the standard conversion sequence
894
+ would be ill-formed, a program that necessitates such a binding is
895
+ ill-formed.
 
896
 
897
  A reference to type “*cv1* `T1`” is initialized by an expression of type
898
  “*cv2* `T2`” as follows:
899
 
900
  - If the reference is an lvalue reference and the initializer expression
901
  - is an lvalue (but is not a bit-field), and “*cv1* `T1`” is
902
  reference-compatible with “*cv2* `T2`”, or
903
  - has a class type (i.e., `T2` is a class type), where `T1` is not
904
  reference-related to `T2`, and can be converted to an lvalue of type
905
  “*cv3* `T3`”, where “*cv1* `T1`” is reference-compatible with “*cv3*
906
+ `T3`”[^7] (this conversion is selected by enumerating the applicable
907
+ conversion functions [[over.match.ref]] and choosing the best one
908
+ through overload resolution [[over.match]]),
909
 
910
  then the reference is bound to the initializer expression lvalue in
911
  the first case and to the lvalue result of the conversion in the
912
  second case (or, in either case, to the appropriate base class
913
  subobject of the object).
914
+ \[*Note 2*: The usual lvalue-to-rvalue [[conv.lval]], array-to-pointer
915
+ [[conv.array]], and function-to-pointer [[conv.func]] standard
916
+ conversions are not needed, and therefore are suppressed, when such
917
+ direct bindings to lvalues are done. — *end note*]
 
918
  \[*Example 3*:
919
  ``` cpp
920
  double d = 2.0;
921
  double& rd = d; // rd refers to d
922
  const double& rcd = d; // rcd refers to d
 
927
  const A& rca = b; // rca refers to A subobject in b
928
  int& ir = B(); // ir refers to the result of B::operator int&
929
  ```
930
 
931
  — *end example*]
932
+ - Otherwise, if the reference is an lvalue reference to a type that is
933
+ not const-qualified or is volatile-qualified, the program is
934
+ ill-formed.
935
  \[*Example 4*:
936
  ``` cpp
937
  double& rd2 = 2.0; // error: not an lvalue and reference not const
938
  int i = 2;
939
  double& rd3 = i; // error: type mismatch and reference not const
940
  ```
941
 
942
  — *end example*]
943
+ - Otherwise, if the initializer expression
944
  - is an rvalue (but not a bit-field) or function lvalue and “*cv1*
945
  `T1`” is reference-compatible with “*cv2* `T2`”, or
946
  - has a class type (i.e., `T2` is a class type), where `T1` is not
947
  reference-related to `T2`, and can be converted to an rvalue or
948
  function lvalue of type “*cv3* `T3`”, where “*cv1* `T1`” is
949
  reference-compatible with “*cv3* `T3`” (see  [[over.match.ref]]),
950
 
951
+ then the value of the initializer expression in the first case and the
952
+ result of the conversion in the second case is called the converted
953
+ initializer. If the converted initializer is a prvalue, its type `T4`
954
+ is adjusted to type “*cv1* `T4`” [[conv.qual]] and the temporary
955
+ materialization conversion [[conv.rval]] is applied. In any case, the
956
+ reference is bound to the resulting glvalue (or to an appropriate base
957
+ class subobject).
958
  \[*Example 5*:
959
  ``` cpp
960
  struct A { };
961
  struct B : A { } b;
962
  extern B f();
 
972
  B&& rrb = x; // bound directly to the result of operator B
973
  ```
974
 
975
  — *end example*]
976
  - Otherwise:
977
+ - If `T1` or `T2` is a class type and `T1` is not reference-related to
978
+ `T2`, user-defined conversions are considered using the rules for
979
+ copy-initialization of an object of type “*cv1* `T1`” by
980
  user-defined conversion ([[dcl.init]], [[over.match.copy]],
981
+ [[over.match.conv]]); the program is ill-formed if the corresponding
982
+ non-reference copy-initialization would be ill-formed. The result of
983
+ the call to the conversion function, as described for the
984
+ non-reference copy-initialization, is then used to direct-initialize
985
+ the reference. For this direct-initialization, user-defined
986
+ conversions are not considered.
 
987
  - Otherwise, the initializer expression is implicitly converted to a
988
  prvalue of type “*cv1* `T1`”. The temporary materialization
989
  conversion is applied and the reference is bound to the result.
990
 
991
  If `T1` is reference-related to `T2`:
992
  - *cv1* shall be the same cv-qualification as, or greater
993
  cv-qualification than, *cv2*; and
994
+ - if the reference is an rvalue reference, the initializer expression
995
+ shall not be an lvalue.
996
 
997
  \[*Example 6*:
998
  ``` cpp
999
  struct Banana { };
1000
  struct Enigma { operator const Banana(); };
1001
  struct Alaska { operator Banana&(); };
1002
  void enigmatic() {
1003
  typedef const Banana ConstBanana;
1004
+ Banana &&banana1 = ConstBanana(); // error
1005
+ Banana &&banana2 = Enigma(); // error
1006
+ Banana &&banana3 = Alaska(); // error
1007
  }
1008
 
1009
  const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
1010
  double&& rrd = 2; // rrd refers to temporary with value 2.0
1011
  const volatile int cvi = 1;
 
1022
  ```
1023
 
1024
  — *end example*]
1025
 
1026
  In all cases except the last (i.e., implicitly converting the
1027
+ initializer expression to the referenced type), the reference is said to
1028
+ *bind directly* to the initializer expression.
1029
 
1030
  [*Note 3*: [[class.temporary]] describes the lifetime of temporaries
1031
  bound to references. — *end note*]
1032
 
1033
  ### List-initialization <a id="dcl.init.list">[[dcl.init.list]]</a>
1034
 
1035
  *List-initialization* is initialization of an object or reference from a
1036
  *braced-init-list*. Such an initializer is called an *initializer list*,
1037
+ and the comma-separated *initializer-clause*s of the *initializer-list*
1038
+ or *designated-initializer-clause*s of the *designated-initializer-list*
1039
+ are called the *elements* of the initializer list. An initializer list
1040
+ may be empty. List-initialization can occur in direct-initialization or
1041
  copy-initialization contexts; list-initialization in a
1042
  direct-initialization context is called *direct-list-initialization* and
1043
  list-initialization in a copy-initialization context is called
1044
  *copy-list-initialization*.
1045
 
1046
  [*Note 1*:
1047
 
1048
  List-initialization can be used
1049
 
1050
+ - as the initializer in a variable definition [[dcl.init]]
1051
+ - as the initializer in a *new-expression* [[expr.new]]
1052
+ - in a `return` statement [[stmt.return]]
1053
+ - as a *for-range-initializer* [[stmt.iter]]
1054
+ - as a function argument [[expr.call]]
1055
+ - as a subscript [[expr.sub]]
1056
+ - as an argument to a constructor invocation ([[dcl.init]],
1057
  [[expr.type.conv]])
1058
+ - as an initializer for a non-static data member [[class.mem]]
1059
+ - in a *mem-initializer* [[class.base.init]]
1060
+ - on the right-hand side of an assignment [[expr.ass]]
1061
 
1062
  [*Example 1*:
1063
 
1064
  ``` cpp
1065
  int a = {1};
 
1075
  — *end example*]
1076
 
1077
  — *end note*]
1078
 
1079
  A constructor is an *initializer-list constructor* if its first
1080
+ parameter is of type `std::initializer_list<E>` or reference to
1081
+ cv `std::initializer_list<E>` for some type `E`, and either there are no
1082
+ other parameters or else all other parameters have default arguments
1083
+ [[dcl.fct.default]].
1084
 
1085
  [*Note 2*: Initializer-list constructors are favored over other
1086
+ constructors in list-initialization [[over.match.list]]. Passing an
1087
  initializer list as the argument to the constructor template
1088
  `template<class T> C(T)` of a class `C` does not create an
1089
  initializer-list constructor, because an initializer list argument
1090
+ causes the corresponding parameter to be a non-deduced context
1091
+ [[temp.deduct.call]]. — *end note*]
1092
 
1093
  The template `std::initializer_list` is not predefined; if the header
1094
+ `<initializer_list>` is not imported or included prior to a use of
1095
  `std::initializer_list` — even an implicit use in which the type is not
1096
+ named [[dcl.spec.auto]] — the program is ill-formed.
1097
 
1098
  List-initialization of an object or reference of type `T` is defined as
1099
  follows:
1100
 
1101
+ - If the *braced-init-list* contains a *designated-initializer-list*,
1102
+ `T` shall be an aggregate class. The ordered *identifier*s in the
1103
+ *designator*s of the *designated-initializer-list* shall form a
1104
+ subsequence of the ordered *identifier*s in the direct non-static data
1105
+ members of `T`. Aggregate initialization is performed
1106
+ [[dcl.init.aggr]].
1107
+ \[*Example 2*:
1108
+ ``` cpp
1109
+ struct A { int x; int y; int z; };
1110
+ A a{.y = 2, .x = 1}; // error: designator order does not match declaration order
1111
+ A b{.x = 1, .z = 2}; // OK, b.y initialized to 0
1112
+ ```
1113
+
1114
+ — *end example*]
1115
  - If `T` is an aggregate class and the initializer list has a single
1116
  element of type *cv* `U`, where `U` is `T` or a class derived from
1117
  `T`, the object is initialized from that element (by
1118
  copy-initialization for copy-list-initialization, or by
1119
  direct-initialization for direct-list-initialization).
1120
  - Otherwise, if `T` is a character array and the initializer list has a
1121
+ single element that is an appropriately-typed *string-literal*
1122
+ [[dcl.init.string]], initialization is performed as described in that
1123
+ subclause.
1124
  - Otherwise, if `T` is an aggregate, aggregate initialization is
1125
+ performed [[dcl.init.aggr]].
1126
+ \[*Example 3*:
1127
  ``` cpp
1128
  double ad[] = { 1, 2.0 }; // OK
1129
  int ai[] = { 1, 2.0 }; // error: narrowing
1130
 
1131
  struct S2 {
 
1142
  type with a default constructor, the object is value-initialized.
1143
  - Otherwise, if `T` is a specialization of `std::initializer_list<E>`,
1144
  the object is constructed as described below.
1145
  - Otherwise, if `T` is a class type, constructors are considered. The
1146
  applicable constructors are enumerated and the best one is chosen
1147
+ through overload resolution ([[over.match]], [[over.match.list]]). If
1148
+ a narrowing conversion (see below) is required to convert any of the
1149
+ arguments, the program is ill-formed.
1150
+ \[*Example 4*:
1151
  ``` cpp
1152
  struct S {
1153
  S(std::initializer_list<double>); // #1
1154
  S(std::initializer_list<int>); // #2
1155
  S(); // #3
 
1159
  S s2 = { 1, 2, 3 }; // invoke #2
1160
  S s3 = { }; // invoke #3
1161
  ```
1162
 
1163
  — *end example*]
1164
+ \[*Example 5*:
1165
  ``` cpp
1166
  struct Map {
1167
  Map(std::initializer_list<std::pair<std::string,int>>);
1168
  };
1169
  Map ship = {{"Sophie",14}, {"Surprise",28}};
1170
  ```
1171
 
1172
  — *end example*]
1173
+ \[*Example 6*:
1174
  ``` cpp
1175
  struct S {
1176
  // no initializer-list constructors
1177
  S(int, double, double); // #1
1178
  S(); // #2
 
1182
  S s2 { 1.0, 2, 3 }; // error: narrowing
1183
  S s3 { }; // OK: invoke #2
1184
  ```
1185
 
1186
  — *end example*]
1187
+ - Otherwise, if `T` is an enumeration with a fixed underlying type
1188
+ [[dcl.enum]] `U`, the *initializer-list* has a single element `v`, `v`
1189
+ can be implicitly converted to `U`, and the initialization is
1190
+ direct-list-initialization, the object is initialized with the value
1191
+ `T(v)` [[expr.type.conv]]; if a narrowing conversion is required to
1192
+ convert `v` to `U`, the program is ill-formed.
1193
+ \[*Example 7*:
1194
  ``` cpp
1195
  enum byte : unsigned char { };
1196
  byte b { 42 }; // OK
1197
  byte c = { 42 }; // error
1198
  byte d = byte{ 42 }; // OK; same value as b
 
1215
  reference-related to `E`, the object or reference is initialized from
1216
  that element (by copy-initialization for copy-list-initialization, or
1217
  by direct-initialization for direct-list-initialization); if a
1218
  narrowing conversion (see below) is required to convert the element to
1219
  `T`, the program is ill-formed.
1220
+ \[*Example 8*:
1221
  ``` cpp
1222
  int x1 {2}; // OK
1223
  int x2 {2.0}; // error: narrowing
1224
  ```
1225
 
1226
  — *end example*]
1227
+ - Otherwise, if `T` is a reference type, a prvalue is generated. The
1228
+ prvalue initializes its result object by copy-list-initialization. The
1229
+ prvalue is then used to direct-initialize the reference. The type of
1230
+ the temporary is the type referenced by `T`, unless `T` is “reference
1231
+ to array of unknown bound of `U`”, in which case the type of the
1232
+ temporary is the type of `x` in the declaration `U x[] H`, where H is
1233
+ the initializer list.
1234
  \[*Note 3*: As usual, the binding will fail and the program is
1235
  ill-formed if the reference type is an lvalue reference to a non-const
1236
  type. — *end note*]
1237
+ \[*Example 9*:
1238
  ``` cpp
1239
  struct S {
1240
  S(std::initializer_list<double>); // #1
1241
  S(const std::string&); // #2
1242
  // ...
 
1245
  const S& r2 { "Spinach" }; // OK: invoke #2
1246
  S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
1247
  const int& i1 = { 1 }; // OK
1248
  const int& i2 = { 1.1 }; // error: narrowing
1249
  const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
1250
+
1251
+ struct A { } a;
1252
+ struct B { explicit B(const A&); };
1253
+ const B& b2{a}; // error: cannot copy-list-initialize B temporary from A
1254
  ```
1255
 
1256
  — *end example*]
1257
  - Otherwise, if the initializer list has no elements, the object is
1258
  value-initialized.
 
 
 
 
 
 
 
1259
  \[*Example 10*:
1260
  ``` cpp
1261
+ int** pp {}; // initialized to null pointer
1262
+ ```
1263
+
1264
+ — *end example*]
1265
+ - Otherwise, the program is ill-formed.
1266
+ \[*Example 11*:
1267
+ ``` cpp
1268
  struct A { int i; int j; };
1269
  A a1 { 1, 2 }; // aggregate initialization
1270
  A a2 { 1.2 }; // error: narrowing
1271
  struct B {
1272
  B(std::initializer_list<int>);
 
1284
  ```
1285
 
1286
  — *end example*]
1287
 
1288
  Within the *initializer-list* of a *braced-init-list*, the
1289
+ *initializer-clause*s, including any that result from pack expansions
1290
+ [[temp.variadic]], are evaluated in the order in which they appear. That
1291
+ is, every value computation and side effect associated with a given
1292
  *initializer-clause* is sequenced before every value computation and
1293
  side effect associated with any *initializer-clause* that follows it in
1294
  the comma-separated list of the *initializer-list*.
1295
 
1296
  [*Note 4*: This evaluation ordering holds regardless of the semantics
 
1298
  *initializer-list* are interpreted as arguments of a constructor call,
1299
  even though ordinarily there are no sequencing constraints on the
1300
  arguments of a call. — *end note*]
1301
 
1302
  An object of type `std::initializer_list<E>` is constructed from an
1303
+ initializer list as if the implementation generated and materialized
1304
+ [[conv.rval]] a prvalue of type “array of N `const E`”, where N is the
1305
  number of elements in the initializer list. Each element of that array
1306
  is copy-initialized with the corresponding element of the initializer
1307
  list, and the `std::initializer_list<E>` object is constructed to refer
1308
  to that array.
1309
 
1310
  [*Note 5*: A constructor or conversion function selected for the copy
1311
+ is required to be accessible [[class.access]] in the context of the
1312
  initializer list. — *end note*]
1313
 
1314
  If a narrowing conversion is required to initialize any of the elements,
1315
  the program is ill-formed.
1316
 
1317
+ [*Example 12*:
1318
 
1319
  ``` cpp
1320
  struct X {
1321
  X(std::initializer_list<double> v);
1322
  };
 
1334
  assuming that the implementation can construct an `initializer_list`
1335
  object with a pair of pointers.
1336
 
1337
  — *end example*]
1338
 
1339
+ The array has the same lifetime as any other temporary object
1340
+ [[class.temporary]], except that initializing an `initializer_list`
1341
  object from the array extends the lifetime of the array exactly like
1342
  binding a reference to a temporary.
1343
 
1344
+ [*Example 13*:
1345
 
1346
  ``` cpp
1347
  typedef std::complex<double> cmplx;
1348
  std::vector<cmplx> v1 = { 1, 2, 3 };
1349
 
 
1362
  function call, so the array created for `{ 1, 2, 3 }` has
1363
  full-expression lifetime. For `i3`, the `initializer_list` object is a
1364
  variable, so the array persists for the lifetime of the variable. For
1365
  `i4`, the `initializer_list` object is initialized in the constructor’s
1366
  *ctor-initializer* as if by binding a temporary array to a reference
1367
+ member, so the program is ill-formed [[class.base.init]].
1368
 
1369
  — *end example*]
1370
 
1371
  [*Note 6*: The implementation is free to allocate the array in
1372
  read-only memory if an explicit array with the same initializer could be
 
1384
  value after conversion will fit into the target type and will produce
1385
  the original value when converted back to the original type, or
1386
  - from an integer type or unscoped enumeration type to an integer type
1387
  that cannot represent all the values of the original type, except
1388
  where the source is a constant expression whose value after integral
1389
+ promotions will fit into the target type, or
1390
+ - from a pointer type or a pointer-to-member type to `bool`.
1391
 
1392
  [*Note 7*: As indicated above, such conversions are not allowed at the
1393
  top level in list-initializations. — *end note*]
1394
 
1395
+ [*Example 14*:
1396
 
1397
  ``` cpp
1398
  int x = 999; // x is not a constant expression
1399
  const int y = 999;
1400
  const int z = 99;
 
1408
  signed int si1 =
1409
  { (unsigned int)-1 }; // error: narrows
1410
  int ii = {2.0}; // error: narrows
1411
  float f1 { x }; // error: might narrow
1412
  float f2 { 7 }; // OK: 7 can be exactly represented as a float
1413
+ bool b = {"meow"}; // error: narrows
1414
  int f(int);
1415
+ int a[] = { 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level
 
1416
  ```
1417
 
1418
  — *end example*]
1419