From Jason Turner

[dcl.init.list]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpej3ghql0/{from.md → to.md} +182 -115
tmp/tmpej3ghql0/{from.md → to.md} RENAMED
@@ -6,24 +6,30 @@ 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*. List-initialization can be used
 
 
 
 
12
 
13
  - as the initializer in a variable definition ([[dcl.init]])
14
- - as the initializer in a new expression ([[expr.new]])
15
  - in a return statement ([[stmt.return]])
16
  - as a *for-range-initializer* ([[stmt.iter]])
17
  - as a function argument ([[expr.call]])
18
  - as a subscript ([[expr.sub]])
19
  - as an argument to a constructor invocation ([[dcl.init]], 
20
  [[expr.type.conv]])
21
  - as an initializer for a non-static data member ([[class.mem]])
22
  - in a *mem-initializer* ([[class.base.init]])
23
  - on the right-hand side of an assignment ([[expr.ass]])
24
 
 
 
25
  ``` cpp
26
  int a = {1};
27
  std::complex<double> z{1,2};
28
  new std::vector<std::string>{"once", "upon", "a", "time"}; // 4 string elements
29
  f( {"Nicholas","Annemarie"} ); // pass list of two elements
@@ -31,30 +37,48 @@ return { "Norah" }; // return list of one element
31
  int* e {}; // initialization to zero / null pointer
32
  x = double{1}; // explicitly construct a double
33
  std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
34
  ```
35
 
 
 
 
 
36
  A constructor is an *initializer-list constructor* if its first
37
  parameter is of type `std::initializer_list<E>` or reference to possibly
38
  cv-qualified `std::initializer_list<E>` for some type `E`, and either
39
  there are no other parameters or else all other parameters have default
40
- arguments ([[dcl.fct.default]]). Initializer-list constructors are
41
- favored over other constructors in list-initialization (
42
- [[over.match.list]]). Passing an initializer list as the argument to the
43
- constructor template `template<class T> C(T)` of a class `C` does not
44
- create an initializer-list constructor, because an initializer list
45
- argument causes the corresponding parameter to be a non-deduced
46
- context ([[temp.deduct.call]]). The template `std::initializer_list` is
47
- not predefined; if the header `<initializer_list>` is not included prior
48
- to a use of `std::initializer_list` even an implicit use in which the
49
- type is not named ([[dcl.spec.auto]]) — the program is ill-formed.
 
 
 
 
50
 
51
  List-initialization of an object or reference of type `T` is defined as
52
  follows:
53
 
54
- - If `T` is an aggregate, aggregate initialization is performed (
55
- [[dcl.init.aggr]]).
 
 
 
 
 
 
 
 
 
 
56
  ``` cpp
57
  double ad[] = { 1, 2.0 }; // OK
58
  int ai[] = { 1, 2.0 }; // error: narrowing
59
 
60
  struct S2 {
@@ -63,22 +87,22 @@ follows:
63
  };
64
  S2 s21 = { 1, 2, 3.0 }; // OK
65
  S2 s22 { 1.0, 2, 3 }; // error: narrowing
66
  S2 s23 { }; // OK: default to 0,0,0
67
  ```
 
 
68
  - Otherwise, if the initializer list has no elements and `T` is a class
69
  type with a default constructor, the object is value-initialized.
70
- - Otherwise, if `T` is a specialization of `std::initializer_list<E>`, a
71
- prvalue `initializer_list` object is constructed as described below
72
- and used to initialize the object according to the rules for
73
- initialization of an object from a class of the same type (
74
- [[dcl.init]]).
75
  - Otherwise, if `T` is a class type, constructors are considered. The
76
  applicable constructors are enumerated and the best one is chosen
77
  through overload resolution ([[over.match]],  [[over.match.list]]).
78
  If a narrowing conversion (see below) is required to convert any of
79
  the arguments, the program is ill-formed.
 
80
  ``` cpp
81
  struct S {
82
  S(std::initializer_list<double>); // #1
83
  S(std::initializer_list<int>); // #2
84
  S(); // #3
@@ -87,17 +111,21 @@ follows:
87
  S s1 = { 1.0, 2.0, 3.0 }; // invoke #1
88
  S s2 = { 1, 2, 3 }; // invoke #2
89
  S s3 = { }; // invoke #3
90
  ```
91
 
 
 
