tmp/tmp9vxv5rq3/{from.md → to.md}
RENAMED
|
@@ -11,10 +11,11 @@ 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 function argument ([[expr.call]])
|
| 17 |
- as a subscript ([[expr.sub]])
|
| 18 |
- as an argument to a constructor invocation ([[dcl.init]],
|
| 19 |
[[expr.type.conv]])
|
| 20 |
- as an initializer for a non-static data member ([[class.mem]])
|
|
@@ -36,22 +37,24 @@ A constructor is an *initializer-list constructor* if its first
|
|
| 36 |
parameter is of type `std::initializer_list<E>` or reference to possibly
|
| 37 |
cv-qualified `std::initializer_list<E>` for some type `E`, and either
|
| 38 |
there are no other parameters or else all other parameters have default
|
| 39 |
arguments ([[dcl.fct.default]]). Initializer-list constructors are
|
| 40 |
favored over other constructors in list-initialization (
|
| 41 |
-
[[over.match.list]]).
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
type is not named ([[dcl.spec.auto]]) — the program is ill-formed.
|
| 45 |
|
| 46 |
List-initialization of an object or reference of type `T` is defined as
|
| 47 |
follows:
|
| 48 |
|
| 49 |
-
- If
|
| 50 |
-
|
| 51 |
-
- Otherwise, if `T` is an aggregate, aggregate initialization is
|
| 52 |
-
performed ([[dcl.init.aggr]]).
|
| 53 |
``` cpp
|
| 54 |
double ad[] = { 1, 2.0 }; // OK
|
| 55 |
int ai[] = { 1, 2.0 }; // error: narrowing
|
| 56 |
|
| 57 |
struct S2 {
|
|
@@ -60,13 +63,15 @@ follows:
|
|
| 60 |
};
|
| 61 |
S2 s21 = { 1, 2, 3.0 }; // OK
|
| 62 |
S2 s22 { 1.0, 2, 3 }; // error: narrowing
|
| 63 |
S2 s23 { }; // OK: default to 0,0,0
|
| 64 |
```
|
| 65 |
-
- Otherwise, if `T` is a
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
initialization of an object from a class of the same type (
|
| 69 |
[[dcl.init]]).
|
| 70 |
- Otherwise, if `T` is a class type, constructors are considered. The
|
| 71 |
applicable constructors are enumerated and the best one is chosen
|
| 72 |
through overload resolution ([[over.match]], [[over.match.list]]).
|
|
@@ -100,15 +105,25 @@ follows:
|
|
| 100 |
};
|
| 101 |
S s1 = { 1, 2, 3.0 }; // OK: invoke #1
|
| 102 |
S s2 { 1.0, 2, 3 }; // error: narrowing
|
| 103 |
S s3 { }; // OK: invoke #2
|
| 104 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
- Otherwise, if `T` is a reference type, a prvalue temporary of the type
|
| 106 |
-
referenced by `T` is list-initialized
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
type
|
|
|
|
| 110 |
``` cpp
|
| 111 |
struct S {
|
| 112 |
S(std::initializer_list<double>); // #1
|
| 113 |
S(const std::string&); // #2
|
| 114 |
// ...
|
|
@@ -118,18 +133,10 @@ follows:
|
|
| 118 |
S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
|
| 119 |
const int& i1 = { 1 }; // OK
|
| 120 |
const int& i2 = { 1.1 }; // error: narrowing
|
| 121 |
const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
|
| 122 |
```
|
| 123 |
-
- Otherwise, if the initializer list has a single element, the object or
|
| 124 |
-
reference is initialized from that element; if a narrowing conversion
|
| 125 |
-
(see below) is required to convert the element to `T`, the program is
|
| 126 |
-
ill-formed.
|
| 127 |
-
``` cpp
|
| 128 |
-
int x1 {2}; // OK
|
| 129 |
-
int x2 {2.0}; // error: narrowing
|
| 130 |
-
```
|
| 131 |
- Otherwise, if the initializer list has no elements, the object is
|
| 132 |
value-initialized.
|
| 133 |
``` cpp
|
| 134 |
int** pp {}; // initialized to null pointer
|
| 135 |
```
|
|
@@ -164,17 +171,19 @@ ordering holds regardless of the semantics of the initialization; for
|
|
| 164 |
example, it applies when the elements of the *initializer-list* are
|
| 165 |
interpreted as arguments of a constructor call, even though ordinarily
|
| 166 |
there are no sequencing constraints on the arguments of a call.
|
| 167 |
|
| 168 |
An object of type `std::initializer_list<E>` is constructed from an
|
| 169 |
-
initializer list as if the implementation allocated
|
| 170 |
-
elements of type `E`, where N is the number of elements in the
|
| 171 |
initializer list. Each element of that array is copy-initialized with
|
| 172 |
the corresponding element of the initializer list, and the
|
| 173 |
`std::initializer_list<E>` object is constructed to refer to that array.
|
| 174 |
-
|
| 175 |
-
the
|
|
|
|
|
|
|
| 176 |
|
| 177 |
``` cpp
|
| 178 |
struct X {
|
| 179 |
X(std::initializer_list<double> v);
|
| 180 |
};
|
|
@@ -183,35 +192,47 @@ X x{ 1,2,3 };
|
|
| 183 |
|
| 184 |
The initialization will be implemented in a way roughly equivalent to
|
| 185 |
this:
|
| 186 |
|
| 187 |
``` cpp
|
| 188 |
-
double __a[3] = {double{1}, double{2}, double{3}};
|
| 189 |
X x(std::initializer_list<double>(__a, __a+3));
|
| 190 |
```
|
| 191 |
|
| 192 |
assuming that the implementation can construct an `initializer_list`
|
| 193 |
object with a pair of pointers.
|
| 194 |
|
| 195 |
-
The
|
| 196 |
-
|
|
|
|
|
|
|
| 197 |
|
| 198 |
``` cpp
|
| 199 |
typedef std::complex<double> cmplx;
|
| 200 |
std::vector<cmplx> v1 = { 1, 2, 3 };
|
| 201 |
|
| 202 |
void f() {
|
| 203 |
std::vector<cmplx> v2{ 1, 2, 3 };
|
| 204 |
std::initializer_list<int> i3 = { 1, 2, 3 };
|
| 205 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
```
|
| 207 |
|
| 208 |
-
For `v1` and `v2`, the `initializer_list` object
|
| 209 |
-
`{ 1, 2, 3 }`
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
A *narrowing conversion* is an implicit conversion
|
| 215 |
|
| 216 |
- from a floating-point type to an integer type, or
|
| 217 |
- from `long double` to `double` or `float`, or from `double` to
|
|
@@ -222,13 +243,12 @@ A *narrowing conversion* is an implicit conversion
|
|
| 222 |
type, except where the source is a constant expression and the actual
|
| 223 |
value after conversion will fit into the target type and will produce
|
| 224 |
the original value when converted back to the original type, or
|
| 225 |
- from an integer type or unscoped enumeration type to an integer type
|
| 226 |
that cannot represent all the values of the original type, except
|
| 227 |
-
where the source is a constant expression
|
| 228 |
-
|
| 229 |
-
value when converted back to the original type.
|
| 230 |
|
| 231 |
As indicated above, such conversions are not allowed at the top level in
|
| 232 |
list-initializations.
|
| 233 |
|
| 234 |
``` cpp
|
|
@@ -266,12 +286,13 @@ int a[] =
|
|
| 266 |
[basic.lookup.udir]: basic.md#basic.lookup.udir
|
| 267 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 268 |
[basic.lval]: basic.md#basic.lval
|
| 269 |
[basic.namespace]: #basic.namespace
|
| 270 |
[basic.scope]: basic.md#basic.scope
|
| 271 |
-
[basic.scope.
|
| 272 |
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
|
|
|
| 273 |
[basic.scope.proto]: basic.md#basic.scope.proto
|
| 274 |
[basic.start]: basic.md#basic.start
|
| 275 |
[basic.start.init]: basic.md#basic.start.init
|
| 276 |
[basic.stc]: basic.md#basic.stc
|
| 277 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
|
@@ -295,11 +316,10 @@ int a[] =
|
|
| 295 |
[class.inhctor]: special.md#class.inhctor
|
| 296 |
[class.init]: special.md#class.init
|
| 297 |
[class.mem]: class.md#class.mem
|
| 298 |
[class.member.lookup]: class.md#class.member.lookup
|
| 299 |
[class.mfct]: class.md#class.mfct
|
| 300 |
-
[class.mfct.non-static]: class.md#class.mfct.non-static
|
| 301 |
[class.name]: class.md#class.name
|
| 302 |
[class.qual]: basic.md#class.qual
|
| 303 |
[class.static]: class.md#class.static
|
| 304 |
[class.static.data]: class.md#class.static.data
|
| 305 |
[class.temporary]: special.md#class.temporary
|
|
@@ -307,19 +327,21 @@ int a[] =
|
|
| 307 |
[class.union]: class.md#class.union
|
| 308 |
[class.virtual]: class.md#class.virtual
|
| 309 |
[conv]: conv.md#conv
|
| 310 |
[conv.array]: conv.md#conv.array
|
| 311 |
[conv.func]: conv.md#conv.func
|
|
|
|
| 312 |
[conv.lval]: conv.md#conv.lval
|
| 313 |
[conv.prom]: conv.md#conv.prom
|
| 314 |
[conv.ptr]: conv.md#conv.ptr
|
| 315 |
[dcl.align]: #dcl.align
|
| 316 |
[dcl.ambig.res]: #dcl.ambig.res
|
| 317 |
[dcl.array]: #dcl.array
|
| 318 |
[dcl.asm]: #dcl.asm
|
| 319 |
[dcl.attr]: #dcl.attr
|
| 320 |
[dcl.attr.depend]: #dcl.attr.depend
|
|
|
|
| 321 |
[dcl.attr.grammar]: #dcl.attr.grammar
|
| 322 |
[dcl.attr.noreturn]: #dcl.attr.noreturn
|
| 323 |
[dcl.constexpr]: #dcl.constexpr
|
| 324 |
[dcl.dcl]: #dcl.dcl
|
| 325 |
[dcl.decl]: #dcl.decl
|
|
@@ -357,15 +379,18 @@ int a[] =
|
|
| 357 |
[except.throw]: except.md#except.throw
|
| 358 |
[expr]: expr.md#expr
|
| 359 |
[expr.alignof]: expr.md#expr.alignof
|
| 360 |
[expr.ass]: expr.md#expr.ass
|
| 361 |
[expr.call]: expr.md#expr.call
|
|
|
|
|
|
|
|
|
|
| 362 |
[expr.const]: expr.md#expr.const
|
| 363 |
[expr.const.cast]: expr.md#expr.const.cast
|
| 364 |
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 365 |
[expr.new]: expr.md#expr.new
|
| 366 |
-
[expr.prim]: expr.md#expr.prim
|
| 367 |
[expr.ref]: expr.md#expr.ref
|
| 368 |
[expr.static.cast]: expr.md#expr.static.cast
|
| 369 |
[expr.sub]: expr.md#expr.sub
|
| 370 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 371 |
[expr.unary]: expr.md#expr.unary
|
|
@@ -376,19 +401,19 @@ int a[] =
|
|
| 376 |
[intro.multithread]: intro.md#intro.multithread
|
| 377 |
[lex.charset]: lex.md#lex.charset
|
| 378 |
[lex.digraph]: lex.md#lex.digraph
|
| 379 |
[lex.key]: lex.md#lex.key
|
| 380 |
[lex.name]: lex.md#lex.name
|
|
|
|
| 381 |
[namespace.alias]: #namespace.alias
|
| 382 |
[namespace.def]: #namespace.def
|
| 383 |
[namespace.memdef]: #namespace.memdef
|
| 384 |
[namespace.qual]: basic.md#namespace.qual
|
| 385 |
[namespace.udecl]: #namespace.udecl
|
| 386 |
[namespace.udir]: #namespace.udir
|
| 387 |
[namespace.unnamed]: #namespace.unnamed
|
| 388 |
[over]: over.md#over
|
| 389 |
-
[over.ics.rank]: over.md#over.ics.rank
|
| 390 |
[over.match]: over.md#over.match
|
| 391 |
[over.match.conv]: over.md#over.match.conv
|
| 392 |
[over.match.copy]: over.md#over.match.copy
|
| 393 |
[over.match.ctor]: over.md#over.match.ctor
|
| 394 |
[over.match.list]: over.md#over.match.list
|
|
@@ -404,11 +429,10 @@ int a[] =
|
|
| 404 |
[stmt.select]: stmt.md#stmt.select
|
| 405 |
[stmt.stmt]: stmt.md#stmt.stmt
|
| 406 |
[support.runtime]: language.md#support.runtime
|
| 407 |
[tab:simple.type.specifiers]: #tab:simple.type.specifiers
|
| 408 |
[temp]: temp.md#temp
|
| 409 |
-
[temp.arg]: temp.md#temp.arg
|
| 410 |
[temp.arg.type]: temp.md#temp.arg.type
|
| 411 |
[temp.class.spec]: temp.md#temp.class.spec
|
| 412 |
[temp.deduct.call]: temp.md#temp.deduct.call
|
| 413 |
[temp.dep]: temp.md#temp.dep
|
| 414 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
|
@@ -423,39 +447,35 @@ int a[] =
|
|
| 423 |
|
| 424 |
[^1]: The “implicit int” rule of C is no longer supported.
|
| 425 |
|
| 426 |
[^2]: The inline keyword has no effect on the linkage of a function.
|
| 427 |
|
| 428 |
-
[^3]:
|
| 429 |
-
conversion ([[conv.lval]]) if the corresponding copy-initialization
|
| 430 |
-
requires one.
|
| 431 |
-
|
| 432 |
-
[^4]: There is no special provision for a *decl-specifier-seq* that
|
| 433 |
lacks a *type-specifier* or that has a *type-specifier* that only
|
| 434 |
specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
|
| 435 |
supported.
|
| 436 |
|
| 437 |
-
[^
|
| 438 |
semantics for the enumeration type. It does not preclude an
|
| 439 |
expression of enumeration type from having a value that falls
|
| 440 |
outside this range.
|
| 441 |
|
| 442 |
-
[^
|
| 443 |
linkage, they are effectively qualified by a name unique to their
|
| 444 |
translation unit and therefore can never be seen from any other
|
| 445 |
translation unit.
|
| 446 |
|
| 447 |
-
[^
|
| 448 |
unqualified.
|
| 449 |
|
| 450 |
-
[^
|
| 451 |
resolved by considering whether one member hides the other along
|
| 452 |
some paths ([[class.member.lookup]]). There is no such
|
| 453 |
disambiguation when considering the set of names found as a result
|
| 454 |
of following *using-directive*s.
|
| 455 |
|
| 456 |
-
[^
|
| 457 |
the corresponding sequence of declarations each with a single
|
| 458 |
declarator. That is
|
| 459 |
|
| 460 |
`T D1, D2, ... Dn;`
|
| 461 |
|
|
@@ -485,42 +505,40 @@ int a[] =
|
|
| 485 |
`auto i = 1, j = 2.0; \textrm{// error: deduced types for \tcode{i} and \tcode{j} do not match}`
|
| 486 |
as opposed to
|
| 487 |
`auto i = 1; \textrm{// OK: \tcode{i} deduced to have type \tcode{int}}`
|
| 488 |
`auto j = 2.0; \textrm{// OK: \tcode{j} deduced to have type \tcode{double}}`
|
| 489 |
|
| 490 |
-
[^
|
| 491 |
in function return types.
|
| 492 |
|
| 493 |
-
[^
|
| 494 |
to array of unknown bound of `T`” and where means any sequence of
|
| 495 |
“pointer to” and “array of” derived declarator types. This exclusion
|
| 496 |
applies to the parameters of the function, and if a parameter is a
|
| 497 |
pointer to function or pointer to member function then to its
|
| 498 |
parameters also, etc.
|
| 499 |
|
| 500 |
-
[^
|
| 501 |
comma (so the ellipsis will be parsed as part of the
|
| 502 |
*parameter-declaration-clause*) or by introducing a name for the
|
| 503 |
parameter (so the ellipsis will be parsed as part of the
|
| 504 |
*declarator-id*).
|
| 505 |
|
| 506 |
-
[^
|
| 507 |
declarations of pointers to functions, references to functions, or
|
| 508 |
`typedef` declarations.
|
| 509 |
|
| 510 |
-
[^
|
| 511 |
variables with names that are reserved to the implementation (
|
| 512 |
[[global.names]]). If a predefined variable is not odr-used (
|
| 513 |
[[basic.def.odr]]), its string value need not be present in the
|
| 514 |
program image.
|
| 515 |
|
| 516 |
-
[^
|
| 517 |
-
|
| 518 |
-
|
| 519 |
|
| 520 |
-
[^
|
| 521 |
nonetheless C++does not have zero length arrays.
|
| 522 |
|
| 523 |
-
[^
|
| 524 |
-
|
| 525 |
-
[^18]: This requires a conversion function ([[class.conv.fct]])
|
| 526 |
returning a reference type.
|
|
|
|
| 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]])
|
|
|
|
| 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 |
};
|
| 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]]).
|
|
|
|
| 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 |
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 |
```
|
|
|
|
| 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 |
};
|
|
|
|
| 192 |
|
| 193 |
The initialization will be implemented in a way roughly equivalent to
|
| 194 |
this:
|
| 195 |
|
| 196 |
``` cpp
|
| 197 |
+
const double __a[3] = {double{1}, double{2}, double{3}};
|
| 198 |
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() {
|
| 214 |
std::vector<cmplx> v2{ 1, 2, 3 };
|
| 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
|
|
|
|
| 243 |
type, except where the source is a constant expression and the actual
|
| 244 |
value after conversion will fit into the target type and will produce
|
| 245 |
the original value when converted back to the original type, or
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 379 |
[except.throw]: except.md#except.throw
|
| 380 |
[expr]: expr.md#expr
|
| 381 |
[expr.alignof]: expr.md#expr.alignof
|
| 382 |
[expr.ass]: expr.md#expr.ass
|
| 383 |
[expr.call]: expr.md#expr.call
|
| 384 |
+
[expr.cast]: expr.md#expr.cast
|
| 385 |
+
[expr.comma]: expr.md#expr.comma
|
| 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
|
|
|
|
| 401 |
[intro.multithread]: intro.md#intro.multithread
|
| 402 |
[lex.charset]: lex.md#lex.charset
|
| 403 |
[lex.digraph]: lex.md#lex.digraph
|
| 404 |
[lex.key]: lex.md#lex.key
|
| 405 |
[lex.name]: lex.md#lex.name
|
| 406 |
+
[lex.string]: lex.md#lex.string
|
| 407 |
[namespace.alias]: #namespace.alias
|
| 408 |
[namespace.def]: #namespace.def
|
| 409 |
[namespace.memdef]: #namespace.memdef
|
| 410 |
[namespace.qual]: basic.md#namespace.qual
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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.
|
| 456 |
|
| 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 |
|
|
|
|
| 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.
|