- tmp/tmpm618q014/{from.md → to.md} +152 -146
tmp/tmpm618q014/{from.md → to.md}
RENAMED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
## Initializers <a id="dcl.init">[[dcl.init]]</a>
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
initializations regardless of syntactic context, including the
|
| 5 |
initialization of a function parameter [[expr.call]], the initialization
|
| 6 |
of a return value [[stmt.return]], or when an initializer follows a
|
| 7 |
declarator.
|
| 8 |
|
|
@@ -57,13 +59,13 @@ designator:
|
|
| 57 |
expr-or-braced-init-list:
|
| 58 |
expression
|
| 59 |
braced-init-list
|
| 60 |
```
|
| 61 |
|
| 62 |
-
[*Note 1*: The rules in
|
| 63 |
-
|
| 64 |
-
|
| 65 |
|
| 66 |
Except for objects declared with the `constexpr` specifier, for which
|
| 67 |
see [[dcl.constexpr]], an *initializer* in the definition of a variable
|
| 68 |
can consist of arbitrary expressions involving literals and previously
|
| 69 |
declared variables and functions, regardless of the variable’s storage
|
|
@@ -85,26 +87,26 @@ int c(b);
|
|
| 85 |
|
| 86 |
[*Note 3*: The order of initialization of variables with static storage
|
| 87 |
duration is described in [[basic.start]] and
|
| 88 |
[[stmt.dcl]]. — *end note*]
|
| 89 |
|
| 90 |
-
A declaration of a
|
| 91 |
-
|
| 92 |
|
| 93 |
To *zero-initialize* an object or reference of type `T` means:
|
| 94 |
|
| 95 |
-
- if `T` is a scalar type [[
|
| 96 |
-
the value obtained by converting the integer literal
|
| 97 |
-
`T`;[^5]
|
| 98 |
- if `T` is a (possibly cv-qualified) non-union class type, its padding
|
| 99 |
-
bits [[
|
| 100 |
-
data member, each non-virtual base class subobject, and, if
|
| 101 |
-
is not a base class subobject, each virtual base class
|
| 102 |
-
zero-initialized;
|
| 103 |
- if `T` is a (possibly cv-qualified) union type, its padding bits
|
| 104 |
-
[[
|
| 105 |
-
non-static named data member is zero-initialized;
|
| 106 |
- if `T` is an array type, each element is zero-initialized;
|
| 107 |
- if `T` is a reference type, no initialization is performed.
|
| 108 |
|
| 109 |
To *default-initialize* an object of type `T` means:
|
| 110 |
|
|
@@ -154,62 +156,25 @@ of an entity of reference type is ill-formed.
|
|
| 154 |
[*Note 4*: For every object of static storage duration, static
|
| 155 |
initialization [[basic.start.static]] is performed at program startup
|
| 156 |
before any other initialization takes place. In some cases, additional
|
| 157 |
initialization is done later. — *end note*]
|
| 158 |
|
| 159 |
-
An object whose initializer is an empty set of parentheses, i.e., `()`,
|
| 160 |
-
shall be value-initialized.
|
| 161 |
-
|
| 162 |
-
[*Note 5*:
|
| 163 |
-
|
| 164 |
-
Since `()` is not permitted by the syntax for *initializer*,
|
| 165 |
-
|
| 166 |
-
``` cpp
|
| 167 |
-
X a();
|
| 168 |
-
```
|
| 169 |
-
|
| 170 |
-
is not the declaration of an object of class `X`, but the declaration of
|
| 171 |
-
a function taking no argument and returning an `X`. The form `()` is
|
| 172 |
-
permitted in certain other initialization contexts ([[expr.new]],
|
| 173 |
-
[[expr.type.conv]], [[class.base.init]]).
|
| 174 |
-
|
| 175 |
-
— *end note*]
|
| 176 |
-
|
| 177 |
If no initializer is specified for an object, the object is
|
| 178 |
default-initialized.
|
| 179 |
|
| 180 |
-
An initializer for a static member is in the scope of the member’s
|
| 181 |
-
class.
|
| 182 |
-
|
| 183 |
-
[*Example 2*:
|
| 184 |
-
|
| 185 |
-
``` cpp
|
| 186 |
-
int a;
|
| 187 |
-
|
| 188 |
-
struct X {
|
| 189 |
-
static int a;
|
| 190 |
-
static int b;
|
| 191 |
-
};
|
| 192 |
-
|
| 193 |
-
int X::a = 1;
|
| 194 |
-
int X::b = a; // X::b = X::a
|
| 195 |
-
```
|
| 196 |
-
|
| 197 |
-
— *end example*]
|
| 198 |
-
|
| 199 |
If the entity being initialized does not have class type, the
|
| 200 |
*expression-list* in a parenthesized initializer shall be a single
|
| 201 |
expression.
|
| 202 |
|
| 203 |
The initialization that occurs in the `=` form of a
|
| 204 |
*brace-or-equal-initializer* or *condition* [[stmt.select]], as well as
|
| 205 |
in argument passing, function return, throwing an exception
|
| 206 |
[[except.throw]], handling an exception [[except.handle]], and aggregate
|
| 207 |
-
member initialization
|
| 208 |
-
*copy-initialization*.
|
| 209 |
|
| 210 |
-
[*Note
|
| 211 |
[[class.copy.ctor]]. — *end note*]
|
| 212 |
|
| 213 |
The initialization that occurs
|
| 214 |
|
| 215 |
- for an *initializer* that is a parenthesized *expression-list* or a
|
|
@@ -234,10 +199,21 @@ defined.
|
|
| 234 |
- If the destination type is an array of characters, an array of
|
| 235 |
`char8_t`, an array of `char16_t`, an array of `char32_t`, or an array
|
| 236 |
of `wchar_t`, and the initializer is a *string-literal*, see
|
| 237 |
[[dcl.init.string]].
|
| 238 |
- If the initializer is `()`, the object is value-initialized.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
- Otherwise, if the destination type is an array, the object is
|
| 240 |
initialized as follows. Let x₁, …, xₖ be the elements of the
|
| 241 |
*expression-list*. If the destination type is an array of unknown
|
| 242 |
bound, it is defined as having k elements. Let n denote the array size
|
| 243 |
after this potential adjustment. If k is greater than n, the program
|
|
@@ -250,12 +226,12 @@ defined.
|
|
| 250 |
- Otherwise, if the destination type is a (possibly cv-qualified) class
|
| 251 |
type:
|
| 252 |
- If the initializer expression is a prvalue and the cv-unqualified
|
| 253 |
version of the source type is the same class as the class of the
|
| 254 |
destination, the initializer expression is used to initialize the
|
| 255 |
-
destination object. \[*Example
|
| 256 |
-
|
| 257 |
- Otherwise, if the initialization is direct-initialization, or if it
|
| 258 |
is copy-initialization where the cv-unqualified version of the
|
| 259 |
source type is the same class as, or a derived class of, the class
|
| 260 |
of the destination, constructors are considered. The applicable
|
| 261 |
constructors are enumerated [[over.match.ctor]], and the best one is
|
|
@@ -278,11 +254,11 @@ defined.
|
|
| 278 |
\[*Note 7*:
|
| 279 |
By contrast with direct-list-initialization, narrowing conversions
|
| 280 |
[[dcl.init.list]] are permitted, designators are not permitted, a
|
| 281 |
temporary object bound to a reference does not have its lifetime
|
| 282 |
extended [[class.temporary]], and there is no brace elision.
|
| 283 |
-
\[*Example
|
| 284 |
``` cpp
|
| 285 |
struct A {
|
| 286 |
int a;
|
| 287 |
int&& r;
|
| 288 |
};
|
|
@@ -340,13 +316,24 @@ defined.
|
|
| 340 |
int c = b;
|
| 341 |
```
|
| 342 |
|
| 343 |
— *end note*]
|
| 344 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
An *initializer-clause* followed by an ellipsis is a pack expansion
|
| 346 |
[[temp.variadic]].
|
| 347 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
If the initializer is a parenthesized *expression-list*, the expressions
|
| 349 |
are evaluated in the order specified for function calls [[expr.call]].
|
| 350 |
|
| 351 |
The same *identifier* shall not appear in multiple *designator*s of a
|
| 352 |
*designated-initializer-list*.
|
|
@@ -368,23 +355,24 @@ A declaration that specifies the initialization of a variable, whether
|
|
| 368 |
from an explicit initializer or by default-initialization, is called the
|
| 369 |
*initializing declaration* of that variable.
|
| 370 |
|
| 371 |
[*Note 10*: In most cases this is the defining declaration
|
| 372 |
[[basic.def]] of the variable, but the initializing declaration of a
|
| 373 |
-
non-inline static data member [[class.static.data]]
|
| 374 |
-
declaration within the class definition and not the definition
|
| 375 |
-
|
| 376 |
|
| 377 |
### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
|
| 378 |
|
| 379 |
An *aggregate* is an array or a class [[class]] with
|
| 380 |
|
| 381 |
- no user-declared or inherited constructors [[class.ctor]],
|
| 382 |
- no private or protected direct non-static data members
|
| 383 |
[[class.access]],
|
| 384 |
-
- no
|
| 385 |
-
- no virtual
|
|
|
|
| 386 |
|
| 387 |
[*Note 1*: Aggregate initialization does not allow accessing protected
|
| 388 |
and private base class’ members or constructors. — *end note*]
|
| 389 |
|
| 390 |
The *elements* of an aggregate are:
|
|
@@ -397,29 +385,29 @@ The *elements* of an aggregate are:
|
|
| 397 |
When an aggregate is initialized by an initializer list as specified in
|
| 398 |
[[dcl.init.list]], the elements of the initializer list are taken as
|
| 399 |
initializers for the elements of the aggregate. The *explicitly
|
| 400 |
initialized elements* of the aggregate are determined as follows:
|
| 401 |
|
| 402 |
-
- If the initializer list is a
|
| 403 |
-
aggregate shall be of class type,
|
| 404 |
-
*designator* shall name a direct non-static
|
| 405 |
-
and the explicitly initialized elements of
|
| 406 |
-
elements that are, or contain, those members.
|
| 407 |
-
- If the initializer list is
|
| 408 |
-
initialized elements of the aggregate are the first n
|
| 409 |
-
aggregate, where n is the number of elements in the
|
|
|
|
| 410 |
- Otherwise, the initializer list must be `{}`, and there are no
|
| 411 |
explicitly initialized elements.
|
| 412 |
|
| 413 |
For each explicitly initialized element:
|
| 414 |
|
| 415 |
-
- If the element is an anonymous union
|
| 416 |
-
is a *designated-initializer-list*, the
|
| 417 |
-
initialized by the *
|
| 418 |
-
*
|
| 419 |
-
|
| 420 |
-
*designated-initializer-clause*.
|
| 421 |
\[*Example 1*:
|
| 422 |
``` cpp
|
| 423 |
struct C {
|
| 424 |
union {
|
| 425 |
int a;
|
|
@@ -436,11 +424,15 @@ For each explicitly initialized element:
|
|
| 436 |
*brace-or-equal-initializer* of the corresponding
|
| 437 |
*designated-initializer-clause*. If that initializer is of the form
|
| 438 |
*assignment-expression* or `= `*assignment-expression* and a narrowing
|
| 439 |
conversion [[dcl.init.list]] is required to convert the expression,
|
| 440 |
the program is ill-formed.
|
| 441 |
-
\[*Note 2*: If
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
element is list-initialized, which will result in a recursive
|
| 443 |
application of the rules in this subclause if the element is an
|
| 444 |
aggregate. — *end note*]
|
| 445 |
\[*Example 2*:
|
| 446 |
``` cpp
|
|
@@ -536,11 +528,11 @@ expression not enclosed in braces, as described in [[dcl.init]].
|
|
| 536 |
|
| 537 |
The destructor for each element of class type is potentially invoked
|
| 538 |
[[class.dtor]] from the context where the aggregate initialization
|
| 539 |
occurs.
|
| 540 |
|
| 541 |
-
[*Note
|
| 542 |
fully-constructed subobjects in case an exception is thrown
|
| 543 |
[[except.ctor]]. — *end note*]
|
| 544 |
|
| 545 |
An array of unknown bound initialized with a brace-enclosed
|
| 546 |
*initializer-list* containing `n` *initializer-clause*s is defined as
|
|
@@ -558,11 +550,11 @@ elements since no size was specified and there are three initializers.
|
|
| 558 |
— *end example*]
|
| 559 |
|
| 560 |
An array of unknown bound shall not be initialized with an empty
|
| 561 |
*braced-init-list* `{}`.[^6]
|
| 562 |
|
| 563 |
-
[*Note
|
| 564 |
|
| 565 |
A default member initializer does not determine the bound for a member
|
| 566 |
array of unknown bound. Since the default member initializer is ignored
|
| 567 |
if a suitable *mem-initializer* is present [[class.base.init]], the
|
| 568 |
default member initializer is not considered to initialize the array of
|
|
@@ -578,11 +570,11 @@ struct S {
|
|
| 578 |
|
| 579 |
— *end example*]
|
| 580 |
|
| 581 |
— *end note*]
|
| 582 |
|
| 583 |
-
[*Note
|
| 584 |
|
| 585 |
Static data members, non-static data members of anonymous union members,
|
| 586 |
and unnamed bit-fields are not considered elements of the aggregate.
|
| 587 |
|
| 588 |
[*Example 6*:
|
|
@@ -665,11 +657,11 @@ struct A {
|
|
| 665 |
}; // Initialization not required for A::s3 because A::i3 is also not initialized
|
| 666 |
```
|
| 667 |
|
| 668 |
— *end example*]
|
| 669 |
|
| 670 |
-
When initializing a
|
| 671 |
initialize the elements with the last (rightmost) index of the array
|
| 672 |
varying the fastest [[dcl.array]].
|
| 673 |
|
| 674 |
[*Example 10*:
|
| 675 |
|
|
@@ -739,11 +731,11 @@ the element with an *assignment-expression*. If the
|
|
| 739 |
*assignment-expression* can initialize an element, the element is
|
| 740 |
initialized. Otherwise, if the element is itself a subaggregate, brace
|
| 741 |
elision is assumed and the *assignment-expression* is considered for the
|
| 742 |
initialization of the first element of the subaggregate.
|
| 743 |
|
| 744 |
-
[*Note
|
| 745 |
subaggregates with no elements; an *initializer-clause* for the entire
|
| 746 |
subobject is required. — *end note*]
|
| 747 |
|
| 748 |
[*Example 12*:
|
| 749 |
|
|
@@ -764,16 +756,16 @@ Braces are elided around the *initializer-clause* for `b.a1.i`. `b.a1.i`
|
|
| 764 |
is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
|
| 765 |
initialized with whatever `a.operator int()` returns.
|
| 766 |
|
| 767 |
— *end example*]
|
| 768 |
|
| 769 |
-
[*Note
|
| 770 |
elements of a class type with a user-declared constructor
|
| 771 |
[[class.ctor]]. Initialization of these aggregate objects is described
|
| 772 |
in [[class.expl.init]]. — *end note*]
|
| 773 |
|
| 774 |
-
[*Note
|
| 775 |
duration is static or dynamic is specified in [[basic.start.static]],
|
| 776 |
[[basic.start.dynamic]], and [[stmt.dcl]]. — *end note*]
|
| 777 |
|
| 778 |
When a union is initialized with an initializer list, there shall not be
|
| 779 |
more than one explicitly initialized element.
|
|
@@ -791,23 +783,27 @@ u f = { .b = "asdf" };
|
|
| 791 |
u g = { .a = 1, .b = "asdf" }; // error
|
| 792 |
```
|
| 793 |
|
| 794 |
— *end example*]
|
| 795 |
|
| 796 |
-
[*Note
|
| 797 |
*initializer-clause* for a union member can be omitted if the union is a
|
| 798 |
member of another aggregate. — *end note*]
|
| 799 |
|
| 800 |
### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
|
| 801 |
|
| 802 |
An array of ordinary character type [[basic.fundamental]], `char8_t`
|
| 803 |
-
array, `char16_t` array, `char32_t` array, or `wchar_t` array
|
| 804 |
initialized by an ordinary string literal, UTF-8 string literal, UTF-16
|
| 805 |
string literal, UTF-32 string literal, or wide string literal,
|
| 806 |
respectively, or by an appropriately-typed *string-literal* enclosed in
|
| 807 |
-
braces [[lex.string]].
|
| 808 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 809 |
|
| 810 |
[*Example 1*:
|
| 811 |
|
| 812 |
``` cpp
|
| 813 |
char msg[] = "Syntax error on line %s\n";
|
|
@@ -835,12 +831,12 @@ If there are fewer initializers than there are array elements, each
|
|
| 835 |
element not explicitly initialized shall be zero-initialized
|
| 836 |
[[dcl.init]].
|
| 837 |
|
| 838 |
### References <a id="dcl.init.ref">[[dcl.init.ref]]</a>
|
| 839 |
|
| 840 |
-
A variable whose declared type is “reference to
|
| 841 |
-
|
| 842 |
|
| 843 |
[*Example 1*:
|
| 844 |
|
| 845 |
``` cpp
|
| 846 |
int g(int) noexcept;
|
|
@@ -905,14 +901,14 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
|
|
| 905 |
“*cv3* `T3`”, where “*cv1* `T1`” is reference-compatible with “*cv3*
|
| 906 |
`T3`”[^7] (this conversion is selected by enumerating the applicable
|
| 907 |
conversion functions [[over.match.ref]] and choosing the best one
|
| 908 |
through overload resolution [[over.match]]),
|
| 909 |
|
| 910 |
-
then the reference
|
| 911 |
-
|
| 912 |
-
|
| 913 |
-
|
| 914 |
\[*Note 2*: The usual lvalue-to-rvalue [[conv.lval]], array-to-pointer
|
| 915 |
[[conv.array]], and function-to-pointer [[conv.func]] standard
|
| 916 |
conversions are not needed, and therefore are suppressed, when such
|
| 917 |
direct bindings to lvalues are done. — *end note*]
|
| 918 |
\[*Example 3*:
|
|
@@ -946,55 +942,57 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
|
|
| 946 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 947 |
reference-related to `T2`, and can be converted to an rvalue or
|
| 948 |
function lvalue of type “*cv3* `T3`”, where “*cv1* `T1`” is
|
| 949 |
reference-compatible with “*cv3* `T3`” (see [[over.match.ref]]),
|
| 950 |
|
| 951 |
-
then the
|
| 952 |
-
|
| 953 |
-
|
| 954 |
-
|
| 955 |
-
|
| 956 |
-
|
| 957 |
-
class subobject).
|
| 958 |
\[*Example 5*:
|
| 959 |
``` cpp
|
| 960 |
struct A { };
|
| 961 |
struct B : A { } b;
|
| 962 |
extern B f();
|
| 963 |
-
const A& rca2 = f(); //
|
| 964 |
A&& rra = f(); // same as above
|
| 965 |
struct X {
|
| 966 |
operator B();
|
| 967 |
operator int&();
|
| 968 |
} x;
|
| 969 |
-
const A& r = x; //
|
| 970 |
int i2 = 42;
|
| 971 |
-
int&& rri = static_cast<int&&>(i2); //
|
| 972 |
-
B&& rrb = x; //
|
| 973 |
```
|
| 974 |
|
| 975 |
— *end example*]
|
| 976 |
- Otherwise:
|
| 977 |
- If `T1` or `T2` is a class type and `T1` is not reference-related to
|
| 978 |
`T2`, user-defined conversions are considered using the rules for
|
| 979 |
copy-initialization of an object of type “*cv1* `T1`” by
|
| 980 |
-
user-defined conversion
|
| 981 |
-
[[over.match.conv]]
|
| 982 |
-
|
| 983 |
-
|
| 984 |
-
non-reference copy-initialization, is
|
| 985 |
-
the reference. For this
|
| 986 |
-
conversions are not considered.
|
| 987 |
- Otherwise, the initializer expression is implicitly converted to a
|
| 988 |
-
prvalue of type “
|
| 989 |
-
|
|
|
|
| 990 |
|
| 991 |
If `T1` is reference-related to `T2`:
|
| 992 |
- *cv1* shall be the same cv-qualification as, or greater
|
| 993 |
cv-qualification than, *cv2*; and
|
| 994 |
- if the reference is an rvalue reference, the initializer expression
|
| 995 |
-
shall not be an lvalue.
|
|
|
|
|
|
|
| 996 |
|
| 997 |
\[*Example 6*:
|
| 998 |
``` cpp
|
| 999 |
struct Banana { };
|
| 1000 |
struct Enigma { operator const Banana(); };
|
|
@@ -1025,11 +1023,11 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
|
|
| 1025 |
|
| 1026 |
In all cases except the last (i.e., implicitly converting the
|
| 1027 |
initializer expression to the referenced type), the reference is said to
|
| 1028 |
*bind directly* to the initializer expression.
|
| 1029 |
|
| 1030 |
-
[*Note
|
| 1031 |
bound to references. — *end note*]
|
| 1032 |
|
| 1033 |
### List-initialization <a id="dcl.init.list">[[dcl.init.list]]</a>
|
| 1034 |
|
| 1035 |
*List-initialization* is initialization of an object or reference from a
|
|
@@ -1051,12 +1049,12 @@ List-initialization can be used
|
|
| 1051 |
- as the initializer in a *new-expression* [[expr.new]]
|
| 1052 |
- in a `return` statement [[stmt.return]]
|
| 1053 |
- as a *for-range-initializer* [[stmt.iter]]
|
| 1054 |
- as a function argument [[expr.call]]
|
| 1055 |
- as a subscript [[expr.sub]]
|
| 1056 |
-
- as an argument to a constructor invocation
|
| 1057 |
-
[[expr.type.conv]]
|
| 1058 |
- as an initializer for a non-static data member [[class.mem]]
|
| 1059 |
- in a *mem-initializer* [[class.base.init]]
|
| 1060 |
- on the right-hand side of an assignment [[expr.ass]]
|
| 1061 |
|
| 1062 |
[*Example 1*:
|
|
@@ -1088,12 +1086,13 @@ initializer list as the argument to the constructor template
|
|
| 1088 |
`template<class T> C(T)` of a class `C` does not create an
|
| 1089 |
initializer-list constructor, because an initializer list argument
|
| 1090 |
causes the corresponding parameter to be a non-deduced context
|
| 1091 |
[[temp.deduct.call]]. — *end note*]
|
| 1092 |
|
| 1093 |
-
The template `std::initializer_list` is not predefined; if
|
| 1094 |
-
|
|
|
|
| 1095 |
`std::initializer_list` — even an implicit use in which the type is not
|
| 1096 |
named [[dcl.spec.auto]] — the program is ill-formed.
|
| 1097 |
|
| 1098 |
List-initialization of an object or reference of type `T` is defined as
|
| 1099 |
follows:
|
|
@@ -1132,22 +1131,22 @@ follows:
|
|
| 1132 |
int m1;
|
| 1133 |
double m2, m3;
|
| 1134 |
};
|
| 1135 |
S2 s21 = { 1, 2, 3.0 }; // OK
|
| 1136 |
S2 s22 { 1.0, 2, 3 }; // error: narrowing
|
| 1137 |
-
S2 s23 { }; // OK
|
| 1138 |
```
|
| 1139 |
|
| 1140 |
— *end example*]
|
| 1141 |
- Otherwise, if the initializer list has no elements and `T` is a class
|
| 1142 |
type with a default constructor, the object is value-initialized.
|
| 1143 |
- Otherwise, if `T` is a specialization of `std::initializer_list<E>`,
|
| 1144 |
the object is constructed as described below.
|
| 1145 |
- Otherwise, if `T` is a class type, constructors are considered. The
|
| 1146 |
applicable constructors are enumerated and the best one is chosen
|
| 1147 |
-
through overload resolution
|
| 1148 |
-
|
| 1149 |
arguments, the program is ill-formed.
|
| 1150 |
\[*Example 4*:
|
| 1151 |
``` cpp
|
| 1152 |
struct S {
|
| 1153 |
S(std::initializer_list<double>); // #1
|
|
@@ -1176,13 +1175,13 @@ follows:
|
|
| 1176 |
// no initializer-list constructors
|
| 1177 |
S(int, double, double); // #1
|
| 1178 |
S(); // #2
|
| 1179 |
// ...
|
| 1180 |
};
|
| 1181 |
-
S s1 = { 1, 2, 3.0 }; // OK
|
| 1182 |
S s2 { 1.0, 2, 3 }; // error: narrowing
|
| 1183 |
-
S s3 { }; // OK
|
| 1184 |
```
|
| 1185 |
|
| 1186 |
— *end example*]
|
| 1187 |
- Otherwise, if `T` is an enumeration with a fixed underlying type
|
| 1188 |
[[dcl.enum]] `U`, the *initializer-list* has a single element `v`, `v`
|
|
@@ -1223,32 +1222,32 @@ follows:
|
|
| 1223 |
int x2 {2.0}; // error: narrowing
|
| 1224 |
```
|
| 1225 |
|
| 1226 |
— *end example*]
|
| 1227 |
- Otherwise, if `T` is a reference type, a prvalue is generated. The
|
| 1228 |
-
prvalue initializes its result object by copy-list-initialization
|
| 1229 |
-
prvalue is then used to direct-initialize
|
| 1230 |
-
the
|
| 1231 |
-
to array of unknown bound of `U`”, in which
|
| 1232 |
-
|
| 1233 |
-
the initializer list.
|
| 1234 |
\[*Note 3*: As usual, the binding will fail and the program is
|
| 1235 |
ill-formed if the reference type is an lvalue reference to a non-const
|
| 1236 |
type. — *end note*]
|
| 1237 |
\[*Example 9*:
|
| 1238 |
``` cpp
|
| 1239 |
struct S {
|
| 1240 |
S(std::initializer_list<double>); // #1
|
| 1241 |
S(const std::string&); // #2
|
| 1242 |
// ...
|
| 1243 |
};
|
| 1244 |
-
const S& r1 = { 1, 2, 3.0 }; // OK
|
| 1245 |
-
const S& r2 { "Spinach" }; // OK
|
| 1246 |
S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
|
| 1247 |
const int& i1 = { 1 }; // OK
|
| 1248 |
const int& i2 = { 1.1 }; // error: narrowing
|
| 1249 |
-
const int (&iar)[2] = { 1, 2 }; // OK
|
| 1250 |
|
| 1251 |
struct A { } a;
|
| 1252 |
struct B { explicit B(const A&); };
|
| 1253 |
const B& b2{a}; // error: cannot copy-list-initialize B temporary from A
|
| 1254 |
```
|
|
@@ -1367,27 +1366,34 @@ variable, so the array persists for the lifetime of the variable. For
|
|
| 1367 |
member, so the program is ill-formed [[class.base.init]].
|
| 1368 |
|
| 1369 |
— *end example*]
|
| 1370 |
|
| 1371 |
[*Note 6*: The implementation is free to allocate the array in
|
| 1372 |
-
read-only memory if an explicit array with the same initializer
|
| 1373 |
so allocated. — *end note*]
|
| 1374 |
|
| 1375 |
A *narrowing conversion* is an implicit conversion
|
| 1376 |
|
| 1377 |
- from a floating-point type to an integer type, or
|
| 1378 |
-
- from
|
| 1379 |
-
|
|
|
|
| 1380 |
actual value after conversion is within the range of values that can
|
| 1381 |
be represented (even if it cannot be represented exactly), or
|
| 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
|
| 1387 |
that cannot represent all the values of the original type, except
|
| 1388 |
-
where
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1389 |
promotions will fit into the target type, or
|
| 1390 |
- from a pointer type or a pointer-to-member type to `bool`.
|
| 1391 |
|
| 1392 |
[*Note 7*: As indicated above, such conversions are not allowed at the
|
| 1393 |
top level in list-initializations. — *end note*]
|
|
@@ -1396,24 +1402,24 @@ top level in list-initializations. — *end note*]
|
|
| 1396 |
|
| 1397 |
``` cpp
|
| 1398 |
int x = 999; // x is not a constant expression
|
| 1399 |
const int y = 999;
|
| 1400 |
const int z = 99;
|
| 1401 |
-
char c1 = x; // OK, though it
|
| 1402 |
-
char c2{x}; // error:
|
| 1403 |
char c3{y}; // error: narrows (assuming char is 8 bits)
|
| 1404 |
-
char c4{z}; // OK
|
| 1405 |
-
unsigned char uc1 = {5}; // OK
|
| 1406 |
unsigned char uc2 = {-1}; // error: narrows
|
| 1407 |
unsigned int ui1 = {-1}; // error: narrows
|
| 1408 |
signed int si1 =
|
| 1409 |
{ (unsigned int)-1 }; // error: narrows
|
| 1410 |
int ii = {2.0}; // error: narrows
|
| 1411 |
-
float f1 { x }; // error:
|
| 1412 |
-
float f2 { 7 }; // OK
|
| 1413 |
bool b = {"meow"}; // error: narrows
|
| 1414 |
int f(int);
|
| 1415 |
-
int a[] = { 2, f(2), f(2.0) }; // OK
|
| 1416 |
```
|
| 1417 |
|
| 1418 |
— *end example*]
|
| 1419 |
|
|
|
|
| 1 |
## Initializers <a id="dcl.init">[[dcl.init]]</a>
|
| 2 |
|
| 3 |
+
### General <a id="dcl.init.general">[[dcl.init.general]]</a>
|
| 4 |
+
|
| 5 |
+
The process of initialization described in [[dcl.init]] applies to all
|
| 6 |
initializations regardless of syntactic context, including the
|
| 7 |
initialization of a function parameter [[expr.call]], the initialization
|
| 8 |
of a return value [[stmt.return]], or when an initializer follows a
|
| 9 |
declarator.
|
| 10 |
|
|
|
|
| 59 |
expr-or-braced-init-list:
|
| 60 |
expression
|
| 61 |
braced-init-list
|
| 62 |
```
|
| 63 |
|
| 64 |
+
[*Note 1*: The rules in [[dcl.init]] apply even if the grammar permits
|
| 65 |
+
only the *brace-or-equal-initializer* form of *initializer* in a given
|
| 66 |
+
context. — *end note*]
|
| 67 |
|
| 68 |
Except for objects declared with the `constexpr` specifier, for which
|
| 69 |
see [[dcl.constexpr]], an *initializer* in the definition of a variable
|
| 70 |
can consist of arbitrary expressions involving literals and previously
|
| 71 |
declared variables and functions, regardless of the variable’s storage
|
|
|
|
| 87 |
|
| 88 |
[*Note 3*: The order of initialization of variables with static storage
|
| 89 |
duration is described in [[basic.start]] and
|
| 90 |
[[stmt.dcl]]. — *end note*]
|
| 91 |
|
| 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 a scalar type [[term.scalar.type]], the object 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
|
| 103 |
+
the object is not a base class subobject, each virtual base class
|
| 104 |
+
subobject is zero-initialized;
|
| 105 |
- if `T` is a (possibly cv-qualified) union type, its padding bits
|
| 106 |
+
[[term.padding.bits]] are initialized to zero bits and the object’s
|
| 107 |
+
first non-static named data member is zero-initialized;
|
| 108 |
- if `T` is an array type, each element is zero-initialized;
|
| 109 |
- if `T` is a reference type, no initialization is performed.
|
| 110 |
|
| 111 |
To *default-initialize* an object of type `T` means:
|
| 112 |
|
|
|
|
| 156 |
[*Note 4*: For every object of static storage duration, static
|
| 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
|
| 170 |
in argument passing, function return, throwing an exception
|
| 171 |
[[except.throw]], handling an exception [[except.handle]], and aggregate
|
| 172 |
+
member initialization other than by a *designated-initializer-clause*
|
| 173 |
+
[[dcl.init.aggr]], is called *copy-initialization*.
|
| 174 |
|
| 175 |
+
[*Note 5*: Copy-initialization can invoke a move
|
| 176 |
[[class.copy.ctor]]. — *end note*]
|
| 177 |
|
| 178 |
The initialization that occurs
|
| 179 |
|
| 180 |
- for an *initializer* that is a parenthesized *expression-list* or a
|
|
|
|
| 199 |
- If the destination type is an array of characters, an array of
|
| 200 |
`char8_t`, an array of `char16_t`, an array of `char32_t`, or an array
|
| 201 |
of `wchar_t`, and the initializer is a *string-literal*, see
|
| 202 |
[[dcl.init.string]].
|
| 203 |
- If the initializer is `()`, the object is value-initialized.
|
| 204 |
+
\[*Note 6*:
|
| 205 |
+
Since `()` is not permitted by the syntax for *initializer*,
|
| 206 |
+
``` cpp
|
| 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 |
+
is permitted in certain other initialization contexts
|
| 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. Let x₁, …, xₖ be the elements of the
|
| 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
|
|
|
|
| 226 |
- Otherwise, if the destination type is a (possibly cv-qualified) class
|
| 227 |
type:
|
| 228 |
- If the initializer expression is a prvalue and the cv-unqualified
|
| 229 |
version of the source type is the same class as the class of the
|
| 230 |
destination, the initializer expression is used to initialize the
|
| 231 |
+
destination object. \[*Example 2*: `T x = T(T(T()));`
|
| 232 |
+
value-initializes `x`. — *end example*]
|
| 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 class as, or a derived class of, the class
|
| 236 |
of the destination, constructors are considered. The applicable
|
| 237 |
constructors are enumerated [[over.match.ctor]], and the best one is
|
|
|
|
| 254 |
\[*Note 7*:
|
| 255 |
By contrast with direct-list-initialization, narrowing conversions
|
| 256 |
[[dcl.init.list]] are permitted, designators are not permitted, a
|
| 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 {
|
| 262 |
int a;
|
| 263 |
int&& r;
|
| 264 |
};
|
|
|
|
| 316 |
int c = b;
|
| 317 |
```
|
| 318 |
|
| 319 |
— *end note*]
|
| 320 |
|
| 321 |
+
An immediate invocation [[expr.const]] that is not evaluated where it
|
| 322 |
+
appears [[dcl.fct.default]], [[class.mem.general]] is evaluated and
|
| 323 |
+
checked for whether it is a constant expression at the point where the
|
| 324 |
+
enclosing *initializer* is used in a function call, a constructor
|
| 325 |
+
definition, or an aggregate initialization.
|
| 326 |
+
|
| 327 |
An *initializer-clause* followed by an ellipsis is a pack expansion
|
| 328 |
[[temp.variadic]].
|
| 329 |
|
| 330 |
+
Initialization includes the evaluation of all subexpressions of each
|
| 331 |
+
*initializer-clause* of the initializer (possibly nested within
|
| 332 |
+
*braced-init-list*s) and the creation of any temporary objects for
|
| 333 |
+
function arguments or return values [[class.temporary]].
|
| 334 |
+
|
| 335 |
If the initializer is a parenthesized *expression-list*, the expressions
|
| 336 |
are evaluated in the order specified for function calls [[expr.call]].
|
| 337 |
|
| 338 |
The same *identifier* shall not appear in multiple *designator*s of a
|
| 339 |
*designated-initializer-list*.
|
|
|
|
| 355 |
from an explicit initializer or by default-initialization, is called the
|
| 356 |
*initializing declaration* of that variable.
|
| 357 |
|
| 358 |
[*Note 10*: In most cases this is the defining declaration
|
| 359 |
[[basic.def]] of the variable, but the initializing declaration of a
|
| 360 |
+
non-inline static data member [[class.static.data]] can be the
|
| 361 |
+
declaration within the class definition and not the definition (if any)
|
| 362 |
+
outside it. — *end note*]
|
| 363 |
|
| 364 |
### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
|
| 365 |
|
| 366 |
An *aggregate* is an array or a class [[class]] with
|
| 367 |
|
| 368 |
- no user-declared or inherited constructors [[class.ctor]],
|
| 369 |
- no private or protected direct non-static data members
|
| 370 |
[[class.access]],
|
| 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 or constructors. — *end note*]
|
| 377 |
|
| 378 |
The *elements* of an aggregate are:
|
|
|
|
| 385 |
When an aggregate is initialized by an initializer list as specified in
|
| 386 |
[[dcl.init.list]], the elements of the initializer list are taken as
|
| 387 |
initializers for the elements of the aggregate. The *explicitly
|
| 388 |
initialized elements* of the aggregate are determined as follows:
|
| 389 |
|
| 390 |
+
- If the initializer list is a brace-enclosed
|
| 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 the first n
|
| 397 |
+
elements of the aggregate, where n is the number of elements in the
|
| 398 |
+
initializer list.
|
| 399 |
- Otherwise, the initializer list must be `{}`, and there are no
|
| 400 |
explicitly initialized elements.
|
| 401 |
|
| 402 |
For each explicitly initialized element:
|
| 403 |
|
| 404 |
+
- If the element is an anonymous union member and the initializer list
|
| 405 |
+
is a brace-enclosed *designated-initializer-list*, the element is
|
| 406 |
+
initialized by the *braced-init-list* `{ `*D*` }`, where *D* is the
|
| 407 |
+
*designated-initializer-clause* naming a member of the anonymous union
|
| 408 |
+
member. There shall be only one such *designated-initializer-clause*.
|
|
|
|
| 409 |
\[*Example 1*:
|
| 410 |
``` cpp
|
| 411 |
struct C {
|
| 412 |
union {
|
| 413 |
int a;
|
|
|
|
| 424 |
*brace-or-equal-initializer* of the corresponding
|
| 425 |
*designated-initializer-clause*. If that initializer is of the form
|
| 426 |
*assignment-expression* or `= `*assignment-expression* and a narrowing
|
| 427 |
conversion [[dcl.init.list]] is required to convert the expression,
|
| 428 |
the program is ill-formed.
|
| 429 |
+
\[*Note 2*: If the initialization is by
|
| 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*:
|
| 438 |
``` cpp
|
|
|
|
| 528 |
|
| 529 |
The destructor for each element of class type is potentially invoked
|
| 530 |
[[class.dtor]] from the context where the aggregate initialization
|
| 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 |
An array of unknown bound initialized with a brace-enclosed
|
| 538 |
*initializer-list* containing `n` *initializer-clause*s is defined as
|
|
|
|
| 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 |
|
| 557 |
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
|
|
|
|
| 570 |
|
| 571 |
— *end example*]
|
| 572 |
|
| 573 |
— *end note*]
|
| 574 |
|
| 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 6*:
|
|
|
|
| 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 10*:
|
| 667 |
|
|
|
|
| 731 |
*assignment-expression* can initialize an element, the element is
|
| 732 |
initialized. Otherwise, if the element is itself a subaggregate, brace
|
| 733 |
elision is assumed and the *assignment-expression* is considered for the
|
| 734 |
initialization of the first element of the subaggregate.
|
| 735 |
|
| 736 |
+
[*Note 7*: As specified above, brace elision cannot apply to
|
| 737 |
subaggregates with no elements; an *initializer-clause* for the entire
|
| 738 |
subobject is required. — *end note*]
|
| 739 |
|
| 740 |
[*Example 12*:
|
| 741 |
|
|
|
|
| 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 8*: An aggregate array or an aggregate class can contain
|
| 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 9*: Whether the initialization of aggregates with static storage
|
| 767 |
duration is static or dynamic is specified in [[basic.start.static]],
|
| 768 |
[[basic.start.dynamic]], and [[stmt.dcl]]. — *end note*]
|
| 769 |
|
| 770 |
When a union is initialized with an initializer list, there shall not be
|
| 771 |
more than one explicitly initialized element.
|
|
|
|
| 783 |
u g = { .a = 1, .b = "asdf" }; // error
|
| 784 |
```
|
| 785 |
|
| 786 |
— *end example*]
|
| 787 |
|
| 788 |
+
[*Note 10*: As described above, the braces around the
|
| 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 |
|
| 794 |
An array of ordinary character type [[basic.fundamental]], `char8_t`
|
| 795 |
+
array, `char16_t` array, `char32_t` array, or `wchar_t` array may be
|
| 796 |
initialized by an ordinary string literal, UTF-8 string literal, UTF-16
|
| 797 |
string literal, UTF-32 string literal, or wide string literal,
|
| 798 |
respectively, or by an appropriately-typed *string-literal* enclosed in
|
| 799 |
+
braces [[lex.string]]. Additionally, an array of `char` or
|
| 800 |
+
`unsigned char` may be initialized by a UTF-8 string literal, or by such
|
| 801 |
+
a string literal enclosed in braces. Successive characters of the value
|
| 802 |
+
of the *string-literal* initialize the elements of the array, with an
|
| 803 |
+
integral conversion [[conv.integral]] if necessary for the source and
|
| 804 |
+
destination value.
|
| 805 |
|
| 806 |
[*Example 1*:
|
| 807 |
|
| 808 |
``` cpp
|
| 809 |
char msg[] = "Syntax error on line %s\n";
|
|
|
|
| 831 |
element not explicitly initialized shall be zero-initialized
|
| 832 |
[[dcl.init]].
|
| 833 |
|
| 834 |
### References <a id="dcl.init.ref">[[dcl.init.ref]]</a>
|
| 835 |
|
| 836 |
+
A variable whose declared type is “reference to `T`” [[dcl.ref]] shall
|
| 837 |
+
be initialized.
|
| 838 |
|
| 839 |
[*Example 1*:
|
| 840 |
|
| 841 |
``` cpp
|
| 842 |
int g(int) noexcept;
|
|
|
|
| 901 |
“*cv3* `T3`”, where “*cv1* `T1`” is reference-compatible with “*cv3*
|
| 902 |
`T3`”[^7] (this conversion is selected by enumerating the applicable
|
| 903 |
conversion functions [[over.match.ref]] and choosing the best one
|
| 904 |
through overload resolution [[over.match]]),
|
| 905 |
|
| 906 |
+
then the reference binds to the initializer expression lvalue in the
|
| 907 |
+
first case and to the lvalue result of the conversion in the second
|
| 908 |
+
case (or, in either case, to the appropriate base class subobject of
|
| 909 |
+
the object).
|
| 910 |
\[*Note 2*: The usual lvalue-to-rvalue [[conv.lval]], array-to-pointer
|
| 911 |
[[conv.array]], and function-to-pointer [[conv.func]] standard
|
| 912 |
conversions are not needed, and therefore are suppressed, when such
|
| 913 |
direct bindings to lvalues are done. — *end note*]
|
| 914 |
\[*Example 3*:
|
|
|
|
| 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 or
|
| 944 |
function lvalue of type “*cv3* `T3`”, where “*cv1* `T1`” is
|
| 945 |
reference-compatible with “*cv3* `T3`” (see [[over.match.ref]]),
|
| 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 `T4` is adjusted to
|
| 950 |
+
type “*cv1* `T4`” [[conv.qual]] and the temporary materialization
|
| 951 |
+
conversion [[conv.rval]] is applied. In any case, the reference binds
|
| 952 |
+
to the resulting glvalue (or to an appropriate base class subobject).
|
|
|
|
| 953 |
\[*Example 5*:
|
| 954 |
``` cpp
|
| 955 |
struct A { };
|
| 956 |
struct B : A { } b;
|
| 957 |
extern B f();
|
| 958 |
+
const A& rca2 = f(); // binds to the A subobject of the B rvalue.
|
| 959 |
A&& rra = f(); // same as above
|
| 960 |
struct X {
|
| 961 |
operator B();
|
| 962 |
operator int&();
|
| 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 and `T1` is not reference-related to
|
| 973 |
`T2`, user-defined conversions are considered using the rules for
|
| 974 |
copy-initialization of an object of type “*cv1* `T1`” by
|
| 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. For this
|
| 981 |
+
direct-initialization, user-defined conversions are not considered.
|
| 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(); };
|
|
|
|
| 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 4*: [[class.temporary]] describes the lifetime of temporaries
|
| 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
|
|
|
|
| 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 subscript [[expr.sub]]
|
| 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.ass]]
|
| 1059 |
|
| 1060 |
[*Example 1*:
|
|
|
|
| 1086 |
`template<class T> C(T)` of a class `C` does not create an
|
| 1087 |
initializer-list constructor, because an initializer list argument
|
| 1088 |
causes the corresponding parameter to be a non-deduced context
|
| 1089 |
[[temp.deduct.call]]. — *end note*]
|
| 1090 |
|
| 1091 |
+
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 defined as
|
| 1098 |
follows:
|
|
|
|
| 1131 |
int m1;
|
| 1132 |
double m2, m3;
|
| 1133 |
};
|
| 1134 |
S2 s21 = { 1, 2, 3.0 }; // OK
|
| 1135 |
S2 s22 { 1.0, 2, 3 }; // error: narrowing
|
| 1136 |
+
S2 s23 { }; // OK, default to 0,0,0
|
| 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<E>`,
|
| 1143 |
the object is constructed as described below.
|
| 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
|
|
|
|
| 1175 |
// no initializer-list constructors
|
| 1176 |
S(int, double, double); // #1
|
| 1177 |
S(); // #2
|
| 1178 |
// ...
|
| 1179 |
};
|
| 1180 |
+
S s1 = { 1, 2, 3.0 }; // OK, invoke #1
|
| 1181 |
S s2 { 1.0, 2, 3 }; // error: narrowing
|
| 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`, `v`
|
|
|
|
| 1222 |
int x2 {2.0}; // error: narrowing
|
| 1223 |
```
|
| 1224 |
|
| 1225 |
— *end example*]
|
| 1226 |
- Otherwise, if `T` is a reference type, a prvalue is generated. The
|
| 1227 |
+
prvalue initializes its result object by copy-list-initialization from
|
| 1228 |
+
the initializer list. The prvalue is then used to direct-initialize
|
| 1229 |
+
the reference. The type of the prvalue is the type referenced by `T`,
|
| 1230 |
+
unless `T` is “reference to array of unknown bound of `U`”, in which
|
| 1231 |
+
case the type of the prvalue is the type of `x` in the declaration
|
| 1232 |
+
`U x[] H`, where H is the initializer list.
|
| 1233 |
\[*Note 3*: As usual, the binding will fail and the program is
|
| 1234 |
ill-formed if the reference type is an lvalue reference to a non-const
|
| 1235 |
type. — *end note*]
|
| 1236 |
\[*Example 9*:
|
| 1237 |
``` cpp
|
| 1238 |
struct S {
|
| 1239 |
S(std::initializer_list<double>); // #1
|
| 1240 |
S(const std::string&); // #2
|
| 1241 |
// ...
|
| 1242 |
};
|
| 1243 |
+
const S& r1 = { 1, 2, 3.0 }; // OK, invoke #1
|
| 1244 |
+
const S& r2 { "Spinach" }; // OK, invoke #2
|
| 1245 |
S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
|
| 1246 |
const int& i1 = { 1 }; // OK
|
| 1247 |
const int& i2 = { 1.1 }; // error: narrowing
|
| 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 |
```
|
|
|
|
| 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 source is a constant expression and the
|
| 1380 |
actual value after conversion is within the range of values that can
|
| 1381 |
be represented (even if it cannot be represented exactly), or
|
| 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
|
| 1387 |
that cannot represent all the values of the original type, except
|
| 1388 |
+
where
|
| 1389 |
+
- the source is a bit-field whose width w is less than that of its
|
| 1390 |
+
type (or, for an enumeration type, its underlying type) and the
|
| 1391 |
+
target type can represent all the values of a hypothetical extended
|
| 1392 |
+
integer type with width w and with the same signedness as the
|
| 1393 |
+
original type or
|
| 1394 |
+
- the source is a constant expression whose value after integral
|
| 1395 |
promotions will fit into the target type, or
|
| 1396 |
- from a pointer type or a pointer-to-member type to `bool`.
|
| 1397 |
|
| 1398 |
[*Note 7*: As indicated above, such conversions are not allowed at the
|
| 1399 |
top level in list-initializations. — *end note*]
|
|
|
|
| 1402 |
|
| 1403 |
``` cpp
|
| 1404 |
int x = 999; // x is not a constant expression
|
| 1405 |
const int y = 999;
|
| 1406 |
const int z = 99;
|
| 1407 |
+
char c1 = x; // OK, though it potentially narrows (in this case, it does narrow)
|
| 1408 |
+
char c2{x}; // error: potentially narrows
|
| 1409 |
char c3{y}; // error: narrows (assuming char is 8 bits)
|
| 1410 |
+
char c4{z}; // OK, no narrowing needed
|
| 1411 |
+
unsigned char uc1 = {5}; // OK, no narrowing needed
|
| 1412 |
unsigned char uc2 = {-1}; // error: narrows
|
| 1413 |
unsigned int ui1 = {-1}; // error: narrows
|
| 1414 |
signed int si1 =
|
| 1415 |
{ (unsigned int)-1 }; // error: narrows
|
| 1416 |
int ii = {2.0}; // error: narrows
|
| 1417 |
+
float f1 { x }; // error: potentially narrows
|
| 1418 |
+
float f2 { 7 }; // OK, 7 can be exactly represented as a float
|
| 1419 |
bool b = {"meow"}; // error: narrows
|
| 1420 |
int f(int);
|
| 1421 |
+
int a[] = { 2, f(2), f(2.0) }; // OK, the double-to-int conversion is not at the top level
|
| 1422 |
```
|
| 1423 |
|
| 1424 |
— *end example*]
|
| 1425 |
|