From Jason Turner

[dcl.init.list]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp14wq0vx5/{from.md → to.md} +86 -310
tmp/tmp14wq0vx5/{from.md → to.md} RENAMED
@@ -1,32 +1,33 @@
1
  ### List-initialization <a id="dcl.init.list">[[dcl.init.list]]</a>
2
 
3
  *List-initialization* is initialization of an object or reference from a
4
  *braced-init-list*. Such an initializer is called an *initializer list*,
5
- and the comma-separated *initializer-clause*s of the list are called the
6
- *elements* of the initializer list. An initializer list may be empty.
7
- List-initialization can occur in direct-initialization or
 
8
  copy-initialization contexts; list-initialization in a
9
  direct-initialization context is called *direct-list-initialization* and
10
  list-initialization in a copy-initialization context is called
11
  *copy-list-initialization*.
12
 
13
  [*Note 1*:
14
 
15
  List-initialization can be used
16
 
17
- - as the initializer in a variable definition ([[dcl.init]])
18
- - as the initializer in a *new-expression* ([[expr.new]])
19
- - in a return statement ([[stmt.return]])
20
- - as a *for-range-initializer* ([[stmt.iter]])
21
- - as a function argument ([[expr.call]])
22
- - as a subscript ([[expr.sub]])
23
- - as an argument to a constructor invocation ([[dcl.init]], 
24
  [[expr.type.conv]])
25
- - as an initializer for a non-static data member ([[class.mem]])
26
- - in a *mem-initializer* ([[class.base.init]])
27
- - on the right-hand side of an assignment ([[expr.ass]])
28
 
29
  [*Example 1*:
30
 
31
  ``` cpp
32
  int a = {1};
@@ -42,43 +43,57 @@ std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
42
  — *end example*]
43
 
44
  — *end note*]
45
 
46
  A constructor is an *initializer-list constructor* if its first
47
- parameter is of type `std::initializer_list<E>` or reference to possibly
48
- cv-qualified `std::initializer_list<E>` for some type `E`, and either
49
- there are no other parameters or else all other parameters have default
50
- arguments ([[dcl.fct.default]]).
51
 
52
  [*Note 2*: Initializer-list constructors are favored over other
53
- constructors in list-initialization ([[over.match.list]]). Passing an
54
  initializer list as the argument to the constructor template
55
  `template<class T> C(T)` of a class `C` does not create an
56
  initializer-list constructor, because an initializer list argument
57
- causes the corresponding parameter to be a non-deduced context (
58
- [[temp.deduct.call]]). — *end note*]
59
 
60
  The template `std::initializer_list` is not predefined; if the header
61
- `<initializer_list>` is not included prior to a use of
62
  `std::initializer_list` — even an implicit use in which the type is not
63
- named ([[dcl.spec.auto]]) — the program is ill-formed.
64
 
65
  List-initialization of an object or reference of type `T` is defined as
66
  follows:
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  - If `T` is an aggregate class and the initializer list has a single
69
  element of type *cv* `U`, where `U` is `T` or a class derived from
70
  `T`, the object is initialized from that element (by
71
  copy-initialization for copy-list-initialization, or by
72
  direct-initialization for direct-list-initialization).
73
  - Otherwise, if `T` is a character array and the initializer list has a
74
- single element that is an appropriately-typed string literal (
75
- [[dcl.init.string]]), initialization is performed as described in that
76
- section.
77
  - Otherwise, if `T` is an aggregate, aggregate initialization is
78
- performed ([[dcl.init.aggr]]).
79
- \[*Example 2*:
80
  ``` cpp
81
  double ad[] = { 1, 2.0 }; // OK
82
  int ai[] = { 1, 2.0 }; // error: narrowing
83
 
84
  struct S2 {
@@ -95,14 +110,14 @@ follows:
95
  type with a default constructor, the object is value-initialized.
96
  - Otherwise, if `T` is a specialization of `std::initializer_list<E>`,
97
  the object is constructed as described below.
98
  - Otherwise, if `T` is a class type, constructors are considered. The
99
  applicable constructors are enumerated and the best one is chosen
100
- through overload resolution ([[over.match]],  [[over.match.list]]).
101
- If a narrowing conversion (see below) is required to convert any of
102
- the arguments, the program is ill-formed.
103
- \[*Example 3*:
104
  ``` cpp
105
  struct S {
106
  S(std::initializer_list<double>); // #1
107
  S(std::initializer_list<int>); // #2
108
  S(); // #3
@@ -112,20 +127,20 @@ follows:
112
  S s2 = { 1, 2, 3 }; // invoke #2
113
  S s3 = { }; // invoke #3
114
  ```
115
 
116
  — *end example*]
117
- \[*Example 4*:
118
  ``` cpp
119
  struct Map {
120
  Map(std::initializer_list<std::pair<std::string,int>>);
121
  };
122
  Map ship = {{"Sophie",14}, {"Surprise",28}};
123
  ```
124
 
125
  — *end example*]
126
- \[*Example 5*:
127
  ``` cpp
128
  struct S {
129
  // no initializer-list constructors
130
  S(int, double, double); // #1
131
  S(); // #2
@@ -135,17 +150,17 @@ follows:
135
  S s2 { 1.0, 2, 3 }; // error: narrowing
136
  S s3 { }; // OK: invoke #2
137
  ```
138
 
139
  — *end example*]
140
- - Otherwise, if `T` is an enumeration with a fixed underlying type (
141
- [[dcl.enum]]), the *initializer-list* has a single element `v`, and
142
- the initialization is direct-list-initialization, the object is
143
- initialized with the value `T(v)` ([[expr.type.conv]]); if a
144
- narrowing conversion is required to convert `v` to the underlying type
145
- of `T`, the program is ill-formed.
146
- \[*Example 6*:
147
  ``` cpp
148
  enum byte : unsigned char { };
149
  byte b { 42 }; // OK
150
  byte c = { 42 }; // error
151
  byte d = byte{ 42 }; // OK; same value as b
@@ -168,26 +183,28 @@ follows:
168
  reference-related to `E`, the object or reference is initialized from
169
  that element (by copy-initialization for copy-list-initialization, or
170
  by direct-initialization for direct-list-initialization); if a
171
  narrowing conversion (see below) is required to convert the element to
172
  `T`, the program is ill-formed.
173
- \[*Example 7*:
174
  ``` cpp
175
  int x1 {2}; // OK
176
  int x2 {2.0}; // error: narrowing
177
  ```
178
 
179
  — *end example*]
180
- - Otherwise, if `T` is a reference type, a prvalue of the type
181
- referenced by `T` is generated. The prvalue initializes its result
182
- object by copy-list-initialization or direct-list-initialization,
183
- depending on the kind of initialization for the reference. The prvalue
184
- is then used to direct-initialize the reference.
 
 
185
  \[*Note 3*: As usual, the binding will fail and the program is
186
  ill-formed if the reference type is an lvalue reference to a non-const
187
  type. — *end note*]
188
- \[*Example 8*:
189
  ``` cpp
190
  struct S {
191
  S(std::initializer_list<double>); // #1
192
  S(const std::string&); // #2
193
  // ...
@@ -196,23 +213,27 @@ follows:
196
  const S& r2 { "Spinach" }; // OK: invoke #2
197
  S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
198
  const int& i1 = { 1 }; // OK
199
  const int& i2 = { 1.1 }; // error: narrowing
200
  const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
 
 
 
 
201
  ```
202
 
203
  — *end example*]
204
  - Otherwise, if the initializer list has no elements, the object is
205
  value-initialized.
206
- \[*Example 9*:
207
  ``` cpp
208
  int** pp {}; // initialized to null pointer
209
  ```
210
 
211
  — *end example*]
212
  - Otherwise, the program is ill-formed.
213
- \[*Example 10*:
214
  ``` cpp
215
  struct A { int i; int j; };
216
  A a1 { 1, 2 }; // aggregate initialization
217
  A a2 { 1.2 }; // error: narrowing
218
  struct B {
@@ -231,13 +252,13 @@ follows:
231
  ```
232
 
233
  — *end example*]
234
 
235
  Within the *initializer-list* of a *braced-init-list*, the
236
- *initializer-clause*s, including any that result from pack expansions (
237
- [[temp.variadic]]), are evaluated in the order in which they appear.
238
- That is, every value computation and side effect associated with a given
239
  *initializer-clause* is sequenced before every value computation and
240
  side effect associated with any *initializer-clause* that follows it in
241
  the comma-separated list of the *initializer-list*.
242
 
243
  [*Note 4*: This evaluation ordering holds regardless of the semantics
@@ -245,25 +266,25 @@ of the initialization; for example, it applies when the elements of the
245
  *initializer-list* are interpreted as arguments of a constructor call,
246
  even though ordinarily there are no sequencing constraints on the
247
  arguments of a call. — *end note*]
248
 
249
  An object of type `std::initializer_list<E>` is constructed from an
250
- initializer list as if the implementation generated and materialized (
251
- [[conv.rval]]) a prvalue of type “array of N `const E`”, where N is the
252
  number of elements in the initializer list. Each element of that array
253
  is copy-initialized with the corresponding element of the initializer
254
  list, and the `std::initializer_list<E>` object is constructed to refer
255
  to that array.
256
 
257
  [*Note 5*: A constructor or conversion function selected for the copy
258
- shall be accessible (Clause  [[class.access]]) in the context of the
259
  initializer list. — *end note*]
260
 
261
  If a narrowing conversion is required to initialize any of the elements,
262
  the program is ill-formed.
263
 
264
- [*Example 11*:
265
 
266
  ``` cpp
267
  struct X {
268
  X(std::initializer_list<double> v);
269
  };
@@ -281,16 +302,16 @@ X x(std::initializer_list<double>(__a, __a+3));
281
  assuming that the implementation can construct an `initializer_list`
282
  object with a pair of pointers.
283
 
284
  — *end example*]
285
 
286
- The array has the same lifetime as any other temporary object (
287
- [[class.temporary]]), except that initializing an `initializer_list`
288
  object from the array extends the lifetime of the array exactly like
289
  binding a reference to a temporary.
290
 
291
- [*Example 12*:
292
 
293
  ``` cpp
294
  typedef std::complex<double> cmplx;
295
  std::vector<cmplx> v1 = { 1, 2, 3 };
296
 
@@ -309,11 +330,11 @@ For `v1` and `v2`, the `initializer_list` object is a parameter in a
309
  function call, so the array created for `{ 1, 2, 3 }` has
310
  full-expression lifetime. For `i3`, the `initializer_list` object is a
311
  variable, so the array persists for the lifetime of the variable. For
312
  `i4`, the `initializer_list` object is initialized in the constructor’s
313
  *ctor-initializer* as if by binding a temporary array to a reference
314
- member, so the program is ill-formed ([[class.base.init]]).
315
 
316
  — *end example*]
317
 
318
  [*Note 6*: The implementation is free to allocate the array in
319
  read-only memory if an explicit array with the same initializer could be
@@ -331,16 +352,17 @@ A *narrowing conversion* is an implicit conversion
331
  value after conversion will fit into the target type and will produce
332
  the original value when converted back to the original type, or
333
  - from an integer type or unscoped enumeration type to an integer type
334
  that cannot represent all the values of the original type, except
335
  where the source is a constant expression whose value after integral
336
- promotions will fit into the target type.
 
337
 
338
  [*Note 7*: As indicated above, such conversions are not allowed at the
339
  top level in list-initializations. — *end note*]
340
 
341
- [*Example 13*:
342
 
343
  ``` cpp
344
  int x = 999; // x is not a constant expression
345
  const int y = 999;
346
  const int z = 99;
@@ -354,258 +376,12 @@ unsigned int ui1 = {-1}; // error: narrows
354
  signed int si1 =
355
  { (unsigned int)-1 }; // error: narrows
356
  int ii = {2.0}; // error: narrows
357
  float f1 { x }; // error: might narrow
358
  float f2 { 7 }; // OK: 7 can be exactly represented as a float
 
359
  int f(int);
360
- int a[] =
361
- { 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level
362
  ```
363
 
364
  — *end example*]
365
 
366
- <!-- Link reference definitions -->
367
- [basic.align]: basic.md#basic.align
368
- [basic.compound]: basic.md#basic.compound
369
- [basic.def]: basic.md#basic.def
370
- [basic.def.odr]: basic.md#basic.def.odr
371
- [basic.fundamental]: basic.md#basic.fundamental
372
- [basic.life]: basic.md#basic.life
373
- [basic.link]: basic.md#basic.link
374
- [basic.lookup]: basic.md#basic.lookup
375
- [basic.lookup.argdep]: basic.md#basic.lookup.argdep
376
- [basic.lookup.classref]: basic.md#basic.lookup.classref
377
- [basic.lookup.elab]: basic.md#basic.lookup.elab
378
- [basic.lookup.qual]: basic.md#basic.lookup.qual
379
- [basic.lookup.udir]: basic.md#basic.lookup.udir
380
- [basic.lookup.unqual]: basic.md#basic.lookup.unqual
381
- [basic.lval]: basic.md#basic.lval
382
- [basic.namespace]: #basic.namespace
383
- [basic.scope]: basic.md#basic.scope
384
- [basic.scope.block]: basic.md#basic.scope.block
385
- [basic.scope.declarative]: basic.md#basic.scope.declarative
386
- [basic.scope.namespace]: basic.md#basic.scope.namespace
387
- [basic.scope.pdecl]: basic.md#basic.scope.pdecl
388
- [basic.scope.proto]: basic.md#basic.scope.proto
389
- [basic.start]: basic.md#basic.start
390
- [basic.start.dynamic]: basic.md#basic.start.dynamic
391
- [basic.start.static]: basic.md#basic.start.static
392
- [basic.stc]: basic.md#basic.stc
393
- [basic.stc.auto]: basic.md#basic.stc.auto
394
- [basic.stc.dynamic]: basic.md#basic.stc.dynamic
395
- [basic.stc.static]: basic.md#basic.stc.static
396
- [basic.stc.thread]: basic.md#basic.stc.thread
397
- [basic.type.qualifier]: basic.md#basic.type.qualifier
398
- [basic.types]: basic.md#basic.types
399
- [class]: class.md#class
400
- [class.access]: class.md#class.access
401
- [class.base.init]: special.md#class.base.init
402
- [class.bit]: class.md#class.bit
403
- [class.conv]: special.md#class.conv
404
- [class.conv.ctor]: special.md#class.conv.ctor
405
- [class.conv.fct]: special.md#class.conv.fct
406
- [class.copy]: special.md#class.copy
407
- [class.ctor]: special.md#class.ctor
408
- [class.dtor]: special.md#class.dtor
409
- [class.expl.init]: special.md#class.expl.init
410
- [class.friend]: class.md#class.friend
411
- [class.inhctor.init]: special.md#class.inhctor.init
412
- [class.init]: special.md#class.init
413
- [class.mem]: class.md#class.mem
414
- [class.member.lookup]: class.md#class.member.lookup
415
- [class.mfct]: class.md#class.mfct
416
- [class.mi]: class.md#class.mi
417
- [class.name]: class.md#class.name
418
- [class.qual]: basic.md#class.qual
419
- [class.static]: class.md#class.static
420
- [class.static.data]: class.md#class.static.data
421
- [class.temporary]: special.md#class.temporary
422
- [class.union]: class.md#class.union
423
- [class.union.anon]: class.md#class.union.anon
424
- [class.virtual]: class.md#class.virtual
425
- [conv]: conv.md#conv
426
- [conv.array]: conv.md#conv.array
427
- [conv.func]: conv.md#conv.func
428
- [conv.integral]: conv.md#conv.integral
429
- [conv.lval]: conv.md#conv.lval
430
- [conv.prom]: conv.md#conv.prom
431
- [conv.ptr]: conv.md#conv.ptr
432
- [conv.qual]: conv.md#conv.qual
433
- [conv.rval]: conv.md#conv.rval
434
- [cstddef.syn]: language.md#cstddef.syn
435
- [dcl.align]: #dcl.align
436
- [dcl.ambig.res]: #dcl.ambig.res
437
- [dcl.array]: #dcl.array
438
- [dcl.asm]: #dcl.asm
439
- [dcl.attr]: #dcl.attr
440
- [dcl.attr.depend]: #dcl.attr.depend
441
- [dcl.attr.deprecated]: #dcl.attr.deprecated
442
- [dcl.attr.fallthrough]: #dcl.attr.fallthrough
443
- [dcl.attr.grammar]: #dcl.attr.grammar
444
- [dcl.attr.nodiscard]: #dcl.attr.nodiscard
445
- [dcl.attr.noreturn]: #dcl.attr.noreturn
446
- [dcl.attr.unused]: #dcl.attr.unused
447
- [dcl.constexpr]: #dcl.constexpr
448
- [dcl.dcl]: #dcl.dcl
449
- [dcl.decl]: #dcl.decl
450
- [dcl.enum]: #dcl.enum
451
- [dcl.fct]: #dcl.fct
452
- [dcl.fct.def]: #dcl.fct.def
453
- [dcl.fct.def.default]: #dcl.fct.def.default
454
- [dcl.fct.def.delete]: #dcl.fct.def.delete
455
- [dcl.fct.def.general]: #dcl.fct.def.general
456
- [dcl.fct.default]: #dcl.fct.default
457
- [dcl.fct.spec]: #dcl.fct.spec
458
- [dcl.friend]: #dcl.friend
459
- [dcl.init]: #dcl.init
460
- [dcl.init.aggr]: #dcl.init.aggr
461
- [dcl.init.list]: #dcl.init.list
462
- [dcl.init.ref]: #dcl.init.ref
463
- [dcl.init.string]: #dcl.init.string
464
- [dcl.inline]: #dcl.inline
465
- [dcl.link]: #dcl.link
466
- [dcl.meaning]: #dcl.meaning
467
- [dcl.mptr]: #dcl.mptr
468
- [dcl.name]: #dcl.name
469
- [dcl.ptr]: #dcl.ptr
470
- [dcl.ref]: #dcl.ref
471
- [dcl.spec]: #dcl.spec
472
- [dcl.spec.auto]: #dcl.spec.auto
473
- [dcl.stc]: #dcl.stc
474
- [dcl.struct.bind]: #dcl.struct.bind
475
- [dcl.type]: #dcl.type
476
- [dcl.type.auto.deduct]: #dcl.type.auto.deduct
477
- [dcl.type.class.deduct]: #dcl.type.class.deduct
478
- [dcl.type.cv]: #dcl.type.cv
479
- [dcl.type.elab]: #dcl.type.elab
480
- [dcl.type.simple]: #dcl.type.simple
481
- [dcl.typedef]: #dcl.typedef
482
- [except.handle]: except.md#except.handle
483
- [except.spec]: except.md#except.spec
484
- [except.throw]: except.md#except.throw
485
- [expr]: expr.md#expr
486
- [expr.alignof]: expr.md#expr.alignof
487
- [expr.ass]: expr.md#expr.ass
488
- [expr.call]: expr.md#expr.call
489
- [expr.cast]: expr.md#expr.cast
490
- [expr.comma]: expr.md#expr.comma
491
- [expr.cond]: expr.md#expr.cond
492
- [expr.const]: expr.md#expr.const
493
- [expr.const.cast]: expr.md#expr.const.cast
494
- [expr.mptr.oper]: expr.md#expr.mptr.oper
495
- [expr.new]: expr.md#expr.new
496
- [expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
497
- [expr.prim.this]: expr.md#expr.prim.this
498
- [expr.ref]: expr.md#expr.ref
499
- [expr.static.cast]: expr.md#expr.static.cast
500
- [expr.sub]: expr.md#expr.sub
501
- [expr.type.conv]: expr.md#expr.type.conv
502
- [expr.unary]: expr.md#expr.unary
503
- [expr.unary.op]: expr.md#expr.unary.op
504
- [intro.compliance]: intro.md#intro.compliance
505
- [intro.execution]: intro.md#intro.execution
506
- [intro.multithread]: intro.md#intro.multithread
507
- [lex.charset]: lex.md#lex.charset
508
- [lex.digraph]: lex.md#lex.digraph
509
- [lex.key]: lex.md#lex.key
510
- [lex.name]: lex.md#lex.name
511
- [lex.string]: lex.md#lex.string
512
- [namespace.alias]: #namespace.alias
513
- [namespace.def]: #namespace.def
514
- [namespace.memdef]: #namespace.memdef
515
- [namespace.qual]: basic.md#namespace.qual
516
- [namespace.udecl]: #namespace.udecl
517
- [namespace.udir]: #namespace.udir
518
- [namespace.unnamed]: #namespace.unnamed
519
- [over]: over.md#over
520
- [over.match]: over.md#over.match
521
- [over.match.class.deduct]: over.md#over.match.class.deduct
522
- [over.match.conv]: over.md#over.match.conv
523
- [over.match.copy]: over.md#over.match.copy
524
- [over.match.ctor]: over.md#over.match.ctor
525
- [over.match.list]: over.md#over.match.list
526
- [over.match.ref]: over.md#over.match.ref
527
- [over.oper]: over.md#over.oper
528
- [over.sub]: over.md#over.sub
529
- [stmt.ambig]: stmt.md#stmt.ambig
530
- [stmt.dcl]: stmt.md#stmt.dcl
531
- [stmt.expr]: stmt.md#stmt.expr
532
- [stmt.if]: stmt.md#stmt.if
533
- [stmt.iter]: stmt.md#stmt.iter
534
- [stmt.label]: stmt.md#stmt.label
535
- [stmt.return]: stmt.md#stmt.return
536
- [stmt.select]: stmt.md#stmt.select
537
- [stmt.stmt]: stmt.md#stmt.stmt
538
- [stmt.switch]: stmt.md#stmt.switch
539
- [support.runtime]: language.md#support.runtime
540
- [tab:simple.type.specifiers]: #tab:simple.type.specifiers
541
- [temp]: temp.md#temp
542
- [temp.arg.type]: temp.md#temp.arg.type
543
- [temp.class.spec]: temp.md#temp.class.spec
544
- [temp.deduct]: temp.md#temp.deduct
545
- [temp.deduct.call]: temp.md#temp.deduct.call
546
- [temp.dep]: temp.md#temp.dep
547
- [temp.expl.spec]: temp.md#temp.expl.spec
548
- [temp.explicit]: temp.md#temp.explicit
549
- [temp.inst]: temp.md#temp.inst
550
- [temp.mem]: temp.md#temp.mem
551
- [temp.names]: temp.md#temp.names
552
- [temp.param]: temp.md#temp.param
553
- [temp.res]: temp.md#temp.res
554
- [temp.spec]: temp.md#temp.spec
555
- [temp.variadic]: temp.md#temp.variadic
556
-
557
- [^1]: The “implicit int” rule of C is no longer supported.
558
-
559
- [^2]: The `inline` keyword has no effect on the linkage of a function.
560
-
561
- [^3]: There is no special provision for a *decl-specifier-seq* that
562
- lacks a *type-specifier* or that has a *type-specifier* that only
563
- specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
564
- supported.
565
-
566
- [^4]: This set of values is used to define promotion and conversion
567
- semantics for the enumeration type. It does not preclude an
568
- expression of enumeration type from having a value that falls
569
- outside this range.
570
-
571
- [^5]: this implies that the name of the class or function is
572
- unqualified.
573
-
574
- [^6]: A *using-declaration* with more than one *using-declarator* is
575
- equivalent to a corresponding sequence of *using-declaration*s with
576
- one *using-declarator* each.
577
-
578
- [^7]: During name lookup in a class hierarchy, some ambiguities may be
579
- resolved by considering whether one member hides the other along
580
- some paths ([[class.member.lookup]]). There is no such
581
- disambiguation when considering the set of names found as a result
582
- of following *using-directive*s.
583
-
584
- [^8]: As indicated by syntax, cv-qualifiers are a significant component
585
- in function return types.
586
-
587
- [^9]: One can explicitly disambiguate the parse either by introducing a
588
- comma (so the ellipsis will be parsed as part of the
589
- *parameter-declaration-clause*) or by introducing a name for the
590
- parameter (so the ellipsis will be parsed as part of the
591
- *declarator-id*).
592
-
593
- [^10]: This means that default arguments cannot appear, for example, in
594
- declarations of pointers to functions, references to functions, or
595
- `typedef` declarations.
596
-
597
- [^11]: Implementations are permitted to provide additional predefined
598
- variables with names that are reserved to the implementation (
599
- [[lex.name]]). If a predefined variable is not odr-used (
600
- [[basic.def.odr]]), its string value need not be present in the
601
- program image.
602
-
603
- [^12]: As specified in  [[conv.ptr]], converting an integer literal
604
- whose value is `0` to a pointer type results in a null pointer
605
- value.
606
-
607
- [^13]: The syntax provides for empty *initializer-list*s, but
608
- nonetheless C++does not have zero length arrays.
609
-
610
- [^14]: This requires a conversion function ([[class.conv.fct]])
611
- returning a reference type.
 
1
  ### List-initialization <a id="dcl.init.list">[[dcl.init.list]]</a>
2
 
3
  *List-initialization* is initialization of an object or reference from a
4
  *braced-init-list*. Such an initializer is called an *initializer list*,
5
+ and the comma-separated *initializer-clause*s of the *initializer-list*
6
+ or *designated-initializer-clause*s of the *designated-initializer-list*
7
+ are called the *elements* of the initializer list. An initializer list
8
+ may be empty. List-initialization can occur in direct-initialization or
9
  copy-initialization contexts; list-initialization in a
10
  direct-initialization context is called *direct-list-initialization* and
11
  list-initialization in a copy-initialization context is called
12
  *copy-list-initialization*.
13
 
14
  [*Note 1*:
15
 
16
  List-initialization can be used
17
 
18
+ - as the initializer in a variable definition [[dcl.init]]
19
+ - as the initializer in a *new-expression* [[expr.new]]
20
+ - in a `return` statement [[stmt.return]]
21
+ - as a *for-range-initializer* [[stmt.iter]]
22
+ - as a function argument [[expr.call]]
23
+ - as a subscript [[expr.sub]]
24
+ - as an argument to a constructor invocation ([[dcl.init]],
25
  [[expr.type.conv]])
26
+ - as an initializer for a non-static data member [[class.mem]]
27
+ - in a *mem-initializer* [[class.base.init]]
28
+ - on the right-hand side of an assignment [[expr.ass]]
29
 
30
  [*Example 1*:
31
 
32
  ``` cpp
33
  int a = {1};
 
43
  — *end example*]
44
 
45
  — *end note*]
46
 
47
  A constructor is an *initializer-list constructor* if its first
48
+ parameter is of type `std::initializer_list<E>` or reference to
49
+ cv `std::initializer_list<E>` for some type `E`, and either there are no
50
+ other parameters or else all other parameters have default arguments
51
+ [[dcl.fct.default]].
52
 
53
  [*Note 2*: Initializer-list constructors are favored over other
54
+ constructors in list-initialization [[over.match.list]]. Passing an
55
  initializer list as the argument to the constructor template
56
  `template<class T> C(T)` of a class `C` does not create an
57
  initializer-list constructor, because an initializer list argument
58
+ causes the corresponding parameter to be a non-deduced context
59
+ [[temp.deduct.call]]. — *end note*]
60
 
61
  The template `std::initializer_list` is not predefined; if the header
62
+ `<initializer_list>` is not imported or included prior to a use of
63
  `std::initializer_list` — even an implicit use in which the type is not
64
+ named [[dcl.spec.auto]] — the program is ill-formed.
65
 
66
  List-initialization of an object or reference of type `T` is defined as
67
  follows:
68
 
69
+ - If the *braced-init-list* contains a *designated-initializer-list*,
70
+ `T` shall be an aggregate class. The ordered *identifier*s in the
71
+ *designator*s of the *designated-initializer-list* shall form a
72
+ subsequence of the ordered *identifier*s in the direct non-static data
73
+ members of `T`. Aggregate initialization is performed
74
+ [[dcl.init.aggr]].
75
+ \[*Example 2*:
76
+ ``` cpp
77
+ struct A { int x; int y; int z; };
78
+ A a{.y = 2, .x = 1}; // error: designator order does not match declaration order
79
+ A b{.x = 1, .z = 2}; // OK, b.y initialized to 0
80
+ ```
81
+
82
+ — *end example*]
83
  - If `T` is an aggregate class and the initializer list has a single
84
  element of type *cv* `U`, where `U` is `T` or a class derived from
85
  `T`, the object is initialized from that element (by
86
  copy-initialization for copy-list-initialization, or by
87
  direct-initialization for direct-list-initialization).
88
  - Otherwise, if `T` is a character array and the initializer list has a
89
+ single element that is an appropriately-typed *string-literal*
90
+ [[dcl.init.string]], initialization is performed as described in that
91
+ subclause.
92
  - Otherwise, if `T` is an aggregate, aggregate initialization is
93
+ performed [[dcl.init.aggr]].
94
+ \[*Example 3*:
95
  ``` cpp
96
  double ad[] = { 1, 2.0 }; // OK
97
  int ai[] = { 1, 2.0 }; // error: narrowing
98
 
99
  struct S2 {
 
110
  type with a default constructor, the object is value-initialized.
111
  - Otherwise, if `T` is a specialization of `std::initializer_list<E>`,
112
  the object is constructed as described below.
113
  - Otherwise, if `T` is a class type, constructors are considered. The
114
  applicable constructors are enumerated and the best one is chosen
115
+ through overload resolution ([[over.match]], [[over.match.list]]). If
116
+ a narrowing conversion (see below) is required to convert any of the
117
+ arguments, the program is ill-formed.
118
+ \[*Example 4*:
119
  ``` cpp
120
  struct S {
121
  S(std::initializer_list<double>); // #1
122
  S(std::initializer_list<int>); // #2
123
  S(); // #3
 
127
  S s2 = { 1, 2, 3 }; // invoke #2
128
  S s3 = { }; // invoke #3
129
  ```
130
 
131
  — *end example*]
132
+ \[*Example 5*:
133
  ``` cpp
134
  struct Map {
135
  Map(std::initializer_list<std::pair<std::string,int>>);
136
  };
137
  Map ship = {{"Sophie",14}, {"Surprise",28}};
138
  ```
139
 
140
  — *end example*]
141
+ \[*Example 6*:
142
  ``` cpp
143
  struct S {
144
  // no initializer-list constructors
145
  S(int, double, double); // #1
146
  S(); // #2
 
150
  S s2 { 1.0, 2, 3 }; // error: narrowing
151
  S s3 { }; // OK: invoke #2
152
  ```
153
 
154
  — *end example*]
155
+ - Otherwise, if `T` is an enumeration with a fixed underlying type
156
+ [[dcl.enum]] `U`, the *initializer-list* has a single element `v`, `v`
157
+ can be implicitly converted to `U`, and the initialization is
158
+ direct-list-initialization, the object is initialized with the value
159
+ `T(v)` [[expr.type.conv]]; if a narrowing conversion is required to
160
+ convert `v` to `U`, the program is ill-formed.
161
+ \[*Example 7*:
162
  ``` cpp
163
  enum byte : unsigned char { };
164
  byte b { 42 }; // OK
165
  byte c = { 42 }; // error
166
  byte d = byte{ 42 }; // OK; same value as b
 
183
  reference-related to `E`, the object or reference is initialized from
184
  that element (by copy-initialization for copy-list-initialization, or
185
  by direct-initialization for direct-list-initialization); if a
186
  narrowing conversion (see below) is required to convert the element to
187
  `T`, the program is ill-formed.
188
+ \[*Example 8*:
189
  ``` cpp
190
  int x1 {2}; // OK
191
  int x2 {2.0}; // error: narrowing
192
  ```
193
 
194
  — *end example*]
195
+ - Otherwise, if `T` is a reference type, a prvalue is generated. The
196
+ prvalue initializes its result object by copy-list-initialization. The
197
+ prvalue is then used to direct-initialize the reference. The type of
198
+ the temporary is the type referenced by `T`, unless `T` is “reference
199
+ to array of unknown bound of `U`”, in which case the type of the
200
+ temporary is the type of `x` in the declaration `U x[] H`, where H is
201
+ the initializer list.
202
  \[*Note 3*: As usual, the binding will fail and the program is
203
  ill-formed if the reference type is an lvalue reference to a non-const
204
  type. — *end note*]
205
+ \[*Example 9*:
206
  ``` cpp
207
  struct S {
208
  S(std::initializer_list<double>); // #1
209
  S(const std::string&); // #2
210
  // ...
 
213
  const S& r2 { "Spinach" }; // OK: invoke #2
214
  S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
215
  const int& i1 = { 1 }; // OK
216
  const int& i2 = { 1.1 }; // error: narrowing
217
  const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
218
+
219
+ struct A { } a;
220
+ struct B { explicit B(const A&); };
221
+ const B& b2{a}; // error: cannot copy-list-initialize B temporary from A
222
  ```
223
 
224
  — *end example*]
225
  - Otherwise, if the initializer list has no elements, the object is
226
  value-initialized.
227
+ \[*Example 10*:
228
  ``` cpp
229
  int** pp {}; // initialized to null pointer
230
  ```
231
 
232
  — *end example*]
233
  - Otherwise, the program is ill-formed.
234
+ \[*Example 11*:
235
  ``` cpp
236
  struct A { int i; int j; };
237
  A a1 { 1, 2 }; // aggregate initialization
238
  A a2 { 1.2 }; // error: narrowing
239
  struct B {
 
252
  ```
253
 
254
  — *end example*]
255
 
256
  Within the *initializer-list* of a *braced-init-list*, the
257
+ *initializer-clause*s, including any that result from pack expansions
258
+ [[temp.variadic]], are evaluated in the order in which they appear. That
259
+ is, every value computation and side effect associated with a given
260
  *initializer-clause* is sequenced before every value computation and
261
  side effect associated with any *initializer-clause* that follows it in
262
  the comma-separated list of the *initializer-list*.
263
 
264
  [*Note 4*: This evaluation ordering holds regardless of the semantics
 
266
  *initializer-list* are interpreted as arguments of a constructor call,
267
  even though ordinarily there are no sequencing constraints on the
268
  arguments of a call. — *end note*]
269
 
270
  An object of type `std::initializer_list<E>` is constructed from an
271
+ initializer list as if the implementation generated and materialized
272
+ [[conv.rval]] a prvalue of type “array of N `const E`”, where N is the
273
  number of elements in the initializer list. Each element of that array
274
  is copy-initialized with the corresponding element of the initializer
275
  list, and the `std::initializer_list<E>` object is constructed to refer
276
  to that array.
277
 
278
  [*Note 5*: A constructor or conversion function selected for the copy
279
+ is required to be accessible [[class.access]] in the context of the
280
  initializer list. — *end note*]
281
 
282
  If a narrowing conversion is required to initialize any of the elements,
283
  the program is ill-formed.
284
 
285
+ [*Example 12*:
286
 
287
  ``` cpp
288
  struct X {
289
  X(std::initializer_list<double> v);
290
  };
 
302
  assuming that the implementation can construct an `initializer_list`
303
  object with a pair of pointers.
304
 
305
  — *end example*]
306
 
307
+ The array has the same lifetime as any other temporary object
308
+ [[class.temporary]], except that initializing an `initializer_list`
309
  object from the array extends the lifetime of the array exactly like
310
  binding a reference to a temporary.
311
 
312
+ [*Example 13*:
313
 
314
  ``` cpp
315
  typedef std::complex<double> cmplx;
316
  std::vector<cmplx> v1 = { 1, 2, 3 };
317
 
 
330
  function call, so the array created for `{ 1, 2, 3 }` has
331
  full-expression lifetime. For `i3`, the `initializer_list` object is a
332
  variable, so the array persists for the lifetime of the variable. For
333
  `i4`, the `initializer_list` object is initialized in the constructor’s
334
  *ctor-initializer* as if by binding a temporary array to a reference
335
+ member, so the program is ill-formed [[class.base.init]].
336
 
337
  — *end example*]
338
 
339
  [*Note 6*: The implementation is free to allocate the array in
340
  read-only memory if an explicit array with the same initializer could be
 
352
  value after conversion will fit into the target type and will produce
353
  the original value when converted back to the original type, or
354
  - from an integer type or unscoped enumeration type to an integer type
355
  that cannot represent all the values of the original type, except
356
  where the source is a constant expression whose value after integral
357
+ promotions will fit into the target type, or
358
+ - from a pointer type or a pointer-to-member type to `bool`.
359
 
360
  [*Note 7*: As indicated above, such conversions are not allowed at the
361
  top level in list-initializations. — *end note*]
362
 
363
+ [*Example 14*:
364
 
365
  ``` cpp
366
  int x = 999; // x is not a constant expression
367
  const int y = 999;
368
  const int z = 99;
 
376
  signed int si1 =
377
  { (unsigned int)-1 }; // error: narrows
378
  int ii = {2.0}; // error: narrows
379
  float f1 { x }; // error: might narrow
380
  float f2 { 7 }; // OK: 7 can be exactly represented as a float
381
+ bool b = {"meow"}; // error: narrows
382
  int f(int);
383
+ int a[] = { 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level
 
384
  ```
385
 
386
  — *end example*]
387