- tmp/tmpbtg9xd55/{from.md → to.md} +610 -307
tmp/tmpbtg9xd55/{from.md → to.md}
RENAMED
|
@@ -2,13 +2,12 @@
|
|
| 2 |
|
| 3 |
A declarator can specify an initial value for the identifier being
|
| 4 |
declared. The identifier designates a variable being initialized. The
|
| 5 |
process of initialization described in the remainder of [[dcl.init]]
|
| 6 |
applies also to initializations specified by other syntactic contexts,
|
| 7 |
-
such as the initialization of function parameters
|
| 8 |
-
|
| 9 |
-
[[stmt.return]]).
|
| 10 |
|
| 11 |
``` bnf
|
| 12 |
initializer:
|
| 13 |
brace-or-equal-initializer
|
| 14 |
'(' expression-list ')'
|
|
@@ -36,59 +35,87 @@ initializer-list:
|
|
| 36 |
braced-init-list:
|
| 37 |
'{' initializer-list ','ₒₚₜ '}'
|
| 38 |
'{' '}'
|
| 39 |
```
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
Except for objects declared with the `constexpr` specifier, for which
|
| 42 |
see [[dcl.constexpr]], an *initializer* in the definition of a variable
|
| 43 |
can consist of arbitrary expressions involving literals and previously
|
| 44 |
declared variables and functions, regardless of the variable’s storage
|
| 45 |
duration.
|
| 46 |
|
|
|
|
|
|
|
| 47 |
``` cpp
|
| 48 |
int f(int);
|
| 49 |
int a = 2;
|
| 50 |
int b = f(a);
|
| 51 |
int c(b);
|
| 52 |
```
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
A declaration of a block-scope variable with external or internal
|
| 60 |
linkage that has an *initializer* is ill-formed.
|
| 61 |
|
| 62 |
To *zero-initialize* an object or reference of type `T` means:
|
| 63 |
|
| 64 |
- if `T` is a scalar type ([[basic.types]]), the object is initialized
|
| 65 |
to the value obtained by converting the integer literal `0` (zero) to
|
| 66 |
-
`T`;[^
|
| 67 |
- if `T` is a (possibly cv-qualified) non-union class type, each
|
| 68 |
-
non-static data member
|
| 69 |
-
|
|
|
|
| 70 |
- if `T` is a (possibly cv-qualified) union type, the object’s first
|
| 71 |
non-static named data member is zero-initialized and padding is
|
| 72 |
initialized to zero bits;
|
| 73 |
- if `T` is an array type, each element is zero-initialized;
|
| 74 |
- if `T` is a reference type, no initialization is performed.
|
| 75 |
|
| 76 |
To *default-initialize* an object of type `T` means:
|
| 77 |
|
| 78 |
-
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
-
|
| 85 |
-
-
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
To *value-initialize* an object of type `T` means:
|
| 92 |
|
| 93 |
- if `T` is a (possibly cv-qualified) class type (Clause [[class]])
|
| 94 |
with either no default constructor ([[class.ctor]]) or a default
|
|
@@ -100,26 +127,22 @@ To *value-initialize* an object of type `T` means:
|
|
| 100 |
and if `T` has a non-trivial default constructor, the object is
|
| 101 |
default-initialized;
|
| 102 |
- if `T` is an array type, then each element is value-initialized;
|
| 103 |
- otherwise, the object is zero-initialized.
|
| 104 |
|
| 105 |
-
An object that is value-initialized is deemed to be constructed and thus
|
| 106 |
-
subject to provisions of this International Standard applying to
|
| 107 |
-
“constructed” objects, objects “for which the constructor has
|
| 108 |
-
completed,” etc., even if no constructor is invoked for the object’s
|
| 109 |
-
initialization.
|
| 110 |
-
|
| 111 |
A program that calls for default-initialization or value-initialization
|
| 112 |
of an entity of reference type is ill-formed.
|
| 113 |
|
| 114 |
-
Every object of static storage duration is zero-initialized
|
| 115 |
-
startup before any other initialization takes place. In some
|
| 116 |
-
additional initialization is done later.
|
| 117 |
|
| 118 |
An object whose initializer is an empty set of parentheses, i.e., `()`,
|
| 119 |
shall be value-initialized.
|
| 120 |
|
|
|
|
|
|
|
| 121 |
Since `()` is not permitted by the syntax for *initializer*,
|
| 122 |
|
| 123 |
``` cpp
|
| 124 |
X a();
|
| 125 |
```
|
|
@@ -127,53 +150,71 @@ X a();
|
|
| 127 |
is not the declaration of an object of class `X`, but the declaration of
|
| 128 |
a function taking no argument and returning an `X`. The form `()` is
|
| 129 |
permitted in certain other initialization contexts ([[expr.new]],
|
| 130 |
[[expr.type.conv]], [[class.base.init]]).
|
| 131 |
|
|
|
|
|
|
|
| 132 |
If no initializer is specified for an object, the object is
|
| 133 |
default-initialized. When storage for an object with automatic or
|
| 134 |
dynamic storage duration is obtained, the object has an *indeterminate
|
| 135 |
value*, and if no initialization is performed for the object, that
|
| 136 |
object retains an indeterminate value until that value is replaced (
|
| 137 |
-
[[expr.ass]]).
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
- If an indeterminate value of unsigned narrow character type (
|
| 143 |
-
[[basic.fundamental]])
|
|
|
|
| 144 |
- the second or third operand of a conditional expression (
|
| 145 |
[[expr.cond]]),
|
| 146 |
- the right operand of a comma expression ([[expr.comma]]),
|
| 147 |
-
- the operand of a cast or conversion
|
| 148 |
-
|
| 149 |
-
|
|
|
|
| 150 |
- a discarded-value expression (Clause [[expr]]),
|
| 151 |
|
| 152 |
then the result of the operation is an indeterminate value.
|
| 153 |
-
- If an indeterminate value of unsigned narrow character type
|
| 154 |
-
produced by the evaluation of the right operand of
|
| 155 |
-
operator ([[expr.ass]]) whose first operand is an
|
| 156 |
-
narrow character type
|
| 157 |
-
the object referred to by
|
|
|
|
| 158 |
- If an indeterminate value of unsigned narrow character type is
|
| 159 |
produced by the evaluation of the initialization expression when
|
| 160 |
initializing an object of unsigned narrow character type, that object
|
| 161 |
is initialized to an indeterminate value.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
``` cpp
|
| 164 |
int f(bool b) {
|
| 165 |
unsigned char c;
|
| 166 |
unsigned char d = c; // OK, d has an indeterminate value
|
| 167 |
int e = d; // undefined behavior
|
| 168 |
return b ? d : 0; // undefined behavior if b is true
|
| 169 |
}
|
| 170 |
```
|
| 171 |
|
|
|
|
|
|
|
| 172 |
An initializer for a static member is in the scope of the member’s
|
| 173 |
class.
|
| 174 |
|
|
|
|
|
|
|
| 175 |
``` cpp
|
| 176 |
int a;
|
| 177 |
|
| 178 |
struct X {
|
| 179 |
static int a;
|
|
@@ -182,60 +223,65 @@ struct X {
|
|
| 182 |
|
| 183 |
int X::a = 1;
|
| 184 |
int X::b = a; // X::b = X::a
|
| 185 |
```
|
| 186 |
|
| 187 |
-
|
| 188 |
-
insignificant, but does matter when the initializer or the entity being
|
| 189 |
-
initialized has a class type; see below. If the entity being initialized
|
| 190 |
-
does not have class type, the *expression-list* in a parenthesized
|
| 191 |
-
initializer shall be a single expression.
|
| 192 |
|
| 193 |
-
|
|
|
|
|
|
|
| 194 |
|
| 195 |
-
``
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
as well as in argument passing, function return, throwing an exception (
|
| 200 |
[[except.throw]]), handling an exception ([[except.handle]]), and
|
| 201 |
-
aggregate member initialization ([[dcl.init.aggr]]) is called
|
| 202 |
-
*copy-initialization*.
|
| 203 |
-
|
|
|
|
|
|
|
| 204 |
|
| 205 |
The initialization that occurs in the forms
|
| 206 |
|
| 207 |
``` cpp
|
| 208 |
T x(a);
|
| 209 |
T x{a};
|
| 210 |
```
|
| 211 |
|
| 212 |
-
as well as in `new` expressions
|
| 213 |
-
expressions
|
| 214 |
-
conversions
|
| 215 |
-
[[class.base.init]])
|
|
|
|
| 216 |
|
| 217 |
The semantics of initializers are as follows. The *destination type* is
|
| 218 |
the type of the object or reference being initialized and the *source
|
| 219 |
type* is the type of the initializer expression. If the initializer is
|
| 220 |
not a single (possibly parenthesized) expression, the source type is not
|
| 221 |
defined.
|
| 222 |
|
| 223 |
-
- If the initializer is a (non-parenthesized) *braced-init-list*
|
| 224 |
-
object or reference is list-initialized (
|
|
|
|
| 225 |
- If the destination type is a reference type, see [[dcl.init.ref]].
|
| 226 |
- If the destination type is an array of characters, an array of
|
| 227 |
`char16_t`, an array of `char32_t`, or an array of `wchar_t`, and the
|
| 228 |
initializer is a string literal, see [[dcl.init.string]].
|
| 229 |
- If the initializer is `()`, the object is value-initialized.
|
| 230 |
- Otherwise, if the destination type is an array, the program is
|
| 231 |
ill-formed.
|
| 232 |
- If the destination type is a (possibly cv-qualified) class type:
|
| 233 |
-
- If the
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
destination
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
constructors are enumerated ([[over.match.ctor]]), and the best one
|
| 238 |
is chosen through overload resolution ([[over.match]]). The
|
| 239 |
constructor so selected is called to initialize the object, with the
|
| 240 |
initializer expression or *expression-list* as its argument(s). If
|
| 241 |
no constructor applies, or the overload resolution is ambiguous, the
|
|
@@ -246,20 +292,15 @@ defined.
|
|
| 246 |
to a derived class thereof are enumerated as described in
|
| 247 |
[[over.match.copy]], and the best one is chosen through overload
|
| 248 |
resolution ([[over.match]]). If the conversion cannot be done or is
|
| 249 |
ambiguous, the initialization is ill-formed. The function selected
|
| 250 |
is called with the initializer expression as its argument; if the
|
| 251 |
-
function is a constructor, the call
|
| 252 |
-
cv-unqualified version of the destination type
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
the
|
| 256 |
-
copy-initialization. In certain cases, an implementation is
|
| 257 |
-
permitted to eliminate the copying inherent in this
|
| 258 |
-
direct-initialization by constructing the intermediate result
|
| 259 |
-
directly into the object being initialized; see
|
| 260 |
-
[[class.temporary]], [[class.copy]].
|
| 261 |
- Otherwise, if the source type is a (possibly cv-qualified) class type,
|
| 262 |
conversion functions are considered. The applicable conversion
|
| 263 |
functions are enumerated ([[over.match.conv]]), and the best one is
|
| 264 |
chosen through overload resolution ([[over.match]]). The user-defined
|
| 265 |
conversion so selected is called to convert the initializer expression
|
|
@@ -268,40 +309,84 @@ defined.
|
|
| 268 |
- Otherwise, the initial value of the object being initialized is the
|
| 269 |
(possibly converted) value of the initializer expression. Standard
|
| 270 |
conversions (Clause [[conv]]) will be used, if necessary, to convert
|
| 271 |
the initializer expression to the cv-unqualified version of the
|
| 272 |
destination type; no user-defined conversions are considered. If the
|
| 273 |
-
conversion cannot be done, the initialization is ill-formed.
|
| 274 |
-
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
| 276 |
``` cpp
|
| 277 |
int a;
|
| 278 |
const int b = a;
|
| 279 |
int c = b;
|
| 280 |
```
|
| 281 |
|
|
|
|
|
|
|
| 282 |
An *initializer-clause* followed by an ellipsis is a pack expansion (
|
| 283 |
[[temp.variadic]]).
|
| 284 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
|
| 286 |
|
| 287 |
-
An *aggregate* is an array or a class (Clause [[class]]) with
|
| 288 |
-
user-provided constructors ([[class.ctor]]), no private or protected
|
| 289 |
-
non-static data members (Clause [[class.access]]), no base classes
|
| 290 |
-
(Clause [[class.derived]]), and no virtual functions (
|
| 291 |
-
[[class.virtual]]).
|
| 292 |
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
``` cpp
|
| 305 |
struct A {
|
| 306 |
int x;
|
| 307 |
struct B {
|
|
@@ -311,29 +396,79 @@ struct A {
|
|
| 311 |
} a = { 1, { 2, 3 } };
|
| 312 |
```
|
| 313 |
|
| 314 |
initializes `a.x` with 1, `a.b.i` with 2, `a.b.j` with 3.
|
| 315 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
An aggregate that is a class can also be initialized with a single
|
| 317 |
expression not enclosed in braces, as described in [[dcl.init]].
|
| 318 |
|
| 319 |
-
An array of unknown
|
| 320 |
*initializer-list* containing `n` *initializer-clause*s, where `n` shall
|
| 321 |
-
be greater than zero, is defined as having `n` elements
|
| 322 |
[[dcl.array]]).
|
| 323 |
|
|
|
|
|
|
|
| 324 |
``` cpp
|
| 325 |
int x[] = { 1, 3, 5 };
|
| 326 |
```
|
| 327 |
|
| 328 |
declares and initializes `x` as a one-dimensional array that has three
|
| 329 |
elements since no size was specified and there are three initializers.
|
|
|
|
|
|
|
|
|
|
| 330 |
An empty initializer list `{}` shall not be used as the
|
| 331 |
-
*initializer-clause
|
| 332 |
|
| 333 |
-
|
| 334 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
|
| 336 |
``` cpp
|
| 337 |
struct A {
|
| 338 |
int i;
|
| 339 |
static int s;
|
|
@@ -343,27 +478,47 @@ struct A {
|
|
| 343 |
} a = { 1, 2, 3 };
|
| 344 |
```
|
| 345 |
|
| 346 |
Here, the second initializer 2 initializes `a.j` and not the static data
|
| 347 |
member `A::s`, and the third initializer 3 initializes `a.k` and not the
|
| 348 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
|
| 350 |
An *initializer-list* is ill-formed if the number of
|
| 351 |
-
*initializer-clause*s exceeds the number of
|
| 352 |
-
|
|
|
|
| 353 |
|
| 354 |
``` cpp
|
| 355 |
char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
|
| 356 |
```
|
| 357 |
|
| 358 |
is ill-formed.
|
| 359 |
|
|
|
|
|
|
|
| 360 |
If there are fewer *initializer-clause*s in the list than there are
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
[[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
|
| 366 |
``` cpp
|
| 367 |
struct S { int a; const char* b; int c; int d = b[a]; };
|
| 368 |
S ss = { 1, "asdf" };
|
| 369 |
```
|
|
@@ -378,17 +533,39 @@ X a[] = { 1, 2, 3, 4, 5, 6 };
|
|
| 378 |
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
|
| 379 |
```
|
| 380 |
|
| 381 |
`a` and `b` have the same value
|
| 382 |
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
*initializer-list* for an object of type `C` unless the
|
| 387 |
-
*initializer-clause*s for all
|
| 388 |
omitted.
|
| 389 |
|
|
|
|
|
|
|
| 390 |
``` cpp
|
| 391 |
struct S { } s;
|
| 392 |
struct A {
|
| 393 |
S s1;
|
| 394 |
int i1;
|
|
@@ -402,17 +579,18 @@ struct A {
|
|
| 402 |
s, // Required initialization
|
| 403 |
0
|
| 404 |
}; // Initialization not required for A::s3 because A::i3 is also not initialized
|
| 405 |
```
|
| 406 |
|
| 407 |
-
|
| 408 |
-
reference type uninitialized, the program is ill-formed.
|
| 409 |
|
| 410 |
When initializing a multi-dimensional array, the *initializer-clause*s
|
| 411 |
initialize the elements with the last (rightmost) index of the array
|
| 412 |
varying the fastest ([[dcl.array]]).
|
| 413 |
|
|
|
|
|
|
|
| 414 |
``` cpp
|
| 415 |
int x[2][2] = { 3, 1, 4, 2 };
|
| 416 |
```
|
| 417 |
|
| 418 |
initializes `x[0][0]` to `3`, `x[0][1]` to `1`, `x[1][0]` to `4`, and
|
|
@@ -425,20 +603,24 @@ float y[4][3] = {
|
|
| 425 |
```
|
| 426 |
|
| 427 |
initializes the first column of `y` (regarded as a two-dimensional
|
| 428 |
array) and leaves the rest zero.
|
| 429 |
|
|
|
|
|
|
|
| 430 |
Braces can be elided in an *initializer-list* as follows. If the
|
| 431 |
*initializer-list* begins with a left brace, then the succeeding
|
| 432 |
-
comma-separated list of *initializer-clause*s initializes the
|
| 433 |
-
a subaggregate; it is erroneous for there to be more
|
| 434 |
-
*initializer-clause*s than
|
| 435 |
for a subaggregate does not begin with a left brace, then only enough
|
| 436 |
-
*initializer-clause*s from the list are taken to initialize the
|
| 437 |
of the subaggregate; any remaining *initializer-clause*s are left to
|
| 438 |
-
initialize the next
|
| 439 |
-
subaggregate is
|
|
|
|
|
|
|
| 440 |
|
| 441 |
``` cpp
|
| 442 |
float y[4][3] = {
|
| 443 |
{ 1, 3, 5 },
|
| 444 |
{ 2, 4, 6 },
|
|
@@ -464,19 +646,24 @@ float y[4][3] = {
|
|
| 464 |
|
| 465 |
The initializer for `y` begins with a left brace, but the one for `y[0]`
|
| 466 |
does not, therefore three elements from the list are used. Likewise the
|
| 467 |
next three are taken successively for `y[1]` and `y[2]`.
|
| 468 |
|
|
|
|
|
|
|
| 469 |
All implicit type conversions (Clause [[conv]]) are considered when
|
| 470 |
-
initializing the
|
| 471 |
-
|
| 472 |
-
initialized. Otherwise, if the
|
| 473 |
elision is assumed and the *assignment-expression* is considered for the
|
| 474 |
-
initialization of the first
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
|
|
|
|
|
|
|
|
|
| 478 |
|
| 479 |
``` cpp
|
| 480 |
struct A {
|
| 481 |
int i;
|
| 482 |
operator int();
|
|
@@ -491,34 +678,41 @@ B b = { 4, a, a };
|
|
| 491 |
|
| 492 |
Braces are elided around the *initializer-clause* for `b.a1.i`. `b.a1.i`
|
| 493 |
is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
|
| 494 |
initialized with whatever `a.operator int()` returns.
|
| 495 |
|
| 496 |
-
|
| 497 |
-
type with a user-provided constructor ([[class.ctor]]). Initialization
|
| 498 |
-
of these aggregate objects is described in [[class.expl.init]].
|
| 499 |
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
[[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 503 |
|
| 504 |
When a union is initialized with a brace-enclosed initializer, the
|
| 505 |
braces shall only contain an *initializer-clause* for the first
|
| 506 |
non-static data member of the union.
|
| 507 |
|
|
|
|
|
|
|
| 508 |
``` cpp
|
| 509 |
union u { int a; const char* b; };
|
| 510 |
u a = { 1 };
|
| 511 |
u b = a;
|
| 512 |
u c = 1; // error
|
| 513 |
u d = { 0, "asdf" }; // error
|
| 514 |
u e = { "asdf" }; // error
|
| 515 |
```
|
| 516 |
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
|
|
|
|
|
|
| 520 |
|
| 521 |
### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
|
| 522 |
|
| 523 |
An array of narrow character type ([[basic.fundamental]]), `char16_t`
|
| 524 |
array, `char32_t` array, or `wchar_t` array can be initialized by a
|
|
@@ -526,38 +720,47 @@ narrow string literal, `char16_t` string literal, `char32_t` string
|
|
| 526 |
literal, or wide string literal, respectively, or by an
|
| 527 |
appropriately-typed string literal enclosed in braces ([[lex.string]]).
|
| 528 |
Successive characters of the value of the string literal initialize the
|
| 529 |
elements of the array.
|
| 530 |
|
|
|
|
|
|
|
| 531 |
``` cpp
|
| 532 |
char msg[] = "Syntax error on line %s\n";
|
| 533 |
```
|
| 534 |
|
| 535 |
shows a character array whose members are initialized with a
|
| 536 |
*string-literal*. Note that because `'\n'` is a single character and
|
| 537 |
because a trailing `'\0'` is appended, `sizeof(msg)` is `25`.
|
| 538 |
|
|
|
|
|
|
|
| 539 |
There shall not be more initializers than there are array elements.
|
| 540 |
|
|
|
|
|
|
|
| 541 |
``` cpp
|
| 542 |
char cv[4] = "asdf"; // error
|
| 543 |
```
|
| 544 |
|
| 545 |
is ill-formed since there is no space for the implied trailing `'\0'`.
|
| 546 |
|
|
|
|
|
|
|
| 547 |
If there are fewer initializers than there are array elements, each
|
| 548 |
element not explicitly initialized shall be zero-initialized (
|
| 549 |
[[dcl.init]]).
|
| 550 |
|
| 551 |
### References <a id="dcl.init.ref">[[dcl.init.ref]]</a>
|
| 552 |
|
| 553 |
-
A variable declared
|
| 554 |
-
|
| 555 |
-
|
|
|
|
| 556 |
|
| 557 |
``` cpp
|
| 558 |
-
int g(int);
|
| 559 |
void f() {
|
| 560 |
int i;
|
| 561 |
int& r = i; // r refers to i
|
| 562 |
r = 1; // the value of i becomes 1
|
| 563 |
int* p = &r; // p points to i
|
|
@@ -568,56 +771,75 @@ void f() {
|
|
| 568 |
int (&ra)[3] = a; // ra refers to the array a
|
| 569 |
ra[1] = i; // modifies a[1]
|
| 570 |
}
|
| 571 |
```
|
| 572 |
|
|
|
|
|
|
|
| 573 |
A reference cannot be changed to refer to another object after
|
| 574 |
-
initialization.
|
| 575 |
-
|
| 576 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
|
| 578 |
The initializer can be omitted for a reference only in a parameter
|
| 579 |
declaration ([[dcl.fct]]), in the declaration of a function return
|
| 580 |
type, in the declaration of a class member within its class definition (
|
| 581 |
[[class.mem]]), and where the `extern` specifier is explicitly used.
|
| 582 |
|
|
|
|
|
|
|
| 583 |
``` cpp
|
| 584 |
int& r1; // error: initializer missing
|
| 585 |
extern int& r2; // OK
|
| 586 |
```
|
| 587 |
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 598 |
|
| 599 |
A reference to type “*cv1* `T1`” is initialized by an expression of type
|
| 600 |
“*cv2* `T2`” as follows:
|
| 601 |
|
| 602 |
- If the reference is an lvalue reference and the initializer expression
|
| 603 |
-
- is an lvalue (but is not a bit-field), and “ `T1`” is
|
| 604 |
-
reference-compatible with “ `T2`
|
| 605 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 606 |
reference-related to `T2`, and can be converted to an lvalue of type
|
| 607 |
-
“ `T3`
|
| 608 |
-
(this conversion is selected by enumerating the
|
| 609 |
-
conversion functions ([[over.match.ref]]) and choosing
|
| 610 |
-
through overload resolution ([[over.match]])),
|
| 611 |
|
| 612 |
then the reference is bound to the initializer expression lvalue in
|
| 613 |
the first case and to the lvalue result of the conversion in the
|
| 614 |
second case (or, in either case, to the appropriate base class
|
| 615 |
-
subobject of the object).
|
|
|
|
| 616 |
array-to-pointer ([[conv.array]]), and function-to-pointer (
|
| 617 |
[[conv.func]]) standard conversions are not needed, and therefore are
|
| 618 |
-
suppressed, when such direct bindings to lvalues are
|
|
|
|
|
|
|
| 619 |
``` cpp
|
| 620 |
double d = 2.0;
|
| 621 |
double& rd = d; // rd refers to d
|
| 622 |
const double& rcd = d; // rcd refers to d
|
| 623 |
|
|
@@ -625,36 +847,39 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
|
|
| 625 |
struct B : A { operator int&(); } b;
|
| 626 |
A& ra = b; // ra refers to A subobject in b
|
| 627 |
const A& rca = b; // rca refers to A subobject in b
|
| 628 |
int& ir = B(); // ir refers to the result of B::operator int&
|
| 629 |
```
|
|
|
|
|
|
|
| 630 |
- Otherwise, the reference shall be an lvalue reference to a
|
| 631 |
non-volatile const type (i.e., *cv1* shall be `const`), or the
|
| 632 |
reference shall be an rvalue reference.
|
|
|
|
| 633 |
``` cpp
|
| 634 |
double& rd2 = 2.0; // error: not an lvalue and reference not const
|
| 635 |
int i = 2;
|
| 636 |
double& rd3 = i; // error: type mismatch and reference not const
|
| 637 |
```
|
| 638 |
|
|
|
|
| 639 |
- If the initializer expression
|
| 640 |
-
- is an
|
| 641 |
-
|
| 642 |
-
“*cv2* `T2`”, or
|
| 643 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 644 |
-
reference-related to `T2`, and can be converted to an
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
[[over.match.ref]]),
|
| 648 |
|
| 649 |
-
then the
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
|
|
|
| 656 |
``` cpp
|
| 657 |
struct A { };
|
| 658 |
struct B : A { } b;
|
| 659 |
extern B f();
|
| 660 |
const A& rca2 = f(); // bound to the A subobject of the B rvalue.
|
|
@@ -665,59 +890,70 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
|
|
| 665 |
} x;
|
| 666 |
const A& r = x; // bound to the A subobject of the result of the conversion
|
| 667 |
int i2 = 42;
|
| 668 |
int&& rri = static_cast<int&&>(i2); // bound directly to i2
|
| 669 |
B&& rrb = x; // bound directly to the result of operator B
|
| 670 |
-
int&& rri2 = X(); // error: lvalue-to-rvalue conversion applied to the
|
| 671 |
-
// result of operator int&
|
| 672 |
```
|
|
|
|
|
|
|
| 673 |
- Otherwise:
|
| 674 |
-
- If `T1` is a class type
|
| 675 |
-
|
| 676 |
-
`T1`” by
|
| 677 |
-
[[over.match.copy]]
|
|
|
|
| 678 |
corresponding non-reference copy-initialization would be
|
| 679 |
ill-formed. The result of the call to the conversion function, as
|
| 680 |
described for the non-reference copy-initialization, is then used
|
| 681 |
-
to direct-initialize the reference.
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
|
| 688 |
If `T1` is reference-related to `T2`:
|
| 689 |
- *cv1* shall be the same cv-qualification as, or greater
|
| 690 |
cv-qualification than, *cv2*; and
|
| 691 |
- if the reference is an rvalue reference, the initializer
|
| 692 |
expression shall not be an lvalue.
|
| 693 |
|
|
|
|
| 694 |
``` cpp
|
| 695 |
struct Banana { };
|
| 696 |
struct Enigma { operator const Banana(); };
|
|
|
|
| 697 |
void enigmatic() {
|
| 698 |
typedef const Banana ConstBanana;
|
| 699 |
Banana &&banana1 = ConstBanana(); // ill-formed
|
| 700 |
Banana &&banana2 = Enigma(); // ill-formed
|
|
|
|
| 701 |
}
|
| 702 |
|
| 703 |
const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
|
| 704 |
double&& rrd = 2; // rrd refers to temporary with value 2.0
|
| 705 |
const volatile int cvi = 1;
|
| 706 |
-
const int& r2 = cvi; // error:
|
|
|
|
|
|
|
|
|
|
| 707 |
double d2 = 1.0;
|
| 708 |
-
double&& rrd2 = d2; // error:
|
|
|
|
|
|
|
| 709 |
int i3 = 2;
|
| 710 |
double&& rrd3 = i3; // rrd3 refers to temporary with value 2.0
|
| 711 |
```
|
| 712 |
|
| 713 |
-
|
| 714 |
-
temporary from the initializer expression), the reference is said to
|
| 715 |
-
*bind directly* to the initializer expression.
|
| 716 |
|
| 717 |
-
|
| 718 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 719 |
|
| 720 |
### List-initialization <a id="dcl.init.list">[[dcl.init.list]]</a>
|
| 721 |
|
| 722 |
*List-initialization* is initialization of an object or reference from a
|
| 723 |
*braced-init-list*. Such an initializer is called an *initializer list*,
|
|
@@ -725,24 +961,30 @@ and the comma-separated *initializer-clause*s of the list are called the
|
|
| 725 |
*elements* of the initializer list. An initializer list may be empty.
|
| 726 |
List-initialization can occur in direct-initialization or
|
| 727 |
copy-initialization contexts; list-initialization in a
|
| 728 |
direct-initialization context is called *direct-list-initialization* and
|
| 729 |
list-initialization in a copy-initialization context is called
|
| 730 |
-
*copy-list-initialization*.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 731 |
|
| 732 |
- as the initializer in a variable definition ([[dcl.init]])
|
| 733 |
-
- as the initializer in a new
|
| 734 |
- in a return statement ([[stmt.return]])
|
| 735 |
- as a *for-range-initializer* ([[stmt.iter]])
|
| 736 |
- as a function argument ([[expr.call]])
|
| 737 |
- as a subscript ([[expr.sub]])
|
| 738 |
- as an argument to a constructor invocation ([[dcl.init]],
|
| 739 |
[[expr.type.conv]])
|
| 740 |
- as an initializer for a non-static data member ([[class.mem]])
|
| 741 |
- in a *mem-initializer* ([[class.base.init]])
|
| 742 |
- on the right-hand side of an assignment ([[expr.ass]])
|
| 743 |
|
|
|
|
|
|
|
| 744 |
``` cpp
|
| 745 |
int a = {1};
|
| 746 |
std::complex<double> z{1,2};
|
| 747 |
new std::vector<std::string>{"once", "upon", "a", "time"}; // 4 string elements
|
| 748 |
f( {"Nicholas","Annemarie"} ); // pass list of two elements
|
|
@@ -750,30 +992,48 @@ return { "Norah" }; // return list of one element
|
|
| 750 |
int* e {}; // initialization to zero / null pointer
|
| 751 |
x = double{1}; // explicitly construct a double
|
| 752 |
std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
|
| 753 |
```
|
| 754 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 755 |
A constructor is an *initializer-list constructor* if its first
|
| 756 |
parameter is of type `std::initializer_list<E>` or reference to possibly
|
| 757 |
cv-qualified `std::initializer_list<E>` for some type `E`, and either
|
| 758 |
there are no other parameters or else all other parameters have default
|
| 759 |
-
arguments ([[dcl.fct.default]]).
|
| 760 |
-
|
| 761 |
-
[
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 769 |
|
| 770 |
List-initialization of an object or reference of type `T` is defined as
|
| 771 |
follows:
|
| 772 |
|
| 773 |
-
- If `T` is an aggregate
|
| 774 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 775 |
``` cpp
|
| 776 |
double ad[] = { 1, 2.0 }; // OK
|
| 777 |
int ai[] = { 1, 2.0 }; // error: narrowing
|
| 778 |
|
| 779 |
struct S2 {
|
|
@@ -782,22 +1042,22 @@ follows:
|
|
| 782 |
};
|
| 783 |
S2 s21 = { 1, 2, 3.0 }; // OK
|
| 784 |
S2 s22 { 1.0, 2, 3 }; // error: narrowing
|
| 785 |
S2 s23 { }; // OK: default to 0,0,0
|
| 786 |
```
|
|
|
|
|
|
|
| 787 |
- Otherwise, if the initializer list has no elements and `T` is a class
|
| 788 |
type with a default constructor, the object is value-initialized.
|
| 789 |
-
- Otherwise, if `T` is a specialization of `std::initializer_list<E>`,
|
| 790 |
-
|
| 791 |
-
and used to initialize the object according to the rules for
|
| 792 |
-
initialization of an object from a class of the same type (
|
| 793 |
-
[[dcl.init]]).
|
| 794 |
- Otherwise, if `T` is a class type, constructors are considered. The
|
| 795 |
applicable constructors are enumerated and the best one is chosen
|
| 796 |
through overload resolution ([[over.match]], [[over.match.list]]).
|
| 797 |
If a narrowing conversion (see below) is required to convert any of
|
| 798 |
the arguments, the program is ill-formed.
|
|
|
|
| 799 |
``` cpp
|
| 800 |
struct S {
|
| 801 |
S(std::initializer_list<double>); // #1
|
| 802 |
S(std::initializer_list<int>); // #2
|
| 803 |
S(); // #3
|
|
@@ -806,17 +1066,21 @@ follows:
|
|
| 806 |
S s1 = { 1.0, 2.0, 3.0 }; // invoke #1
|
| 807 |
S s2 = { 1, 2, 3 }; // invoke #2
|
| 808 |
S s3 = { }; // invoke #3
|
| 809 |
```
|
| 810 |
|
|
|
|
|
|
|
| 811 |
``` cpp
|
| 812 |
struct Map {
|
| 813 |
Map(std::initializer_list<std::pair<std::string,int>>);
|
| 814 |
};
|
| 815 |
Map ship = {{"Sophie",14}, {"Surprise",28}};
|
| 816 |
```
|
| 817 |
|
|
|
|
|
|
|
| 818 |
``` cpp
|
| 819 |
struct S {
|
| 820 |
// no initializer-list constructors
|
| 821 |
S(int, double, double); // #1
|
| 822 |
S(); // #2
|
|
@@ -824,25 +1088,61 @@ follows:
|
|
| 824 |
};
|
| 825 |
S s1 = { 1, 2, 3.0 }; // OK: invoke #1
|
| 826 |
S s2 { 1.0, 2, 3 }; // error: narrowing
|
| 827 |
S s3 { }; // OK: invoke #2
|
| 828 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 829 |
- Otherwise, if the initializer list has a single element of type `E`
|
| 830 |
and either `T` is not a reference type or its referenced type is
|
| 831 |
reference-related to `E`, the object or reference is initialized from
|
| 832 |
-
that element
|
| 833 |
-
|
|
|
|
|
|
|
|
|
|
| 834 |
``` cpp
|
| 835 |
int x1 {2}; // OK
|
| 836 |
int x2 {2.0}; // error: narrowing
|
| 837 |
```
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 844 |
``` cpp
|
| 845 |
struct S {
|
| 846 |
S(std::initializer_list<double>); // #1
|
| 847 |
S(const std::string&); // #2
|
| 848 |
// ...
|
|
@@ -852,16 +1152,22 @@ follows:
|
|
| 852 |
S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
|
| 853 |
const int& i1 = { 1 }; // OK
|
| 854 |
const int& i2 = { 1.1 }; // error: narrowing
|
| 855 |
const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
|
| 856 |
```
|
|
|
|
|
|
|
| 857 |
- Otherwise, if the initializer list has no elements, the object is
|
| 858 |
value-initialized.
|
|
|
|
| 859 |
``` cpp
|
| 860 |
int** pp {}; // initialized to null pointer
|
| 861 |
```
|
|
|
|
|
|
|
| 862 |
- Otherwise, the program is ill-formed.
|
|
|
|
| 863 |
``` cpp
|
| 864 |
struct A { int i; int j; };
|
| 865 |
A a1 { 1, 2 }; // aggregate initialization
|
| 866 |
A a2 { 1.2 }; // error: narrowing
|
| 867 |
struct B {
|
|
@@ -877,32 +1183,42 @@ follows:
|
|
| 877 |
|
| 878 |
int j { 1 }; // initialize to 1
|
| 879 |
int k { }; // initialize to 0
|
| 880 |
```
|
| 881 |
|
|
|
|
|
|
|
| 882 |
Within the *initializer-list* of a *braced-init-list*, the
|
| 883 |
*initializer-clause*s, including any that result from pack expansions (
|
| 884 |
[[temp.variadic]]), are evaluated in the order in which they appear.
|
| 885 |
That is, every value computation and side effect associated with a given
|
| 886 |
*initializer-clause* is sequenced before every value computation and
|
| 887 |
side effect associated with any *initializer-clause* that follows it in
|
| 888 |
-
the comma-separated list of the *initializer-list*.
|
| 889 |
-
|
| 890 |
-
|
| 891 |
-
|
| 892 |
-
|
|
|
|
|
|
|
| 893 |
|
| 894 |
An object of type `std::initializer_list<E>` is constructed from an
|
| 895 |
-
initializer list as if the implementation
|
| 896 |
-
|
| 897 |
-
initializer list. Each element of that array
|
| 898 |
-
the corresponding element of the initializer
|
| 899 |
-
`std::initializer_list<E>` object is constructed to refer
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 904 |
|
| 905 |
``` cpp
|
| 906 |
struct X {
|
| 907 |
X(std::initializer_list<double> v);
|
| 908 |
};
|
|
@@ -918,15 +1234,19 @@ X x(std::initializer_list<double>(__a, __a+3));
|
|
| 918 |
```
|
| 919 |
|
| 920 |
assuming that the implementation can construct an `initializer_list`
|
| 921 |
object with a pair of pointers.
|
| 922 |
|
|
|
|
|
|
|
| 923 |
The array has the same lifetime as any other temporary object (
|
| 924 |
[[class.temporary]]), except that initializing an `initializer_list`
|
| 925 |
object from the array extends the lifetime of the array exactly like
|
| 926 |
binding a reference to a temporary.
|
| 927 |
|
|
|
|
|
|
|
| 928 |
``` cpp
|
| 929 |
typedef std::complex<double> cmplx;
|
| 930 |
std::vector<cmplx> v1 = { 1, 2, 3 };
|
| 931 |
|
| 932 |
void f() {
|
|
@@ -934,24 +1254,27 @@ void f() {
|
|
| 934 |
std::initializer_list<int> i3 = { 1, 2, 3 };
|
| 935 |
}
|
| 936 |
|
| 937 |
struct A {
|
| 938 |
std::initializer_list<int> i4;
|
| 939 |
-
A() : i4{ 1, 2, 3 } {} //
|
| 940 |
};
|
| 941 |
```
|
| 942 |
|
| 943 |
For `v1` and `v2`, the `initializer_list` object is a parameter in a
|
| 944 |
function call, so the array created for `{ 1, 2, 3 }` has
|
| 945 |
full-expression lifetime. For `i3`, the `initializer_list` object is a
|
| 946 |
variable, so the array persists for the lifetime of the variable. For
|
| 947 |
-
`i4`, the `initializer_list` object is initialized in
|
| 948 |
-
*ctor-initializer*
|
| 949 |
-
|
| 950 |
-
|
| 951 |
-
|
| 952 |
-
|
|
|
|
|
|
|
|
|
|
| 953 |
|
| 954 |
A *narrowing conversion* is an implicit conversion
|
| 955 |
|
| 956 |
- from a floating-point type to an integer type, or
|
| 957 |
- from `long double` to `double` or `float`, or from `double` to
|
|
@@ -965,12 +1288,14 @@ A *narrowing conversion* is an implicit conversion
|
|
| 965 |
- from an integer type or unscoped enumeration type to an integer type
|
| 966 |
that cannot represent all the values of the original type, except
|
| 967 |
where the source is a constant expression whose value after integral
|
| 968 |
promotions will fit into the target type.
|
| 969 |
|
| 970 |
-
As indicated above, such conversions are not allowed at the
|
| 971 |
-
list-initializations.
|
|
|
|
|
|
|
| 972 |
|
| 973 |
``` cpp
|
| 974 |
int x = 999; // x is not a constant expression
|
| 975 |
const int y = 999;
|
| 976 |
const int z = 99;
|
|
@@ -989,34 +1314,41 @@ float f2 { 7 }; // OK: 7 can be exactly represented as a float
|
|
| 989 |
int f(int);
|
| 990 |
int a[] =
|
| 991 |
{ 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level
|
| 992 |
```
|
| 993 |
|
|
|
|
|
|
|
| 994 |
<!-- Link reference definitions -->
|
|
|
|
| 995 |
[basic.compound]: basic.md#basic.compound
|
| 996 |
[basic.def]: basic.md#basic.def
|
| 997 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 998 |
[basic.fundamental]: basic.md#basic.fundamental
|
| 999 |
[basic.life]: basic.md#basic.life
|
| 1000 |
[basic.link]: basic.md#basic.link
|
| 1001 |
[basic.lookup]: basic.md#basic.lookup
|
| 1002 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
|
|
|
| 1003 |
[basic.lookup.elab]: basic.md#basic.lookup.elab
|
| 1004 |
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
| 1005 |
[basic.lookup.udir]: basic.md#basic.lookup.udir
|
| 1006 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 1007 |
[basic.lval]: basic.md#basic.lval
|
| 1008 |
[basic.namespace]: #basic.namespace
|
| 1009 |
[basic.scope]: basic.md#basic.scope
|
| 1010 |
[basic.scope.block]: basic.md#basic.scope.block
|
|
|
|
| 1011 |
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
| 1012 |
[basic.scope.pdecl]: basic.md#basic.scope.pdecl
|
| 1013 |
[basic.scope.proto]: basic.md#basic.scope.proto
|
| 1014 |
[basic.start]: basic.md#basic.start
|
| 1015 |
-
[basic.start.
|
|
|
|
| 1016 |
[basic.stc]: basic.md#basic.stc
|
| 1017 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
|
|
|
| 1018 |
[basic.stc.static]: basic.md#basic.stc.static
|
| 1019 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 1020 |
[basic.type.qualifier]: basic.md#basic.type.qualifier
|
| 1021 |
[basic.types]: basic.md#basic.types
|
| 1022 |
[class]: class.md#class
|
|
@@ -1026,43 +1358,49 @@ int a[] =
|
|
| 1026 |
[class.conv]: special.md#class.conv
|
| 1027 |
[class.conv.ctor]: special.md#class.conv.ctor
|
| 1028 |
[class.conv.fct]: special.md#class.conv.fct
|
| 1029 |
[class.copy]: special.md#class.copy
|
| 1030 |
[class.ctor]: special.md#class.ctor
|
| 1031 |
-
[class.derived]: class.md#class.derived
|
| 1032 |
[class.dtor]: special.md#class.dtor
|
| 1033 |
[class.expl.init]: special.md#class.expl.init
|
| 1034 |
[class.friend]: class.md#class.friend
|
| 1035 |
-
[class.inhctor]: special.md#class.inhctor
|
| 1036 |
[class.init]: special.md#class.init
|
| 1037 |
[class.mem]: class.md#class.mem
|
| 1038 |
[class.member.lookup]: class.md#class.member.lookup
|
| 1039 |
[class.mfct]: class.md#class.mfct
|
|
|
|
| 1040 |
[class.name]: class.md#class.name
|
| 1041 |
[class.qual]: basic.md#class.qual
|
| 1042 |
[class.static]: class.md#class.static
|
| 1043 |
[class.static.data]: class.md#class.static.data
|
| 1044 |
[class.temporary]: special.md#class.temporary
|
| 1045 |
-
[class.this]: class.md#class.this
|
| 1046 |
[class.union]: class.md#class.union
|
|
|
|
| 1047 |
[class.virtual]: class.md#class.virtual
|
| 1048 |
[conv]: conv.md#conv
|
| 1049 |
[conv.array]: conv.md#conv.array
|
| 1050 |
[conv.func]: conv.md#conv.func
|
| 1051 |
[conv.integral]: conv.md#conv.integral
|
| 1052 |
[conv.lval]: conv.md#conv.lval
|
| 1053 |
[conv.prom]: conv.md#conv.prom
|
| 1054 |
[conv.ptr]: conv.md#conv.ptr
|
|
|
|
|
|
|
|
|
|
| 1055 |
[dcl.align]: #dcl.align
|
| 1056 |
[dcl.ambig.res]: #dcl.ambig.res
|
| 1057 |
[dcl.array]: #dcl.array
|
| 1058 |
[dcl.asm]: #dcl.asm
|
| 1059 |
[dcl.attr]: #dcl.attr
|
| 1060 |
[dcl.attr.depend]: #dcl.attr.depend
|
| 1061 |
[dcl.attr.deprecated]: #dcl.attr.deprecated
|
|
|
|
| 1062 |
[dcl.attr.grammar]: #dcl.attr.grammar
|
|
|
|
| 1063 |
[dcl.attr.noreturn]: #dcl.attr.noreturn
|
|
|
|
| 1064 |
[dcl.constexpr]: #dcl.constexpr
|
| 1065 |
[dcl.dcl]: #dcl.dcl
|
| 1066 |
[dcl.decl]: #dcl.decl
|
| 1067 |
[dcl.enum]: #dcl.enum
|
| 1068 |
[dcl.fct]: #dcl.fct
|
|
@@ -1076,25 +1414,28 @@ int a[] =
|
|
| 1076 |
[dcl.init]: #dcl.init
|
| 1077 |
[dcl.init.aggr]: #dcl.init.aggr
|
| 1078 |
[dcl.init.list]: #dcl.init.list
|
| 1079 |
[dcl.init.ref]: #dcl.init.ref
|
| 1080 |
[dcl.init.string]: #dcl.init.string
|
|
|
|
| 1081 |
[dcl.link]: #dcl.link
|
| 1082 |
[dcl.meaning]: #dcl.meaning
|
| 1083 |
[dcl.mptr]: #dcl.mptr
|
| 1084 |
[dcl.name]: #dcl.name
|
| 1085 |
[dcl.ptr]: #dcl.ptr
|
| 1086 |
[dcl.ref]: #dcl.ref
|
| 1087 |
[dcl.spec]: #dcl.spec
|
| 1088 |
[dcl.spec.auto]: #dcl.spec.auto
|
| 1089 |
[dcl.stc]: #dcl.stc
|
|
|
|
| 1090 |
[dcl.type]: #dcl.type
|
|
|
|
|
|
|
| 1091 |
[dcl.type.cv]: #dcl.type.cv
|
| 1092 |
[dcl.type.elab]: #dcl.type.elab
|
| 1093 |
[dcl.type.simple]: #dcl.type.simple
|
| 1094 |
[dcl.typedef]: #dcl.typedef
|
| 1095 |
-
[depr.register]: future.md#depr.register
|
| 1096 |
[except.handle]: except.md#except.handle
|
| 1097 |
[except.spec]: except.md#except.spec
|
| 1098 |
[except.throw]: except.md#except.throw
|
| 1099 |
[expr]: expr.md#expr
|
| 1100 |
[expr.alignof]: expr.md#expr.alignof
|
|
@@ -1105,18 +1446,18 @@ int a[] =
|
|
| 1105 |
[expr.cond]: expr.md#expr.cond
|
| 1106 |
[expr.const]: expr.md#expr.const
|
| 1107 |
[expr.const.cast]: expr.md#expr.const.cast
|
| 1108 |
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 1109 |
[expr.new]: expr.md#expr.new
|
| 1110 |
-
[expr.prim.lambda]: expr.md#expr.prim.lambda
|
|
|
|
| 1111 |
[expr.ref]: expr.md#expr.ref
|
| 1112 |
[expr.static.cast]: expr.md#expr.static.cast
|
| 1113 |
[expr.sub]: expr.md#expr.sub
|
| 1114 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 1115 |
[expr.unary]: expr.md#expr.unary
|
| 1116 |
[expr.unary.op]: expr.md#expr.unary.op
|
| 1117 |
-
[global.names]: library.md#global.names
|
| 1118 |
[intro.compliance]: intro.md#intro.compliance
|
| 1119 |
[intro.execution]: intro.md#intro.execution
|
| 1120 |
[intro.multithread]: intro.md#intro.multithread
|
| 1121 |
[lex.charset]: lex.md#lex.charset
|
| 1122 |
[lex.digraph]: lex.md#lex.digraph
|
|
@@ -1130,30 +1471,34 @@ int a[] =
|
|
| 1130 |
[namespace.udecl]: #namespace.udecl
|
| 1131 |
[namespace.udir]: #namespace.udir
|
| 1132 |
[namespace.unnamed]: #namespace.unnamed
|
| 1133 |
[over]: over.md#over
|
| 1134 |
[over.match]: over.md#over.match
|
|
|
|
| 1135 |
[over.match.conv]: over.md#over.match.conv
|
| 1136 |
[over.match.copy]: over.md#over.match.copy
|
| 1137 |
[over.match.ctor]: over.md#over.match.ctor
|
| 1138 |
[over.match.list]: over.md#over.match.list
|
| 1139 |
[over.match.ref]: over.md#over.match.ref
|
| 1140 |
[over.oper]: over.md#over.oper
|
| 1141 |
[over.sub]: over.md#over.sub
|
| 1142 |
[stmt.ambig]: stmt.md#stmt.ambig
|
| 1143 |
-
[stmt.block]: stmt.md#stmt.block
|
| 1144 |
[stmt.dcl]: stmt.md#stmt.dcl
|
| 1145 |
-
[stmt.
|
|
|
|
| 1146 |
[stmt.iter]: stmt.md#stmt.iter
|
|
|
|
| 1147 |
[stmt.return]: stmt.md#stmt.return
|
| 1148 |
[stmt.select]: stmt.md#stmt.select
|
| 1149 |
[stmt.stmt]: stmt.md#stmt.stmt
|
|
|
|
| 1150 |
[support.runtime]: language.md#support.runtime
|
| 1151 |
[tab:simple.type.specifiers]: #tab:simple.type.specifiers
|
| 1152 |
[temp]: temp.md#temp
|
| 1153 |
[temp.arg.type]: temp.md#temp.arg.type
|
| 1154 |
[temp.class.spec]: temp.md#temp.class.spec
|
|
|
|
| 1155 |
[temp.deduct.call]: temp.md#temp.deduct.call
|
| 1156 |
[temp.dep]: temp.md#temp.dep
|
| 1157 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 1158 |
[temp.explicit]: temp.md#temp.explicit
|
| 1159 |
[temp.inst]: temp.md#temp.inst
|
|
@@ -1164,11 +1509,11 @@ int a[] =
|
|
| 1164 |
[temp.spec]: temp.md#temp.spec
|
| 1165 |
[temp.variadic]: temp.md#temp.variadic
|
| 1166 |
|
| 1167 |
[^1]: The “implicit int” rule of C is no longer supported.
|
| 1168 |
|
| 1169 |
-
[^2]: The inline keyword has no effect on the linkage of a function.
|
| 1170 |
|
| 1171 |
[^3]: There is no special provision for a *decl-specifier-seq* that
|
| 1172 |
lacks a *type-specifier* or that has a *type-specifier* that only
|
| 1173 |
specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
|
| 1174 |
supported.
|
|
@@ -1176,88 +1521,46 @@ int a[] =
|
|
| 1176 |
[^4]: This set of values is used to define promotion and conversion
|
| 1177 |
semantics for the enumeration type. It does not preclude an
|
| 1178 |
expression of enumeration type from having a value that falls
|
| 1179 |
outside this range.
|
| 1180 |
|
| 1181 |
-
[^5]:
|
| 1182 |
-
linkage, they are effectively qualified by a name unique to their
|
| 1183 |
-
translation unit and therefore can never be seen from any other
|
| 1184 |
-
translation unit.
|
| 1185 |
-
|
| 1186 |
-
[^6]: this implies that the name of the class or function is
|
| 1187 |
unqualified.
|
| 1188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1189 |
[^7]: During name lookup in a class hierarchy, some ambiguities may be
|
| 1190 |
resolved by considering whether one member hides the other along
|
| 1191 |
some paths ([[class.member.lookup]]). There is no such
|
| 1192 |
disambiguation when considering the set of names found as a result
|
| 1193 |
of following *using-directive*s.
|
| 1194 |
|
| 1195 |
-
[^8]:
|
| 1196 |
-
the corresponding sequence of declarations each with a single
|
| 1197 |
-
declarator. That is
|
| 1198 |
-
|
| 1199 |
-
`T D1, D2, ... Dn;`
|
| 1200 |
-
|
| 1201 |
-
is usually equivalent to
|
| 1202 |
-
|
| 1203 |
-
`T D1; T D2; ... T Dn;`
|
| 1204 |
-
|
| 1205 |
-
where `T` is a *decl-specifier-seq* and each `Di` is an
|
| 1206 |
-
*init-declarator*. An exception occurs when a name introduced by one
|
| 1207 |
-
of the *declarator*s hides a type name used by the
|
| 1208 |
-
*decl-specifiers*, so that when the same *decl-specifiers* are used
|
| 1209 |
-
in a subsequent declaration, they do not have the same meaning, as
|
| 1210 |
-
in
|
| 1211 |
-
|
| 1212 |
-
`struct S { ... };`
|
| 1213 |
-
`S S, T; \textrm{// declare two instances of \tcode{struct S}}`
|
| 1214 |
-
|
| 1215 |
-
which is not equivalent to
|
| 1216 |
-
|
| 1217 |
-
`struct S { ... };`
|
| 1218 |
-
`S S;`
|
| 1219 |
-
`S T; \textrm{// error}`
|
| 1220 |
-
|
| 1221 |
-
Another exception occurs when `T` is `auto` ([[dcl.spec.auto]]),
|
| 1222 |
-
for example:
|
| 1223 |
-
|
| 1224 |
-
`auto i = 1, j = 2.0; \textrm{// error: deduced types for \tcode{i} and \tcode{j} do not match}`
|
| 1225 |
-
as opposed to
|
| 1226 |
-
`auto i = 1; \textrm{// OK: \tcode{i} deduced to have type \tcode{int}}`
|
| 1227 |
-
`auto j = 2.0; \textrm{// OK: \tcode{j} deduced to have type \tcode{double}}`
|
| 1228 |
-
|
| 1229 |
-
[^9]: As indicated by syntax, cv-qualifiers are a significant component
|
| 1230 |
in function return types.
|
| 1231 |
|
| 1232 |
-
[^
|
| 1233 |
-
to array of unknown bound of `T`” and where means any sequence of
|
| 1234 |
-
“pointer to” and “array of” derived declarator types. This exclusion
|
| 1235 |
-
applies to the parameters of the function, and if a parameter is a
|
| 1236 |
-
pointer to function or pointer to member function then to its
|
| 1237 |
-
parameters also, etc.
|
| 1238 |
-
|
| 1239 |
-
[^11]: One can explicitly disambiguate the parse either by introducing a
|
| 1240 |
comma (so the ellipsis will be parsed as part of the
|
| 1241 |
*parameter-declaration-clause*) or by introducing a name for the
|
| 1242 |
parameter (so the ellipsis will be parsed as part of the
|
| 1243 |
*declarator-id*).
|
| 1244 |
|
| 1245 |
-
[^
|
| 1246 |
declarations of pointers to functions, references to functions, or
|
| 1247 |
`typedef` declarations.
|
| 1248 |
|
| 1249 |
-
[^
|
| 1250 |
variables with names that are reserved to the implementation (
|
| 1251 |
-
[[
|
| 1252 |
[[basic.def.odr]]), its string value need not be present in the
|
| 1253 |
program image.
|
| 1254 |
|
| 1255 |
-
[^
|
| 1256 |
whose value is `0` to a pointer type results in a null pointer
|
| 1257 |
value.
|
| 1258 |
|
| 1259 |
-
[^
|
| 1260 |
nonetheless C++does not have zero length arrays.
|
| 1261 |
|
| 1262 |
-
[^
|
| 1263 |
returning a reference type.
|
|
|
|
| 2 |
|
| 3 |
A declarator can specify an initial value for the identifier being
|
| 4 |
declared. The identifier designates a variable being initialized. The
|
| 5 |
process of initialization described in the remainder of [[dcl.init]]
|
| 6 |
applies also to initializations specified by other syntactic contexts,
|
| 7 |
+
such as the initialization of function parameters ([[expr.call]]) or
|
| 8 |
+
the initialization of return values ([[stmt.return]]).
|
|
|
|
| 9 |
|
| 10 |
``` bnf
|
| 11 |
initializer:
|
| 12 |
brace-or-equal-initializer
|
| 13 |
'(' expression-list ')'
|
|
|
|
| 35 |
braced-init-list:
|
| 36 |
'{' initializer-list ','ₒₚₜ '}'
|
| 37 |
'{' '}'
|
| 38 |
```
|
| 39 |
|
| 40 |
+
``` bnf
|
| 41 |
+
expr-or-braced-init-list:
|
| 42 |
+
expression
|
| 43 |
+
braced-init-list
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
Except for objects declared with the `constexpr` specifier, for which
|
| 47 |
see [[dcl.constexpr]], an *initializer* in the definition of a variable
|
| 48 |
can consist of arbitrary expressions involving literals and previously
|
| 49 |
declared variables and functions, regardless of the variable’s storage
|
| 50 |
duration.
|
| 51 |
|
| 52 |
+
[*Example 1*:
|
| 53 |
+
|
| 54 |
``` cpp
|
| 55 |
int f(int);
|
| 56 |
int a = 2;
|
| 57 |
int b = f(a);
|
| 58 |
int c(b);
|
| 59 |
```
|
| 60 |
|
| 61 |
+
— *end example*]
|
| 62 |
|
| 63 |
+
[*Note 1*: Default arguments are more restricted; see
|
| 64 |
+
[[dcl.fct.default]]. — *end note*]
|
| 65 |
+
|
| 66 |
+
[*Note 2*: The order of initialization of variables with static storage
|
| 67 |
+
duration is described in [[basic.start]] and
|
| 68 |
+
[[stmt.dcl]]. — *end note*]
|
| 69 |
|
| 70 |
A declaration of a block-scope variable with external or internal
|
| 71 |
linkage that has an *initializer* is ill-formed.
|
| 72 |
|
| 73 |
To *zero-initialize* an object or reference of type `T` means:
|
| 74 |
|
| 75 |
- if `T` is a scalar type ([[basic.types]]), the object is initialized
|
| 76 |
to the value obtained by converting the integer literal `0` (zero) to
|
| 77 |
+
`T`;[^12]
|
| 78 |
- if `T` is a (possibly cv-qualified) non-union class type, each
|
| 79 |
+
non-static data member, each non-virtual base class subobject, and, if
|
| 80 |
+
the object is not a base class subobject, each virtual base class
|
| 81 |
+
subobject is zero-initialized and padding is initialized to zero bits;
|
| 82 |
- if `T` is a (possibly cv-qualified) union type, the object’s first
|
| 83 |
non-static named data member is zero-initialized and padding is
|
| 84 |
initialized to zero bits;
|
| 85 |
- if `T` is an array type, each element is zero-initialized;
|
| 86 |
- if `T` is a reference type, no initialization is performed.
|
| 87 |
|
| 88 |
To *default-initialize* an object of type `T` means:
|
| 89 |
|
| 90 |
+
- If `T` is a (possibly cv-qualified) class type (Clause [[class]]),
|
| 91 |
+
constructors are considered. The applicable constructors are
|
| 92 |
+
enumerated ([[over.match.ctor]]), and the best one for the
|
| 93 |
+
*initializer* `()` is chosen through overload resolution (
|
| 94 |
+
[[over.match]]). The constructor thus selected is called, with an
|
| 95 |
+
empty argument list, to initialize the object.
|
| 96 |
+
- If `T` is an array type, each element is default-initialized.
|
| 97 |
+
- Otherwise, no initialization is performed.
|
| 98 |
|
| 99 |
+
A class type `T` is *const-default-constructible* if
|
| 100 |
+
default-initialization of `T` would invoke a user-provided constructor
|
| 101 |
+
of `T` (not inherited from a base class) or if
|
| 102 |
+
|
| 103 |
+
- each direct non-variant non-static data member `M` of `T` has a
|
| 104 |
+
default member initializer or, if `M` is of class type `X` (or array
|
| 105 |
+
thereof), `X` is const-default-constructible,
|
| 106 |
+
- if `T` is a union with at least one non-static data member, exactly
|
| 107 |
+
one variant member has a default member initializer,
|
| 108 |
+
- if `T` is not a union, for each anonymous union member with at least
|
| 109 |
+
one non-static data member (if any), exactly one non-static data
|
| 110 |
+
member has a default member initializer, and
|
| 111 |
+
- each potentially constructed base class of `T` is
|
| 112 |
+
const-default-constructible.
|
| 113 |
+
|
| 114 |
+
If a program calls for the default-initialization of an object of a
|
| 115 |
+
const-qualified type `T`, `T` shall be a const-default-constructible
|
| 116 |
+
class type or array thereof.
|
| 117 |
|
| 118 |
To *value-initialize* an object of type `T` means:
|
| 119 |
|
| 120 |
- if `T` is a (possibly cv-qualified) class type (Clause [[class]])
|
| 121 |
with either no default constructor ([[class.ctor]]) or a default
|
|
|
|
| 127 |
and if `T` has a non-trivial default constructor, the object is
|
| 128 |
default-initialized;
|
| 129 |
- if `T` is an array type, then each element is value-initialized;
|
| 130 |
- otherwise, the object is zero-initialized.
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
A program that calls for default-initialization or value-initialization
|
| 133 |
of an entity of reference type is ill-formed.
|
| 134 |
|
| 135 |
+
[*Note 3*: Every object of static storage duration is zero-initialized
|
| 136 |
+
at program startup before any other initialization takes place. In some
|
| 137 |
+
cases, additional initialization is done later. — *end note*]
|
| 138 |
|
| 139 |
An object whose initializer is an empty set of parentheses, i.e., `()`,
|
| 140 |
shall be value-initialized.
|
| 141 |
|
| 142 |
+
[*Note 4*:
|
| 143 |
+
|
| 144 |
Since `()` is not permitted by the syntax for *initializer*,
|
| 145 |
|
| 146 |
``` cpp
|
| 147 |
X a();
|
| 148 |
```
|
|
|
|
| 150 |
is not the declaration of an object of class `X`, but the declaration of
|
| 151 |
a function taking no argument and returning an `X`. The form `()` is
|
| 152 |
permitted in certain other initialization contexts ([[expr.new]],
|
| 153 |
[[expr.type.conv]], [[class.base.init]]).
|
| 154 |
|
| 155 |
+
— *end note*]
|
| 156 |
+
|
| 157 |
If no initializer is specified for an object, the object is
|
| 158 |
default-initialized. When storage for an object with automatic or
|
| 159 |
dynamic storage duration is obtained, the object has an *indeterminate
|
| 160 |
value*, and if no initialization is performed for the object, that
|
| 161 |
object retains an indeterminate value until that value is replaced (
|
| 162 |
+
[[expr.ass]]).
|
| 163 |
+
|
| 164 |
+
[*Note 5*: Objects with static or thread storage duration are
|
| 165 |
+
zero-initialized, see [[basic.start.static]]. — *end note*]
|
| 166 |
+
|
| 167 |
+
If an indeterminate value is produced by an evaluation, the behavior is
|
| 168 |
+
undefined except in the following cases:
|
| 169 |
|
| 170 |
- If an indeterminate value of unsigned narrow character type (
|
| 171 |
+
[[basic.fundamental]]) or `std::byte` type ([[cstddef.syn]]) is
|
| 172 |
+
produced by the evaluation of:
|
| 173 |
- the second or third operand of a conditional expression (
|
| 174 |
[[expr.cond]]),
|
| 175 |
- the right operand of a comma expression ([[expr.comma]]),
|
| 176 |
+
- the operand of a cast or conversion ([[conv.integral]],
|
| 177 |
+
[[expr.type.conv]], [[expr.static.cast]], [[expr.cast]]) to an
|
| 178 |
+
unsigned narrow character type or `std::byte` type (
|
| 179 |
+
[[cstddef.syn]]), or
|
| 180 |
- a discarded-value expression (Clause [[expr]]),
|
| 181 |
|
| 182 |
then the result of the operation is an indeterminate value.
|
| 183 |
+
- If an indeterminate value of unsigned narrow character type or
|
| 184 |
+
`std::byte` type is produced by the evaluation of the right operand of
|
| 185 |
+
a simple assignment operator ([[expr.ass]]) whose first operand is an
|
| 186 |
+
lvalue of unsigned narrow character type or `std::byte` type, an
|
| 187 |
+
indeterminate value replaces the value of the object referred to by
|
| 188 |
+
the left operand.
|
| 189 |
- If an indeterminate value of unsigned narrow character type is
|
| 190 |
produced by the evaluation of the initialization expression when
|
| 191 |
initializing an object of unsigned narrow character type, that object
|
| 192 |
is initialized to an indeterminate value.
|
| 193 |
+
- If an indeterminate value of unsigned narrow character type or
|
| 194 |
+
`std::byte` type is produced by the evaluation of the initialization
|
| 195 |
+
expression when initializing an object of `std::byte` type, that
|
| 196 |
+
object is initialized to an indeterminate value.
|
| 197 |
+
|
| 198 |
+
[*Example 2*:
|
| 199 |
|
| 200 |
``` cpp
|
| 201 |
int f(bool b) {
|
| 202 |
unsigned char c;
|
| 203 |
unsigned char d = c; // OK, d has an indeterminate value
|
| 204 |
int e = d; // undefined behavior
|
| 205 |
return b ? d : 0; // undefined behavior if b is true
|
| 206 |
}
|
| 207 |
```
|
| 208 |
|
| 209 |
+
— *end example*]
|
| 210 |
+
|
| 211 |
An initializer for a static member is in the scope of the member’s
|
| 212 |
class.
|
| 213 |
|
| 214 |
+
[*Example 3*:
|
| 215 |
+
|
| 216 |
``` cpp
|
| 217 |
int a;
|
| 218 |
|
| 219 |
struct X {
|
| 220 |
static int a;
|
|
|
|
| 223 |
|
| 224 |
int X::a = 1;
|
| 225 |
int X::b = a; // X::b = X::a
|
| 226 |
```
|
| 227 |
|
| 228 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
+
If the entity being initialized does not have class type, the
|
| 231 |
+
*expression-list* in a parenthesized initializer shall be a single
|
| 232 |
+
expression.
|
| 233 |
|
| 234 |
+
The initialization that occurs in the `=` form of a
|
| 235 |
+
*brace-or-equal-initializer* or *condition* ([[stmt.select]]), as well
|
| 236 |
+
as in argument passing, function return, throwing an exception (
|
|
|
|
|
|
|
| 237 |
[[except.throw]]), handling an exception ([[except.handle]]), and
|
| 238 |
+
aggregate member initialization ([[dcl.init.aggr]]), is called
|
| 239 |
+
*copy-initialization*.
|
| 240 |
+
|
| 241 |
+
[*Note 6*: Copy-initialization may invoke a move (
|
| 242 |
+
[[class.copy]]). — *end note*]
|
| 243 |
|
| 244 |
The initialization that occurs in the forms
|
| 245 |
|
| 246 |
``` cpp
|
| 247 |
T x(a);
|
| 248 |
T x{a};
|
| 249 |
```
|
| 250 |
|
| 251 |
+
as well as in `new` expressions ([[expr.new]]), `static_cast`
|
| 252 |
+
expressions ([[expr.static.cast]]), functional notation type
|
| 253 |
+
conversions ([[expr.type.conv]]), *mem-initializer*s (
|
| 254 |
+
[[class.base.init]]), and the *braced-init-list* form of a *condition*
|
| 255 |
+
is called *direct-initialization*.
|
| 256 |
|
| 257 |
The semantics of initializers are as follows. The *destination type* is
|
| 258 |
the type of the object or reference being initialized and the *source
|
| 259 |
type* is the type of the initializer expression. If the initializer is
|
| 260 |
not a single (possibly parenthesized) expression, the source type is not
|
| 261 |
defined.
|
| 262 |
|
| 263 |
+
- If the initializer is a (non-parenthesized) *braced-init-list* or is
|
| 264 |
+
`=` *braced-init-list*, the object or reference is list-initialized (
|
| 265 |
+
[[dcl.init.list]]).
|
| 266 |
- If the destination type is a reference type, see [[dcl.init.ref]].
|
| 267 |
- If the destination type is an array of characters, an array of
|
| 268 |
`char16_t`, an array of `char32_t`, or an array of `wchar_t`, and the
|
| 269 |
initializer is a string literal, see [[dcl.init.string]].
|
| 270 |
- If the initializer is `()`, the object is value-initialized.
|
| 271 |
- Otherwise, if the destination type is an array, the program is
|
| 272 |
ill-formed.
|
| 273 |
- If the destination type is a (possibly cv-qualified) class type:
|
| 274 |
+
- If the initializer expression is a prvalue and the cv-unqualified
|
| 275 |
+
version of the source type is the same class as the class of the
|
| 276 |
+
destination, the initializer expression is used to initialize the
|
| 277 |
+
destination object. \[*Example 4*: `T x = T(T(T()));` calls the `T`
|
| 278 |
+
default constructor to initialize `x`. — *end example*]
|
| 279 |
+
- Otherwise, if the initialization is direct-initialization, or if it
|
| 280 |
+
is copy-initialization where the cv-unqualified version of the
|
| 281 |
+
source type is the same class as, or a derived class of, the class
|
| 282 |
+
of the destination, constructors are considered. The applicable
|
| 283 |
constructors are enumerated ([[over.match.ctor]]), and the best one
|
| 284 |
is chosen through overload resolution ([[over.match]]). The
|
| 285 |
constructor so selected is called to initialize the object, with the
|
| 286 |
initializer expression or *expression-list* as its argument(s). If
|
| 287 |
no constructor applies, or the overload resolution is ambiguous, the
|
|
|
|
| 292 |
to a derived class thereof are enumerated as described in
|
| 293 |
[[over.match.copy]], and the best one is chosen through overload
|
| 294 |
resolution ([[over.match]]). If the conversion cannot be done or is
|
| 295 |
ambiguous, the initialization is ill-formed. The function selected
|
| 296 |
is called with the initializer expression as its argument; if the
|
| 297 |
+
function is a constructor, the call is a prvalue of the
|
| 298 |
+
cv-unqualified version of the destination type whose result object
|
| 299 |
+
is initialized by the constructor. The call is used to
|
| 300 |
+
direct-initialize, according to the rules above, the object that is
|
| 301 |
+
the destination of the copy-initialization.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
- Otherwise, if the source type is a (possibly cv-qualified) class type,
|
| 303 |
conversion functions are considered. The applicable conversion
|
| 304 |
functions are enumerated ([[over.match.conv]]), and the best one is
|
| 305 |
chosen through overload resolution ([[over.match]]). The user-defined
|
| 306 |
conversion so selected is called to convert the initializer expression
|
|
|
|
| 309 |
- Otherwise, the initial value of the object being initialized is the
|
| 310 |
(possibly converted) value of the initializer expression. Standard
|
| 311 |
conversions (Clause [[conv]]) will be used, if necessary, to convert
|
| 312 |
the initializer expression to the cv-unqualified version of the
|
| 313 |
destination type; no user-defined conversions are considered. If the
|
| 314 |
+
conversion cannot be done, the initialization is ill-formed. When
|
| 315 |
+
initializing a bit-field with a value that it cannot represent, the
|
| 316 |
+
resulting value of the bit-field is *implementation-defined*.
|
| 317 |
+
\[*Note 7*:
|
| 318 |
+
An expression of type “*cv1* `T`” can initialize an object of type
|
| 319 |
+
“*cv2* `T`” independently of the cv-qualifiers *cv1* and *cv2*.
|
| 320 |
``` cpp
|
| 321 |
int a;
|
| 322 |
const int b = a;
|
| 323 |
int c = b;
|
| 324 |
```
|
| 325 |
|
| 326 |
+
— *end note*]
|
| 327 |
+
|
| 328 |
An *initializer-clause* followed by an ellipsis is a pack expansion (
|
| 329 |
[[temp.variadic]]).
|
| 330 |
|
| 331 |
+
If the initializer is a parenthesized *expression-list*, the expressions
|
| 332 |
+
are evaluated in the order specified for function calls (
|
| 333 |
+
[[expr.call]]).
|
| 334 |
+
|
| 335 |
+
An object whose initialization has completed is deemed to be
|
| 336 |
+
constructed, even if no constructor of the object’s class is invoked for
|
| 337 |
+
the initialization.
|
| 338 |
+
|
| 339 |
+
[*Note 8*: Such an object might have been value-initialized or
|
| 340 |
+
initialized by aggregate initialization ([[dcl.init.aggr]]) or by an
|
| 341 |
+
inherited constructor ([[class.inhctor.init]]). — *end note*]
|
| 342 |
+
|
| 343 |
+
A declaration that specifies the initialization of a variable, whether
|
| 344 |
+
from an explicit initializer or by default-initialization, is called the
|
| 345 |
+
*initializing declaration* of that variable.
|
| 346 |
+
|
| 347 |
+
[*Note 9*: In most cases this is the defining declaration (
|
| 348 |
+
[[basic.def]]) of the variable, but the initializing declaration of a
|
| 349 |
+
non-inline static data member ([[class.static.data]]) might be the
|
| 350 |
+
declaration within the class definition and not the definition at
|
| 351 |
+
namespace scope. — *end note*]
|
| 352 |
+
|
| 353 |
### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
|
| 354 |
|
| 355 |
+
An *aggregate* is an array or a class (Clause [[class]]) with
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
+
- no user-provided, `explicit`, or inherited constructors (
|
| 358 |
+
[[class.ctor]]),
|
| 359 |
+
- no private or protected non-static data members (Clause
|
| 360 |
+
[[class.access]]),
|
| 361 |
+
- no virtual functions ([[class.virtual]]), and
|
| 362 |
+
- no virtual, private, or protected base classes ([[class.mi]]).
|
| 363 |
+
|
| 364 |
+
[*Note 1*: Aggregate initialization does not allow accessing protected
|
| 365 |
+
and private base class’ members or constructors. — *end note*]
|
| 366 |
+
|
| 367 |
+
The *elements* of an aggregate are:
|
| 368 |
+
|
| 369 |
+
- for an array, the array elements in increasing subscript order, or
|
| 370 |
+
- for a class, the direct base classes in declaration order, followed by
|
| 371 |
+
the direct non-static data members ([[class.mem]]) that are not
|
| 372 |
+
members of an anonymous union, in declaration order.
|
| 373 |
+
|
| 374 |
+
When an aggregate is initialized by an initializer list as specified in
|
| 375 |
+
[[dcl.init.list]], the elements of the initializer list are taken as
|
| 376 |
+
initializers for the elements of the aggregate, in order. Each element
|
| 377 |
+
is copy-initialized from the corresponding *initializer-clause*. If the
|
| 378 |
+
*initializer-clause* is an expression and a narrowing conversion (
|
| 379 |
+
[[dcl.init.list]]) is required to convert the expression, the program is
|
| 380 |
+
ill-formed.
|
| 381 |
+
|
| 382 |
+
[*Note 2*: If an *initializer-clause* is itself an initializer list,
|
| 383 |
+
the element is list-initialized, which will result in a recursive
|
| 384 |
+
application of the rules in this section if the element is an
|
| 385 |
+
aggregate. — *end note*]
|
| 386 |
+
|
| 387 |
+
[*Example 1*:
|
| 388 |
|
| 389 |
``` cpp
|
| 390 |
struct A {
|
| 391 |
int x;
|
| 392 |
struct B {
|
|
|
|
| 396 |
} a = { 1, { 2, 3 } };
|
| 397 |
```
|
| 398 |
|
| 399 |
initializes `a.x` with 1, `a.b.i` with 2, `a.b.j` with 3.
|
| 400 |
|
| 401 |
+
``` cpp
|
| 402 |
+
struct base1 { int b1, b2 = 42; };
|
| 403 |
+
struct base2 {
|
| 404 |
+
base2() {
|
| 405 |
+
b3 = 42;
|
| 406 |
+
}
|
| 407 |
+
int b3;
|
| 408 |
+
};
|
| 409 |
+
struct derived : base1, base2 {
|
| 410 |
+
int d;
|
| 411 |
+
};
|
| 412 |
+
|
| 413 |
+
derived d1{{1, 2}, {}, 4};
|
| 414 |
+
derived d2{{}, {}, 4};
|
| 415 |
+
```
|
| 416 |
+
|
| 417 |
+
initializes `d1.b1` with 1, `d1.b2` with 2, `d1.b3` with 42, `d1.d` with
|
| 418 |
+
4, and `d2.b1` with 0, `d2.b2` with 42, `d2.b3` with 42, `d2.d` with 4.
|
| 419 |
+
|
| 420 |
+
— *end example*]
|
| 421 |
+
|
| 422 |
An aggregate that is a class can also be initialized with a single
|
| 423 |
expression not enclosed in braces, as described in [[dcl.init]].
|
| 424 |
|
| 425 |
+
An array of unknown bound initialized with a brace-enclosed
|
| 426 |
*initializer-list* containing `n` *initializer-clause*s, where `n` shall
|
| 427 |
+
be greater than zero, is defined as having `n` elements (
|
| 428 |
[[dcl.array]]).
|
| 429 |
|
| 430 |
+
[*Example 2*:
|
| 431 |
+
|
| 432 |
``` cpp
|
| 433 |
int x[] = { 1, 3, 5 };
|
| 434 |
```
|
| 435 |
|
| 436 |
declares and initializes `x` as a one-dimensional array that has three
|
| 437 |
elements since no size was specified and there are three initializers.
|
| 438 |
+
|
| 439 |
+
— *end example*]
|
| 440 |
+
|
| 441 |
An empty initializer list `{}` shall not be used as the
|
| 442 |
+
*initializer-clause* for an array of unknown bound.[^13]
|
| 443 |
|
| 444 |
+
[*Note 3*:
|
| 445 |
+
|
| 446 |
+
A default member initializer does not determine the bound for a member
|
| 447 |
+
array of unknown bound. Since the default member initializer is ignored
|
| 448 |
+
if a suitable *mem-initializer* is present ([[class.base.init]]), the
|
| 449 |
+
default member initializer is not considered to initialize the array of
|
| 450 |
+
unknown bound.
|
| 451 |
+
|
| 452 |
+
[*Example 3*:
|
| 453 |
+
|
| 454 |
+
``` cpp
|
| 455 |
+
struct S {
|
| 456 |
+
int y[] = { 0 }; // error: non-static data member of incomplete type
|
| 457 |
+
};
|
| 458 |
+
```
|
| 459 |
+
|
| 460 |
+
— *end example*]
|
| 461 |
+
|
| 462 |
+
— *end note*]
|
| 463 |
+
|
| 464 |
+
[*Note 4*:
|
| 465 |
+
|
| 466 |
+
Static data members and unnamed bit-fields are not considered elements
|
| 467 |
+
of the aggregate.
|
| 468 |
+
|
| 469 |
+
[*Example 4*:
|
| 470 |
|
| 471 |
``` cpp
|
| 472 |
struct A {
|
| 473 |
int i;
|
| 474 |
static int s;
|
|
|
|
| 478 |
} a = { 1, 2, 3 };
|
| 479 |
```
|
| 480 |
|
| 481 |
Here, the second initializer 2 initializes `a.j` and not the static data
|
| 482 |
member `A::s`, and the third initializer 3 initializes `a.k` and not the
|
| 483 |
+
unnamed bit-field before it.
|
| 484 |
+
|
| 485 |
+
— *end example*]
|
| 486 |
+
|
| 487 |
+
— *end note*]
|
| 488 |
|
| 489 |
An *initializer-list* is ill-formed if the number of
|
| 490 |
+
*initializer-clause*s exceeds the number of elements to initialize.
|
| 491 |
+
|
| 492 |
+
[*Example 5*:
|
| 493 |
|
| 494 |
``` cpp
|
| 495 |
char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
|
| 496 |
```
|
| 497 |
|
| 498 |
is ill-formed.
|
| 499 |
|
| 500 |
+
— *end example*]
|
| 501 |
+
|
| 502 |
If there are fewer *initializer-clause*s in the list than there are
|
| 503 |
+
elements in a non-union aggregate, then each element not explicitly
|
| 504 |
+
initialized is initialized as follows:
|
| 505 |
+
|
| 506 |
+
- If the element has a default member initializer ([[class.mem]]), the
|
| 507 |
+
element is initialized from that initializer.
|
| 508 |
+
- Otherwise, if the element is not a reference, the element is
|
| 509 |
+
copy-initialized from an empty initializer list ([[dcl.init.list]]).
|
| 510 |
+
- Otherwise, the program is ill-formed.
|
| 511 |
+
|
| 512 |
+
If the aggregate is a union and the initializer list is empty, then
|
| 513 |
+
|
| 514 |
+
- if any variant member has a default member initializer, that member is
|
| 515 |
+
initialized from its default member initializer;
|
| 516 |
+
- otherwise, the first member of the union (if any) is copy-initialized
|
| 517 |
+
from an empty initializer list.
|
| 518 |
+
|
| 519 |
+
[*Example 6*:
|
| 520 |
|
| 521 |
``` cpp
|
| 522 |
struct S { int a; const char* b; int c; int d = b[a]; };
|
| 523 |
S ss = { 1, "asdf" };
|
| 524 |
```
|
|
|
|
| 533 |
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
|
| 534 |
```
|
| 535 |
|
| 536 |
`a` and `b` have the same value
|
| 537 |
|
| 538 |
+
— *end example*]
|
| 539 |
+
|
| 540 |
+
If a reference member is initialized from its default member initializer
|
| 541 |
+
and a potentially-evaluated subexpression thereof is an aggregate
|
| 542 |
+
initialization that would use that default member initializer, the
|
| 543 |
+
program is ill-formed.
|
| 544 |
+
|
| 545 |
+
[*Example 7*:
|
| 546 |
+
|
| 547 |
+
``` cpp
|
| 548 |
+
struct A;
|
| 549 |
+
extern A a;
|
| 550 |
+
struct A {
|
| 551 |
+
const A& a1 { A{a,a} }; // OK
|
| 552 |
+
const A& a2 { A{} }; // error
|
| 553 |
+
};
|
| 554 |
+
A a{a,a}; // OK
|
| 555 |
+
```
|
| 556 |
+
|
| 557 |
+
— *end example*]
|
| 558 |
+
|
| 559 |
+
If an aggregate class `C` contains a subaggregate element `e` with no
|
| 560 |
+
elements, the *initializer-clause* for `e` shall not be omitted from an
|
| 561 |
*initializer-list* for an object of type `C` unless the
|
| 562 |
+
*initializer-clause*s for all elements of `C` following `e` are also
|
| 563 |
omitted.
|
| 564 |
|
| 565 |
+
[*Example 8*:
|
| 566 |
+
|
| 567 |
``` cpp
|
| 568 |
struct S { } s;
|
| 569 |
struct A {
|
| 570 |
S s1;
|
| 571 |
int i1;
|
|
|
|
| 579 |
s, // Required initialization
|
| 580 |
0
|
| 581 |
}; // Initialization not required for A::s3 because A::i3 is also not initialized
|
| 582 |
```
|
| 583 |
|
| 584 |
+
— *end example*]
|
|
|
|
| 585 |
|
| 586 |
When initializing a multi-dimensional array, the *initializer-clause*s
|
| 587 |
initialize the elements with the last (rightmost) index of the array
|
| 588 |
varying the fastest ([[dcl.array]]).
|
| 589 |
|
| 590 |
+
[*Example 9*:
|
| 591 |
+
|
| 592 |
``` cpp
|
| 593 |
int x[2][2] = { 3, 1, 4, 2 };
|
| 594 |
```
|
| 595 |
|
| 596 |
initializes `x[0][0]` to `3`, `x[0][1]` to `1`, `x[1][0]` to `4`, and
|
|
|
|
| 603 |
```
|
| 604 |
|
| 605 |
initializes the first column of `y` (regarded as a two-dimensional
|
| 606 |
array) and leaves the rest zero.
|
| 607 |
|
| 608 |
+
— *end example*]
|
| 609 |
+
|
| 610 |
Braces can be elided in an *initializer-list* as follows. If the
|
| 611 |
*initializer-list* begins with a left brace, then the succeeding
|
| 612 |
+
comma-separated list of *initializer-clause*s initializes the elements
|
| 613 |
+
of a subaggregate; it is erroneous for there to be more
|
| 614 |
+
*initializer-clause*s than elements. If, however, the *initializer-list*
|
| 615 |
for a subaggregate does not begin with a left brace, then only enough
|
| 616 |
+
*initializer-clause*s from the list are taken to initialize the elements
|
| 617 |
of the subaggregate; any remaining *initializer-clause*s are left to
|
| 618 |
+
initialize the next element of the aggregate of which the current
|
| 619 |
+
subaggregate is an element.
|
| 620 |
+
|
| 621 |
+
[*Example 10*:
|
| 622 |
|
| 623 |
``` cpp
|
| 624 |
float y[4][3] = {
|
| 625 |
{ 1, 3, 5 },
|
| 626 |
{ 2, 4, 6 },
|
|
|
|
| 646 |
|
| 647 |
The initializer for `y` begins with a left brace, but the one for `y[0]`
|
| 648 |
does not, therefore three elements from the list are used. Likewise the
|
| 649 |
next three are taken successively for `y[1]` and `y[2]`.
|
| 650 |
|
| 651 |
+
— *end example*]
|
| 652 |
+
|
| 653 |
All implicit type conversions (Clause [[conv]]) are considered when
|
| 654 |
+
initializing the element with an *assignment-expression*. If the
|
| 655 |
+
*assignment-expression* can initialize an element, the element is
|
| 656 |
+
initialized. Otherwise, if the element is itself a subaggregate, brace
|
| 657 |
elision is assumed and the *assignment-expression* is considered for the
|
| 658 |
+
initialization of the first element of the subaggregate.
|
| 659 |
+
|
| 660 |
+
[*Note 5*: As specified above, brace elision cannot apply to
|
| 661 |
+
subaggregates with no elements; an *initializer-clause* for the entire
|
| 662 |
+
subobject is required. — *end note*]
|
| 663 |
+
|
| 664 |
+
[*Example 11*:
|
| 665 |
|
| 666 |
``` cpp
|
| 667 |
struct A {
|
| 668 |
int i;
|
| 669 |
operator int();
|
|
|
|
| 678 |
|
| 679 |
Braces are elided around the *initializer-clause* for `b.a1.i`. `b.a1.i`
|
| 680 |
is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
|
| 681 |
initialized with whatever `a.operator int()` returns.
|
| 682 |
|
| 683 |
+
— *end example*]
|
|
|
|
|
|
|
| 684 |
|
| 685 |
+
[*Note 6*: An aggregate array or an aggregate class may contain
|
| 686 |
+
elements of a class type with a user-provided constructor (
|
| 687 |
+
[[class.ctor]]). Initialization of these aggregate objects is described
|
| 688 |
+
in [[class.expl.init]]. — *end note*]
|
| 689 |
+
|
| 690 |
+
[*Note 7*: Whether the initialization of aggregates with static storage
|
| 691 |
+
duration is static or dynamic is specified in [[basic.start.static]],
|
| 692 |
+
[[basic.start.dynamic]], and [[stmt.dcl]]. — *end note*]
|
| 693 |
|
| 694 |
When a union is initialized with a brace-enclosed initializer, the
|
| 695 |
braces shall only contain an *initializer-clause* for the first
|
| 696 |
non-static data member of the union.
|
| 697 |
|
| 698 |
+
[*Example 12*:
|
| 699 |
+
|
| 700 |
``` cpp
|
| 701 |
union u { int a; const char* b; };
|
| 702 |
u a = { 1 };
|
| 703 |
u b = a;
|
| 704 |
u c = 1; // error
|
| 705 |
u d = { 0, "asdf" }; // error
|
| 706 |
u e = { "asdf" }; // error
|
| 707 |
```
|
| 708 |
|
| 709 |
+
— *end example*]
|
| 710 |
+
|
| 711 |
+
[*Note 8*: As described above, the braces around the
|
| 712 |
+
*initializer-clause* for a union member can be omitted if the union is a
|
| 713 |
+
member of another aggregate. — *end note*]
|
| 714 |
|
| 715 |
### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
|
| 716 |
|
| 717 |
An array of narrow character type ([[basic.fundamental]]), `char16_t`
|
| 718 |
array, `char32_t` array, or `wchar_t` array can be initialized by a
|
|
|
|
| 720 |
literal, or wide string literal, respectively, or by an
|
| 721 |
appropriately-typed string literal enclosed in braces ([[lex.string]]).
|
| 722 |
Successive characters of the value of the string literal initialize the
|
| 723 |
elements of the array.
|
| 724 |
|
| 725 |
+
[*Example 1*:
|
| 726 |
+
|
| 727 |
``` cpp
|
| 728 |
char msg[] = "Syntax error on line %s\n";
|
| 729 |
```
|
| 730 |
|
| 731 |
shows a character array whose members are initialized with a
|
| 732 |
*string-literal*. Note that because `'\n'` is a single character and
|
| 733 |
because a trailing `'\0'` is appended, `sizeof(msg)` is `25`.
|
| 734 |
|
| 735 |
+
— *end example*]
|
| 736 |
+
|
| 737 |
There shall not be more initializers than there are array elements.
|
| 738 |
|
| 739 |
+
[*Example 2*:
|
| 740 |
+
|
| 741 |
``` cpp
|
| 742 |
char cv[4] = "asdf"; // error
|
| 743 |
```
|
| 744 |
|
| 745 |
is ill-formed since there is no space for the implied trailing `'\0'`.
|
| 746 |
|
| 747 |
+
— *end example*]
|
| 748 |
+
|
| 749 |
If there are fewer initializers than there are array elements, each
|
| 750 |
element not explicitly initialized shall be zero-initialized (
|
| 751 |
[[dcl.init]]).
|
| 752 |
|
| 753 |
### References <a id="dcl.init.ref">[[dcl.init.ref]]</a>
|
| 754 |
|
| 755 |
+
A variable whose declared type is “reference to type `T`” ([[dcl.ref]])
|
| 756 |
+
shall be initialized.
|
| 757 |
+
|
| 758 |
+
[*Example 1*:
|
| 759 |
|
| 760 |
``` cpp
|
| 761 |
+
int g(int) noexcept;
|
| 762 |
void f() {
|
| 763 |
int i;
|
| 764 |
int& r = i; // r refers to i
|
| 765 |
r = 1; // the value of i becomes 1
|
| 766 |
int* p = &r; // p points to i
|
|
|
|
| 771 |
int (&ra)[3] = a; // ra refers to the array a
|
| 772 |
ra[1] = i; // modifies a[1]
|
| 773 |
}
|
| 774 |
```
|
| 775 |
|
| 776 |
+
— *end example*]
|
| 777 |
+
|
| 778 |
A reference cannot be changed to refer to another object after
|
| 779 |
+
initialization.
|
| 780 |
+
|
| 781 |
+
[*Note 1*: Assignment to a reference assigns to the object referred to
|
| 782 |
+
by the reference ([[expr.ass]]). — *end note*]
|
| 783 |
+
|
| 784 |
+
Argument passing ([[expr.call]]) and function value return (
|
| 785 |
+
[[stmt.return]]) are initializations.
|
| 786 |
|
| 787 |
The initializer can be omitted for a reference only in a parameter
|
| 788 |
declaration ([[dcl.fct]]), in the declaration of a function return
|
| 789 |
type, in the declaration of a class member within its class definition (
|
| 790 |
[[class.mem]]), and where the `extern` specifier is explicitly used.
|
| 791 |
|
| 792 |
+
[*Example 2*:
|
| 793 |
+
|
| 794 |
``` cpp
|
| 795 |
int& r1; // error: initializer missing
|
| 796 |
extern int& r2; // OK
|
| 797 |
```
|
| 798 |
|
| 799 |
+
— *end example*]
|
| 800 |
+
|
| 801 |
+
Given types “*cv1* `T1`” and “*cv2* `T2`”, “*cv1* `T1`” is
|
| 802 |
+
*reference-related* to “*cv2* `T2`” if `T1` is the same type as `T2`, or
|
| 803 |
+
`T1` is a base class of `T2`. “*cv1* `T1`” is *reference-compatible*
|
| 804 |
+
with “*cv2* `T2`” if
|
| 805 |
+
|
| 806 |
+
- `T1` is reference-related to `T2`, or
|
| 807 |
+
- `T2` is “`noexcept` function” and `T1` is “function”, where the
|
| 808 |
+
function types are otherwise the same,
|
| 809 |
+
|
| 810 |
+
and *cv1* is the same cv-qualification as, or greater cv-qualification
|
| 811 |
+
than, *cv2*. In all cases where the reference-related or
|
| 812 |
+
reference-compatible relationship of two types is used to establish the
|
| 813 |
+
validity of a reference binding, and `T1` is a base class of `T2`, a
|
| 814 |
+
program that necessitates such a binding is ill-formed if `T1` is an
|
| 815 |
+
inaccessible (Clause [[class.access]]) or ambiguous (
|
| 816 |
+
[[class.member.lookup]]) base class of `T2`.
|
| 817 |
|
| 818 |
A reference to type “*cv1* `T1`” is initialized by an expression of type
|
| 819 |
“*cv2* `T2`” as follows:
|
| 820 |
|
| 821 |
- If the reference is an lvalue reference and the initializer expression
|
| 822 |
+
- is an lvalue (but is not a bit-field), and “*cv1* `T1`” is
|
| 823 |
+
reference-compatible with “*cv2* `T2`”, or
|
| 824 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 825 |
reference-related to `T2`, and can be converted to an lvalue of type
|
| 826 |
+
“*cv3* `T3`”, where “*cv1* `T1`” is reference-compatible with “*cv3*
|
| 827 |
+
`T3`”[^14] (this conversion is selected by enumerating the
|
| 828 |
+
applicable conversion functions ([[over.match.ref]]) and choosing
|
| 829 |
+
the best one through overload resolution ([[over.match]])),
|
| 830 |
|
| 831 |
then the reference is bound to the initializer expression lvalue in
|
| 832 |
the first case and to the lvalue result of the conversion in the
|
| 833 |
second case (or, in either case, to the appropriate base class
|
| 834 |
+
subobject of the object).
|
| 835 |
+
\[*Note 2*: The usual lvalue-to-rvalue ([[conv.lval]]),
|
| 836 |
array-to-pointer ([[conv.array]]), and function-to-pointer (
|
| 837 |
[[conv.func]]) standard conversions are not needed, and therefore are
|
| 838 |
+
suppressed, when such direct bindings to lvalues are
|
| 839 |
+
done. — *end note*]
|
| 840 |
+
\[*Example 3*:
|
| 841 |
``` cpp
|
| 842 |
double d = 2.0;
|
| 843 |
double& rd = d; // rd refers to d
|
| 844 |
const double& rcd = d; // rcd refers to d
|
| 845 |
|
|
|
|
| 847 |
struct B : A { operator int&(); } b;
|
| 848 |
A& ra = b; // ra refers to A subobject in b
|
| 849 |
const A& rca = b; // rca refers to A subobject in b
|
| 850 |
int& ir = B(); // ir refers to the result of B::operator int&
|
| 851 |
```
|
| 852 |
+
|
| 853 |
+
— *end example*]
|
| 854 |
- Otherwise, the reference shall be an lvalue reference to a
|
| 855 |
non-volatile const type (i.e., *cv1* shall be `const`), or the
|
| 856 |
reference shall be an rvalue reference.
|
| 857 |
+
\[*Example 4*:
|
| 858 |
``` cpp
|
| 859 |
double& rd2 = 2.0; // error: not an lvalue and reference not const
|
| 860 |
int i = 2;
|
| 861 |
double& rd3 = i; // error: type mismatch and reference not const
|
| 862 |
```
|
| 863 |
|
| 864 |
+
— *end example*]
|
| 865 |
- If the initializer expression
|
| 866 |
+
- is an rvalue (but not a bit-field) or function lvalue and “*cv1*
|
| 867 |
+
`T1`” is reference-compatible with “*cv2* `T2`”, or
|
|
|
|
| 868 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 869 |
+
reference-related to `T2`, and can be converted to an rvalue or
|
| 870 |
+
function lvalue of type “*cv3* `T3`”, where “*cv1* `T1`” is
|
| 871 |
+
reference-compatible with “*cv3* `T3`” (see [[over.match.ref]]),
|
|
|
|
| 872 |
|
| 873 |
+
then the value of the initializer expression in the first case and
|
| 874 |
+
the result of the conversion in the second case is called the
|
| 875 |
+
converted initializer. If the converted initializer is a prvalue,
|
| 876 |
+
its type `T4` is adjusted to type “*cv1* `T4`” ([[conv.qual]]) and
|
| 877 |
+
the temporary materialization conversion ([[conv.rval]]) is
|
| 878 |
+
applied. In any case, the reference is bound to the resulting
|
| 879 |
+
glvalue (or to an appropriate base class subobject).
|
| 880 |
+
\[*Example 5*:
|
| 881 |
``` cpp
|
| 882 |
struct A { };
|
| 883 |
struct B : A { } b;
|
| 884 |
extern B f();
|
| 885 |
const A& rca2 = f(); // bound to the A subobject of the B rvalue.
|
|
|
|
| 890 |
} x;
|
| 891 |
const A& r = x; // bound to the A subobject of the result of the conversion
|
| 892 |
int i2 = 42;
|
| 893 |
int&& rri = static_cast<int&&>(i2); // bound directly to i2
|
| 894 |
B&& rrb = x; // bound directly to the result of operator B
|
|
|
|
|
|
|
| 895 |
```
|
| 896 |
+
|
| 897 |
+
— *end example*]
|
| 898 |
- Otherwise:
|
| 899 |
+
- If `T1` or `T2` is a class type and `T1` is not reference-related
|
| 900 |
+
to `T2`, user-defined conversions are considered using the rules
|
| 901 |
+
for copy-initialization of an object of type “*cv1* `T1`” by
|
| 902 |
+
user-defined conversion ([[dcl.init]], [[over.match.copy]],
|
| 903 |
+
[[over.match.conv]]); the program is ill-formed if the
|
| 904 |
corresponding non-reference copy-initialization would be
|
| 905 |
ill-formed. The result of the call to the conversion function, as
|
| 906 |
described for the non-reference copy-initialization, is then used
|
| 907 |
+
to direct-initialize the reference. For this
|
| 908 |
+
direct-initialization, user-defined conversions are not
|
| 909 |
+
considered.
|
| 910 |
+
- Otherwise, the initializer expression is implicitly converted to a
|
| 911 |
+
prvalue of type “*cv1* `T1`”. The temporary materialization
|
| 912 |
+
conversion is applied and the reference is bound to the result.
|
| 913 |
|
| 914 |
If `T1` is reference-related to `T2`:
|
| 915 |
- *cv1* shall be the same cv-qualification as, or greater
|
| 916 |
cv-qualification than, *cv2*; and
|
| 917 |
- if the reference is an rvalue reference, the initializer
|
| 918 |
expression shall not be an lvalue.
|
| 919 |
|
| 920 |
+
\[*Example 6*:
|
| 921 |
``` cpp
|
| 922 |
struct Banana { };
|
| 923 |
struct Enigma { operator const Banana(); };
|
| 924 |
+
struct Alaska { operator Banana&(); };
|
| 925 |
void enigmatic() {
|
| 926 |
typedef const Banana ConstBanana;
|
| 927 |
Banana &&banana1 = ConstBanana(); // ill-formed
|
| 928 |
Banana &&banana2 = Enigma(); // ill-formed
|
| 929 |
+
Banana &&banana3 = Alaska(); // ill-formed
|
| 930 |
}
|
| 931 |
|
| 932 |
const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
|
| 933 |
double&& rrd = 2; // rrd refers to temporary with value 2.0
|
| 934 |
const volatile int cvi = 1;
|
| 935 |
+
const int& r2 = cvi; // error: cv-qualifier dropped
|
| 936 |
+
struct A { operator volatile int&(); } a;
|
| 937 |
+
const int& r3 = a; // error: cv-qualifier dropped
|
| 938 |
+
// from result of conversion function
|
| 939 |
double d2 = 1.0;
|
| 940 |
+
double&& rrd2 = d2; // error: initializer is lvalue of related type
|
| 941 |
+
struct X { operator int&(); };
|
| 942 |
+
int&& rri2 = X(); // error: result of conversion function is lvalue of related type
|
| 943 |
int i3 = 2;
|
| 944 |
double&& rrd3 = i3; // rrd3 refers to temporary with value 2.0
|
| 945 |
```
|
| 946 |
|
| 947 |
+
— *end example*]
|
|
|
|
|
|
|
| 948 |
|
| 949 |
+
In all cases except the last (i.e., implicitly converting the
|
| 950 |
+
initializer expression to the underlying type of the reference), the
|
| 951 |
+
reference is said to *bind directly* to the initializer expression.
|
| 952 |
+
|
| 953 |
+
[*Note 3*: [[class.temporary]] describes the lifetime of temporaries
|
| 954 |
+
bound to references. — *end note*]
|
| 955 |
|
| 956 |
### List-initialization <a id="dcl.init.list">[[dcl.init.list]]</a>
|
| 957 |
|
| 958 |
*List-initialization* is initialization of an object or reference from a
|
| 959 |
*braced-init-list*. Such an initializer is called an *initializer list*,
|
|
|
|
| 961 |
*elements* of the initializer list. An initializer list may be empty.
|
| 962 |
List-initialization can occur in direct-initialization or
|
| 963 |
copy-initialization contexts; list-initialization in a
|
| 964 |
direct-initialization context is called *direct-list-initialization* and
|
| 965 |
list-initialization in a copy-initialization context is called
|
| 966 |
+
*copy-list-initialization*.
|
| 967 |
+
|
| 968 |
+
[*Note 1*:
|
| 969 |
+
|
| 970 |
+
List-initialization can be used
|
| 971 |
|
| 972 |
- as the initializer in a variable definition ([[dcl.init]])
|
| 973 |
+
- as the initializer in a *new-expression* ([[expr.new]])
|
| 974 |
- in a return statement ([[stmt.return]])
|
| 975 |
- as a *for-range-initializer* ([[stmt.iter]])
|
| 976 |
- as a function argument ([[expr.call]])
|
| 977 |
- as a subscript ([[expr.sub]])
|
| 978 |
- as an argument to a constructor invocation ([[dcl.init]],
|
| 979 |
[[expr.type.conv]])
|
| 980 |
- as an initializer for a non-static data member ([[class.mem]])
|
| 981 |
- in a *mem-initializer* ([[class.base.init]])
|
| 982 |
- on the right-hand side of an assignment ([[expr.ass]])
|
| 983 |
|
| 984 |
+
[*Example 1*:
|
| 985 |
+
|
| 986 |
``` cpp
|
| 987 |
int a = {1};
|
| 988 |
std::complex<double> z{1,2};
|
| 989 |
new std::vector<std::string>{"once", "upon", "a", "time"}; // 4 string elements
|
| 990 |
f( {"Nicholas","Annemarie"} ); // pass list of two elements
|
|
|
|
| 992 |
int* e {}; // initialization to zero / null pointer
|
| 993 |
x = double{1}; // explicitly construct a double
|
| 994 |
std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
|
| 995 |
```
|
| 996 |
|
| 997 |
+
— *end example*]
|
| 998 |
+
|
| 999 |
+
— *end note*]
|
| 1000 |
+
|
| 1001 |
A constructor is an *initializer-list constructor* if its first
|
| 1002 |
parameter is of type `std::initializer_list<E>` or reference to possibly
|
| 1003 |
cv-qualified `std::initializer_list<E>` for some type `E`, and either
|
| 1004 |
there are no other parameters or else all other parameters have default
|
| 1005 |
+
arguments ([[dcl.fct.default]]).
|
| 1006 |
+
|
| 1007 |
+
[*Note 2*: Initializer-list constructors are favored over other
|
| 1008 |
+
constructors in list-initialization ([[over.match.list]]). Passing an
|
| 1009 |
+
initializer list as the argument to the constructor template
|
| 1010 |
+
`template<class T> C(T)` of a class `C` does not create an
|
| 1011 |
+
initializer-list constructor, because an initializer list argument
|
| 1012 |
+
causes the corresponding parameter to be a non-deduced context (
|
| 1013 |
+
[[temp.deduct.call]]). — *end note*]
|
| 1014 |
+
|
| 1015 |
+
The template `std::initializer_list` is not predefined; if the header
|
| 1016 |
+
`<initializer_list>` is not included prior to a use of
|
| 1017 |
+
`std::initializer_list` — even an implicit use in which the type is not
|
| 1018 |
+
named ([[dcl.spec.auto]]) — the program is ill-formed.
|
| 1019 |
|
| 1020 |
List-initialization of an object or reference of type `T` is defined as
|
| 1021 |
follows:
|
| 1022 |
|
| 1023 |
+
- If `T` is an aggregate class and the initializer list has a single
|
| 1024 |
+
element of type *cv* `U`, where `U` is `T` or a class derived from
|
| 1025 |
+
`T`, the object is initialized from that element (by
|
| 1026 |
+
copy-initialization for copy-list-initialization, or by
|
| 1027 |
+
direct-initialization for direct-list-initialization).
|
| 1028 |
+
- Otherwise, if `T` is a character array and the initializer list has a
|
| 1029 |
+
single element that is an appropriately-typed string literal (
|
| 1030 |
+
[[dcl.init.string]]), initialization is performed as described in that
|
| 1031 |
+
section.
|
| 1032 |
+
- Otherwise, if `T` is an aggregate, aggregate initialization is
|
| 1033 |
+
performed ([[dcl.init.aggr]]).
|
| 1034 |
+
\[*Example 2*:
|
| 1035 |
``` cpp
|
| 1036 |
double ad[] = { 1, 2.0 }; // OK
|
| 1037 |
int ai[] = { 1, 2.0 }; // error: narrowing
|
| 1038 |
|
| 1039 |
struct S2 {
|
|
|
|
| 1042 |
};
|
| 1043 |
S2 s21 = { 1, 2, 3.0 }; // OK
|
| 1044 |
S2 s22 { 1.0, 2, 3 }; // error: narrowing
|
| 1045 |
S2 s23 { }; // OK: default to 0,0,0
|
| 1046 |
```
|
| 1047 |
+
|
| 1048 |
+
— *end example*]
|
| 1049 |
- Otherwise, if the initializer list has no elements and `T` is a class
|
| 1050 |
type with a default constructor, the object is value-initialized.
|
| 1051 |
+
- Otherwise, if `T` is a specialization of `std::initializer_list<E>`,
|
| 1052 |
+
the object is constructed as described below.
|
|
|
|
|
|
|
|
|
|
| 1053 |
- Otherwise, if `T` is a class type, constructors are considered. The
|
| 1054 |
applicable constructors are enumerated and the best one is chosen
|
| 1055 |
through overload resolution ([[over.match]], [[over.match.list]]).
|
| 1056 |
If a narrowing conversion (see below) is required to convert any of
|
| 1057 |
the arguments, the program is ill-formed.
|
| 1058 |
+
\[*Example 3*:
|
| 1059 |
``` cpp
|
| 1060 |
struct S {
|
| 1061 |
S(std::initializer_list<double>); // #1
|
| 1062 |
S(std::initializer_list<int>); // #2
|
| 1063 |
S(); // #3
|
|
|
|
| 1066 |
S s1 = { 1.0, 2.0, 3.0 }; // invoke #1
|
| 1067 |
S s2 = { 1, 2, 3 }; // invoke #2
|
| 1068 |
S s3 = { }; // invoke #3
|
| 1069 |
```
|
| 1070 |
|
| 1071 |
+
— *end example*]
|
| 1072 |
+
\[*Example 4*:
|
| 1073 |
``` cpp
|
| 1074 |
struct Map {
|
| 1075 |
Map(std::initializer_list<std::pair<std::string,int>>);
|
| 1076 |
};
|
| 1077 |
Map ship = {{"Sophie",14}, {"Surprise",28}};
|
| 1078 |
```
|
| 1079 |
|
| 1080 |
+
— *end example*]
|
| 1081 |
+
\[*Example 5*:
|
| 1082 |
``` cpp
|
| 1083 |
struct S {
|
| 1084 |
// no initializer-list constructors
|
| 1085 |
S(int, double, double); // #1
|
| 1086 |
S(); // #2
|
|
|
|
| 1088 |
};
|
| 1089 |
S s1 = { 1, 2, 3.0 }; // OK: invoke #1
|
| 1090 |
S s2 { 1.0, 2, 3 }; // error: narrowing
|
| 1091 |
S s3 { }; // OK: invoke #2
|
| 1092 |
```
|
| 1093 |
+
|
| 1094 |
+
— *end example*]
|
| 1095 |
+
- Otherwise, if `T` is an enumeration with a fixed underlying type (
|
| 1096 |
+
[[dcl.enum]]), the *initializer-list* has a single element `v`, and
|
| 1097 |
+
the initialization is direct-list-initialization, the object is
|
| 1098 |
+
initialized with the value `T(v)` ([[expr.type.conv]]); if a
|
| 1099 |
+
narrowing conversion is required to convert `v` to the underlying type
|
| 1100 |
+
of `T`, the program is ill-formed.
|
| 1101 |
+
\[*Example 6*:
|
| 1102 |
+
``` cpp
|
| 1103 |
+
enum byte : unsigned char { };
|
| 1104 |
+
byte b { 42 }; // OK
|
| 1105 |
+
byte c = { 42 }; // error
|
| 1106 |
+
byte d = byte{ 42 }; // OK; same value as b
|
| 1107 |
+
byte e { -1 }; // error
|
| 1108 |
+
|
| 1109 |
+
struct A { byte b; };
|
| 1110 |
+
A a1 = { { 42 } }; // error
|
| 1111 |
+
A a2 = { byte{ 42 } }; // OK
|
| 1112 |
+
|
| 1113 |
+
void f(byte);
|
| 1114 |
+
f({ 42 }); // error
|
| 1115 |
+
|
| 1116 |
+
enum class Handle : uint32_t { Invalid = 0 };
|
| 1117 |
+
Handle h { 42 }; // OK
|
| 1118 |
+
```
|
| 1119 |
+
|
| 1120 |
+
— *end example*]
|
| 1121 |
- Otherwise, if the initializer list has a single element of type `E`
|
| 1122 |
and either `T` is not a reference type or its referenced type is
|
| 1123 |
reference-related to `E`, the object or reference is initialized from
|
| 1124 |
+
that element (by copy-initialization for copy-list-initialization, or
|
| 1125 |
+
by direct-initialization for direct-list-initialization); if a
|
| 1126 |
+
narrowing conversion (see below) is required to convert the element to
|
| 1127 |
+
`T`, the program is ill-formed.
|
| 1128 |
+
\[*Example 7*:
|
| 1129 |
``` cpp
|
| 1130 |
int x1 {2}; // OK
|
| 1131 |
int x2 {2.0}; // error: narrowing
|
| 1132 |
```
|
| 1133 |
+
|
| 1134 |
+
— *end example*]
|
| 1135 |
+
- Otherwise, if `T` is a reference type, a prvalue of the type
|
| 1136 |
+
referenced by `T` is generated. The prvalue initializes its result
|
| 1137 |
+
object by copy-list-initialization or direct-list-initialization,
|
| 1138 |
+
depending on the kind of initialization for the reference. The prvalue
|
| 1139 |
+
is then used to direct-initialize the reference.
|
| 1140 |
+
\[*Note 3*: As usual, the binding will fail and the program is
|
| 1141 |
+
ill-formed if the reference type is an lvalue reference to a non-const
|
| 1142 |
+
type. — *end note*]
|
| 1143 |
+
\[*Example 8*:
|
| 1144 |
``` cpp
|
| 1145 |
struct S {
|
| 1146 |
S(std::initializer_list<double>); // #1
|
| 1147 |
S(const std::string&); // #2
|
| 1148 |
// ...
|
|
|
|
| 1152 |
S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
|
| 1153 |
const int& i1 = { 1 }; // OK
|
| 1154 |
const int& i2 = { 1.1 }; // error: narrowing
|
| 1155 |
const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
|
| 1156 |
```
|
| 1157 |
+
|
| 1158 |
+
— *end example*]
|
| 1159 |
- Otherwise, if the initializer list has no elements, the object is
|
| 1160 |
value-initialized.
|
| 1161 |
+
\[*Example 9*:
|
| 1162 |
``` cpp
|
| 1163 |
int** pp {}; // initialized to null pointer
|
| 1164 |
```
|
| 1165 |
+
|
| 1166 |
+
— *end example*]
|
| 1167 |
- Otherwise, the program is ill-formed.
|
| 1168 |
+
\[*Example 10*:
|
| 1169 |
``` cpp
|
| 1170 |
struct A { int i; int j; };
|
| 1171 |
A a1 { 1, 2 }; // aggregate initialization
|
| 1172 |
A a2 { 1.2 }; // error: narrowing
|
| 1173 |
struct B {
|
|
|
|
| 1183 |
|
| 1184 |
int j { 1 }; // initialize to 1
|
| 1185 |
int k { }; // initialize to 0
|
| 1186 |
```
|
| 1187 |
|
| 1188 |
+
— *end example*]
|
| 1189 |
+
|
| 1190 |
Within the *initializer-list* of a *braced-init-list*, the
|
| 1191 |
*initializer-clause*s, including any that result from pack expansions (
|
| 1192 |
[[temp.variadic]]), are evaluated in the order in which they appear.
|
| 1193 |
That is, every value computation and side effect associated with a given
|
| 1194 |
*initializer-clause* is sequenced before every value computation and
|
| 1195 |
side effect associated with any *initializer-clause* that follows it in
|
| 1196 |
+
the comma-separated list of the *initializer-list*.
|
| 1197 |
+
|
| 1198 |
+
[*Note 4*: This evaluation ordering holds regardless of the semantics
|
| 1199 |
+
of the initialization; for example, it applies when the elements of the
|
| 1200 |
+
*initializer-list* are interpreted as arguments of a constructor call,
|
| 1201 |
+
even though ordinarily there are no sequencing constraints on the
|
| 1202 |
+
arguments of a call. — *end note*]
|
| 1203 |
|
| 1204 |
An object of type `std::initializer_list<E>` is constructed from an
|
| 1205 |
+
initializer list as if the implementation generated and materialized (
|
| 1206 |
+
[[conv.rval]]) a prvalue of type “array of N `const E`”, where N is the
|
| 1207 |
+
number of elements in the initializer list. Each element of that array
|
| 1208 |
+
is copy-initialized with the corresponding element of the initializer
|
| 1209 |
+
list, and the `std::initializer_list<E>` object is constructed to refer
|
| 1210 |
+
to that array.
|
| 1211 |
+
|
| 1212 |
+
[*Note 5*: A constructor or conversion function selected for the copy
|
| 1213 |
+
shall be accessible (Clause [[class.access]]) in the context of the
|
| 1214 |
+
initializer list. — *end note*]
|
| 1215 |
+
|
| 1216 |
+
If a narrowing conversion is required to initialize any of the elements,
|
| 1217 |
+
the program is ill-formed.
|
| 1218 |
+
|
| 1219 |
+
[*Example 11*:
|
| 1220 |
|
| 1221 |
``` cpp
|
| 1222 |
struct X {
|
| 1223 |
X(std::initializer_list<double> v);
|
| 1224 |
};
|
|
|
|
| 1234 |
```
|
| 1235 |
|
| 1236 |
assuming that the implementation can construct an `initializer_list`
|
| 1237 |
object with a pair of pointers.
|
| 1238 |
|
| 1239 |
+
— *end example*]
|
| 1240 |
+
|
| 1241 |
The array has the same lifetime as any other temporary object (
|
| 1242 |
[[class.temporary]]), except that initializing an `initializer_list`
|
| 1243 |
object from the array extends the lifetime of the array exactly like
|
| 1244 |
binding a reference to a temporary.
|
| 1245 |
|
| 1246 |
+
[*Example 12*:
|
| 1247 |
+
|
| 1248 |
``` cpp
|
| 1249 |
typedef std::complex<double> cmplx;
|
| 1250 |
std::vector<cmplx> v1 = { 1, 2, 3 };
|
| 1251 |
|
| 1252 |
void f() {
|
|
|
|
| 1254 |
std::initializer_list<int> i3 = { 1, 2, 3 };
|
| 1255 |
}
|
| 1256 |
|
| 1257 |
struct A {
|
| 1258 |
std::initializer_list<int> i4;
|
| 1259 |
+
A() : i4{ 1, 2, 3 } {} // ill-formed, would create a dangling reference
|
| 1260 |
};
|
| 1261 |
```
|
| 1262 |
|
| 1263 |
For `v1` and `v2`, the `initializer_list` object is a parameter in a
|
| 1264 |
function call, so the array created for `{ 1, 2, 3 }` has
|
| 1265 |
full-expression lifetime. For `i3`, the `initializer_list` object is a
|
| 1266 |
variable, so the array persists for the lifetime of the variable. For
|
| 1267 |
+
`i4`, the `initializer_list` object is initialized in the constructor’s
|
| 1268 |
+
*ctor-initializer* as if by binding a temporary array to a reference
|
| 1269 |
+
member, so the program is ill-formed ([[class.base.init]]).
|
| 1270 |
+
|
| 1271 |
+
— *end example*]
|
| 1272 |
+
|
| 1273 |
+
[*Note 6*: The implementation is free to allocate the array in
|
| 1274 |
+
read-only memory if an explicit array with the same initializer could be
|
| 1275 |
+
so allocated. — *end note*]
|
| 1276 |
|
| 1277 |
A *narrowing conversion* is an implicit conversion
|
| 1278 |
|
| 1279 |
- from a floating-point type to an integer type, or
|
| 1280 |
- from `long double` to `double` or `float`, or from `double` to
|
|
|
|
| 1288 |
- from an integer type or unscoped enumeration type to an integer type
|
| 1289 |
that cannot represent all the values of the original type, except
|
| 1290 |
where the source is a constant expression whose value after integral
|
| 1291 |
promotions will fit into the target type.
|
| 1292 |
|
| 1293 |
+
[*Note 7*: As indicated above, such conversions are not allowed at the
|
| 1294 |
+
top level in list-initializations. — *end note*]
|
| 1295 |
+
|
| 1296 |
+
[*Example 13*:
|
| 1297 |
|
| 1298 |
``` cpp
|
| 1299 |
int x = 999; // x is not a constant expression
|
| 1300 |
const int y = 999;
|
| 1301 |
const int z = 99;
|
|
|
|
| 1314 |
int f(int);
|
| 1315 |
int a[] =
|
| 1316 |
{ 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level
|
| 1317 |
```
|
| 1318 |
|
| 1319 |
+
— *end example*]
|
| 1320 |
+
|
| 1321 |
<!-- Link reference definitions -->
|
| 1322 |
+
[basic.align]: basic.md#basic.align
|
| 1323 |
[basic.compound]: basic.md#basic.compound
|
| 1324 |
[basic.def]: basic.md#basic.def
|
| 1325 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 1326 |
[basic.fundamental]: basic.md#basic.fundamental
|
| 1327 |
[basic.life]: basic.md#basic.life
|
| 1328 |
[basic.link]: basic.md#basic.link
|
| 1329 |
[basic.lookup]: basic.md#basic.lookup
|
| 1330 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 1331 |
+
[basic.lookup.classref]: basic.md#basic.lookup.classref
|
| 1332 |
[basic.lookup.elab]: basic.md#basic.lookup.elab
|
| 1333 |
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
| 1334 |
[basic.lookup.udir]: basic.md#basic.lookup.udir
|
| 1335 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 1336 |
[basic.lval]: basic.md#basic.lval
|
| 1337 |
[basic.namespace]: #basic.namespace
|
| 1338 |
[basic.scope]: basic.md#basic.scope
|
| 1339 |
[basic.scope.block]: basic.md#basic.scope.block
|
| 1340 |
+
[basic.scope.declarative]: basic.md#basic.scope.declarative
|
| 1341 |
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
| 1342 |
[basic.scope.pdecl]: basic.md#basic.scope.pdecl
|
| 1343 |
[basic.scope.proto]: basic.md#basic.scope.proto
|
| 1344 |
[basic.start]: basic.md#basic.start
|
| 1345 |
+
[basic.start.dynamic]: basic.md#basic.start.dynamic
|
| 1346 |
+
[basic.start.static]: basic.md#basic.start.static
|
| 1347 |
[basic.stc]: basic.md#basic.stc
|
| 1348 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
| 1349 |
+
[basic.stc.dynamic]: basic.md#basic.stc.dynamic
|
| 1350 |
[basic.stc.static]: basic.md#basic.stc.static
|
| 1351 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 1352 |
[basic.type.qualifier]: basic.md#basic.type.qualifier
|
| 1353 |
[basic.types]: basic.md#basic.types
|
| 1354 |
[class]: class.md#class
|
|
|
|
| 1358 |
[class.conv]: special.md#class.conv
|
| 1359 |
[class.conv.ctor]: special.md#class.conv.ctor
|
| 1360 |
[class.conv.fct]: special.md#class.conv.fct
|
| 1361 |
[class.copy]: special.md#class.copy
|
| 1362 |
[class.ctor]: special.md#class.ctor
|
|
|
|
| 1363 |
[class.dtor]: special.md#class.dtor
|
| 1364 |
[class.expl.init]: special.md#class.expl.init
|
| 1365 |
[class.friend]: class.md#class.friend
|
| 1366 |
+
[class.inhctor.init]: special.md#class.inhctor.init
|
| 1367 |
[class.init]: special.md#class.init
|
| 1368 |
[class.mem]: class.md#class.mem
|
| 1369 |
[class.member.lookup]: class.md#class.member.lookup
|
| 1370 |
[class.mfct]: class.md#class.mfct
|
| 1371 |
+
[class.mi]: class.md#class.mi
|
| 1372 |
[class.name]: class.md#class.name
|
| 1373 |
[class.qual]: basic.md#class.qual
|
| 1374 |
[class.static]: class.md#class.static
|
| 1375 |
[class.static.data]: class.md#class.static.data
|
| 1376 |
[class.temporary]: special.md#class.temporary
|
|
|
|
| 1377 |
[class.union]: class.md#class.union
|
| 1378 |
+
[class.union.anon]: class.md#class.union.anon
|
| 1379 |
[class.virtual]: class.md#class.virtual
|
| 1380 |
[conv]: conv.md#conv
|
| 1381 |
[conv.array]: conv.md#conv.array
|
| 1382 |
[conv.func]: conv.md#conv.func
|
| 1383 |
[conv.integral]: conv.md#conv.integral
|
| 1384 |
[conv.lval]: conv.md#conv.lval
|
| 1385 |
[conv.prom]: conv.md#conv.prom
|
| 1386 |
[conv.ptr]: conv.md#conv.ptr
|
| 1387 |
+
[conv.qual]: conv.md#conv.qual
|
| 1388 |
+
[conv.rval]: conv.md#conv.rval
|
| 1389 |
+
[cstddef.syn]: language.md#cstddef.syn
|
| 1390 |
[dcl.align]: #dcl.align
|
| 1391 |
[dcl.ambig.res]: #dcl.ambig.res
|
| 1392 |
[dcl.array]: #dcl.array
|
| 1393 |
[dcl.asm]: #dcl.asm
|
| 1394 |
[dcl.attr]: #dcl.attr
|
| 1395 |
[dcl.attr.depend]: #dcl.attr.depend
|
| 1396 |
[dcl.attr.deprecated]: #dcl.attr.deprecated
|
| 1397 |
+
[dcl.attr.fallthrough]: #dcl.attr.fallthrough
|
| 1398 |
[dcl.attr.grammar]: #dcl.attr.grammar
|
| 1399 |
+
[dcl.attr.nodiscard]: #dcl.attr.nodiscard
|
| 1400 |
[dcl.attr.noreturn]: #dcl.attr.noreturn
|
| 1401 |
+
[dcl.attr.unused]: #dcl.attr.unused
|
| 1402 |
[dcl.constexpr]: #dcl.constexpr
|
| 1403 |
[dcl.dcl]: #dcl.dcl
|
| 1404 |
[dcl.decl]: #dcl.decl
|
| 1405 |
[dcl.enum]: #dcl.enum
|
| 1406 |
[dcl.fct]: #dcl.fct
|
|
|
|
| 1414 |
[dcl.init]: #dcl.init
|
| 1415 |
[dcl.init.aggr]: #dcl.init.aggr
|
| 1416 |
[dcl.init.list]: #dcl.init.list
|
| 1417 |
[dcl.init.ref]: #dcl.init.ref
|
| 1418 |
[dcl.init.string]: #dcl.init.string
|
| 1419 |
+
[dcl.inline]: #dcl.inline
|
| 1420 |
[dcl.link]: #dcl.link
|
| 1421 |
[dcl.meaning]: #dcl.meaning
|
| 1422 |
[dcl.mptr]: #dcl.mptr
|
| 1423 |
[dcl.name]: #dcl.name
|
| 1424 |
[dcl.ptr]: #dcl.ptr
|
| 1425 |
[dcl.ref]: #dcl.ref
|
| 1426 |
[dcl.spec]: #dcl.spec
|
| 1427 |
[dcl.spec.auto]: #dcl.spec.auto
|
| 1428 |
[dcl.stc]: #dcl.stc
|
| 1429 |
+
[dcl.struct.bind]: #dcl.struct.bind
|
| 1430 |
[dcl.type]: #dcl.type
|
| 1431 |
+
[dcl.type.auto.deduct]: #dcl.type.auto.deduct
|
| 1432 |
+
[dcl.type.class.deduct]: #dcl.type.class.deduct
|
| 1433 |
[dcl.type.cv]: #dcl.type.cv
|
| 1434 |
[dcl.type.elab]: #dcl.type.elab
|
| 1435 |
[dcl.type.simple]: #dcl.type.simple
|
| 1436 |
[dcl.typedef]: #dcl.typedef
|
|
|
|
| 1437 |
[except.handle]: except.md#except.handle
|
| 1438 |
[except.spec]: except.md#except.spec
|
| 1439 |
[except.throw]: except.md#except.throw
|
| 1440 |
[expr]: expr.md#expr
|
| 1441 |
[expr.alignof]: expr.md#expr.alignof
|
|
|
|
| 1446 |
[expr.cond]: expr.md#expr.cond
|
| 1447 |
[expr.const]: expr.md#expr.const
|
| 1448 |
[expr.const.cast]: expr.md#expr.const.cast
|
| 1449 |
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 1450 |
[expr.new]: expr.md#expr.new
|
| 1451 |
+
[expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
|
| 1452 |
+
[expr.prim.this]: expr.md#expr.prim.this
|
| 1453 |
[expr.ref]: expr.md#expr.ref
|
| 1454 |
[expr.static.cast]: expr.md#expr.static.cast
|
| 1455 |
[expr.sub]: expr.md#expr.sub
|
| 1456 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 1457 |
[expr.unary]: expr.md#expr.unary
|
| 1458 |
[expr.unary.op]: expr.md#expr.unary.op
|
|
|
|
| 1459 |
[intro.compliance]: intro.md#intro.compliance
|
| 1460 |
[intro.execution]: intro.md#intro.execution
|
| 1461 |
[intro.multithread]: intro.md#intro.multithread
|
| 1462 |
[lex.charset]: lex.md#lex.charset
|
| 1463 |
[lex.digraph]: lex.md#lex.digraph
|
|
|
|
| 1471 |
[namespace.udecl]: #namespace.udecl
|
| 1472 |
[namespace.udir]: #namespace.udir
|
| 1473 |
[namespace.unnamed]: #namespace.unnamed
|
| 1474 |
[over]: over.md#over
|
| 1475 |
[over.match]: over.md#over.match
|
| 1476 |
+
[over.match.class.deduct]: over.md#over.match.class.deduct
|
| 1477 |
[over.match.conv]: over.md#over.match.conv
|
| 1478 |
[over.match.copy]: over.md#over.match.copy
|
| 1479 |
[over.match.ctor]: over.md#over.match.ctor
|
| 1480 |
[over.match.list]: over.md#over.match.list
|
| 1481 |
[over.match.ref]: over.md#over.match.ref
|
| 1482 |
[over.oper]: over.md#over.oper
|
| 1483 |
[over.sub]: over.md#over.sub
|
| 1484 |
[stmt.ambig]: stmt.md#stmt.ambig
|
|
|
|
| 1485 |
[stmt.dcl]: stmt.md#stmt.dcl
|
| 1486 |
+
[stmt.expr]: stmt.md#stmt.expr
|
| 1487 |
+
[stmt.if]: stmt.md#stmt.if
|
| 1488 |
[stmt.iter]: stmt.md#stmt.iter
|
| 1489 |
+
[stmt.label]: stmt.md#stmt.label
|
| 1490 |
[stmt.return]: stmt.md#stmt.return
|
| 1491 |
[stmt.select]: stmt.md#stmt.select
|
| 1492 |
[stmt.stmt]: stmt.md#stmt.stmt
|
| 1493 |
+
[stmt.switch]: stmt.md#stmt.switch
|
| 1494 |
[support.runtime]: language.md#support.runtime
|
| 1495 |
[tab:simple.type.specifiers]: #tab:simple.type.specifiers
|
| 1496 |
[temp]: temp.md#temp
|
| 1497 |
[temp.arg.type]: temp.md#temp.arg.type
|
| 1498 |
[temp.class.spec]: temp.md#temp.class.spec
|
| 1499 |
+
[temp.deduct]: temp.md#temp.deduct
|
| 1500 |
[temp.deduct.call]: temp.md#temp.deduct.call
|
| 1501 |
[temp.dep]: temp.md#temp.dep
|
| 1502 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 1503 |
[temp.explicit]: temp.md#temp.explicit
|
| 1504 |
[temp.inst]: temp.md#temp.inst
|
|
|
|
| 1509 |
[temp.spec]: temp.md#temp.spec
|
| 1510 |
[temp.variadic]: temp.md#temp.variadic
|
| 1511 |
|
| 1512 |
[^1]: The “implicit int” rule of C is no longer supported.
|
| 1513 |
|
| 1514 |
+
[^2]: The `inline` keyword has no effect on the linkage of a function.
|
| 1515 |
|
| 1516 |
[^3]: There is no special provision for a *decl-specifier-seq* that
|
| 1517 |
lacks a *type-specifier* or that has a *type-specifier* that only
|
| 1518 |
specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
|
| 1519 |
supported.
|
|
|
|
| 1521 |
[^4]: This set of values is used to define promotion and conversion
|
| 1522 |
semantics for the enumeration type. It does not preclude an
|
| 1523 |
expression of enumeration type from having a value that falls
|
| 1524 |
outside this range.
|
| 1525 |
|
| 1526 |
+
[^5]: this implies that the name of the class or function is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1527 |
unqualified.
|
| 1528 |
|
| 1529 |
+
[^6]: A *using-declaration* with more than one *using-declarator* is
|
| 1530 |
+
equivalent to a corresponding sequence of *using-declaration*s with
|
| 1531 |
+
one *using-declarator* each.
|
| 1532 |
+
|
| 1533 |
[^7]: During name lookup in a class hierarchy, some ambiguities may be
|
| 1534 |
resolved by considering whether one member hides the other along
|
| 1535 |
some paths ([[class.member.lookup]]). There is no such
|
| 1536 |
disambiguation when considering the set of names found as a result
|
| 1537 |
of following *using-directive*s.
|
| 1538 |
|
| 1539 |
+
[^8]: As indicated by syntax, cv-qualifiers are a significant component
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1540 |
in function return types.
|
| 1541 |
|
| 1542 |
+
[^9]: One can explicitly disambiguate the parse either by introducing a
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1543 |
comma (so the ellipsis will be parsed as part of the
|
| 1544 |
*parameter-declaration-clause*) or by introducing a name for the
|
| 1545 |
parameter (so the ellipsis will be parsed as part of the
|
| 1546 |
*declarator-id*).
|
| 1547 |
|
| 1548 |
+
[^10]: This means that default arguments cannot appear, for example, in
|
| 1549 |
declarations of pointers to functions, references to functions, or
|
| 1550 |
`typedef` declarations.
|
| 1551 |
|
| 1552 |
+
[^11]: Implementations are permitted to provide additional predefined
|
| 1553 |
variables with names that are reserved to the implementation (
|
| 1554 |
+
[[lex.name]]). If a predefined variable is not odr-used (
|
| 1555 |
[[basic.def.odr]]), its string value need not be present in the
|
| 1556 |
program image.
|
| 1557 |
|
| 1558 |
+
[^12]: As specified in [[conv.ptr]], converting an integer literal
|
| 1559 |
whose value is `0` to a pointer type results in a null pointer
|
| 1560 |
value.
|
| 1561 |
|
| 1562 |
+
[^13]: The syntax provides for empty *initializer-list*s, but
|
| 1563 |
nonetheless C++does not have zero length arrays.
|
| 1564 |
|
| 1565 |
+
[^14]: This requires a conversion function ([[class.conv.fct]])
|
| 1566 |
returning a reference type.
|