92
  ``` cpp
93
  struct Map {
94
  Map(std::initializer_list<std::pair<std::string,int>>);
95
  };
96
  Map ship = {{"Sophie",14}, {"Surprise",28}};
97
  ```
98
 
 
 
99
  ``` cpp
100
  struct S {
101
  // no initializer-list constructors
102
  S(int, double, double); // #1
103
  S(); // #2
@@ -105,25 +133,61 @@ follows:
105
  };
106
  S s1 = { 1, 2, 3.0 }; // OK: invoke #1
107
  S s2 { 1.0, 2, 3 }; // error: narrowing
108
  S s3 { }; // OK: invoke #2
109
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  - Otherwise, if the initializer list has a single element of type `E`
111
  and either `T` is not a reference type or its referenced type is
112
  reference-related to `E`, the object or reference is initialized from
113
- that element; if a narrowing conversion (see below) is required to
114
- convert the element to `T`, the program is ill-formed.
 
 
 
115
  ``` cpp
116
  int x1 {2}; // OK
117
  int x2 {2.0}; // error: narrowing
118
  ```
119
- - Otherwise, if `T` is a reference type, a prvalue temporary of the type
120
- referenced by `T` is copy-list-initialized or direct-list-initialized,
121
- depending on the kind of initialization for the reference, and the
122
- reference is bound to that temporary. As usual, the binding will fail
123
- and the program is ill-formed if the reference type is an lvalue
124
- reference to a non-const type.
 
 
 
 
 
125
  ``` cpp
