- tmp/tmpr2az0aq1/{from.md → to.md} +319 -234
tmp/tmpr2az0aq1/{from.md → to.md}
RENAMED
|
@@ -92,11 +92,13 @@ duration is described in [[basic.start]] and
|
|
| 92 |
A declaration D of a variable with linkage shall not have an
|
| 93 |
*initializer* if D inhabits a block scope.
|
| 94 |
|
| 95 |
To *zero-initialize* an object or reference of type `T` means:
|
| 96 |
|
| 97 |
-
- if `T` is
|
|
|
|
|
|
|
| 98 |
initialized to the value obtained by converting the integer literal
|
| 99 |
`0` (zero) to `T`;[^5]
|
| 100 |
- if `T` is a (possibly cv-qualified) non-union class type, its padding
|
| 101 |
bits [[term.padding.bits]] are initialized to zero bits and each
|
| 102 |
non-static data member, each non-virtual base class subobject, and, if
|
|
@@ -114,11 +116,14 @@ To *default-initialize* an object of type `T` means:
|
|
| 114 |
are considered. The applicable constructors are enumerated
|
| 115 |
[[over.match.ctor]], and the best one for the *initializer* `()` is
|
| 116 |
chosen through overload resolution [[over.match]]. The constructor
|
| 117 |
thus selected is called, with an empty argument list, to initialize
|
| 118 |
the object.
|
| 119 |
-
- If `T` is an array type,
|
|
|
|
|
|
|
|
|
|
| 120 |
- Otherwise, no initialization is performed.
|
| 121 |
|
| 122 |
A class type `T` is *const-default-constructible* if
|
| 123 |
default-initialization of `T` would invoke a user-provided constructor
|
| 124 |
of `T` (not inherited from a base class) or if
|
|
@@ -133,37 +138,36 @@ of `T` (not inherited from a base class) or if
|
|
| 133 |
member has a default member initializer, and
|
| 134 |
- each potentially constructed base class of `T` is
|
| 135 |
const-default-constructible.
|
| 136 |
|
| 137 |
If a program calls for the default-initialization of an object of a
|
| 138 |
-
const-qualified type `T`, `T` shall be
|
| 139 |
-
class type or array thereof.
|
| 140 |
|
| 141 |
To *value-initialize* an object of type `T` means:
|
| 142 |
|
| 143 |
-
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
-
|
| 151 |
-
- otherwise, the object is zero-initialized.
|
| 152 |
|
| 153 |
A program that calls for default-initialization or value-initialization
|
| 154 |
of an entity of reference type is ill-formed.
|
| 155 |
|
| 156 |
-
[*Note 4*: For every object
|
| 157 |
initialization [[basic.start.static]] is performed at program startup
|
| 158 |
before any other initialization takes place. In some cases, additional
|
| 159 |
initialization is done later. — *end note*]
|
| 160 |
|
| 161 |
If no initializer is specified for an object, the object is
|
| 162 |
default-initialized.
|
| 163 |
|
| 164 |
-
If the entity being initialized does not have class type, the
|
| 165 |
*expression-list* in a parenthesized initializer shall be a single
|
| 166 |
expression.
|
| 167 |
|
| 168 |
The initialization that occurs in the `=` form of a
|
| 169 |
*brace-or-equal-initializer* or *condition* [[stmt.select]], as well as
|
|
@@ -185,14 +189,14 @@ The initialization that occurs
|
|
| 185 |
- in the *braced-init-list* form of a *condition*
|
| 186 |
|
| 187 |
is called *direct-initialization*.
|
| 188 |
|
| 189 |
The semantics of initializers are as follows. The *destination type* is
|
| 190 |
-
the type of the object or reference being initialized and
|
| 191 |
-
type* is the type of the initializer expression. If the
|
| 192 |
-
not a single (possibly parenthesized) expression, the
|
| 193 |
-
defined.
|
| 194 |
|
| 195 |
- If the initializer is a (non-parenthesized) *braced-init-list* or is
|
| 196 |
`=` *braced-init-list*, the object or reference is list-initialized
|
| 197 |
[[dcl.init.list]].
|
| 198 |
- If the destination type is a reference type, see [[dcl.init.ref]].
|
|
@@ -207,35 +211,35 @@ defined.
|
|
| 207 |
X a();
|
| 208 |
```
|
| 209 |
|
| 210 |
is not the declaration of an object of class `X`, but the declaration
|
| 211 |
of a function taking no arguments and returning an `X`. The form `()`
|
| 212 |
-
|
| 213 |
[[expr.new]], [[expr.type.conv]], [[class.base.init]].
|
| 214 |
— *end note*]
|
| 215 |
- Otherwise, if the destination type is an array, the object is
|
| 216 |
-
initialized as follows.
|
|
|
|
| 217 |
*expression-list*. If the destination type is an array of unknown
|
| 218 |
bound, it is defined as having k elements. Let n denote the array size
|
| 219 |
after this potential adjustment. If k is greater than n, the program
|
| 220 |
is ill-formed. Otherwise, the iᵗʰ array element is copy-initialized
|
| 221 |
with xᵢ for each 1 ≤ i ≤ k, and value-initialized for each k < i ≤ n.
|
| 222 |
For each 1 ≤ i < j ≤ n, every value computation and side effect
|
| 223 |
associated with the initialization of the iᵗʰ element of the array is
|
| 224 |
sequenced before those associated with the initialization of the jᵗʰ
|
| 225 |
element.
|
| 226 |
-
- Otherwise, if the destination type is a
|
| 227 |
-
type:
|
| 228 |
- If the initializer expression is a prvalue and the cv-unqualified
|
| 229 |
-
version of the source type is the same
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
- Otherwise, if the initialization is direct-initialization, or if it
|
| 234 |
is copy-initialization where the cv-unqualified version of the
|
| 235 |
-
source type is the same
|
| 236 |
-
|
| 237 |
constructors are enumerated [[over.match.ctor]], and the best one is
|
| 238 |
chosen through overload resolution [[over.match]]. Then:
|
| 239 |
- If overload resolution is successful, the selected constructor is
|
| 240 |
called to initialize the object, with the initializer expression
|
| 241 |
or *expression-list* as its argument(s).
|
|
@@ -251,11 +255,11 @@ defined.
|
|
| 251 |
value computation and side effect associated with the
|
| 252 |
initialization of eᵢ is sequenced before those associated with the
|
| 253 |
initialization of eⱼ.
|
| 254 |
\[*Note 7*:
|
| 255 |
By contrast with direct-list-initialization, narrowing conversions
|
| 256 |
-
[[dcl.init.list]]
|
| 257 |
temporary object bound to a reference does not have its lifetime
|
| 258 |
extended [[class.temporary]], and there is no brace elision.
|
| 259 |
\[*Example 3*:
|
| 260 |
``` cpp
|
| 261 |
struct A {
|
|
@@ -299,16 +303,16 @@ defined.
|
|
| 299 |
- Otherwise, if the initialization is direct-initialization, the source
|
| 300 |
type is `std::nullptr_t`, and the destination type is `bool`, the
|
| 301 |
initial value of the object being initialized is `false`.
|
| 302 |
- Otherwise, the initial value of the object being initialized is the
|
| 303 |
(possibly converted) value of the initializer expression. A standard
|
| 304 |
-
conversion sequence [[conv]]
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
\[*Note 8*:
|
| 311 |
An expression of type “*cv1* `T`” can initialize an object of type
|
| 312 |
“*cv2* `T`” independently of the cv-qualifiers *cv1* and *cv2*.
|
| 313 |
``` cpp
|
| 314 |
int a;
|
|
@@ -371,11 +375,11 @@ An *aggregate* is an array or a class [[class]] with
|
|
| 371 |
- no private or protected direct base classes [[class.access.base]], and
|
| 372 |
- no virtual functions [[class.virtual]] or virtual base classes
|
| 373 |
[[class.mi]].
|
| 374 |
|
| 375 |
[*Note 1*: Aggregate initialization does not allow accessing protected
|
| 376 |
-
and private base class’ members
|
| 377 |
|
| 378 |
The *elements* of an aggregate are:
|
| 379 |
|
| 380 |
- for an array, the array elements in increasing subscript order, or
|
| 381 |
- for a class, the direct base classes in declaration order, followed by
|
|
@@ -391,13 +395,13 @@ initialized elements* of the aggregate are determined as follows:
|
|
| 391 |
*designated-initializer-list*, the aggregate shall be of class type,
|
| 392 |
the *identifier* in each *designator* shall name a direct non-static
|
| 393 |
data member of the class, and the explicitly initialized elements of
|
| 394 |
the aggregate are the elements that are, or contain, those members.
|
| 395 |
- If the initializer list is a brace-enclosed *initializer-list*, the
|
| 396 |
-
explicitly initialized elements of the aggregate are
|
| 397 |
-
|
| 398 |
-
|
| 399 |
- Otherwise, the initializer list must be `{}`, and there are no
|
| 400 |
explicitly initialized elements.
|
| 401 |
|
| 402 |
For each explicitly initialized element:
|
| 403 |
|
|
@@ -417,21 +421,26 @@ For each explicitly initialized element:
|
|
| 417 |
} c = { .a = 1, .x = 3 };
|
| 418 |
```
|
| 419 |
|
| 420 |
initializes `c.a` with 1 and `c.x` with 3.
|
| 421 |
— *end example*]
|
| 422 |
-
- Otherwise, the
|
| 423 |
-
*initializer-
|
| 424 |
*brace-or-equal-initializer* of the corresponding
|
| 425 |
*designated-initializer-clause*. If that initializer is of the form
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
*designated-initializer-clause*, its form determines whether
|
| 431 |
-
copy-initialization or direct-initialization is
|
| 432 |
performed. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
\[*Note 3*: If an initializer is itself an initializer list, the
|
| 434 |
element is list-initialized, which will result in a recursive
|
| 435 |
application of the rules in this subclause if the element is an
|
| 436 |
aggregate. — *end note*]
|
| 437 |
\[*Example 2*:
|
|
@@ -490,19 +499,11 @@ struct S { int a; const char* b; int c; int d = b[a]; };
|
|
| 490 |
S ss = { 1, "asdf" };
|
| 491 |
```
|
| 492 |
|
| 493 |
initializes `ss.a` with 1, `ss.b` with `"asdf"`, `ss.c` with the value
|
| 494 |
of an expression of the form `int{}` (that is, `0`), and `ss.d` with the
|
| 495 |
-
value of `ss.b[ss.a]` (that is, `'s'`)
|
| 496 |
-
|
| 497 |
-
``` cpp
|
| 498 |
-
struct X { int i, j, k = 42; };
|
| 499 |
-
X a[] = { 1, 2, 3, 4, 5, 6 };
|
| 500 |
-
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
|
| 501 |
-
```
|
| 502 |
-
|
| 503 |
-
`a` and `b` have the same value
|
| 504 |
|
| 505 |
``` cpp
|
| 506 |
struct A {
|
| 507 |
string a;
|
| 508 |
int b = 42;
|
|
@@ -524,21 +525,21 @@ associated with a given element are sequenced before those of any
|
|
| 524 |
element that follows it in order.
|
| 525 |
|
| 526 |
An aggregate that is a class can also be initialized with a single
|
| 527 |
expression not enclosed in braces, as described in [[dcl.init]].
|
| 528 |
|
| 529 |
-
The destructor for each element of class type
|
| 530 |
-
[[class.dtor]] from the context
|
| 531 |
-
occurs.
|
| 532 |
|
| 533 |
[*Note 4*: This provision ensures that destructors can be called for
|
| 534 |
fully-constructed subobjects in case an exception is thrown
|
| 535 |
[[except.ctor]]. — *end note*]
|
| 536 |
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
|
| 541 |
[*Example 4*:
|
| 542 |
|
| 543 |
``` cpp
|
| 544 |
int x[] = { 1, 3, 5 };
|
|
@@ -547,10 +548,24 @@ int x[] = { 1, 3, 5 };
|
|
| 547 |
declares and initializes `x` as a one-dimensional array that has three
|
| 548 |
elements since no size was specified and there are three initializers.
|
| 549 |
|
| 550 |
— *end example*]
|
| 551 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
An array of unknown bound shall not be initialized with an empty
|
| 553 |
*braced-init-list* `{}`.[^6]
|
| 554 |
|
| 555 |
[*Note 5*:
|
| 556 |
|
|
@@ -558,11 +573,11 @@ A default member initializer does not determine the bound for a member
|
|
| 558 |
array of unknown bound. Since the default member initializer is ignored
|
| 559 |
if a suitable *mem-initializer* is present [[class.base.init]], the
|
| 560 |
default member initializer is not considered to initialize the array of
|
| 561 |
unknown bound.
|
| 562 |
|
| 563 |
-
[*Example
|
| 564 |
|
| 565 |
``` cpp
|
| 566 |
struct S {
|
| 567 |
int y[] = { 0 }; // error: non-static data member of incomplete type
|
| 568 |
};
|
|
@@ -575,11 +590,11 @@ struct S {
|
|
| 575 |
[*Note 6*:
|
| 576 |
|
| 577 |
Static data members, non-static data members of anonymous union members,
|
| 578 |
and unnamed bit-fields are not considered elements of the aggregate.
|
| 579 |
|
| 580 |
-
[*Example
|
| 581 |
|
| 582 |
``` cpp
|
| 583 |
struct A {
|
| 584 |
int i;
|
| 585 |
static int s;
|
|
@@ -595,23 +610,10 @@ unnamed bit-field before it.
|
|
| 595 |
|
| 596 |
— *end example*]
|
| 597 |
|
| 598 |
— *end note*]
|
| 599 |
|
| 600 |
-
An *initializer-list* is ill-formed if the number of
|
| 601 |
-
*initializer-clause*s exceeds the number of elements of the aggregate.
|
| 602 |
-
|
| 603 |
-
[*Example 7*:
|
| 604 |
-
|
| 605 |
-
``` cpp
|
| 606 |
-
char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
|
| 607 |
-
```
|
| 608 |
-
|
| 609 |
-
is ill-formed.
|
| 610 |
-
|
| 611 |
-
— *end example*]
|
| 612 |
-
|
| 613 |
If a member has a default member initializer and a potentially-evaluated
|
| 614 |
subexpression thereof is an aggregate initialization that would use that
|
| 615 |
default member initializer, the program is ill-formed.
|
| 616 |
|
| 617 |
[*Example 8*:
|
|
@@ -630,42 +632,15 @@ struct B {
|
|
| 630 |
};
|
| 631 |
```
|
| 632 |
|
| 633 |
— *end example*]
|
| 634 |
|
| 635 |
-
If an aggregate class `C` contains a subaggregate element `e` with no
|
| 636 |
-
elements, the *initializer-clause* for `e` shall not be omitted from an
|
| 637 |
-
*initializer-list* for an object of type `C` unless the
|
| 638 |
-
*initializer-clause*s for all elements of `C` following `e` are also
|
| 639 |
-
omitted.
|
| 640 |
-
|
| 641 |
-
[*Example 9*:
|
| 642 |
-
|
| 643 |
-
``` cpp
|
| 644 |
-
struct S { } s;
|
| 645 |
-
struct A {
|
| 646 |
-
S s1;
|
| 647 |
-
int i1;
|
| 648 |
-
S s2;
|
| 649 |
-
int i2;
|
| 650 |
-
S s3;
|
| 651 |
-
int i3;
|
| 652 |
-
} a = {
|
| 653 |
-
{ }, // Required initialization
|
| 654 |
-
0,
|
| 655 |
-
s, // Required initialization
|
| 656 |
-
0
|
| 657 |
-
}; // Initialization not required for A::s3 because A::i3 is also not initialized
|
| 658 |
-
```
|
| 659 |
-
|
| 660 |
-
— *end example*]
|
| 661 |
-
|
| 662 |
When initializing a multidimensional array, the *initializer-clause*s
|
| 663 |
initialize the elements with the last (rightmost) index of the array
|
| 664 |
varying the fastest [[dcl.array]].
|
| 665 |
|
| 666 |
-
[*Example
|
| 667 |
|
| 668 |
``` cpp
|
| 669 |
int x[2][2] = { 3, 1, 4, 2 };
|
| 670 |
```
|
| 671 |
|
|
@@ -681,23 +656,77 @@ float y[4][3] = {
|
|
| 681 |
initializes the first column of `y` (regarded as a two-dimensional
|
| 682 |
array) and leaves the rest zero.
|
| 683 |
|
| 684 |
— *end example*]
|
| 685 |
|
| 686 |
-
|
| 687 |
-
*
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 696 |
|
| 697 |
[*Example 11*:
|
| 698 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 699 |
``` cpp
|
| 700 |
float y[4][3] = {
|
| 701 |
{ 1, 3, 5 },
|
| 702 |
{ 2, 4, 6 },
|
| 703 |
{ 3, 5, 7 },
|
|
@@ -705,15 +734,15 @@ float y[4][3] = {
|
|
| 705 |
```
|
| 706 |
|
| 707 |
is a completely-braced initialization: 1, 3, and 5 initialize the first
|
| 708 |
row of the array `y[0]`, namely `y[0][0]`, `y[0][1]`, and `y[0][2]`.
|
| 709 |
Likewise the next two lines initialize `y[1]` and `y[2]`. The
|
| 710 |
-
initializer ends early and therefore `y[3]`s elements are initialized
|
| 711 |
-
if explicitly initialized with an expression of the form `float()`,
|
| 712 |
-
is, are initialized with `0.0`. In the following example, braces in
|
| 713 |
-
*initializer-list* are elided; however the *initializer-list* has
|
| 714 |
-
same effect as the completely-braced *initializer-list* of the above
|
| 715 |
example,
|
| 716 |
|
| 717 |
``` cpp
|
| 718 |
float y[4][3] = {
|
| 719 |
1, 3, 5, 2, 4, 6, 3, 5, 7
|
|
@@ -724,22 +753,39 @@ The initializer for `y` begins with a left brace, but the one for `y[0]`
|
|
| 724 |
does not, therefore three elements from the list are used. Likewise the
|
| 725 |
next three are taken successively for `y[1]` and `y[2]`.
|
| 726 |
|
| 727 |
— *end example*]
|
| 728 |
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 741 |
|
| 742 |
``` cpp
|
| 743 |
struct A {
|
| 744 |
int i;
|
| 745 |
operator int();
|
|
@@ -756,23 +802,24 @@ Braces are elided around the *initializer-clause* for `b.a1.i`. `b.a1.i`
|
|
| 756 |
is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
|
| 757 |
initialized with whatever `a.operator int()` returns.
|
| 758 |
|
| 759 |
— *end example*]
|
| 760 |
|
| 761 |
-
[*Note
|
| 762 |
elements of a class type with a user-declared constructor
|
| 763 |
[[class.ctor]]. Initialization of these aggregate objects is described
|
| 764 |
in [[class.expl.init]]. — *end note*]
|
| 765 |
|
| 766 |
-
[*Note
|
| 767 |
-
duration is static or dynamic is specified in
|
| 768 |
-
[[basic.start.
|
|
|
|
| 769 |
|
| 770 |
When a union is initialized with an initializer list, there shall not be
|
| 771 |
more than one explicitly initialized element.
|
| 772 |
|
| 773 |
-
[*Example
|
| 774 |
|
| 775 |
``` cpp
|
| 776 |
union u { int a; const char* b; };
|
| 777 |
u a = { 1 };
|
| 778 |
u b = a;
|
|
@@ -783,11 +830,11 @@ u f = { .b = "asdf" };
|
|
| 783 |
u g = { .a = 1, .b = "asdf" }; // error
|
| 784 |
```
|
| 785 |
|
| 786 |
— *end example*]
|
| 787 |
|
| 788 |
-
[*Note
|
| 789 |
*initializer-clause* for a union member can be omitted if the union is a
|
| 790 |
member of another aggregate. — *end note*]
|
| 791 |
|
| 792 |
### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
|
| 793 |
|
|
@@ -858,11 +905,11 @@ void f() {
|
|
| 858 |
|
| 859 |
A reference cannot be changed to refer to another object after
|
| 860 |
initialization.
|
| 861 |
|
| 862 |
[*Note 1*: Assignment to a reference assigns to the object referred to
|
| 863 |
-
by the reference [[expr.
|
| 864 |
|
| 865 |
Argument passing [[expr.call]] and function value return [[stmt.return]]
|
| 866 |
are initializations.
|
| 867 |
|
| 868 |
The initializer can be omitted for a reference only in a parameter
|
|
@@ -935,23 +982,25 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
|
|
| 935 |
double& rd3 = i; // error: type mismatch and reference not const
|
| 936 |
```
|
| 937 |
|
| 938 |
— *end example*]
|
| 939 |
- Otherwise, if the initializer expression
|
| 940 |
-
- is an rvalue (but not a bit-field) or
|
| 941 |
-
`T1`” is reference-compatible with “*cv2* `T2`”, or
|
| 942 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 943 |
-
reference-related to `T2`, and can be converted to an rvalue
|
| 944 |
-
|
| 945 |
-
reference-compatible with “*cv3* `T3`” (see
|
|
|
|
| 946 |
|
| 947 |
then the initializer expression in the first case and the converted
|
| 948 |
expression in the second case is called the converted initializer. If
|
| 949 |
-
the converted initializer is a prvalue, its type
|
| 950 |
-
|
| 951 |
-
|
| 952 |
-
|
|
|
|
| 953 |
\[*Example 5*:
|
| 954 |
``` cpp
|
| 955 |
struct A { };
|
| 956 |
struct B : A { } b;
|
| 957 |
extern B f();
|
|
@@ -963,37 +1012,40 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
|
|
| 963 |
} x;
|
| 964 |
const A& r = x; // binds to the A subobject of the result of the conversion
|
| 965 |
int i2 = 42;
|
| 966 |
int&& rri = static_cast<int&&>(i2); // binds directly to i2
|
| 967 |
B&& rrb = x; // binds directly to the result of operator B
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 968 |
```
|
| 969 |
|
| 970 |
— *end example*]
|
| 971 |
-
- Otherwise
|
| 972 |
-
- If `T1` or `T2` is a class type
|
| 973 |
-
|
| 974 |
-
|
| 975 |
-
user-defined conversion
|
| 976 |
[[dcl.init]], [[over.match.copy]], [[over.match.conv]]; the program
|
| 977 |
is ill-formed if the corresponding non-reference copy-initialization
|
| 978 |
-
would be ill-formed. The result of the call to the conversion
|
| 979 |
function, as described for the non-reference copy-initialization, is
|
| 980 |
-
then used to direct-initialize the reference
|
| 981 |
-
direct-initialization, user-defined conversions are not
|
|
|
|
| 982 |
- Otherwise, the initializer expression is implicitly converted to a
|
| 983 |
prvalue of type “`T1`”. The temporary materialization conversion is
|
| 984 |
applied, considering the type of the prvalue to be “*cv1* `T1`”, and
|
| 985 |
the reference is bound to the result.
|
| 986 |
|
| 987 |
-
If `T1` is reference-related to `T2`:
|
| 988 |
-
- *cv1* shall be the same cv-qualification as, or greater
|
| 989 |
-
cv-qualification than, *cv2*; and
|
| 990 |
-
- if the reference is an rvalue reference, the initializer expression
|
| 991 |
-
shall not be an lvalue. \[*Note 3*: This can be affected by whether
|
| 992 |
-
the initializer expression is move-eligible
|
| 993 |
-
[[expr.prim.id.unqual]]. — *end note*]
|
| 994 |
-
|
| 995 |
\[*Example 6*:
|
| 996 |
``` cpp
|
| 997 |
struct Banana { };
|
| 998 |
struct Enigma { operator const Banana(); };
|
| 999 |
struct Alaska { operator Banana&(); };
|
|
@@ -1002,32 +1054,33 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
|
|
| 1002 |
Banana &&banana1 = ConstBanana(); // error
|
| 1003 |
Banana &&banana2 = Enigma(); // error
|
| 1004 |
Banana &&banana3 = Alaska(); // error
|
| 1005 |
}
|
| 1006 |
|
| 1007 |
-
const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
|
| 1008 |
double&& rrd = 2; // rrd refers to temporary with value 2.0
|
| 1009 |
const volatile int cvi = 1;
|
| 1010 |
const int& r2 = cvi; // error: cv-qualifier dropped
|
| 1011 |
struct A { operator volatile int&(); } a;
|
| 1012 |
const int& r3 = a; // error: cv-qualifier dropped
|
| 1013 |
// from result of conversion function
|
| 1014 |
double d2 = 1.0;
|
| 1015 |
-
double&& rrd2 = d2; // error: initializer is lvalue of related type
|
| 1016 |
struct X { operator int&(); };
|
| 1017 |
-
int&& rri2 = X(); // error: result of conversion function is
|
|
|
|
| 1018 |
int i3 = 2;
|
| 1019 |
double&& rrd3 = i3; // rrd3 refers to temporary with value 2.0
|
| 1020 |
```
|
| 1021 |
|
| 1022 |
— *end example*]
|
| 1023 |
|
| 1024 |
In all cases except the last (i.e., implicitly converting the
|
| 1025 |
initializer expression to the referenced type), the reference is said to
|
| 1026 |
*bind directly* to the initializer expression.
|
| 1027 |
|
| 1028 |
-
[*Note
|
| 1029 |
bound to references. — *end note*]
|
| 1030 |
|
| 1031 |
### List-initialization <a id="dcl.init.list">[[dcl.init.list]]</a>
|
| 1032 |
|
| 1033 |
*List-initialization* is initialization of an object or reference from a
|
|
@@ -1037,27 +1090,29 @@ or *designated-initializer-clause*s of the *designated-initializer-list*
|
|
| 1037 |
are called the *elements* of the initializer list. An initializer list
|
| 1038 |
may be empty. List-initialization can occur in direct-initialization or
|
| 1039 |
copy-initialization contexts; list-initialization in a
|
| 1040 |
direct-initialization context is called *direct-list-initialization* and
|
| 1041 |
list-initialization in a copy-initialization context is called
|
| 1042 |
-
*copy-list-initialization*.
|
|
|
|
| 1043 |
|
| 1044 |
[*Note 1*:
|
| 1045 |
|
| 1046 |
List-initialization can be used
|
| 1047 |
|
| 1048 |
-
- as the initializer in a variable definition [[dcl.init]]
|
| 1049 |
-
- as the initializer in a *new-expression* [[expr.new]]
|
| 1050 |
-
- in a `return` statement [[stmt.return]]
|
| 1051 |
-
- as a *for-range-initializer* [[stmt.iter]]
|
| 1052 |
-
- as a function argument [[expr.call]]
|
| 1053 |
-
- as a
|
|
|
|
| 1054 |
- as an argument to a constructor invocation
|
| 1055 |
-
[[dcl.init]], [[expr.type.conv]]
|
| 1056 |
-
- as an initializer for a non-static data member [[class.mem]]
|
| 1057 |
-
- in a *mem-initializer* [[class.base.init]]
|
| 1058 |
-
- on the right-hand side of an assignment [[expr.
|
| 1059 |
|
| 1060 |
[*Example 1*:
|
| 1061 |
|
| 1062 |
``` cpp
|
| 1063 |
int a = {1};
|
|
@@ -1092,29 +1147,29 @@ The template `std::initializer_list` is not predefined; if a standard
|
|
| 1092 |
library declaration [[initializer.list.syn]], [[std.modules]] of
|
| 1093 |
`std::initializer_list` is not reachable from [[module.reach]] a use of
|
| 1094 |
`std::initializer_list` — even an implicit use in which the type is not
|
| 1095 |
named [[dcl.spec.auto]] — the program is ill-formed.
|
| 1096 |
|
| 1097 |
-
List-initialization of an object or reference of type `T` is
|
| 1098 |
-
follows:
|
| 1099 |
|
| 1100 |
-
- If the *braced-init-list* contains a *designated-initializer-list*
|
| 1101 |
-
`T` shall be an aggregate class. The
|
| 1102 |
-
*
|
| 1103 |
-
|
| 1104 |
-
members of `T`. Aggregate
|
| 1105 |
-
[[dcl.init.aggr]].
|
| 1106 |
\[*Example 2*:
|
| 1107 |
``` cpp
|
| 1108 |
struct A { int x; int y; int z; };
|
| 1109 |
A a{.y = 2, .x = 1}; // error: designator order does not match declaration order
|
| 1110 |
A b{.x = 1, .z = 2}; // OK, b.y initialized to 0
|
| 1111 |
```
|
| 1112 |
|
| 1113 |
— *end example*]
|
| 1114 |
- If `T` is an aggregate class and the initializer list has a single
|
| 1115 |
-
element of type *
|
| 1116 |
`T`, the object is initialized from that element (by
|
| 1117 |
copy-initialization for copy-list-initialization, or by
|
| 1118 |
direct-initialization for direct-list-initialization).
|
| 1119 |
- Otherwise, if `T` is a character array and the initializer list has a
|
| 1120 |
single element that is an appropriately-typed *string-literal*
|
|
@@ -1137,28 +1192,30 @@ follows:
|
|
| 1137 |
```
|
| 1138 |
|
| 1139 |
— *end example*]
|
| 1140 |
- Otherwise, if the initializer list has no elements and `T` is a class
|
| 1141 |
type with a default constructor, the object is value-initialized.
|
| 1142 |
-
- Otherwise, if `T` is a specialization of `std::initializer_list
|
| 1143 |
-
|
| 1144 |
- Otherwise, if `T` is a class type, constructors are considered. The
|
| 1145 |
applicable constructors are enumerated and the best one is chosen
|
| 1146 |
through overload resolution [[over.match]], [[over.match.list]]. If a
|
| 1147 |
narrowing conversion (see below) is required to convert any of the
|
| 1148 |
arguments, the program is ill-formed.
|
| 1149 |
\[*Example 4*:
|
| 1150 |
``` cpp
|
| 1151 |
struct S {
|
| 1152 |
S(std::initializer_list<double>); // #1
|
| 1153 |
S(std::initializer_list<int>); // #2
|
| 1154 |
-
S();
|
|
|
|
| 1155 |
// ...
|
| 1156 |
};
|
| 1157 |
S s1 = { 1.0, 2.0, 3.0 }; // invoke #1
|
| 1158 |
S s2 = { 1, 2, 3 }; // invoke #2
|
| 1159 |
-
S s3
|
|
|
|
| 1160 |
```
|
| 1161 |
|
| 1162 |
— *end example*]
|
| 1163 |
\[*Example 5*:
|
| 1164 |
``` cpp
|
|
@@ -1182,15 +1239,16 @@ follows:
|
|
| 1182 |
S s3 { }; // OK, invoke #2
|
| 1183 |
```
|
| 1184 |
|
| 1185 |
— *end example*]
|
| 1186 |
- Otherwise, if `T` is an enumeration with a fixed underlying type
|
| 1187 |
-
[[dcl.enum]] `U`, the *initializer-list* has a single element `v`
|
| 1188 |
-
can be implicitly converted to `U`, and the
|
| 1189 |
-
direct-list-initialization, the object is
|
| 1190 |
-
`T(v)` [[expr.type.conv]]; if a narrowing
|
| 1191 |
-
convert `v` to `U`, the program is
|
|
|
|
| 1192 |
\[*Example 7*:
|
| 1193 |
``` cpp
|
| 1194 |
enum byte : unsigned char { };
|
| 1195 |
byte b { 42 }; // OK
|
| 1196 |
byte c = { 42 }; // error
|
|
@@ -1207,12 +1265,13 @@ follows:
|
|
| 1207 |
enum class Handle : uint32_t { Invalid = 0 };
|
| 1208 |
Handle h { 42 }; // OK
|
| 1209 |
```
|
| 1210 |
|
| 1211 |
— *end example*]
|
| 1212 |
-
- Otherwise, if the initializer list
|
| 1213 |
-
and
|
|
|
|
| 1214 |
reference-related to `E`, the object or reference is initialized from
|
| 1215 |
that element (by copy-initialization for copy-list-initialization, or
|
| 1216 |
by direct-initialization for direct-list-initialization); if a
|
| 1217 |
narrowing conversion (see below) is required to convert the element to
|
| 1218 |
`T`, the program is ill-formed.
|
|
@@ -1248,10 +1307,13 @@ follows:
|
|
| 1248 |
const int (&iar)[2] = { 1, 2 }; // OK, iar is bound to temporary array
|
| 1249 |
|
| 1250 |
struct A { } a;
|
| 1251 |
struct B { explicit B(const A&); };
|
| 1252 |
const B& b2{a}; // error: cannot copy-list-initialize B temporary from A
|
|
|
|
|
|
|
|
|
|
| 1253 |
```
|
| 1254 |
|
| 1255 |
— *end example*]
|
| 1256 |
- Otherwise, if the initializer list has no elements, the object is
|
| 1257 |
value-initialized.
|
|
@@ -1299,49 +1361,75 @@ even though ordinarily there are no sequencing constraints on the
|
|
| 1299 |
arguments of a call. — *end note*]
|
| 1300 |
|
| 1301 |
An object of type `std::initializer_list<E>` is constructed from an
|
| 1302 |
initializer list as if the implementation generated and materialized
|
| 1303 |
[[conv.rval]] a prvalue of type “array of N `const E`”, where N is the
|
| 1304 |
-
number of elements in the initializer list
|
| 1305 |
-
|
| 1306 |
-
|
| 1307 |
-
to
|
|
|
|
| 1308 |
|
| 1309 |
[*Note 5*: A constructor or conversion function selected for the copy
|
| 1310 |
-
|
| 1311 |
initializer list. — *end note*]
|
| 1312 |
|
| 1313 |
If a narrowing conversion is required to initialize any of the elements,
|
| 1314 |
the program is ill-formed.
|
| 1315 |
|
| 1316 |
-
[*
|
|
|
|
| 1317 |
|
| 1318 |
-
|
| 1319 |
-
struct X {
|
| 1320 |
-
X(std::initializer_list<double> v);
|
| 1321 |
-
};
|
| 1322 |
-
X x{ 1,2,3 };
|
| 1323 |
-
```
|
| 1324 |
-
|
| 1325 |
-
The initialization will be implemented in a way roughly equivalent to
|
| 1326 |
-
this:
|
| 1327 |
-
|
| 1328 |
-
``` cpp
|
| 1329 |
-
const double __a[3] = {double{1}, double{2}, double{3}};
|
| 1330 |
-
X x(std::initializer_list<double>(__a, __a+3));
|
| 1331 |
-
```
|
| 1332 |
-
|
| 1333 |
-
assuming that the implementation can construct an `initializer_list`
|
| 1334 |
-
object with a pair of pointers.
|
| 1335 |
-
|
| 1336 |
-
— *end example*]
|
| 1337 |
-
|
| 1338 |
-
The array has the same lifetime as any other temporary object
|
| 1339 |
[[class.temporary]], except that initializing an `initializer_list`
|
| 1340 |
object from the array extends the lifetime of the array exactly like
|
| 1341 |
binding a reference to a temporary.
|
| 1342 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1343 |
[*Example 13*:
|
| 1344 |
|
| 1345 |
``` cpp
|
| 1346 |
typedef std::complex<double> cmplx;
|
| 1347 |
std::vector<cmplx> v1 = { 1, 2, 3 };
|
|
@@ -1365,22 +1453,19 @@ variable, so the array persists for the lifetime of the variable. For
|
|
| 1365 |
*ctor-initializer* as if by binding a temporary array to a reference
|
| 1366 |
member, so the program is ill-formed [[class.base.init]].
|
| 1367 |
|
| 1368 |
— *end example*]
|
| 1369 |
|
| 1370 |
-
[*Note 6*: The implementation is free to allocate the array in
|
| 1371 |
-
read-only memory if an explicit array with the same initializer can be
|
| 1372 |
-
so allocated. — *end note*]
|
| 1373 |
-
|
| 1374 |
A *narrowing conversion* is an implicit conversion
|
| 1375 |
|
| 1376 |
- from a floating-point type to an integer type, or
|
| 1377 |
- from a floating-point type `T` to another floating-point type whose
|
| 1378 |
floating-point conversion rank is neither greater than nor equal to
|
| 1379 |
-
that of `T`, except where the
|
| 1380 |
-
|
| 1381 |
-
|
|
|
|
| 1382 |
- from an integer type or unscoped enumeration type to a floating-point
|
| 1383 |
type, except where the source is a constant expression and the actual
|
| 1384 |
value after conversion will fit into the target type and will produce
|
| 1385 |
the original value when converted back to the original type, or
|
| 1386 |
- from an integer type or unscoped enumeration type to an integer type
|
|
|
|
| 92 |
A declaration D of a variable with linkage shall not have an
|
| 93 |
*initializer* if D inhabits a block scope.
|
| 94 |
|
| 95 |
To *zero-initialize* an object or reference of type `T` means:
|
| 96 |
|
| 97 |
+
- if `T` is `std::meta::info`, the object is initialized to a null
|
| 98 |
+
reflection value;
|
| 99 |
+
- if `T` is any other scalar type [[term.scalar.type]], the object is
|
| 100 |
initialized to the value obtained by converting the integer literal
|
| 101 |
`0` (zero) to `T`;[^5]
|
| 102 |
- if `T` is a (possibly cv-qualified) non-union class type, its padding
|
| 103 |
bits [[term.padding.bits]] are initialized to zero bits and each
|
| 104 |
non-static data member, each non-virtual base class subobject, and, if
|
|
|
|
| 116 |
are considered. The applicable constructors are enumerated
|
| 117 |
[[over.match.ctor]], and the best one for the *initializer* `()` is
|
| 118 |
chosen through overload resolution [[over.match]]. The constructor
|
| 119 |
thus selected is called, with an empty argument list, to initialize
|
| 120 |
the object.
|
| 121 |
+
- If `T` is an array type, the semantic constraints of
|
| 122 |
+
default-initializing a hypothetical element shall be met and each
|
| 123 |
+
element is default-initialized.
|
| 124 |
+
- If `T` is `std::meta::info`, the object is zero-initialized.
|
| 125 |
- Otherwise, no initialization is performed.
|
| 126 |
|
| 127 |
A class type `T` is *const-default-constructible* if
|
| 128 |
default-initialization of `T` would invoke a user-provided constructor
|
| 129 |
of `T` (not inherited from a base class) or if
|
|
|
|
| 138 |
member has a default member initializer, and
|
| 139 |
- each potentially constructed base class of `T` is
|
| 140 |
const-default-constructible.
|
| 141 |
|
| 142 |
If a program calls for the default-initialization of an object of a
|
| 143 |
+
const-qualified type `T`, `T` shall be `std::meta::info` or a
|
| 144 |
+
const-default-constructible class type, or array thereof.
|
| 145 |
|
| 146 |
To *value-initialize* an object of type `T` means:
|
| 147 |
|
| 148 |
+
- If `T` is a (possibly cv-qualified) class type [[class]], then let `C`
|
| 149 |
+
be the constructor selected to default-initialize the object, if any.
|
| 150 |
+
If `C` is not user-provided, the object is first zero-initialized. In
|
| 151 |
+
all cases, the object is then default-initialized.
|
| 152 |
+
- If `T` is an array type, the semantic constraints of
|
| 153 |
+
value-initializing a hypothetical element shall be met and each
|
| 154 |
+
element is value-initialized.
|
| 155 |
+
- Otherwise, the object is zero-initialized.
|
|
|
|
| 156 |
|
| 157 |
A program that calls for default-initialization or value-initialization
|
| 158 |
of an entity of reference type is ill-formed.
|
| 159 |
|
| 160 |
+
[*Note 4*: For every object with static storage duration, static
|
| 161 |
initialization [[basic.start.static]] is performed at program startup
|
| 162 |
before any other initialization takes place. In some cases, additional
|
| 163 |
initialization is done later. — *end note*]
|
| 164 |
|
| 165 |
If no initializer is specified for an object, the object is
|
| 166 |
default-initialized.
|
| 167 |
|
| 168 |
+
If the entity being initialized does not have class or array type, the
|
| 169 |
*expression-list* in a parenthesized initializer shall be a single
|
| 170 |
expression.
|
| 171 |
|
| 172 |
The initialization that occurs in the `=` form of a
|
| 173 |
*brace-or-equal-initializer* or *condition* [[stmt.select]], as well as
|
|
|
|
| 189 |
- in the *braced-init-list* form of a *condition*
|
| 190 |
|
| 191 |
is called *direct-initialization*.
|
| 192 |
|
| 193 |
The semantics of initializers are as follows. The *destination type* is
|
| 194 |
+
the cv-unqualified type of the object or reference being initialized and
|
| 195 |
+
the *source type* is the type of the initializer expression. If the
|
| 196 |
+
initializer is not a single (possibly parenthesized) expression, the
|
| 197 |
+
source type is not defined.
|
| 198 |
|
| 199 |
- If the initializer is a (non-parenthesized) *braced-init-list* or is
|
| 200 |
`=` *braced-init-list*, the object or reference is list-initialized
|
| 201 |
[[dcl.init.list]].
|
| 202 |
- If the destination type is a reference type, see [[dcl.init.ref]].
|
|
|
|
| 211 |
X a();
|
| 212 |
```
|
| 213 |
|
| 214 |
is not the declaration of an object of class `X`, but the declaration
|
| 215 |
of a function taking no arguments and returning an `X`. The form `()`
|
| 216 |
+
can appear in certain other initialization contexts
|
| 217 |
[[expr.new]], [[expr.type.conv]], [[class.base.init]].
|
| 218 |
— *end note*]
|
| 219 |
- Otherwise, if the destination type is an array, the object is
|
| 220 |
+
initialized as follows. The *initializer* shall be of the form `(`
|
| 221 |
+
*expression-list* `)`. Let x₁, …, xₖ be the elements of the
|
| 222 |
*expression-list*. If the destination type is an array of unknown
|
| 223 |
bound, it is defined as having k elements. Let n denote the array size
|
| 224 |
after this potential adjustment. If k is greater than n, the program
|
| 225 |
is ill-formed. Otherwise, the iᵗʰ array element is copy-initialized
|
| 226 |
with xᵢ for each 1 ≤ i ≤ k, and value-initialized for each k < i ≤ n.
|
| 227 |
For each 1 ≤ i < j ≤ n, every value computation and side effect
|
| 228 |
associated with the initialization of the iᵗʰ element of the array is
|
| 229 |
sequenced before those associated with the initialization of the jᵗʰ
|
| 230 |
element.
|
| 231 |
+
- Otherwise, if the destination type is a class type:
|
|
|
|
| 232 |
- If the initializer expression is a prvalue and the cv-unqualified
|
| 233 |
+
version of the source type is the same as the destination type, the
|
| 234 |
+
initializer expression is used to initialize the destination object.
|
| 235 |
+
\[*Example 2*: `T x = T(T(T()));` value-initializes `x`
|
| 236 |
+
[[basic.lval]], [[expr.type.conv]]. — *end example*]
|
| 237 |
- Otherwise, if the initialization is direct-initialization, or if it
|
| 238 |
is copy-initialization where the cv-unqualified version of the
|
| 239 |
+
source type is the same as or is derived from the class of the
|
| 240 |
+
destination type, constructors are considered. The applicable
|
| 241 |
constructors are enumerated [[over.match.ctor]], and the best one is
|
| 242 |
chosen through overload resolution [[over.match]]. Then:
|
| 243 |
- If overload resolution is successful, the selected constructor is
|
| 244 |
called to initialize the object, with the initializer expression
|
| 245 |
or *expression-list* as its argument(s).
|
|
|
|
| 255 |
value computation and side effect associated with the
|
| 256 |
initialization of eᵢ is sequenced before those associated with the
|
| 257 |
initialization of eⱼ.
|
| 258 |
\[*Note 7*:
|
| 259 |
By contrast with direct-list-initialization, narrowing conversions
|
| 260 |
+
[[dcl.init.list]] can appear, designators are not permitted, a
|
| 261 |
temporary object bound to a reference does not have its lifetime
|
| 262 |
extended [[class.temporary]], and there is no brace elision.
|
| 263 |
\[*Example 3*:
|
| 264 |
``` cpp
|
| 265 |
struct A {
|
|
|
|
| 303 |
- Otherwise, if the initialization is direct-initialization, the source
|
| 304 |
type is `std::nullptr_t`, and the destination type is `bool`, the
|
| 305 |
initial value of the object being initialized is `false`.
|
| 306 |
- Otherwise, the initial value of the object being initialized is the
|
| 307 |
(possibly converted) value of the initializer expression. A standard
|
| 308 |
+
conversion sequence [[conv]] is used to convert the initializer
|
| 309 |
+
expression to a prvalue of the destination type; no user-defined
|
| 310 |
+
conversions are considered. If the conversion cannot be done, the
|
| 311 |
+
initialization is ill-formed. When initializing a bit-field with a
|
| 312 |
+
value that it cannot represent, the resulting value of the bit-field
|
| 313 |
+
is *implementation-defined*.
|
| 314 |
\[*Note 8*:
|
| 315 |
An expression of type “*cv1* `T`” can initialize an object of type
|
| 316 |
“*cv2* `T`” independently of the cv-qualifiers *cv1* and *cv2*.
|
| 317 |
``` cpp
|
| 318 |
int a;
|
|
|
|
| 375 |
- no private or protected direct base classes [[class.access.base]], and
|
| 376 |
- no virtual functions [[class.virtual]] or virtual base classes
|
| 377 |
[[class.mi]].
|
| 378 |
|
| 379 |
[*Note 1*: Aggregate initialization does not allow accessing protected
|
| 380 |
+
and private base class’ members, including constructors. — *end note*]
|
| 381 |
|
| 382 |
The *elements* of an aggregate are:
|
| 383 |
|
| 384 |
- for an array, the array elements in increasing subscript order, or
|
| 385 |
- for a class, the direct base classes in declaration order, followed by
|
|
|
|
| 395 |
*designated-initializer-list*, the aggregate shall be of class type,
|
| 396 |
the *identifier* in each *designator* shall name a direct non-static
|
| 397 |
data member of the class, and the explicitly initialized elements of
|
| 398 |
the aggregate are the elements that are, or contain, those members.
|
| 399 |
- If the initializer list is a brace-enclosed *initializer-list*, the
|
| 400 |
+
explicitly initialized elements of the aggregate are those for which
|
| 401 |
+
an element of the initializer list appertains to the aggregate element
|
| 402 |
+
or to a subobject thereof (see below).
|
| 403 |
- Otherwise, the initializer list must be `{}`, and there are no
|
| 404 |
explicitly initialized elements.
|
| 405 |
|
| 406 |
For each explicitly initialized element:
|
| 407 |
|
|
|
|
| 421 |
} c = { .a = 1, .x = 3 };
|
| 422 |
```
|
| 423 |
|
| 424 |
initializes `c.a` with 1 and `c.x` with 3.
|
| 425 |
— *end example*]
|
| 426 |
+
- Otherwise, if the initializer list is a brace-enclosed
|
| 427 |
+
*designated-initializer-list*, the element is initialized with the
|
| 428 |
*brace-or-equal-initializer* of the corresponding
|
| 429 |
*designated-initializer-clause*. If that initializer is of the form
|
| 430 |
+
`= `*assignment-expression* and a narrowing conversion
|
| 431 |
+
[[dcl.init.list]] is required to convert the expression, the program
|
| 432 |
+
is ill-formed. \[*Note 2*: The form of the initializer determines
|
| 433 |
+
whether copy-initialization or direct-initialization is
|
|
|
|
|
|
|
| 434 |
performed. — *end note*]
|
| 435 |
+
- Otherwise, the initializer list is a brace-enclosed
|
| 436 |
+
*initializer-list*. If an *initializer-clause* appertains to the
|
| 437 |
+
aggregate element, then the aggregate element is copy-initialized from
|
| 438 |
+
the *initializer-clause*. Otherwise, the aggregate element is
|
| 439 |
+
copy-initialized from a brace-enclosed *initializer-list* consisting
|
| 440 |
+
of all of the *initializer-clause*s that appertain to subobjects of
|
| 441 |
+
the aggregate element, in the order of appearance.
|
| 442 |
\[*Note 3*: If an initializer is itself an initializer list, the
|
| 443 |
element is list-initialized, which will result in a recursive
|
| 444 |
application of the rules in this subclause if the element is an
|
| 445 |
aggregate. — *end note*]
|
| 446 |
\[*Example 2*:
|
|
|
|
| 499 |
S ss = { 1, "asdf" };
|
| 500 |
```
|
| 501 |
|
| 502 |
initializes `ss.a` with 1, `ss.b` with `"asdf"`, `ss.c` with the value
|
| 503 |
of an expression of the form `int{}` (that is, `0`), and `ss.d` with the
|
| 504 |
+
value of `ss.b[ss.a]` (that is, `'s'`).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
|
| 506 |
``` cpp
|
| 507 |
struct A {
|
| 508 |
string a;
|
| 509 |
int b = 42;
|
|
|
|
| 525 |
element that follows it in order.
|
| 526 |
|
| 527 |
An aggregate that is a class can also be initialized with a single
|
| 528 |
expression not enclosed in braces, as described in [[dcl.init]].
|
| 529 |
|
| 530 |
+
The destructor for each element of class type other than an anonymous
|
| 531 |
+
union member is potentially invoked [[class.dtor]] from the context
|
| 532 |
+
where the aggregate initialization occurs.
|
| 533 |
|
| 534 |
[*Note 4*: This provision ensures that destructors can be called for
|
| 535 |
fully-constructed subobjects in case an exception is thrown
|
| 536 |
[[except.ctor]]. — *end note*]
|
| 537 |
|
| 538 |
+
The number of elements [[dcl.array]] in an array of unknown bound
|
| 539 |
+
initialized with a brace-enclosed *initializer-list* is the number of
|
| 540 |
+
explicitly initialized elements of the array.
|
| 541 |
|
| 542 |
[*Example 4*:
|
| 543 |
|
| 544 |
``` cpp
|
| 545 |
int x[] = { 1, 3, 5 };
|
|
|
|
| 548 |
declares and initializes `x` as a one-dimensional array that has three
|
| 549 |
elements since no size was specified and there are three initializers.
|
| 550 |
|
| 551 |
— *end example*]
|
| 552 |
|
| 553 |
+
[*Example 5*:
|
| 554 |
+
|
| 555 |
+
In
|
| 556 |
+
|
| 557 |
+
``` cpp
|
| 558 |
+
struct X { int i, j, k; };
|
| 559 |
+
X a[] = { 1, 2, 3, 4, 5, 6 };
|
| 560 |
+
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
|
| 561 |
+
```
|
| 562 |
+
|
| 563 |
+
`a` and `b` have the same value.
|
| 564 |
+
|
| 565 |
+
— *end example*]
|
| 566 |
+
|
| 567 |
An array of unknown bound shall not be initialized with an empty
|
| 568 |
*braced-init-list* `{}`.[^6]
|
| 569 |
|
| 570 |
[*Note 5*:
|
| 571 |
|
|
|
|
| 573 |
array of unknown bound. Since the default member initializer is ignored
|
| 574 |
if a suitable *mem-initializer* is present [[class.base.init]], the
|
| 575 |
default member initializer is not considered to initialize the array of
|
| 576 |
unknown bound.
|
| 577 |
|
| 578 |
+
[*Example 6*:
|
| 579 |
|
| 580 |
``` cpp
|
| 581 |
struct S {
|
| 582 |
int y[] = { 0 }; // error: non-static data member of incomplete type
|
| 583 |
};
|
|
|
|
| 590 |
[*Note 6*:
|
| 591 |
|
| 592 |
Static data members, non-static data members of anonymous union members,
|
| 593 |
and unnamed bit-fields are not considered elements of the aggregate.
|
| 594 |
|
| 595 |
+
[*Example 7*:
|
| 596 |
|
| 597 |
``` cpp
|
| 598 |
struct A {
|
| 599 |
int i;
|
| 600 |
static int s;
|
|
|
|
| 610 |
|
| 611 |
— *end example*]
|
| 612 |
|
| 613 |
— *end note*]
|
| 614 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
If a member has a default member initializer and a potentially-evaluated
|
| 616 |
subexpression thereof is an aggregate initialization that would use that
|
| 617 |
default member initializer, the program is ill-formed.
|
| 618 |
|
| 619 |
[*Example 8*:
|
|
|
|
| 632 |
};
|
| 633 |
```
|
| 634 |
|
| 635 |
— *end example*]
|
| 636 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 637 |
When initializing a multidimensional array, the *initializer-clause*s
|
| 638 |
initialize the elements with the last (rightmost) index of the array
|
| 639 |
varying the fastest [[dcl.array]].
|
| 640 |
|
| 641 |
+
[*Example 9*:
|
| 642 |
|
| 643 |
``` cpp
|
| 644 |
int x[2][2] = { 3, 1, 4, 2 };
|
| 645 |
```
|
| 646 |
|
|
|
|
| 656 |
initializes the first column of `y` (regarded as a two-dimensional
|
| 657 |
array) and leaves the rest zero.
|
| 658 |
|
| 659 |
— *end example*]
|
| 660 |
|
| 661 |
+
Each *initializer-clause* in a brace-enclosed *initializer-list* is said
|
| 662 |
+
to *appertain* to an element of the aggregate being initialized or to an
|
| 663 |
+
element of one of its subaggregates. Considering the sequence of
|
| 664 |
+
*initializer-clause*s, and the sequence of aggregate elements initially
|
| 665 |
+
formed as the sequence of elements of the aggregate being initialized
|
| 666 |
+
and potentially modified as described below, each *initializer-clause*
|
| 667 |
+
appertains to the corresponding aggregate element if
|
| 668 |
+
|
| 669 |
+
- the aggregate element is not an aggregate, or
|
| 670 |
+
- the *initializer-clause* begins with a left brace, or
|
| 671 |
+
- the *initializer-clause* is an expression and an implicit conversion
|
| 672 |
+
sequence can be formed that converts the expression to the type of the
|
| 673 |
+
aggregate element, or
|
| 674 |
+
- the aggregate element is an aggregate that itself has no aggregate
|
| 675 |
+
elements.
|
| 676 |
+
|
| 677 |
+
Otherwise, the aggregate element is an aggregate and that subaggregate
|
| 678 |
+
is replaced in the list of aggregate elements by the sequence of its own
|
| 679 |
+
aggregate elements, and the appertainment analysis resumes with the
|
| 680 |
+
first such element and the same *initializer-clause*.
|
| 681 |
+
|
| 682 |
+
[*Note 7*:
|
| 683 |
+
|
| 684 |
+
These rules apply recursively to the aggregate’s subaggregates.
|
| 685 |
+
|
| 686 |
+
[*Example 10*:
|
| 687 |
+
|
| 688 |
+
In
|
| 689 |
+
|
| 690 |
+
``` cpp
|
| 691 |
+
struct S1 { int a, b; };
|
| 692 |
+
struct S2 { S1 s, t; };
|
| 693 |
+
|
| 694 |
+
S2 x[2] = { 1, 2, 3, 4, 5, 6, 7, 8 };
|
| 695 |
+
S2 y[2] = {
|
| 696 |
+
{
|
| 697 |
+
{ 1, 2 },
|
| 698 |
+
{ 3, 4 }
|
| 699 |
+
},
|
| 700 |
+
{
|
| 701 |
+
{ 5, 6 },
|
| 702 |
+
{ 7, 8 }
|
| 703 |
+
}
|
| 704 |
+
};
|
| 705 |
+
```
|
| 706 |
+
|
| 707 |
+
`x` and `y` have the same value.
|
| 708 |
+
|
| 709 |
+
— *end example*]
|
| 710 |
+
|
| 711 |
+
— *end note*]
|
| 712 |
+
|
| 713 |
+
This process continues until all *initializer-clause*s have been
|
| 714 |
+
exhausted. If any *initializer-clause* remains that does not appertain
|
| 715 |
+
to an element of the aggregate or one of its subaggregates, the program
|
| 716 |
+
is ill-formed.
|
| 717 |
|
| 718 |
[*Example 11*:
|
| 719 |
|
| 720 |
+
``` cpp
|
| 721 |
+
char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error: too many initializers
|
| 722 |
+
```
|
| 723 |
+
|
| 724 |
+
— *end example*]
|
| 725 |
+
|
| 726 |
+
[*Example 12*:
|
| 727 |
+
|
| 728 |
``` cpp
|
| 729 |
float y[4][3] = {
|
| 730 |
{ 1, 3, 5 },
|
| 731 |
{ 2, 4, 6 },
|
| 732 |
{ 3, 5, 7 },
|
|
|
|
| 734 |
```
|
| 735 |
|
| 736 |
is a completely-braced initialization: 1, 3, and 5 initialize the first
|
| 737 |
row of the array `y[0]`, namely `y[0][0]`, `y[0][1]`, and `y[0][2]`.
|
| 738 |
Likewise the next two lines initialize `y[1]` and `y[2]`. The
|
| 739 |
+
initializer ends early and therefore `y[3]`’s elements are initialized
|
| 740 |
+
as if explicitly initialized with an expression of the form `float()`,
|
| 741 |
+
that is, are initialized with `0.0`. In the following example, braces in
|
| 742 |
+
the *initializer-list* are elided; however the *initializer-list* has
|
| 743 |
+
the same effect as the completely-braced *initializer-list* of the above
|
| 744 |
example,
|
| 745 |
|
| 746 |
``` cpp
|
| 747 |
float y[4][3] = {
|
| 748 |
1, 3, 5, 2, 4, 6, 3, 5, 7
|
|
|
|
| 753 |
does not, therefore three elements from the list are used. Likewise the
|
| 754 |
next three are taken successively for `y[1]` and `y[2]`.
|
| 755 |
|
| 756 |
— *end example*]
|
| 757 |
|
| 758 |
+
[*Note 8*:
|
| 759 |
+
|
| 760 |
+
The initializer for an empty subaggregate is needed if any initializers
|
| 761 |
+
are provided for subsequent elements.
|
| 762 |
+
|
| 763 |
+
[*Example 13*:
|
| 764 |
+
|
| 765 |
+
``` cpp
|
| 766 |
+
struct S { } s;
|
| 767 |
+
struct A {
|
| 768 |
+
S s1;
|
| 769 |
+
int i1;
|
| 770 |
+
S s2;
|
| 771 |
+
int i2;
|
| 772 |
+
S s3;
|
| 773 |
+
int i3;
|
| 774 |
+
} a = {
|
| 775 |
+
{ }, // Required initialization
|
| 776 |
+
0,
|
| 777 |
+
s, // Required initialization
|
| 778 |
+
0
|
| 779 |
+
}; // Initialization not required for A::s3 because A::i3 is also not initialized
|
| 780 |
+
```
|
| 781 |
+
|
| 782 |
+
— *end example*]
|
| 783 |
+
|
| 784 |
+
— *end note*]
|
| 785 |
+
|
| 786 |
+
[*Example 14*:
|
| 787 |
|
| 788 |
``` cpp
|
| 789 |
struct A {
|
| 790 |
int i;
|
| 791 |
operator int();
|
|
|
|
| 802 |
is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
|
| 803 |
initialized with whatever `a.operator int()` returns.
|
| 804 |
|
| 805 |
— *end example*]
|
| 806 |
|
| 807 |
+
[*Note 9*: An aggregate array or an aggregate class can contain
|
| 808 |
elements of a class type with a user-declared constructor
|
| 809 |
[[class.ctor]]. Initialization of these aggregate objects is described
|
| 810 |
in [[class.expl.init]]. — *end note*]
|
| 811 |
|
| 812 |
+
[*Note 10*: Whether the initialization of aggregates with static
|
| 813 |
+
storage duration is static or dynamic is specified in
|
| 814 |
+
[[basic.start.static]], [[basic.start.dynamic]], and
|
| 815 |
+
[[stmt.dcl]]. — *end note*]
|
| 816 |
|
| 817 |
When a union is initialized with an initializer list, there shall not be
|
| 818 |
more than one explicitly initialized element.
|
| 819 |
|
| 820 |
+
[*Example 15*:
|
| 821 |
|
| 822 |
``` cpp
|
| 823 |
union u { int a; const char* b; };
|
| 824 |
u a = { 1 };
|
| 825 |
u b = a;
|
|
|
|
| 830 |
u g = { .a = 1, .b = "asdf" }; // error
|
| 831 |
```
|
| 832 |
|
| 833 |
— *end example*]
|
| 834 |
|
| 835 |
+
[*Note 11*: As described above, the braces around the
|
| 836 |
*initializer-clause* for a union member can be omitted if the union is a
|
| 837 |
member of another aggregate. — *end note*]
|
| 838 |
|
| 839 |
### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
|
| 840 |
|
|
|
|
| 905 |
|
| 906 |
A reference cannot be changed to refer to another object after
|
| 907 |
initialization.
|
| 908 |
|
| 909 |
[*Note 1*: Assignment to a reference assigns to the object referred to
|
| 910 |
+
by the reference [[expr.assign]]. — *end note*]
|
| 911 |
|
| 912 |
Argument passing [[expr.call]] and function value return [[stmt.return]]
|
| 913 |
are initializations.
|
| 914 |
|
| 915 |
The initializer can be omitted for a reference only in a parameter
|
|
|
|
| 982 |
double& rd3 = i; // error: type mismatch and reference not const
|
| 983 |
```
|
| 984 |
|
| 985 |
— *end example*]
|
| 986 |
- Otherwise, if the initializer expression
|
| 987 |
+
- is an rvalue (but not a bit-field) or an lvalue of function type and
|
| 988 |
+
“*cv1* `T1`” is reference-compatible with “*cv2* `T2`”, or
|
| 989 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 990 |
+
reference-related to `T2`, and can be converted to an rvalue of type
|
| 991 |
+
“*cv3* `T3`” or an lvalue of function type “*cv3* `T3`”, where
|
| 992 |
+
“*cv1* `T1`” is reference-compatible with “*cv3* `T3`” (see
|
| 993 |
+
[[over.match.ref]]),
|
| 994 |
|
| 995 |
then the initializer expression in the first case and the converted
|
| 996 |
expression in the second case is called the converted initializer. If
|
| 997 |
+
the converted initializer is a prvalue, let its type be denoted by
|
| 998 |
+
`T4`; the temporary materialization conversion [[conv.rval]] is
|
| 999 |
+
applied, considering the type of the prvalue to be “*cv1* `T4`”
|
| 1000 |
+
[[conv.qual]]. In any case, the reference binds to the resulting
|
| 1001 |
+
glvalue (or to an appropriate base class subobject).
|
| 1002 |
\[*Example 5*:
|
| 1003 |
``` cpp
|
| 1004 |
struct A { };
|
| 1005 |
struct B : A { } b;
|
| 1006 |
extern B f();
|
|
|
|
| 1012 |
} x;
|
| 1013 |
const A& r = x; // binds to the A subobject of the result of the conversion
|
| 1014 |
int i2 = 42;
|
| 1015 |
int&& rri = static_cast<int&&>(i2); // binds directly to i2
|
| 1016 |
B&& rrb = x; // binds directly to the result of operator B
|
| 1017 |
+
|
| 1018 |
+
constexpr int f() {
|
| 1019 |
+
const int &x = 42;
|
| 1020 |
+
const_cast<int &>(x) = 1; // undefined behavior
|
| 1021 |
+
return x;
|
| 1022 |
+
}
|
| 1023 |
+
constexpr int z = f(); // error: not a constant expression
|
| 1024 |
+
|
| 1025 |
+
typedef int *AP[3]; // array of 3 pointer to int
|
| 1026 |
+
typedef const int *const ACPC[3]; // array of 3 const pointer to const int
|
| 1027 |
+
ACPC &&r = AP{}; // binds directly
|
| 1028 |
```
|
| 1029 |
|
| 1030 |
— *end example*]
|
| 1031 |
+
- Otherwise, `T1` shall not be reference-related to `T2`.
|
| 1032 |
+
- If `T1` or `T2` is a class type, user-defined conversions are
|
| 1033 |
+
considered using the rules for copy-initialization of an object of
|
| 1034 |
+
type “*cv1* `T1`” by user-defined conversion
|
|
|
|
| 1035 |
[[dcl.init]], [[over.match.copy]], [[over.match.conv]]; the program
|
| 1036 |
is ill-formed if the corresponding non-reference copy-initialization
|
| 1037 |
+
would be ill-formed. The result E of the call to the conversion
|
| 1038 |
function, as described for the non-reference copy-initialization, is
|
| 1039 |
+
then used to direct-initialize the reference using the form `(E)`.
|
| 1040 |
+
For this direct-initialization, user-defined conversions are not
|
| 1041 |
+
considered.
|
| 1042 |
- Otherwise, the initializer expression is implicitly converted to a
|
| 1043 |
prvalue of type “`T1`”. The temporary materialization conversion is
|
| 1044 |
applied, considering the type of the prvalue to be “*cv1* `T1`”, and
|
| 1045 |
the reference is bound to the result.
|
| 1046 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1047 |
\[*Example 6*:
|
| 1048 |
``` cpp
|
| 1049 |
struct Banana { };
|
| 1050 |
struct Enigma { operator const Banana(); };
|
| 1051 |
struct Alaska { operator Banana&(); };
|
|
|
|
| 1054 |
Banana &&banana1 = ConstBanana(); // error
|
| 1055 |
Banana &&banana2 = Enigma(); // error
|
| 1056 |
Banana &&banana3 = Alaska(); // error
|
| 1057 |
}
|
| 1058 |
|
| 1059 |
+
const double& rcd2 = 2; // rcd2 refers to temporary with type const double and value 2.0
|
| 1060 |
double&& rrd = 2; // rrd refers to temporary with value 2.0
|
| 1061 |
const volatile int cvi = 1;
|
| 1062 |
const int& r2 = cvi; // error: cv-qualifier dropped
|
| 1063 |
struct A { operator volatile int&(); } a;
|
| 1064 |
const int& r3 = a; // error: cv-qualifier dropped
|
| 1065 |
// from result of conversion function
|
| 1066 |
double d2 = 1.0;
|
| 1067 |
+
double&& rrd2 = d2; // error: initializer is lvalue of reference-related type
|
| 1068 |
struct X { operator int&(); };
|
| 1069 |
+
int&& rri2 = X(); // error: result of conversion function is
|
| 1070 |
+
// lvalue of reference-related type
|
| 1071 |
int i3 = 2;
|
| 1072 |
double&& rrd3 = i3; // rrd3 refers to temporary with value 2.0
|
| 1073 |
```
|
| 1074 |
|
| 1075 |
— *end example*]
|
| 1076 |
|
| 1077 |
In all cases except the last (i.e., implicitly converting the
|
| 1078 |
initializer expression to the referenced type), the reference is said to
|
| 1079 |
*bind directly* to the initializer expression.
|
| 1080 |
|
| 1081 |
+
[*Note 3*: [[class.temporary]] describes the lifetime of temporaries
|
| 1082 |
bound to references. — *end note*]
|
| 1083 |
|
| 1084 |
### List-initialization <a id="dcl.init.list">[[dcl.init.list]]</a>
|
| 1085 |
|
| 1086 |
*List-initialization* is initialization of an object or reference from a
|
|
|
|
| 1090 |
are called the *elements* of the initializer list. An initializer list
|
| 1091 |
may be empty. List-initialization can occur in direct-initialization or
|
| 1092 |
copy-initialization contexts; list-initialization in a
|
| 1093 |
direct-initialization context is called *direct-list-initialization* and
|
| 1094 |
list-initialization in a copy-initialization context is called
|
| 1095 |
+
*copy-list-initialization*. Direct-initialization that is not
|
| 1096 |
+
list-initialization is called *direct-non-list-initialization*.
|
| 1097 |
|
| 1098 |
[*Note 1*:
|
| 1099 |
|
| 1100 |
List-initialization can be used
|
| 1101 |
|
| 1102 |
+
- as the initializer in a variable definition [[dcl.init]],
|
| 1103 |
+
- as the initializer in a *new-expression* [[expr.new]],
|
| 1104 |
+
- in a `return` statement [[stmt.return]],
|
| 1105 |
+
- as a *for-range-initializer* [[stmt.iter]],
|
| 1106 |
+
- as a function argument [[expr.call]],
|
| 1107 |
+
- as a template argument [[temp.arg.nontype]],
|
| 1108 |
+
- as a subscript [[expr.sub]],
|
| 1109 |
- as an argument to a constructor invocation
|
| 1110 |
+
[[dcl.init]], [[expr.type.conv]],
|
| 1111 |
+
- as an initializer for a non-static data member [[class.mem]],
|
| 1112 |
+
- in a *mem-initializer* [[class.base.init]], or
|
| 1113 |
+
- on the right-hand side of an assignment [[expr.assign]].
|
| 1114 |
|
| 1115 |
[*Example 1*:
|
| 1116 |
|
| 1117 |
``` cpp
|
| 1118 |
int a = {1};
|
|
|
|
| 1147 |
library declaration [[initializer.list.syn]], [[std.modules]] of
|
| 1148 |
`std::initializer_list` is not reachable from [[module.reach]] a use of
|
| 1149 |
`std::initializer_list` — even an implicit use in which the type is not
|
| 1150 |
named [[dcl.spec.auto]] — the program is ill-formed.
|
| 1151 |
|
| 1152 |
+
List-initialization of an object or reference of type *cv* `T` is
|
| 1153 |
+
defined as follows:
|
| 1154 |
|
| 1155 |
+
- If the *braced-init-list* contains a *designated-initializer-list* and
|
| 1156 |
+
`T` is not a reference type, `T` shall be an aggregate class. The
|
| 1157 |
+
ordered *identifier*s in the *designator*s of the
|
| 1158 |
+
*designated-initializer-list* shall form a subsequence of the ordered
|
| 1159 |
+
*identifier*s in the direct non-static data members of `T`. Aggregate
|
| 1160 |
+
initialization is performed [[dcl.init.aggr]].
|
| 1161 |
\[*Example 2*:
|
| 1162 |
``` cpp
|
| 1163 |
struct A { int x; int y; int z; };
|
| 1164 |
A a{.y = 2, .x = 1}; // error: designator order does not match declaration order
|
| 1165 |
A b{.x = 1, .z = 2}; // OK, b.y initialized to 0
|
| 1166 |
```
|
| 1167 |
|
| 1168 |
— *end example*]
|
| 1169 |
- If `T` is an aggregate class and the initializer list has a single
|
| 1170 |
+
element of type *cv1* `U`, where `U` is `T` or a class derived from
|
| 1171 |
`T`, the object is initialized from that element (by
|
| 1172 |
copy-initialization for copy-list-initialization, or by
|
| 1173 |
direct-initialization for direct-list-initialization).
|
| 1174 |
- Otherwise, if `T` is a character array and the initializer list has a
|
| 1175 |
single element that is an appropriately-typed *string-literal*
|
|
|
|
| 1192 |
```
|
| 1193 |
|
| 1194 |
— *end example*]
|
| 1195 |
- Otherwise, if the initializer list has no elements and `T` is a class
|
| 1196 |
type with a default constructor, the object is value-initialized.
|
| 1197 |
+
- Otherwise, if `T` is a specialization of `std::initializer_list`, the
|
| 1198 |
+
object is constructed as described below.
|
| 1199 |
- Otherwise, if `T` is a class type, constructors are considered. The
|
| 1200 |
applicable constructors are enumerated and the best one is chosen
|
| 1201 |
through overload resolution [[over.match]], [[over.match.list]]. If a
|
| 1202 |
narrowing conversion (see below) is required to convert any of the
|
| 1203 |
arguments, the program is ill-formed.
|
| 1204 |
\[*Example 4*:
|
| 1205 |
``` cpp
|
| 1206 |
struct S {
|
| 1207 |
S(std::initializer_list<double>); // #1
|
| 1208 |
S(std::initializer_list<int>); // #2
|
| 1209 |
+
S(std::initializer_list<S>); // #3
|
| 1210 |
+
S(); // #4
|
| 1211 |
// ...
|
| 1212 |
};
|
| 1213 |
S s1 = { 1.0, 2.0, 3.0 }; // invoke #1
|
| 1214 |
S s2 = { 1, 2, 3 }; // invoke #2
|
| 1215 |
+
S s3{s2}; // invoke #3 (not the copy constructor)
|
| 1216 |
+
S s4 = { }; // invoke #4
|
| 1217 |
```
|
| 1218 |
|
| 1219 |
— *end example*]
|
| 1220 |
\[*Example 5*:
|
| 1221 |
``` cpp
|
|
|
|
| 1239 |
S s3 { }; // OK, invoke #2
|
| 1240 |
```
|
| 1241 |
|
| 1242 |
— *end example*]
|
| 1243 |
- Otherwise, if `T` is an enumeration with a fixed underlying type
|
| 1244 |
+
[[dcl.enum]] `U`, the *initializer-list* has a single element `v` of
|
| 1245 |
+
scalar type, `v` can be implicitly converted to `U`, and the
|
| 1246 |
+
initialization is direct-list-initialization, the object is
|
| 1247 |
+
initialized with the value `T(v)` [[expr.type.conv]]; if a narrowing
|
| 1248 |
+
conversion is required to convert `v` to `U`, the program is
|
| 1249 |
+
ill-formed.
|
| 1250 |
\[*Example 7*:
|
| 1251 |
``` cpp
|
| 1252 |
enum byte : unsigned char { };
|
| 1253 |
byte b { 42 }; // OK
|
| 1254 |
byte c = { 42 }; // error
|
|
|
|
| 1265 |
enum class Handle : uint32_t { Invalid = 0 };
|
| 1266 |
Handle h { 42 }; // OK
|
| 1267 |
```
|
| 1268 |
|
| 1269 |
— *end example*]
|
| 1270 |
+
- Otherwise, if the initializer list is not a
|
| 1271 |
+
*designated-initializer-list* and has a single element of type `E` and
|
| 1272 |
+
either `T` is not a reference type or its referenced type is
|
| 1273 |
reference-related to `E`, the object or reference is initialized from
|
| 1274 |
that element (by copy-initialization for copy-list-initialization, or
|
| 1275 |
by direct-initialization for direct-list-initialization); if a
|
| 1276 |
narrowing conversion (see below) is required to convert the element to
|
| 1277 |
`T`, the program is ill-formed.
|
|
|
|
| 1307 |
const int (&iar)[2] = { 1, 2 }; // OK, iar is bound to temporary array
|
| 1308 |
|
| 1309 |
struct A { } a;
|
| 1310 |
struct B { explicit B(const A&); };
|
| 1311 |
const B& b2{a}; // error: cannot copy-list-initialize B temporary from A
|
| 1312 |
+
|
| 1313 |
+
struct C { int x; };
|
| 1314 |
+
C&& c = { .x = 1 }; // OK
|
| 1315 |
```
|
| 1316 |
|
| 1317 |
— *end example*]
|
| 1318 |
- Otherwise, if the initializer list has no elements, the object is
|
| 1319 |
value-initialized.
|
|
|
|
| 1361 |
arguments of a call. — *end note*]
|
| 1362 |
|
| 1363 |
An object of type `std::initializer_list<E>` is constructed from an
|
| 1364 |
initializer list as if the implementation generated and materialized
|
| 1365 |
[[conv.rval]] a prvalue of type “array of N `const E`”, where N is the
|
| 1366 |
+
number of elements in the initializer list; this is called the
|
| 1367 |
+
initializer list’s *backing array*. Each element of the backing array is
|
| 1368 |
+
copy-initialized with the corresponding element of the initializer list,
|
| 1369 |
+
and the `std::initializer_list<E>` object is constructed to refer to
|
| 1370 |
+
that array.
|
| 1371 |
|
| 1372 |
[*Note 5*: A constructor or conversion function selected for the copy
|
| 1373 |
+
needs to be accessible [[class.access]] in the context of the
|
| 1374 |
initializer list. — *end note*]
|
| 1375 |
|
| 1376 |
If a narrowing conversion is required to initialize any of the elements,
|
| 1377 |
the program is ill-formed.
|
| 1378 |
|
| 1379 |
+
[*Note 6*: Backing arrays are potentially non-unique objects
|
| 1380 |
+
[[intro.object]]. — *end note*]
|
| 1381 |
|
| 1382 |
+
The backing array has the same lifetime as any other temporary object
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1383 |
[[class.temporary]], except that initializing an `initializer_list`
|
| 1384 |
object from the array extends the lifetime of the array exactly like
|
| 1385 |
binding a reference to a temporary.
|
| 1386 |
|
| 1387 |
+
[*Example 12*:
|
| 1388 |
+
|
| 1389 |
+
``` cpp
|
| 1390 |
+
void f(std::initializer_list<double> il);
|
| 1391 |
+
void g(float x) {
|
| 1392 |
+
f({1, x, 3});
|
| 1393 |
+
}
|
| 1394 |
+
void h() {
|
| 1395 |
+
f({1, 2, 3});
|
| 1396 |
+
}
|
| 1397 |
+
|
| 1398 |
+
struct A {
|
| 1399 |
+
mutable int i;
|
| 1400 |
+
};
|
| 1401 |
+
void q(std::initializer_list<A>);
|
| 1402 |
+
void r() {
|
| 1403 |
+
q({A{1}, A{2}, A{3}});
|
| 1404 |
+
}
|
| 1405 |
+
```
|
| 1406 |
+
|
| 1407 |
+
The initialization will be implemented in a way roughly equivalent to
|
| 1408 |
+
this:
|
| 1409 |
+
|
| 1410 |
+
``` cpp
|
| 1411 |
+
void g(float x) {
|
| 1412 |
+
const double __a[3] = {double{1}, double{x}, double{3}}; // backing array
|
| 1413 |
+
f(std::initializer_list<double>(__a, __a+3));
|
| 1414 |
+
}
|
| 1415 |
+
void h() {
|
| 1416 |
+
static constexpr double __b[3] = {double{1}, double{2}, double{3}}; // backing array
|
| 1417 |
+
f(std::initializer_list<double>(__b, __b+3));
|
| 1418 |
+
}
|
| 1419 |
+
void r() {
|
| 1420 |
+
const A __c[3] = {A{1}, A{2}, A{3}}; // backing array
|
| 1421 |
+
q(std::initializer_list<A>(__c, __c+3));
|
| 1422 |
+
}
|
| 1423 |
+
```
|
| 1424 |
+
|
| 1425 |
+
assuming that the implementation can construct an `initializer_list`
|
| 1426 |
+
object with a pair of pointers, and with the understanding that `__b`
|
| 1427 |
+
does not outlive the call to `f`.
|
| 1428 |
+
|
| 1429 |
+
— *end example*]
|
| 1430 |
+
|
| 1431 |
[*Example 13*:
|
| 1432 |
|
| 1433 |
``` cpp
|
| 1434 |
typedef std::complex<double> cmplx;
|
| 1435 |
std::vector<cmplx> v1 = { 1, 2, 3 };
|
|
|
|
| 1453 |
*ctor-initializer* as if by binding a temporary array to a reference
|
| 1454 |
member, so the program is ill-formed [[class.base.init]].
|
| 1455 |
|
| 1456 |
— *end example*]
|
| 1457 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1458 |
A *narrowing conversion* is an implicit conversion
|
| 1459 |
|
| 1460 |
- from a floating-point type to an integer type, or
|
| 1461 |
- from a floating-point type `T` to another floating-point type whose
|
| 1462 |
floating-point conversion rank is neither greater than nor equal to
|
| 1463 |
+
that of `T`, except where the result of the conversion is a constant
|
| 1464 |
+
expression and either its value is finite and the conversion did not
|
| 1465 |
+
overflow, or the values before and after the conversion are not
|
| 1466 |
+
finite, or
|
| 1467 |
- from an integer type or unscoped enumeration type to a floating-point
|
| 1468 |
type, except where the source is a constant expression and the actual
|
| 1469 |
value after conversion will fit into the target type and will produce
|
| 1470 |
the original value when converted back to the original type, or
|
| 1471 |
- from an integer type or unscoped enumeration type to an integer type
|