126
  struct S {
127
  S(std::initializer_list<double>); // #1
128
  S(const std::string&); // #2
129
  // ...
@@ -133,16 +197,22 @@ follows:
133
  S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
134
  const int& i1 = { 1 }; // OK
135
  const int& i2 = { 1.1 }; // error: narrowing
136
  const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
137
  ```
 
 
138
  - Otherwise, if the initializer list has no elements, the object is
139
  value-initialized.
 
140
  ``` cpp
141
  int** pp {}; // initialized to null pointer
142
  ```
 
 
143
  - Otherwise, the program is ill-formed.
 
144
  ``` cpp
145
  struct A { int i; int j; };
146
  A a1 { 1, 2 }; // aggregate initialization
147
  A a2 { 1.2 }; // error: narrowing
148
  struct B {
@@ -158,32 +228,42 @@ follows:
158
 
159
  int j { 1 }; // initialize to 1
160
  int k { }; // initialize to 0
161
  ```
162
 
 
 
163
  Within the *initializer-list* of a *braced-init-list*, the
164
  *initializer-clause*s, including any that result from pack expansions (
165
  [[temp.variadic]]), are evaluated in the order in which they appear.
166
  That is, every value computation and side effect associated with a given
167
  *initializer-clause* is sequenced before every value computation and
168
  side effect associated with any *initializer-clause* that follows it in
169
- the comma-separated list of the *initializer-list*. This evaluation
170
- ordering holds regardless of the semantics of the initialization; for
171
- example, it applies when the elements of the *initializer-list* are
172
- interpreted as arguments of a constructor call, even though ordinarily
173
- there are no sequencing constraints on the arguments of a call.
 
 
174
 
175
  An object of type `std::initializer_list<E>` is constructed from an
176
- initializer list as if the implementation allocated a temporary array of
177
- N elements of type `const E`, where N is the number of elements in the
178
- initializer list. Each element of that array is copy-initialized with
179
- the corresponding element of the initializer list, and the
180
- `std::initializer_list<E>` object is constructed to refer to that array.
181
- A constructor or conversion function selected for the copy shall be
182
- accessible (Clause  [[class.access]]) in the context of the initializer
183
- list. If a narrowing conversion is required to initialize any of the
184
- elements, the program is ill-formed.
 
 
 
 
 
 
185
 
186
  ``` cpp
187
  struct X {
188
  X(std::initializer_list<double> v);
189
  };
@@ -199,15 +279,19 @@ X x(std::initializer_list<double>(__a, __a+3));
199
  ```
200
 
201
  assuming that the implementation can construct an `initializer_list`
202
  object with a pair of pointers.
203
 
 
 
204
  The array has the same lifetime as any other temporary object (
205
  [[class.temporary]]), except that initializing an `initializer_list`
206
  object from the array extends the lifetime of the array exactly like
207
  binding a reference to a temporary.
208
 
 
 
209
  ``` cpp
210
  typedef std::complex<double> cmplx;
211
  std::vector<cmplx> v1 = { 1, 2, 3 };
212
 
213
  void f() {
@@ -215,24 +299,27 @@ void f() {
215
  std::initializer_list<int> i3 = { 1, 2, 3 };
216
  }
217
 
218
  struct A {
219
  std::initializer_list<int> i4;
220
- A() : i4{ 1, 2, 3 } {} // creates an A with a dangling reference
221
  };
222
  ```
223
 
224
  For `v1` and `v2`, the `initializer_list` object is a parameter in a
225
  function call, so the array created for `{ 1, 2, 3 }` has
226
  full-expression lifetime. For `i3`, the `initializer_list` object is a
227
  variable, so the array persists for the lifetime of the variable. For
228
- `i4`, the `initializer_list` object is initialized in a constructor’s
229
- *ctor-initializer*, so the array persists only until the constructor
230
- exits, and so any use of the elements of `i4` after the constructor
231
- exits produces undefined behavior. The implementation is free to
232
- allocate the array in read-only memory if an explicit array with the
233
- same initializer could be so allocated.
 
 
 
234
 
235
  A *narrowing conversion* is an implicit conversion
236
 
237
  - from a floating-point type to an integer type, or
238
  - from `long double` to `double` or `float`, or from `double` to
@@ -246,12 +333,14 @@ A *narrowing conversion* is an implicit conversion
246
  - from an integer type or unscoped enumeration type to an integer type
247
  that cannot represent all the values of the original type, except
248
  where the source is a constant expression whose value after integral
249
  promotions will fit into the target type.
250
 
251
- As indicated above, such conversions are not allowed at the top level in
252
- list-initializations.
 
 
253
 
254
  ``` cpp
255
  int x = 999; // x is not a constant expression
256
  const int y = 999;
257
  const int z = 99;
@@ -270,34 +359,41 @@ float f2 { 7 }; // OK: 7 can be exactly represented as a float
270
  int f(int);
271
  int a[] =
272
  { 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level
273
  ```
274
 
 
 
275
  <!-- Link reference definitions -->
 
276
  [basic.compound]: basic.md#basic.compound
277
  [basic.def]: basic.md#basic.def
278
  [basic.def.odr]: basic.md#basic.def.odr
279
  [basic.fundamental]: basic.md#basic.fundamental
280
  [basic.life]: basic.md#basic.life
281
  [basic.link]: basic.md#basic.link
282
  [basic.lookup]: basic.md#basic.lookup
283
  [basic.lookup.argdep]: basic.md#basic.lookup.argdep
 
284
  [basic.lookup.elab]: basic.md#basic.lookup.elab
285
  [basic.lookup.qual]: basic.md#basic.lookup.qual
286
  [basic.lookup.udir]: basic.md#basic.lookup.udir
287
  [basic.lookup.unqual]: basic.md#basic.lookup.unqual
288
  [basic.lval]: basic.md#basic.lval
289
  [basic.namespace]: #basic.namespace
290
  [basic.scope]: basic.md#basic.scope
291
  [basic.scope.block]: basic.md#basic.scope.block
 
292
  [basic.scope.namespace]: basic.md#basic.scope.namespace
293
  [basic.scope.pdecl]: basic.md#basic.scope.pdecl
294
  [basic.scope.proto]: basic.md#basic.scope.proto
295
  [basic.start]: basic.md#basic.start
296
- [basic.start.init]: basic.md#basic.start.init
 
297
  [basic.stc]: basic.md#basic.stc
298
  [basic.stc.auto]: basic.md#basic.stc.auto
 
299
  [basic.stc.static]: basic.md#basic.stc.static
300
  [basic.stc.thread]: basic.md#basic.stc.thread
301
  [basic.type.qualifier]: basic.md#basic.type.qualifier
302
  [basic.types]: basic.md#basic.types
303
  [class]: class.md#class
@@ -307,43 +403,49 @@ int a[] =
307
  [class.conv]: special.md#class.conv
308
  [class.conv.ctor]: special.md#class.conv.ctor
309
  [class.conv.fct]: special.md#class.conv.fct
310
  [class.copy]: special.md#class.copy
311
  [class.ctor]: special.md#class.ctor
312
- [class.derived]: class.md#class.derived
313
  [class.dtor]: special.md#class.dtor
314
  [class.expl.init]: special.md#class.expl.init
315
  [class.friend]: class.md#class.friend
316
- [class.inhctor]: special.md#class.inhctor
317
  [class.init]: special.md#class.init
318
  [class.mem]: class.md#class.mem
319
  [class.member.lookup]: class.md#class.member.lookup
320
  [class.mfct]: class.md#class.mfct
 
321
  [class.name]: class.md#class.name
322
  [class.qual]: basic.md#class.qual
323
  [class.static]: class.md#class.static
324
  [class.static.data]: class.md#class.static.data
325
  [class.temporary]: special.md#class.temporary
326
- [class.this]: class.md#class.this
327
  [class.union]: class.md#class.union
 
328
  [class.virtual]: class.md#class.virtual
329
  [conv]: conv.md#conv
330
  [conv.array]: conv.md#conv.array
331
  [conv.func]: conv.md#conv.func
332
  [conv.integral]: conv.md#conv.integral
333
  [conv.lval]: conv.md#conv.lval
334
  [conv.prom]: conv.md#conv.prom
335
  [conv.ptr]: conv.md#conv.ptr
 
 
 
336
  [dcl.align]: #dcl.align
337
  [dcl.ambig.res]: #dcl.ambig.res
338
  [dcl.array]: #dcl.array
339
  [dcl.asm]: #dcl.asm
340
  [dcl.attr]: #dcl.attr
341
  [dcl.attr.depend]: #dcl.attr.depend
342
  [dcl.attr.deprecated]: #dcl.attr.deprecated
 
343
  [dcl.attr.grammar]: #dcl.attr.grammar
 
344
  [dcl.attr.noreturn]: #dcl.attr.noreturn
 
345
  [dcl.constexpr]: #dcl.constexpr
346
  [dcl.dcl]: #dcl.dcl
347
  [dcl.decl]: #dcl.decl
348
  [dcl.enum]: #dcl.enum
349
  [dcl.fct]: #dcl.fct
@@ -357,25 +459,28 @@ int a[] =
357
  [dcl.init]: #dcl.init
358
  [dcl.init.aggr]: #dcl.init.aggr
359
  [dcl.init.list]: #dcl.init.list
360
  [dcl.init.ref]: #dcl.init.ref
361
  [dcl.init.string]: #dcl.init.string
 
362
  [dcl.link]: #dcl.link
363
  [dcl.meaning]: #dcl.meaning
364
  [dcl.mptr]: #dcl.mptr
365
  [dcl.name]: #dcl.name
366
  [dcl.ptr]: #dcl.ptr
367
  [dcl.ref]: #dcl.ref
368
  [dcl.spec]: #dcl.spec
369
  [dcl.spec.auto]: #dcl.spec.auto
370
  [dcl.stc]: #dcl.stc
 
371
  [dcl.type]: #dcl.type
 
 
372
  [dcl.type.cv]: #dcl.type.cv
373
  [dcl.type.elab]: #dcl.type.elab
374
  [dcl.type.simple]: #dcl.type.simple
375
  [dcl.typedef]: #dcl.typedef
376
- [depr.register]: future.md#depr.register
377
  [except.handle]: except.md#except.handle
378
  [except.spec]: except.md#except.spec
379
  [except.throw]: except.md#except.throw
380
  [expr]: expr.md#expr
381
  [expr.alignof]: expr.md#expr.alignof
@@ -386,18 +491,18 @@ int a[] =
386
  [expr.cond]: expr.md#expr.cond
387
  [expr.const]: expr.md#expr.const
388
  [expr.const.cast]: expr.md#expr.const.cast
389
  [expr.mptr.oper]: expr.md#expr.mptr.oper
390
  [expr.new]: expr.md#expr.new
391
- [expr.prim.lambda]: expr.md#expr.prim.lambda
 
392
  [expr.ref]: expr.md#expr.ref
393
  [expr.static.cast]: expr.md#expr.static.cast
394
  [expr.sub]: expr.md#expr.sub
395
  [expr.type.conv]: expr.md#expr.type.conv
396
  [expr.unary]: expr.md#expr.unary
397
  [expr.unary.op]: expr.md#expr.unary.op
398
- [global.names]: library.md#global.names
399
  [intro.compliance]: intro.md#intro.compliance
400
  [intro.execution]: intro.md#intro.execution
401
  [intro.multithread]: intro.md#intro.multithread
402
  [lex.charset]: lex.md#lex.charset
403
  [lex.digraph]: lex.md#lex.digraph
@@ -411,30 +516,34 @@ int a[] =
411
  [namespace.udecl]: #namespace.udecl
412
  [namespace.udir]: #namespace.udir
413
  [namespace.unnamed]: #namespace.unnamed
414
  [over]: over.md#over
415
  [over.match]: over.md#over.match
 
416
  [over.match.conv]: over.md#over.match.conv
417
  [over.match.copy]: over.md#over.match.copy
418
  [over.match.ctor]: over.md#over.match.ctor
419
  [over.match.list]: over.md#over.match.list
420
  [over.match.ref]: over.md#over.match.ref
421
  [over.oper]: over.md#over.oper
422
  [over.sub]: over.md#over.sub
423
  [stmt.ambig]: stmt.md#stmt.ambig
424
- [stmt.block]: stmt.md#stmt.block
425
  [stmt.dcl]: stmt.md#stmt.dcl
426
- [stmt.for]: stmt.md#stmt.for
 
427
  [stmt.iter]: stmt.md#stmt.iter
 
428
  [stmt.return]: stmt.md#stmt.return
429
  [stmt.select]: stmt.md#stmt.select
430
  [stmt.stmt]: stmt.md#stmt.stmt
 
431
  [support.runtime]: language.md#support.runtime
432
  [tab:simple.type.specifiers]: #tab:simple.type.specifiers
433
  [temp]: temp.md#temp
434
  [temp.arg.type]: temp.md#temp.arg.type
435
  [temp.class.spec]: temp.md#temp.class.spec
 
436
  [temp.deduct.call]: temp.md#temp.deduct.call
437
  [temp.dep]: temp.md#temp.dep
438
  [temp.expl.spec]: temp.md#temp.expl.spec
439
  [temp.explicit]: temp.md#temp.explicit
440
  [temp.inst]: temp.md#temp.inst
@@ -445,11 +554,11 @@ int a[] =
445
  [temp.spec]: temp.md#temp.spec
446
  [temp.variadic]: temp.md#temp.variadic
447
 
448
  [^1]: The “implicit int” rule of C is no longer supported.
449
 
450
- [^2]: The inline keyword has no effect on the linkage of a function.
451
 
452
  [^3]: There is no special provision for a *decl-specifier-seq* that
453
  lacks a *type-specifier* or that has a *type-specifier* that only
454
  specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
455
  supported.
@@ -457,88 +566,46 @@ int a[] =
457
  [^4]: This set of values is used to define promotion and conversion
458
  semantics for the enumeration type. It does not preclude an
459
  expression of enumeration type from having a value that falls
460
  outside this range.
461
 
462
- [^5]: Although entities in an unnamed namespace might have external
463
- linkage, they are effectively qualified by a name unique to their
464
- translation unit and therefore can never be seen from any other
465
- translation unit.
466
-
467
- [^6]: this implies that the name of the class or function is
468
  unqualified.
469
 
 
 
 
 
470
  [^7]: During name lookup in a class hierarchy, some ambiguities may be
471
  resolved by considering whether one member hides the other along
472
  some paths ([[class.member.lookup]]). There is no such
473
  disambiguation when considering the set of names found as a result
474
  of following *using-directive*s.
475
 
476
- [^8]: A declaration with several declarators is usually equivalent to
477
- the corresponding sequence of declarations each with a single
478
- declarator. That is
479
-
480
- `T D1, D2, ... Dn;`
481
-
482
- is usually equivalent to
483
-
484
- `T D1; T D2; ... T Dn;`
485
-
486
- where `T` is a *decl-specifier-seq* and each `Di` is an
487
- *init-declarator*. An exception occurs when a name introduced by one
488
- of the *declarator*s hides a type name used by the
489
- *decl-specifiers*, so that when the same *decl-specifiers* are used
490
- in a subsequent declaration, they do not have the same meaning, as
491
- in
492
-
493
- `struct S { ... };`
494
- `S S, T; \textrm{// declare two instances of \tcode{struct S}}`
495
-
496
- which is not equivalent to
497
-
498
- `struct S { ... };`
499
- `S S;`
500
- `S T; \textrm{// error}`
501
-
502
- Another exception occurs when `T` is `auto` ([[dcl.spec.auto]]),
503
- for example:
504
-
505
- `auto i = 1, j = 2.0; \textrm{// error: deduced types for \tcode{i} and \tcode{j} do not match}`
506
- as opposed to
507
- `auto i = 1; \textrm{// OK: \tcode{i} deduced to have type \tcode{int}}`
508
- `auto j = 2.0; \textrm{// OK: \tcode{j} deduced to have type \tcode{double}}`
509
-
510
- [^9]: As indicated by syntax, cv-qualifiers are a significant component
511
  in function return types.
512
 
513
- [^10]: This excludes parameters of type `T2`” where `T2` is “pointer
514
- to array of unknown bound of `T`” and where means any sequence of
515
- “pointer to” and “array of” derived declarator types. This exclusion
516
- applies to the parameters of the function, and if a parameter is a
517
- pointer to function or pointer to member function then to its
518
- parameters also, etc.
519
-
520
- [^11]: One can explicitly disambiguate the parse either by introducing a
521
  comma (so the ellipsis will be parsed as part of the
522
  *parameter-declaration-clause*) or by introducing a name for the
523
  parameter (so the ellipsis will be parsed as part of the
524
  *declarator-id*).
525
 
526
- [^12]: This means that default arguments cannot appear, for example, in
527
  declarations of pointers to functions, references to functions, or
528
  `typedef` declarations.
529
 
530
- [^13]: Implementations are permitted to provide additional predefined
531
  variables with names that are reserved to the implementation (
532
- [[global.names]]). If a predefined variable is not odr-used (
533
  [[basic.def.odr]]), its string value need not be present in the
534
  program image.
535
 
536
- [^14]: As specified in  [[conv.ptr]], converting an integer literal
537
  whose value is `0` to a pointer type results in a null pointer
538
  value.
539
 
540
- [^15]: The syntax provides for empty *initializer-list*s, but
541
  nonetheless C++does not have zero length arrays.
542
 
543
- [^16]: This requires a conversion function ([[class.conv.fct]])
544
  returning a reference type.
 
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};
33
  std::complex<double> z{1,2};
34
  new std::vector<std::string>{"once", "upon", "a", "time"}; // 4 string elements
35
  f( {"Nicholas","Annemarie"} ); // pass list of two elements
 
37
  int* e {}; // initialization to zero / null pointer
38
  x = double{1}; // explicitly construct a double
39
  std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
40
  ```
41
 
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 {
 
87
  };
88
  S2 s21 = { 1, 2, 3.0 }; // OK
89
  S2 s22 { 1.0, 2, 3 }; // error: narrowing
90
  S2 s23 { }; // OK: default to 0,0,0
91
  ```
92
+
93
+ — *end example*]
94
  - Otherwise, if the initializer list has no elements and `T` is a class
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
 
111
  S s1 = { 1.0, 2.0, 3.0 }; // invoke #1
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
 
133
  };
134
  S s1 = { 1, 2, 3.0 }; // OK: invoke #1
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
152
+ byte e { -1 }; // error
153
+
154
+ struct A { byte b; };
155
+ A a1 = { { 42 } }; // error
156
+ A a2 = { byte{ 42 } }; // OK
157
+
158
+ void f(byte);
159
+ f({ 42 }); // error
160
+
161
+ enum class Handle : uint32_t { Invalid = 0 };
162
+ Handle h { 42 }; // OK
163
+ ```
164
+
165
+ — *end example*]
166
  - Otherwise, if the initializer list has a single element of type `E`
167
  and either `T` is not a reference type or its referenced type is
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
  // ...
 
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 {
 
228
 
229
  int j { 1 }; // initialize to 1
230
  int k { }; // initialize to 0
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
244
+ 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
  };
 
279
  ```
280
 
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
 
297
  void f() {
 
299
  std::initializer_list<int> i3 = { 1, 2, 3 };
300
  }
301
 
302
  struct A {
303
  std::initializer_list<int> i4;
304
+ A() : i4{ 1, 2, 3 } {} // ill-formed, would create a dangling reference
305
  };
306
  ```
307
 
308
  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
320
+ so allocated. — *end note*]
321
 
322
  A *narrowing conversion* is an implicit conversion
323
 
324
  - from a floating-point type to an integer type, or
325
  - from `long double` to `double` or `float`, or from `double` to
 
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;
 
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
 
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
 
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
 
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
 
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
 
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.
 
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.