- tmp/tmpgckt6zrt/{from.md → to.md} +1249 -628
tmp/tmpgckt6zrt/{from.md → to.md}
RENAMED
|
@@ -25,11 +25,62 @@ modify the type of the specifiers with operators such as `*` (pointer
|
|
| 25 |
to) and `()` (function returning). Initial values can also be specified
|
| 26 |
in a declarator; initializers are discussed in [[dcl.init]] and
|
| 27 |
[[class.init]].
|
| 28 |
|
| 29 |
Each *init-declarator* in a declaration is analyzed separately as if it
|
| 30 |
-
was in a declaration by itself.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
Declarators have the syntax
|
| 33 |
|
| 34 |
``` bnf
|
| 35 |
declarator:
|
|
@@ -52,16 +103,16 @@ noptr-declarator:
|
|
| 52 |
```
|
| 53 |
|
| 54 |
``` bnf
|
| 55 |
parameters-and-qualifiers:
|
| 56 |
'(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 57 |
-
ref-qualifierₒₚₜ
|
| 58 |
```
|
| 59 |
|
| 60 |
``` bnf
|
| 61 |
trailing-return-type:
|
| 62 |
-
'->'
|
| 63 |
```
|
| 64 |
|
| 65 |
``` bnf
|
| 66 |
ptr-operator:
|
| 67 |
'*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ
|
|
@@ -90,21 +141,10 @@ ref-qualifier:
|
|
| 90 |
``` bnf
|
| 91 |
declarator-id:
|
| 92 |
'...'ₒₚₜ id-expression
|
| 93 |
```
|
| 94 |
|
| 95 |
-
The optional *attribute-specifier-seq* in a *trailing-return-type*
|
| 96 |
-
appertains to the indicated return type. The *type-id* in a
|
| 97 |
-
*trailing-return-type* includes the longest possible sequence of
|
| 98 |
-
*abstract-declarator*s. This resolves the ambiguous binding of array and
|
| 99 |
-
function declarators.
|
| 100 |
-
|
| 101 |
-
``` cpp
|
| 102 |
-
auto f()->int(*)[4]; // function returning a pointer to array[4] of int
|
| 103 |
-
// not function returning array[4] of pointer to int
|
| 104 |
-
```
|
| 105 |
-
|
| 106 |
## Type names <a id="dcl.name">[[dcl.name]]</a>
|
| 107 |
|
| 108 |
To specify type conversions explicitly, and as an argument of `sizeof`,
|
| 109 |
`alignof`, `new`, or `typeid`, the name of a type shall be specified.
|
| 110 |
This can be done with a *type-id*, which is syntactically a declaration
|
|
@@ -114,10 +154,15 @@ entity.
|
|
| 114 |
``` bnf
|
| 115 |
type-id:
|
| 116 |
type-specifier-seq abstract-declaratorₒₚₜ
|
| 117 |
```
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
``` bnf
|
| 120 |
abstract-declarator:
|
| 121 |
ptr-abstract-declarator
|
| 122 |
noptr-abstract-declaratorₒₚₜ parameters-and-qualifiers trailing-return-type
|
| 123 |
abstract-pack-declarator
|
|
@@ -152,25 +197,29 @@ noptr-abstract-pack-declarator:
|
|
| 152 |
It is possible to identify uniquely the location in the
|
| 153 |
*abstract-declarator* where the identifier would appear if the
|
| 154 |
construction were a declarator in a declaration. The named type is then
|
| 155 |
the same as the type of the hypothetical identifier.
|
| 156 |
|
|
|
|
|
|
|
| 157 |
``` cpp
|
| 158 |
int // int i
|
| 159 |
int * // int *pi
|
| 160 |
int *[3] // int *p[3]
|
| 161 |
int (*)[3] // int (*p3i)[3]
|
| 162 |
int *() // int *f()
|
| 163 |
int (*)(double) // int (*pf)(double)
|
| 164 |
```
|
| 165 |
|
| 166 |
-
name respectively the types “`int`
|
| 167 |
-
pointers to `int`
|
| 168 |
-
parameters) returning pointer to `int`
|
| 169 |
-
(`double`) returning `int`
|
| 170 |
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
[[dcl.typedef]]).
|
| 173 |
|
| 174 |
## Ambiguity resolution <a id="dcl.ambig.res">[[dcl.ambig.res]]</a>
|
| 175 |
|
| 176 |
The ambiguity arising from the similarity between a function-style cast
|
|
@@ -178,87 +227,76 @@ and a declaration mentioned in [[stmt.ambig]] can also occur in the
|
|
| 178 |
context of a declaration. In that context, the choice is between a
|
| 179 |
function declaration with a redundant set of parentheses around a
|
| 180 |
parameter name and an object declaration with a function-style cast as
|
| 181 |
the initializer. Just as for the ambiguities mentioned in
|
| 182 |
[[stmt.ambig]], the resolution is to consider any construct that could
|
| 183 |
-
possibly be a declaration a declaration.
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
``` cpp
|
| 189 |
struct S {
|
| 190 |
S(int);
|
| 191 |
};
|
| 192 |
|
| 193 |
void foo(double a) {
|
| 194 |
S w(int(a)); // function declaration
|
| 195 |
S x(int()); // function declaration
|
|
|
|
| 196 |
S y((int)a); // object declaration
|
| 197 |
S z = int(a); // object declaration
|
| 198 |
}
|
| 199 |
```
|
| 200 |
|
| 201 |
-
|
| 202 |
-
and a *type-id* can occur in different contexts. The ambiguity appears
|
| 203 |
-
as a choice between a function-style cast expression and a declaration
|
| 204 |
-
of a type. The resolution is that any construct that could possibly be a
|
| 205 |
-
*type-id* in its syntactic context shall be considered a *type-id*.
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
void foo() {
|
| 212 |
-
const int x = 63;
|
| 213 |
-
new (int(*p)) int; // new-placement expression
|
| 214 |
-
new (int(*[x])); // new type-id
|
| 215 |
-
}
|
| 216 |
-
```
|
| 217 |
|
| 218 |
-
|
| 219 |
|
| 220 |
``` cpp
|
| 221 |
-
template <class T>
|
| 222 |
-
struct
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
```
|
| 228 |
-
|
| 229 |
-
For another example,
|
| 230 |
|
| 231 |
-
|
| 232 |
-
void foo() {
|
| 233 |
-
sizeof(int(1)); // expression
|
| 234 |
sizeof(int()); // type-id (ill-formed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
}
|
| 236 |
```
|
| 237 |
|
| 238 |
-
|
| 239 |
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
|
| 247 |
-
|
| 248 |
-
function declaration, or in a *type-id* that is the operand of a
|
| 249 |
-
`sizeof` or `typeid` operator, when a *type-name* is nested in
|
| 250 |
-
parentheses. In this case, the choice is between the declaration of a
|
| 251 |
-
parameter of type pointer to function and the declaration of a parameter
|
| 252 |
-
with redundant parentheses around the *declarator-id*. The resolution is
|
| 253 |
-
to consider the *type-name* as a *simple-type-specifier* rather than a
|
| 254 |
-
*declarator-id*.
|
| 255 |
|
| 256 |
``` cpp
|
| 257 |
class C { };
|
| 258 |
void f(int(C)) { } // void f(int(*fp)(C c)) { }
|
| 259 |
-
// not: void f(int C)
|
| 260 |
|
| 261 |
int g(C);
|
| 262 |
|
| 263 |
void foo() {
|
| 264 |
f(1); // error: cannot convert 1 to function pointer
|
|
@@ -272,72 +310,82 @@ For another example,
|
|
| 272 |
class C { };
|
| 273 |
void h(int *(C[10])); // void h(int *(*_fp)(C _parm[10]));
|
| 274 |
// not: void h(int *C[10]);
|
| 275 |
```
|
| 276 |
|
|
|
|
|
|
|
| 277 |
## Meaning of declarators <a id="dcl.meaning">[[dcl.meaning]]</a>
|
| 278 |
|
| 279 |
-
A
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
qualifier is the global `::` scope resolution
|
| 296 |
-
*declarator-id* refers to a name declared in the global
|
|
|
|
|
|
|
| 297 |
The optional *attribute-specifier-seq* following a *declarator-id*
|
| 298 |
appertains to the entity that is declared.
|
| 299 |
|
| 300 |
-
A `static`, `thread_local`, `extern`, `
|
| 301 |
-
`
|
| 302 |
-
*declarator-id* in an *init-declarator-list*
|
| 303 |
-
|
| 304 |
-
*declarator*.
|
| 305 |
|
| 306 |
Thus, a declaration of a particular identifier has the form
|
| 307 |
|
| 308 |
``` cpp
|
| 309 |
T D
|
| 310 |
```
|
| 311 |
|
| 312 |
-
where `T` is of the form *attribute-specifier-seqₒₚₜ
|
| 313 |
*decl-specifier-seq* and `D` is a declarator. Following is a recursive
|
| 314 |
procedure for determining the type specified for the contained
|
| 315 |
*declarator-id* by such a declaration.
|
| 316 |
|
| 317 |
First, the *decl-specifier-seq* determines a type. In a declaration
|
| 318 |
|
| 319 |
``` cpp
|
| 320 |
T D
|
| 321 |
```
|
| 322 |
|
| 323 |
-
the *decl-specifier-seq* `T` determines the type `T`.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
| 325 |
``` cpp
|
| 326 |
int unsigned i;
|
| 327 |
```
|
| 328 |
|
| 329 |
the type specifiers `int` `unsigned` determine the type “`unsigned int`”
|
| 330 |
([[dcl.type.simple]]).
|
| 331 |
|
| 332 |
-
|
|
|
|
|
|
|
| 333 |
unadorned identifier the type of this identifier is “`T`”.
|
| 334 |
|
| 335 |
In a declaration `T` `D` where `D` has the form
|
| 336 |
|
| 337 |
``` bnf
|
| 338 |
-
( D1 )
|
| 339 |
```
|
| 340 |
|
| 341 |
the type of the contained *declarator-id* is the same as that of the
|
| 342 |
contained *declarator-id* in the declaration
|
| 343 |
|
|
@@ -354,18 +402,21 @@ In a declaration `T` `D` where `D` has the form
|
|
| 354 |
|
| 355 |
``` bnf
|
| 356 |
'*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
| 357 |
```
|
| 358 |
|
| 359 |
-
and the type of the identifier in the declaration `T` `D1` is
|
| 360 |
-
then the type of the identifier of
|
| 361 |
-
*cv-qualifier*
|
| 362 |
-
|
|
|
|
| 363 |
[[dcl.attr.grammar]]) appertains to the pointer and not to the object
|
| 364 |
pointed to.
|
| 365 |
|
| 366 |
-
|
|
|
|
|
|
|
| 367 |
|
| 368 |
``` cpp
|
| 369 |
const int ci = 10, *pc = &ci, *const cpc = pc, **ppc;
|
| 370 |
int i, *p, *const cp = &i;
|
| 371 |
```
|
|
@@ -402,56 +453,68 @@ ppc = &p; // error
|
|
| 402 |
Each is unacceptable because it would either change the value of an
|
| 403 |
object declared `const` or allow it to be changed through a
|
| 404 |
cv-unqualified pointer later, for example:
|
| 405 |
|
| 406 |
``` cpp
|
| 407 |
-
*ppc = &ci; // OK, but would make p point to ci
|
| 408 |
-
// ... because of previous error
|
| 409 |
*p = 5; // clobber ci
|
| 410 |
```
|
| 411 |
|
|
|
|
|
|
|
| 412 |
See also [[expr.ass]] and [[dcl.init]].
|
| 413 |
|
| 414 |
-
Forming a pointer to reference type is ill-formed; see
|
| 415 |
-
Forming a
|
| 416 |
-
has *cv-qualifier*s or a *ref-qualifier*; see
|
| 417 |
-
address of a bit-field ([[class.bit]]) cannot be
|
| 418 |
-
never point to a bit-field.
|
| 419 |
|
| 420 |
### References <a id="dcl.ref">[[dcl.ref]]</a>
|
| 421 |
|
| 422 |
In a declaration `T` `D` where `D` has either of the forms
|
| 423 |
|
| 424 |
``` bnf
|
| 425 |
'&' attribute-specifier-seqₒₚₜ 'D1'
|
| 426 |
'&&' attribute-specifier-seqₒₚₜ 'D1'
|
| 427 |
```
|
| 428 |
|
| 429 |
-
and the type of the identifier in the declaration `T` `D1` is
|
| 430 |
-
then the type of the identifier of
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
|
|
|
|
|
|
|
|
|
| 436 |
|
| 437 |
``` cpp
|
| 438 |
typedef int& A;
|
| 439 |
const A aref = 3; // ill-formed; lvalue reference to non-const initialized with rvalue
|
| 440 |
```
|
| 441 |
|
| 442 |
The type of `aref` is “lvalue reference to `int`”, not “lvalue reference
|
| 443 |
-
to `const int`”.
|
| 444 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
ill-formed.
|
| 446 |
|
| 447 |
A reference type that is declared using `&` is called an *lvalue
|
| 448 |
reference*, and a reference type that is declared using `&&` is called
|
| 449 |
an *rvalue reference*. Lvalue references and rvalue references are
|
| 450 |
distinct types. Except where explicitly noted, they are semantically
|
| 451 |
equivalent and commonly referred to as references.
|
| 452 |
|
|
|
|
|
|
|
| 453 |
``` cpp
|
| 454 |
void f(double& a) { a += 3.14; }
|
| 455 |
// ...
|
| 456 |
double d = 0;
|
| 457 |
f(d);
|
|
@@ -492,33 +555,42 @@ void k() {
|
|
| 492 |
```
|
| 493 |
|
| 494 |
declares `p` to be a reference to a pointer to `link` so `h(q)` will
|
| 495 |
leave `q` with the value zero. See also [[dcl.init.ref]].
|
| 496 |
|
|
|
|
|
|
|
| 497 |
It is unspecified whether or not a reference requires storage (
|
| 498 |
[[basic.stc]]).
|
| 499 |
|
| 500 |
There shall be no references to references, no arrays of references, and
|
| 501 |
no pointers to references. The declaration of a reference shall contain
|
| 502 |
an *initializer* ([[dcl.init.ref]]) except when the declaration
|
| 503 |
contains an explicit `extern` specifier ([[dcl.stc]]), is a class
|
| 504 |
member ([[class.mem]]) declaration within a class definition, or is the
|
| 505 |
declaration of a parameter or a return type ([[dcl.fct]]); see
|
| 506 |
[[basic.def]]. A reference shall be initialized to refer to a valid
|
| 507 |
-
object or function.
|
|
|
|
|
|
|
| 508 |
well-defined program, because the only way to create such a reference
|
| 509 |
would be to bind it to the “object” obtained by indirection through a
|
| 510 |
null pointer, which causes undefined behavior. As described in
|
| 511 |
-
[[class.bit]], a reference cannot be bound directly to a
|
|
|
|
| 512 |
|
| 513 |
If a *typedef-name* ([[dcl.typedef]], [[temp.param]]) or a
|
| 514 |
*decltype-specifier* ([[dcl.type.simple]]) denotes a type `TR` that is
|
| 515 |
a reference to a type `T`, an attempt to create the type “lvalue
|
| 516 |
reference to cv `TR`” creates the type “lvalue reference to `T`”, while
|
| 517 |
an attempt to create the type “rvalue reference to cv `TR`” creates the
|
| 518 |
type `TR`.
|
| 519 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
``` cpp
|
| 521 |
int i;
|
| 522 |
typedef int& LRI;
|
| 523 |
typedef int&& RRI;
|
| 524 |
|
|
@@ -531,26 +603,33 @@ RRI&& r5 = 5; // r5 has the type int&&
|
|
| 531 |
|
| 532 |
decltype(r2)& r6 = i; // r6 has the type int&
|
| 533 |
decltype(r2)&& r7 = i; // r7 has the type int&
|
| 534 |
```
|
| 535 |
|
| 536 |
-
|
| 537 |
-
|
|
|
|
|
|
|
|
|
|
| 538 |
|
| 539 |
### Pointers to members <a id="dcl.mptr">[[dcl.mptr]]</a>
|
| 540 |
|
| 541 |
In a declaration `T` `D` where `D` has the form
|
| 542 |
|
| 543 |
``` bnf
|
| 544 |
-
nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ D1
|
| 545 |
```
|
| 546 |
|
| 547 |
and the *nested-name-specifier* denotes a class, and the type of the
|
| 548 |
-
identifier in the declaration `T` `D1` is
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
|
| 553 |
``` cpp
|
| 554 |
struct X {
|
| 555 |
void f(int);
|
| 556 |
int a;
|
|
@@ -572,24 +651,24 @@ declaration of `pmc` is well-formed even though `Y` is an incomplete
|
|
| 572 |
type. `pmi` and `pmf` can be used like this:
|
| 573 |
|
| 574 |
``` cpp
|
| 575 |
X obj;
|
| 576 |
// ...
|
| 577 |
-
obj.*pmi = 7; // assign 7 to an integer
|
| 578 |
-
|
| 579 |
-
(obj.*pmf)(7); // call a function member of obj
|
| 580 |
-
// with the argument 7
|
| 581 |
```
|
| 582 |
|
|
|
|
|
|
|
| 583 |
A pointer to member shall not point to a static member of a class (
|
| 584 |
-
[[class.static]]), a member with reference type, or “
|
| 585 |
|
| 586 |
-
See also [[expr.unary]] and [[expr.mptr.oper]]. The type
|
| 587 |
-
member” is distinct from the type “pointer”, that is, a
|
| 588 |
-
member is declared only by the pointer to member declarator
|
| 589 |
-
never by the pointer declarator syntax. There is no
|
| 590 |
-
“reference-to-member” type in C++.
|
| 591 |
|
| 592 |
### Arrays <a id="dcl.array">[[dcl.array]]</a>
|
| 593 |
|
| 594 |
In a declaration `T` `D` where `D` has the form
|
| 595 |
|
|
@@ -598,56 +677,65 @@ In a declaration `T` `D` where `D` has the form
|
|
| 598 |
```
|
| 599 |
|
| 600 |
and the type of the identifier in the declaration `T` `D1` is
|
| 601 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 602 |
`D` is an array type; if the type of the identifier of `D` contains the
|
| 603 |
-
`auto` , the program is ill-formed. `T` is called the
|
| 604 |
-
type*; this type shall not be a reference type,
|
| 605 |
-
cv
|
| 606 |
-
|
| 607 |
converted constant expression of type `std::size_t` and its value shall
|
| 608 |
be greater than zero. The constant expression specifies the *bound* of
|
| 609 |
(number of elements in) the array. If the value of the constant
|
| 610 |
expression is `N`, the array has `N` elements numbered `0` to `N-1`, and
|
| 611 |
-
the type of the identifier of `D` is “
|
| 612 |
-
array
|
| 613 |
-
subobjects of type `T`. Except as noted
|
| 614 |
-
expression is omitted, the type of the identifier
|
| 615 |
-
|
| 616 |
-
`
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
array
|
|
|
|
|
|
|
|
|
|
|
|
|
| 621 |
|
| 622 |
``` cpp
|
| 623 |
typedef int A[5], AA[2][3];
|
| 624 |
typedef const A CA; // type is ``array of 5 const int''
|
| 625 |
typedef const AA CAA; // type is ``array of 2 array of 3 const int''
|
| 626 |
```
|
| 627 |
|
| 628 |
-
|
| 629 |
-
|
|
|
|
|
|
|
| 630 |
|
| 631 |
An array can be constructed from one of the fundamental types (except
|
| 632 |
`void`), from a pointer, from a pointer to member, from a class, from an
|
| 633 |
enumeration type, or from another array.
|
| 634 |
|
| 635 |
When several “array of” specifications are adjacent, a multidimensional
|
| 636 |
-
array is created; only the first of the constant expressions that
|
| 637 |
specify the bounds of the arrays may be omitted. In addition to
|
| 638 |
declarations in which an incomplete object type is allowed, an array
|
| 639 |
bound may be omitted in some cases in the declaration of a function
|
| 640 |
parameter ([[dcl.fct]]). An array bound may also be omitted when the
|
| 641 |
-
declarator is followed by an *initializer*
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
|
|
|
|
|
|
|
|
|
| 649 |
|
| 650 |
``` cpp
|
| 651 |
float fa[17], *afp[17];
|
| 652 |
```
|
| 653 |
|
|
@@ -677,31 +765,38 @@ void f() {
|
|
| 677 |
extern int x[];
|
| 678 |
int i = sizeof(x); // error: incomplete object type
|
| 679 |
}
|
| 680 |
```
|
| 681 |
|
| 682 |
-
|
| 683 |
-
[[conv.array]]. Objects of array types cannot be modified, see
|
| 684 |
-
[[basic.lval]].
|
| 685 |
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 692 |
|
| 693 |
A consistent rule is followed for multidimensional arrays. If `E` is an
|
| 694 |
*n*-dimensional array of rank i × j × … × k, then `E` appearing in an
|
| 695 |
expression that is subject to the array-to-pointer conversion (
|
| 696 |
[[conv.array]]) is converted to a pointer to an (n-1)-dimensional array
|
| 697 |
with rank j × … × k. If the `*` operator, either explicitly or
|
| 698 |
implicitly as a result of subscripting, is applied to this pointer, the
|
| 699 |
result is the pointed-to (n-1)-dimensional array, which itself is
|
| 700 |
immediately converted into a pointer.
|
| 701 |
|
| 702 |
-
|
|
|
|
|
|
|
| 703 |
|
| 704 |
``` cpp
|
| 705 |
int x[3][5];
|
| 706 |
```
|
| 707 |
|
|
@@ -714,50 +809,60 @@ multiplying `i` by the length of the object to which the pointer points,
|
|
| 714 |
namely five integer objects. The results are added and indirection
|
| 715 |
applied to yield an array (of five integers), which in turn is converted
|
| 716 |
to a pointer to the first of the integers. If there is another subscript
|
| 717 |
the same argument applies again; this time the result is an integer.
|
| 718 |
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 723 |
|
| 724 |
### Functions <a id="dcl.fct">[[dcl.fct]]</a>
|
| 725 |
|
| 726 |
In a declaration `T` `D` where `D` has the form
|
| 727 |
|
| 728 |
``` bnf
|
| 729 |
'D1 (' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 730 |
-
ref-qualifierₒₚₜ
|
| 731 |
```
|
| 732 |
|
| 733 |
and the type of the contained *declarator-id* in the declaration `T`
|
| 734 |
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 735 |
-
*declarator-id* in `D` is “
|
| 736 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 737 |
|
| 738 |
In a declaration `T` `D` where `D` has the form
|
| 739 |
|
| 740 |
``` bnf
|
| 741 |
'D1 (' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 742 |
-
ref-qualifierₒₚₜ
|
| 743 |
```
|
| 744 |
|
| 745 |
and the type of the contained *declarator-id* in the declaration `T`
|
| 746 |
`D1` is “*derived-declarator-type-list* `T`”, `T` shall be the single
|
| 747 |
*type-specifier* `auto`. The type of the *declarator-id* in `D` is
|
| 748 |
-
“*derived-declarator-type-list* function of
|
| 749 |
-
(*parameter-declaration-clause*) *cv-qualifier-seq*
|
| 750 |
-
|
|
|
|
|
|
|
| 751 |
*attribute-specifier-seq* appertains to the function type.
|
| 752 |
|
| 753 |
-
A type of either form is a *function type*.[^
|
| 754 |
|
| 755 |
``` bnf
|
| 756 |
parameter-declaration-clause:
|
| 757 |
-
parameter-declaration-listₒₚₜ ...ₒₚₜ
|
| 758 |
-
parameter-declaration-list ',
|
| 759 |
```
|
| 760 |
|
| 761 |
``` bnf
|
| 762 |
parameter-declaration-list:
|
| 763 |
parameter-declaration
|
|
@@ -774,23 +879,30 @@ parameter-declaration:
|
|
| 774 |
|
| 775 |
The optional *attribute-specifier-seq* in a *parameter-declaration*
|
| 776 |
appertains to the parameter.
|
| 777 |
|
| 778 |
The *parameter-declaration-clause* determines the arguments that can be
|
| 779 |
-
specified, and their processing, when the function is called.
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
|
|
|
|
|
|
|
|
|
| 783 |
arguments. A parameter list consisting of a single unnamed parameter of
|
| 784 |
non-dependent type `void` is equivalent to an empty parameter list.
|
| 785 |
Except for this special case, a parameter shall not have type *cv*
|
| 786 |
`void`. If the *parameter-declaration-clause* terminates with an
|
| 787 |
ellipsis or a function parameter pack ([[temp.variadic]]), the number
|
| 788 |
of arguments shall be equal to or greater than the number of parameters
|
| 789 |
that do not have a default argument and are not function parameter
|
| 790 |
-
packs. Where syntactically correct and where “” is not part of an
|
| 791 |
-
*abstract-declarator*, “” is synonymous with “”.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 792 |
|
| 793 |
``` cpp
|
| 794 |
int printf(const char*, ...);
|
| 795 |
```
|
| 796 |
|
|
@@ -801,31 +913,35 @@ arguments.
|
|
| 801 |
printf("hello world");
|
| 802 |
printf("a=%d b=%d", a, b);
|
| 803 |
```
|
| 804 |
|
| 805 |
However, the first argument must be of a type that can be converted to a
|
| 806 |
-
`const` `char*`
|
|
|
|
|
|
|
|
|
|
|
|
|
| 807 |
accessing arguments passed using the ellipsis (see [[expr.call]] and
|
| 808 |
-
[[support.runtime]]).
|
| 809 |
|
| 810 |
A single name can be used for several different functions in a single
|
| 811 |
scope; this is function overloading (Clause [[over]]). All declarations
|
| 812 |
for a function shall agree exactly in both the return type and the
|
| 813 |
parameter-type-list. The type of a function is determined using the
|
| 814 |
following rules. The type of each parameter (including function
|
| 815 |
parameter packs) is determined from its own *decl-specifier-seq* and
|
| 816 |
*declarator*. After determining the type of each parameter, any
|
| 817 |
-
parameter of type “array of `T`” or
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
`int(*)(const int p, decltype(p)*)` and
|
| 826 |
-
identical types.
|
| 827 |
|
| 828 |
A function type with a *cv-qualifier-seq* or a *ref-qualifier*
|
| 829 |
(including a type named by *typedef-name* ([[dcl.typedef]],
|
| 830 |
[[temp.param]])) shall appear only as:
|
| 831 |
|
|
@@ -836,82 +952,101 @@ A function type with a *cv-qualifier-seq* or a *ref-qualifier*
|
|
| 836 |
- the *type-id* in the default argument of a *type-parameter* (
|
| 837 |
[[temp.param]]), or
|
| 838 |
- the *type-id* of a *template-argument* for a *type-parameter* (
|
| 839 |
[[temp.arg.type]]).
|
| 840 |
|
|
|
|
|
|
|
| 841 |
``` cpp
|
| 842 |
typedef int FIC(int) const;
|
| 843 |
FIC f; // ill-formed: does not declare a member function
|
| 844 |
struct S {
|
| 845 |
FIC f; // OK
|
| 846 |
};
|
| 847 |
FIC S::*pm = &S::f; // OK
|
| 848 |
```
|
| 849 |
|
|
|
|
|
|
|
| 850 |
The effect of a *cv-qualifier-seq* in a function declarator is not the
|
| 851 |
same as adding cv-qualification on top of the function type. In the
|
| 852 |
-
latter case, the cv-qualifiers are ignored.
|
| 853 |
-
|
| 854 |
-
function
|
|
|
|
|
|
|
|
|
|
|
|
|
| 855 |
|
| 856 |
``` cpp
|
| 857 |
typedef void F();
|
| 858 |
struct S {
|
| 859 |
const F f; // OK: equivalent to: void f();
|
| 860 |
};
|
| 861 |
```
|
| 862 |
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
|
|
|
|
|
|
|
|
|
| 867 |
initializations of pointers to functions, references to functions, and
|
| 868 |
-
pointers to member functions.
|
| 869 |
|
| 870 |
-
|
|
|
|
|
|
|
| 871 |
|
| 872 |
``` cpp
|
| 873 |
int fseek(FILE*, long, int);
|
| 874 |
```
|
| 875 |
|
| 876 |
declares a function taking three arguments of the specified types, and
|
| 877 |
returning `int` ([[dcl.type]]).
|
| 878 |
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
|
| 884 |
-
|
| 885 |
|
| 886 |
Types shall not be defined in return or parameter types. The type of a
|
| 887 |
parameter or the return type for a function definition shall not be an
|
| 888 |
-
incomplete
|
| 889 |
-
|
| 890 |
-
|
| 891 |
-
classes defined within the class).
|
| 892 |
|
| 893 |
A typedef of function type may be used to declare a function but shall
|
| 894 |
not be used to define a function ([[dcl.fct.def]]).
|
| 895 |
|
|
|
|
|
|
|
| 896 |
``` cpp
|
| 897 |
typedef void F();
|
| 898 |
F fv; // OK: equivalent to void fv();
|
| 899 |
F fv { } // ill-formed
|
| 900 |
void fv() { } // OK: definition of fv
|
| 901 |
```
|
| 902 |
|
|
|
|
|
|
|
| 903 |
An identifier can optionally be provided as a parameter name; if present
|
| 904 |
-
in a function definition ([[dcl.fct.def]]), it names a parameter.
|
| 905 |
-
particular, parameter names are also optional in function definitions
|
| 906 |
-
and names used for a parameter in different declarations and the
|
| 907 |
-
definition of a function need not be the same. If a parameter name is
|
| 908 |
-
present in a function declaration that is not a definition, it cannot be
|
| 909 |
-
used outside of its function declarator because that is the extent of
|
| 910 |
-
its potential scope ([[basic.scope.proto]]).
|
| 911 |
|
| 912 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 913 |
|
| 914 |
``` cpp
|
| 915 |
int i,
|
| 916 |
*pi,
|
| 917 |
f(),
|
|
@@ -931,13 +1066,19 @@ The binding of `*fpi(int)` is `*(fpi(int))`, so the declaration
|
|
| 931 |
suggests, and the same construction in an expression requires, the
|
| 932 |
calling of a function `fpi`, and then using indirection through the
|
| 933 |
(pointer) result to yield an integer. In the declarator
|
| 934 |
`(*pif)(const char*, const char*)`, the extra parentheses are necessary
|
| 935 |
to indicate that indirection through a pointer to a function yields a
|
| 936 |
-
function, which is then called.
|
| 937 |
-
|
| 938 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 939 |
|
| 940 |
``` cpp
|
| 941 |
typedef int IFUNC(int);
|
| 942 |
IFUNC* fpif(int);
|
| 943 |
```
|
|
@@ -959,21 +1100,30 @@ rather than
|
|
| 959 |
|
| 960 |
``` cpp
|
| 961 |
template <class T, class U> decltype((*(T*)0) + (*(U*)0)) add(T t, U u);
|
| 962 |
```
|
| 963 |
|
|
|
|
|
|
|
| 964 |
A *non-template function* is a function that is not a function template
|
| 965 |
-
specialization.
|
|
|
|
|
|
|
| 966 |
|
| 967 |
A *declarator-id* or *abstract-declarator* containing an ellipsis shall
|
| 968 |
only be used in a *parameter-declaration*. Such a
|
| 969 |
*parameter-declaration* is a parameter pack ([[temp.variadic]]). When
|
| 970 |
it is part of a *parameter-declaration-clause*, the parameter pack is a
|
| 971 |
-
function parameter pack ([[temp.variadic]]).
|
| 972 |
-
|
| 973 |
-
|
| 974 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 975 |
|
| 976 |
``` cpp
|
| 977 |
template<typename... T> void f(T (* ...t)(int, int));
|
| 978 |
|
| 979 |
int add(int, int);
|
|
@@ -982,24 +1132,28 @@ float subtract(int, int);
|
|
| 982 |
void g() {
|
| 983 |
f(add, subtract);
|
| 984 |
}
|
| 985 |
```
|
| 986 |
|
|
|
|
|
|
|
| 987 |
There is a syntactic ambiguity when an ellipsis occurs at the end of a
|
| 988 |
*parameter-declaration-clause* without a preceding comma. In this case,
|
| 989 |
the ellipsis is parsed as part of the *abstract-declarator* if the type
|
| 990 |
of the parameter either names a template parameter pack that has not
|
| 991 |
been expanded or contains `auto`; otherwise, it is parsed as part of the
|
| 992 |
-
*parameter-declaration-clause*.[^
|
| 993 |
|
| 994 |
### Default arguments <a id="dcl.fct.default">[[dcl.fct.default]]</a>
|
| 995 |
|
| 996 |
If an *initializer-clause* is specified in a *parameter-declaration*
|
| 997 |
this *initializer-clause* is used as a default argument. Default
|
| 998 |
arguments will be used in calls where trailing arguments are missing.
|
| 999 |
|
| 1000 |
-
|
|
|
|
|
|
|
| 1001 |
|
| 1002 |
``` cpp
|
| 1003 |
void point(int = 3, int = 4);
|
| 1004 |
```
|
| 1005 |
|
|
@@ -1011,17 +1165,20 @@ point(1,2); point(1); point();
|
|
| 1011 |
```
|
| 1012 |
|
| 1013 |
The last two calls are equivalent to `point(1,4)` and `point(3,4)`,
|
| 1014 |
respectively.
|
| 1015 |
|
|
|
|
|
|
|
| 1016 |
A default argument shall be specified only in the
|
| 1017 |
-
*parameter-declaration-clause* of a function declaration or
|
| 1018 |
-
*template-parameter* ([[temp.param]]); in
|
| 1019 |
-
*initializer-clause* shall be an
|
| 1020 |
-
argument shall not be specified for a
|
| 1021 |
-
in a *parameter-declaration-clause*,
|
| 1022 |
-
*declarator* or *abstract-declarator* of a
|
|
|
|
| 1023 |
|
| 1024 |
For non-template functions, default arguments can be added in later
|
| 1025 |
declarations of a function in the same scope. Declarations in different
|
| 1026 |
scopes have completely distinct sets of default arguments. That is,
|
| 1027 |
declarations in inner scopes do not acquire default arguments from
|
|
@@ -1030,33 +1187,35 @@ declaration, each parameter subsequent to a parameter with a default
|
|
| 1030 |
argument shall have a default argument supplied in this or a previous
|
| 1031 |
declaration or shall be a function parameter pack. A default argument
|
| 1032 |
shall not be redefined by a later declaration (not even to the same
|
| 1033 |
value).
|
| 1034 |
|
|
|
|
|
|
|
| 1035 |
``` cpp
|
| 1036 |
void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow
|
| 1037 |
// a parameter with a default argument
|
| 1038 |
void f(int, int);
|
| 1039 |
void f(int, int = 7);
|
| 1040 |
void h() {
|
| 1041 |
f(3); // OK, calls f(3, 7)
|
| 1042 |
-
void f(int = 1, int); // error: does not use default
|
| 1043 |
-
// from surrounding scope
|
| 1044 |
}
|
| 1045 |
void m() {
|
| 1046 |
void f(int, int); // has no defaults
|
| 1047 |
f(4); // error: wrong number of arguments
|
| 1048 |
void f(int, int = 5); // OK
|
| 1049 |
f(4); // OK, calls f(4, 5);
|
| 1050 |
-
void f(int, int = 5); // error: cannot redefine, even to
|
| 1051 |
-
// same value
|
| 1052 |
}
|
| 1053 |
void n() {
|
| 1054 |
f(6); // OK, calls f(6, 7)
|
| 1055 |
}
|
| 1056 |
```
|
| 1057 |
|
|
|
|
|
|
|
| 1058 |
For a given inline function defined in different translation units, the
|
| 1059 |
accumulated sets of default arguments at the end of the translation
|
| 1060 |
units shall be the same; see [[basic.def.odr]]. If a friend declaration
|
| 1061 |
specifies a default argument expression, that declaration shall be a
|
| 1062 |
definition and shall be the only declaration of the function or function
|
|
@@ -1067,12 +1226,15 @@ initializer in a declaration of a variable of the parameter type, using
|
|
| 1067 |
the copy-initialization semantics ([[dcl.init]]). The names in the
|
| 1068 |
default argument are bound, and the semantic constraints are checked, at
|
| 1069 |
the point where the default argument appears. Name lookup and checking
|
| 1070 |
of semantic constraints for default arguments in function templates and
|
| 1071 |
in member functions of class templates are performed as described in
|
| 1072 |
-
[[temp.inst]].
|
| 1073 |
-
|
|
|
|
|
|
|
|
|
|
| 1074 |
|
| 1075 |
``` cpp
|
| 1076 |
int a = 1;
|
| 1077 |
int f(int);
|
| 1078 |
int g(int x = f(a)); // default argument: f(::a)
|
|
@@ -1084,13 +1246,16 @@ void h() {
|
|
| 1084 |
g(); // g(f(::a))
|
| 1085 |
}
|
| 1086 |
}
|
| 1087 |
```
|
| 1088 |
|
| 1089 |
-
|
| 1090 |
-
|
| 1091 |
-
|
|
|
|
|
|
|
|
|
|
| 1092 |
|
| 1093 |
Except for member functions of class templates, the default arguments in
|
| 1094 |
a member function definition that appears outside of the class
|
| 1095 |
definition are added to the set of default arguments provided by the
|
| 1096 |
member function declaration in the class definition; the program is
|
|
@@ -1098,80 +1263,105 @@ ill-formed if a default constructor ([[class.ctor]]), copy or move
|
|
| 1098 |
constructor, or copy or move assignment operator ([[class.copy]]) is so
|
| 1099 |
declared. Default arguments for a member function of a class template
|
| 1100 |
shall be specified on the initial declaration of the member function
|
| 1101 |
within the class template.
|
| 1102 |
|
|
|
|
|
|
|
| 1103 |
``` cpp
|
| 1104 |
class C {
|
| 1105 |
void f(int i = 3);
|
| 1106 |
void g(int i, int j = 99);
|
| 1107 |
};
|
| 1108 |
|
| 1109 |
-
void C::f(int i = 3) {
|
| 1110 |
-
}
|
| 1111 |
-
void C::g(int i = 88, int j) { // in this translation unit,
|
| 1112 |
-
} // C::g can be called with no argument
|
| 1113 |
```
|
| 1114 |
|
| 1115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1116 |
|
| 1117 |
``` cpp
|
| 1118 |
void f() {
|
| 1119 |
int i;
|
| 1120 |
extern void g(int x = i); // error
|
|
|
|
| 1121 |
// ...
|
| 1122 |
}
|
| 1123 |
```
|
| 1124 |
|
| 1125 |
-
|
| 1126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1127 |
|
| 1128 |
``` cpp
|
| 1129 |
class A {
|
| 1130 |
void f(A* p = this) { } // error
|
| 1131 |
};
|
| 1132 |
```
|
| 1133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1134 |
A default argument is evaluated each time the function is called with no
|
| 1135 |
-
argument for the corresponding parameter.
|
| 1136 |
-
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
|
|
|
| 1140 |
|
| 1141 |
``` cpp
|
| 1142 |
int a;
|
| 1143 |
-
int f(int a, int b = a); // error: parameter a
|
| 1144 |
-
// used as default argument
|
| 1145 |
typedef int I;
|
| 1146 |
int g(float I, int b = I(2)); // error: parameter I found
|
| 1147 |
-
int h(int a, int b = sizeof(a)); //
|
| 1148 |
-
// in default argument
|
| 1149 |
```
|
| 1150 |
|
| 1151 |
-
|
| 1152 |
-
|
| 1153 |
-
|
| 1154 |
-
|
| 1155 |
-
|
| 1156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1157 |
|
| 1158 |
``` cpp
|
| 1159 |
int b;
|
| 1160 |
class X {
|
| 1161 |
int a;
|
| 1162 |
-
int mem1(int i = a);
|
| 1163 |
-
// used as default argument
|
| 1164 |
int mem2(int i = b); // OK; use X::b
|
| 1165 |
static int b;
|
| 1166 |
};
|
| 1167 |
```
|
| 1168 |
|
| 1169 |
The declaration of `X::mem2()` is meaningful, however, since no object
|
| 1170 |
is needed to access the static member `X::b`. Classes, objects, and
|
| 1171 |
-
members are described in Clause [[class]].
|
| 1172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1173 |
|
| 1174 |
``` cpp
|
| 1175 |
int f(int = 0);
|
| 1176 |
|
| 1177 |
void h() {
|
|
@@ -1181,10 +1371,12 @@ void h() {
|
|
| 1181 |
|
| 1182 |
int (*p1)(int) = &f;
|
| 1183 |
int (*p2)() = &f; // error: type mismatch
|
| 1184 |
```
|
| 1185 |
|
|
|
|
|
|
|
| 1186 |
When a declaration of a function is introduced by way of a
|
| 1187 |
*using-declaration* ([[namespace.udecl]]), any default argument
|
| 1188 |
information associated with the declaration is made known as well. If
|
| 1189 |
the function is redeclared thereafter in the namespace with additional
|
| 1190 |
default arguments, the additional arguments are also known at any point
|
|
@@ -1194,10 +1386,12 @@ A virtual function call ([[class.virtual]]) uses the default arguments
|
|
| 1194 |
in the declaration of the virtual function determined by the static type
|
| 1195 |
of the pointer or reference denoting the object. An overriding function
|
| 1196 |
in a derived class does not acquire default arguments from the function
|
| 1197 |
it overrides.
|
| 1198 |
|
|
|
|
|
|
|
| 1199 |
``` cpp
|
| 1200 |
struct A {
|
| 1201 |
virtual void f(int a = 7);
|
| 1202 |
};
|
| 1203 |
struct B : public A {
|
|
@@ -1209,10 +1403,12 @@ void m() {
|
|
| 1209 |
pa->f(); // OK, calls pa->B::f(7)
|
| 1210 |
pb->f(); // error: wrong number of arguments for B::f()
|
| 1211 |
}
|
| 1212 |
```
|
| 1213 |
|
|
|
|
|
|
|
| 1214 |
## Function definitions <a id="dcl.fct.def">[[dcl.fct.def]]</a>
|
| 1215 |
|
| 1216 |
### In general <a id="dcl.fct.def.general">[[dcl.fct.def.general]]</a>
|
| 1217 |
|
| 1218 |
Function definitions have the form
|
|
@@ -1234,22 +1430,18 @@ Any informal reference to the body of a function should be interpreted
|
|
| 1234 |
as a reference to the non-terminal *function-body*. The optional
|
| 1235 |
*attribute-specifier-seq* in a *function-definition* appertains to the
|
| 1236 |
function. A *virt-specifier-seq* can be part of a *function-definition*
|
| 1237 |
only if it is a *member-declaration* ([[class.mem]]).
|
| 1238 |
|
| 1239 |
-
|
|
|
|
|
|
|
|
|
|
| 1240 |
|
| 1241 |
-
|
| 1242 |
-
'D1 (' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 1243 |
|
| 1244 |
-
|
| 1245 |
-
```
|
| 1246 |
-
|
| 1247 |
-
as described in [[dcl.fct]]. A function shall be defined only in
|
| 1248 |
-
namespace or class scope.
|
| 1249 |
-
|
| 1250 |
-
a simple example of a complete function definition is
|
| 1251 |
|
| 1252 |
``` cpp
|
| 1253 |
int max(int a, int b, int c) {
|
| 1254 |
int m = (a > b) ? a : b;
|
| 1255 |
return (m > c) ? m : c;
|
|
@@ -1257,26 +1449,30 @@ int max(int a, int b, int c) {
|
|
| 1257 |
```
|
| 1258 |
|
| 1259 |
Here `int` is the *decl-specifier-seq*; `max(int` `a,` `int` `b,` `int`
|
| 1260 |
`c)` is the *declarator*; `{ /* ... */ }` is the *function-body*.
|
| 1261 |
|
|
|
|
|
|
|
| 1262 |
A *ctor-initializer* is used only in a constructor; see [[class.ctor]]
|
| 1263 |
and [[class.init]].
|
| 1264 |
|
| 1265 |
-
A *cv-qualifier-seq*
|
| 1266 |
-
|
| 1267 |
-
|
| 1268 |
-
[
|
| 1269 |
|
| 1270 |
Unused parameters need not be named. For example,
|
| 1271 |
|
| 1272 |
``` cpp
|
| 1273 |
void print(int a, int) {
|
| 1274 |
std::printf("a = %d\n",a);
|
| 1275 |
}
|
| 1276 |
```
|
| 1277 |
|
|
|
|
|
|
|
| 1278 |
In the *function-body*, a *function-local predefined variable* denotes a
|
| 1279 |
block-scope object of static storage duration that is implicitly defined
|
| 1280 |
(see [[basic.scope.block]]).
|
| 1281 |
|
| 1282 |
The function-local predefined variable `__func__` is defined as if a
|
|
@@ -1286,20 +1482,24 @@ definition of the form
|
|
| 1286 |
static const char __func__[] = "function-name";
|
| 1287 |
```
|
| 1288 |
|
| 1289 |
had been provided, where *function-name* is an *implementation-defined*
|
| 1290 |
string. It is unspecified whether such a variable has an address
|
| 1291 |
-
distinct from that of any other object in the program.[^
|
|
|
|
|
|
|
| 1292 |
|
| 1293 |
``` cpp
|
| 1294 |
struct S {
|
| 1295 |
S() : s(__func__) { } // OK
|
| 1296 |
const char* s;
|
| 1297 |
};
|
| 1298 |
void f(const char* s = __func__); // error: __func__ is undeclared
|
| 1299 |
```
|
| 1300 |
|
|
|
|
|
|
|
| 1301 |
### Explicitly-defaulted functions <a id="dcl.fct.def.default">[[dcl.fct.def.default]]</a>
|
| 1302 |
|
| 1303 |
A function definition of the form:
|
| 1304 |
|
| 1305 |
``` bnf
|
|
@@ -1315,52 +1515,58 @@ explicitly defaulted shall
|
|
| 1315 |
copy assignment operator, the parameter type may be “reference to
|
| 1316 |
non-const `T`”, where `T` is the name of the member function’s class)
|
| 1317 |
as if it had been implicitly declared, and
|
| 1318 |
- not have default arguments.
|
| 1319 |
|
| 1320 |
-
An explicitly-defaulted function
|
| 1321 |
-
would have been implicitly declared as
|
| 1322 |
-
explicitly defaulted on its first
|
|
|
|
|
|
|
| 1323 |
|
| 1324 |
-
|
| 1325 |
-
|
| 1326 |
-
|
| 1327 |
-
as if it had been implicitly declared ([[except.spec]]).
|
| 1328 |
-
|
| 1329 |
-
If a function that is explicitly defaulted has an explicit
|
| 1330 |
-
*exception-specification* that is not compatible ([[except.spec]]) with
|
| 1331 |
-
the *exception-specification* on the implicit declaration, then
|
| 1332 |
|
| 1333 |
- if the function is explicitly defaulted on its first declaration, it
|
| 1334 |
is defined as deleted;
|
| 1335 |
- otherwise, the program is ill-formed.
|
| 1336 |
|
|
|
|
|
|
|
| 1337 |
``` cpp
|
| 1338 |
struct S {
|
| 1339 |
constexpr S() = default; // ill-formed: implicit S() is not constexpr
|
| 1340 |
S(int a = 0) = default; // ill-formed: default argument
|
| 1341 |
void operator=(const S&) = default; // ill-formed: non-matching return type
|
| 1342 |
-
~S()
|
| 1343 |
private:
|
| 1344 |
int i;
|
| 1345 |
S(S&); // OK: private copy constructor
|
| 1346 |
};
|
| 1347 |
S::S(S&) = default; // OK: defines copy constructor
|
| 1348 |
```
|
| 1349 |
|
|
|
|
|
|
|
| 1350 |
Explicitly-defaulted functions and implicitly-declared functions are
|
| 1351 |
collectively called *defaulted* functions, and the implementation shall
|
| 1352 |
provide implicit definitions for them ([[class.ctor]] [[class.dtor]],
|
| 1353 |
[[class.copy]]), which might mean defining them as deleted. A function
|
| 1354 |
is *user-provided* if it is user-declared and not explicitly defaulted
|
| 1355 |
or deleted on its first declaration. A user-provided
|
| 1356 |
explicitly-defaulted function (i.e., explicitly defaulted after its
|
| 1357 |
first declaration) is defined at the point where it is explicitly
|
| 1358 |
defaulted; if such a function is implicitly defined as deleted, the
|
| 1359 |
-
program is ill-formed.
|
|
|
|
|
|
|
| 1360 |
declaration can provide efficient execution and concise definition while
|
| 1361 |
-
enabling a stable binary interface to an evolving code
|
|
|
|
|
|
|
|
|
|
| 1362 |
|
| 1363 |
``` cpp
|
| 1364 |
struct trivial {
|
| 1365 |
trivial() = default;
|
| 1366 |
trivial(const trivial&) = default;
|
|
@@ -1374,10 +1580,12 @@ struct nontrivial1 {
|
|
| 1374 |
nontrivial1();
|
| 1375 |
};
|
| 1376 |
nontrivial1::nontrivial1() = default; // not first declaration
|
| 1377 |
```
|
| 1378 |
|
|
|
|
|
|
|
| 1379 |
### Deleted definitions <a id="dcl.fct.def.delete">[[dcl.fct.def.delete]]</a>
|
| 1380 |
|
| 1381 |
A function definition of the form:
|
| 1382 |
|
| 1383 |
``` bnf
|
|
@@ -1386,29 +1594,38 @@ attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator virt-spe
|
|
| 1386 |
|
| 1387 |
is called a *deleted definition*. A function with a deleted definition
|
| 1388 |
is also called a *deleted function*.
|
| 1389 |
|
| 1390 |
A program that refers to a deleted function implicitly or explicitly,
|
| 1391 |
-
other than to declare it, is ill-formed.
|
| 1392 |
-
function implicitly or explicitly and forming a pointer or
|
| 1393 |
-
pointer-to-member to the function. It applies even for references in
|
| 1394 |
-
expressions that are not potentially-evaluated. If a function is
|
| 1395 |
-
overloaded, it is referenced only if the function is selected by
|
| 1396 |
-
overload resolution.
|
| 1397 |
|
| 1398 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1399 |
initialization with
|
| 1400 |
|
| 1401 |
``` cpp
|
| 1402 |
struct onlydouble {
|
| 1403 |
onlydouble() = delete; // OK, but redundant
|
| 1404 |
onlydouble(std::intmax_t) = delete;
|
| 1405 |
onlydouble(double);
|
| 1406 |
};
|
| 1407 |
```
|
| 1408 |
|
| 1409 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1410 |
deleted definitions of a user-declared `operator new` for that class.
|
| 1411 |
|
| 1412 |
``` cpp
|
| 1413 |
struct sometype {
|
| 1414 |
void* operator new(std::size_t) = delete;
|
|
@@ -1416,10 +1633,14 @@ struct sometype {
|
|
| 1416 |
};
|
| 1417 |
sometype* p = new sometype; // error, deleted class operator new
|
| 1418 |
sometype* q = new sometype[3]; // error, deleted class operator new[]
|
| 1419 |
```
|
| 1420 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1421 |
One can make a class uncopyable, i.e. move-only, by using deleted
|
| 1422 |
definitions of the copy constructor and copy assignment operator, and
|
| 1423 |
then providing defaulted definitions of the move constructor and move
|
| 1424 |
assignment operator.
|
| 1425 |
|
|
@@ -1434,32 +1655,128 @@ struct moveonly {
|
|
| 1434 |
};
|
| 1435 |
moveonly* p;
|
| 1436 |
moveonly q(*p); // error, deleted copy constructor
|
| 1437 |
```
|
| 1438 |
|
| 1439 |
-
|
| 1440 |
-
|
| 1441 |
-
|
| 1442 |
-
|
| 1443 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1444 |
|
| 1445 |
``` cpp
|
| 1446 |
struct sometype {
|
| 1447 |
sometype();
|
| 1448 |
};
|
| 1449 |
sometype::sometype() = delete; // ill-formed; not first declaration
|
| 1450 |
```
|
| 1451 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1452 |
## Initializers <a id="dcl.init">[[dcl.init]]</a>
|
| 1453 |
|
| 1454 |
A declarator can specify an initial value for the identifier being
|
| 1455 |
declared. The identifier designates a variable being initialized. The
|
| 1456 |
process of initialization described in the remainder of [[dcl.init]]
|
| 1457 |
applies also to initializations specified by other syntactic contexts,
|
| 1458 |
-
such as the initialization of function parameters
|
| 1459 |
-
|
| 1460 |
-
[[stmt.return]]).
|
| 1461 |
|
| 1462 |
``` bnf
|
| 1463 |
initializer:
|
| 1464 |
brace-or-equal-initializer
|
| 1465 |
'(' expression-list ')'
|
|
@@ -1487,59 +1804,87 @@ initializer-list:
|
|
| 1487 |
braced-init-list:
|
| 1488 |
'{' initializer-list ','ₒₚₜ '}'
|
| 1489 |
'{' '}'
|
| 1490 |
```
|
| 1491 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1492 |
Except for objects declared with the `constexpr` specifier, for which
|
| 1493 |
see [[dcl.constexpr]], an *initializer* in the definition of a variable
|
| 1494 |
can consist of arbitrary expressions involving literals and previously
|
| 1495 |
declared variables and functions, regardless of the variable’s storage
|
| 1496 |
duration.
|
| 1497 |
|
|
|
|
|
|
|
| 1498 |
``` cpp
|
| 1499 |
int f(int);
|
| 1500 |
int a = 2;
|
| 1501 |
int b = f(a);
|
| 1502 |
int c(b);
|
| 1503 |
```
|
| 1504 |
|
| 1505 |
-
|
| 1506 |
|
| 1507 |
-
|
| 1508 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1509 |
|
| 1510 |
A declaration of a block-scope variable with external or internal
|
| 1511 |
linkage that has an *initializer* is ill-formed.
|
| 1512 |
|
| 1513 |
To *zero-initialize* an object or reference of type `T` means:
|
| 1514 |
|
| 1515 |
- if `T` is a scalar type ([[basic.types]]), the object is initialized
|
| 1516 |
to the value obtained by converting the integer literal `0` (zero) to
|
| 1517 |
-
`T`;[^
|
| 1518 |
- if `T` is a (possibly cv-qualified) non-union class type, each
|
| 1519 |
-
non-static data member
|
| 1520 |
-
|
|
|
|
| 1521 |
- if `T` is a (possibly cv-qualified) union type, the object’s first
|
| 1522 |
non-static named data member is zero-initialized and padding is
|
| 1523 |
initialized to zero bits;
|
| 1524 |
- if `T` is an array type, each element is zero-initialized;
|
| 1525 |
- if `T` is a reference type, no initialization is performed.
|
| 1526 |
|
| 1527 |
To *default-initialize* an object of type `T` means:
|
| 1528 |
|
| 1529 |
-
-
|
| 1530 |
-
|
| 1531 |
-
|
| 1532 |
-
|
| 1533 |
-
|
| 1534 |
-
|
| 1535 |
-
-
|
| 1536 |
-
-
|
| 1537 |
|
| 1538 |
-
|
| 1539 |
-
|
| 1540 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1541 |
|
| 1542 |
To *value-initialize* an object of type `T` means:
|
| 1543 |
|
| 1544 |
- if `T` is a (possibly cv-qualified) class type (Clause [[class]])
|
| 1545 |
with either no default constructor ([[class.ctor]]) or a default
|
|
@@ -1551,26 +1896,22 @@ To *value-initialize* an object of type `T` means:
|
|
| 1551 |
and if `T` has a non-trivial default constructor, the object is
|
| 1552 |
default-initialized;
|
| 1553 |
- if `T` is an array type, then each element is value-initialized;
|
| 1554 |
- otherwise, the object is zero-initialized.
|
| 1555 |
|
| 1556 |
-
An object that is value-initialized is deemed to be constructed and thus
|
| 1557 |
-
subject to provisions of this International Standard applying to
|
| 1558 |
-
“constructed” objects, objects “for which the constructor has
|
| 1559 |
-
completed,” etc., even if no constructor is invoked for the object’s
|
| 1560 |
-
initialization.
|
| 1561 |
-
|
| 1562 |
A program that calls for default-initialization or value-initialization
|
| 1563 |
of an entity of reference type is ill-formed.
|
| 1564 |
|
| 1565 |
-
Every object of static storage duration is zero-initialized
|
| 1566 |
-
startup before any other initialization takes place. In some
|
| 1567 |
-
additional initialization is done later.
|
| 1568 |
|
| 1569 |
An object whose initializer is an empty set of parentheses, i.e., `()`,
|
| 1570 |
shall be value-initialized.
|
| 1571 |
|
|
|
|
|
|
|
| 1572 |
Since `()` is not permitted by the syntax for *initializer*,
|
| 1573 |
|
| 1574 |
``` cpp
|
| 1575 |
X a();
|
| 1576 |
```
|
|
@@ -1578,53 +1919,71 @@ X a();
|
|
| 1578 |
is not the declaration of an object of class `X`, but the declaration of
|
| 1579 |
a function taking no argument and returning an `X`. The form `()` is
|
| 1580 |
permitted in certain other initialization contexts ([[expr.new]],
|
| 1581 |
[[expr.type.conv]], [[class.base.init]]).
|
| 1582 |
|
|
|
|
|
|
|
| 1583 |
If no initializer is specified for an object, the object is
|
| 1584 |
default-initialized. When storage for an object with automatic or
|
| 1585 |
dynamic storage duration is obtained, the object has an *indeterminate
|
| 1586 |
value*, and if no initialization is performed for the object, that
|
| 1587 |
object retains an indeterminate value until that value is replaced (
|
| 1588 |
-
[[expr.ass]]).
|
| 1589 |
-
|
| 1590 |
-
|
| 1591 |
-
|
|
|
|
|
|
|
|
|
|
| 1592 |
|
| 1593 |
- If an indeterminate value of unsigned narrow character type (
|
| 1594 |
-
[[basic.fundamental]])
|
|
|
|
| 1595 |
- the second or third operand of a conditional expression (
|
| 1596 |
[[expr.cond]]),
|
| 1597 |
- the right operand of a comma expression ([[expr.comma]]),
|
| 1598 |
-
- the operand of a cast or conversion
|
| 1599 |
-
|
| 1600 |
-
|
|
|
|
| 1601 |
- a discarded-value expression (Clause [[expr]]),
|
| 1602 |
|
| 1603 |
then the result of the operation is an indeterminate value.
|
| 1604 |
-
- If an indeterminate value of unsigned narrow character type
|
| 1605 |
-
produced by the evaluation of the right operand of
|
| 1606 |
-
operator ([[expr.ass]]) whose first operand is an
|
| 1607 |
-
narrow character type
|
| 1608 |
-
the object referred to by
|
|
|
|
| 1609 |
- If an indeterminate value of unsigned narrow character type is
|
| 1610 |
produced by the evaluation of the initialization expression when
|
| 1611 |
initializing an object of unsigned narrow character type, that object
|
| 1612 |
is initialized to an indeterminate value.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1613 |
|
| 1614 |
``` cpp
|
| 1615 |
int f(bool b) {
|
| 1616 |
unsigned char c;
|
| 1617 |
unsigned char d = c; // OK, d has an indeterminate value
|
| 1618 |
int e = d; // undefined behavior
|
| 1619 |
return b ? d : 0; // undefined behavior if b is true
|
| 1620 |
}
|
| 1621 |
```
|
| 1622 |
|
|
|
|
|
|
|
| 1623 |
An initializer for a static member is in the scope of the member’s
|
| 1624 |
class.
|
| 1625 |
|
|
|
|
|
|
|
| 1626 |
``` cpp
|
| 1627 |
int a;
|
| 1628 |
|
| 1629 |
struct X {
|
| 1630 |
static int a;
|
|
@@ -1633,60 +1992,65 @@ struct X {
|
|
| 1633 |
|
| 1634 |
int X::a = 1;
|
| 1635 |
int X::b = a; // X::b = X::a
|
| 1636 |
```
|
| 1637 |
|
| 1638 |
-
|
| 1639 |
-
insignificant, but does matter when the initializer or the entity being
|
| 1640 |
-
initialized has a class type; see below. If the entity being initialized
|
| 1641 |
-
does not have class type, the *expression-list* in a parenthesized
|
| 1642 |
-
initializer shall be a single expression.
|
| 1643 |
|
| 1644 |
-
|
|
|
|
|
|
|
| 1645 |
|
| 1646 |
-
``
|
| 1647 |
-
|
| 1648 |
-
|
| 1649 |
-
|
| 1650 |
-
as well as in argument passing, function return, throwing an exception (
|
| 1651 |
[[except.throw]]), handling an exception ([[except.handle]]), and
|
| 1652 |
-
aggregate member initialization ([[dcl.init.aggr]]) is called
|
| 1653 |
-
*copy-initialization*.
|
| 1654 |
-
|
|
|
|
|
|
|
| 1655 |
|
| 1656 |
The initialization that occurs in the forms
|
| 1657 |
|
| 1658 |
``` cpp
|
| 1659 |
T x(a);
|
| 1660 |
T x{a};
|
| 1661 |
```
|
| 1662 |
|
| 1663 |
-
as well as in `new` expressions
|
| 1664 |
-
expressions
|
| 1665 |
-
conversions
|
| 1666 |
-
[[class.base.init]])
|
|
|
|
| 1667 |
|
| 1668 |
The semantics of initializers are as follows. The *destination type* is
|
| 1669 |
the type of the object or reference being initialized and the *source
|
| 1670 |
type* is the type of the initializer expression. If the initializer is
|
| 1671 |
not a single (possibly parenthesized) expression, the source type is not
|
| 1672 |
defined.
|
| 1673 |
|
| 1674 |
-
- If the initializer is a (non-parenthesized) *braced-init-list*
|
| 1675 |
-
object or reference is list-initialized (
|
|
|
|
| 1676 |
- If the destination type is a reference type, see [[dcl.init.ref]].
|
| 1677 |
- If the destination type is an array of characters, an array of
|
| 1678 |
`char16_t`, an array of `char32_t`, or an array of `wchar_t`, and the
|
| 1679 |
initializer is a string literal, see [[dcl.init.string]].
|
| 1680 |
- If the initializer is `()`, the object is value-initialized.
|
| 1681 |
- Otherwise, if the destination type is an array, the program is
|
| 1682 |
ill-formed.
|
| 1683 |
- If the destination type is a (possibly cv-qualified) class type:
|
| 1684 |
-
- If the
|
| 1685 |
-
|
| 1686 |
-
|
| 1687 |
-
destination
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1688 |
constructors are enumerated ([[over.match.ctor]]), and the best one
|
| 1689 |
is chosen through overload resolution ([[over.match]]). The
|
| 1690 |
constructor so selected is called to initialize the object, with the
|
| 1691 |
initializer expression or *expression-list* as its argument(s). If
|
| 1692 |
no constructor applies, or the overload resolution is ambiguous, the
|
|
@@ -1697,20 +2061,15 @@ defined.
|
|
| 1697 |
to a derived class thereof are enumerated as described in
|
| 1698 |
[[over.match.copy]], and the best one is chosen through overload
|
| 1699 |
resolution ([[over.match]]). If the conversion cannot be done or is
|
| 1700 |
ambiguous, the initialization is ill-formed. The function selected
|
| 1701 |
is called with the initializer expression as its argument; if the
|
| 1702 |
-
function is a constructor, the call
|
| 1703 |
-
cv-unqualified version of the destination type
|
| 1704 |
-
|
| 1705 |
-
|
| 1706 |
-
the
|
| 1707 |
-
copy-initialization. In certain cases, an implementation is
|
| 1708 |
-
permitted to eliminate the copying inherent in this
|
| 1709 |
-
direct-initialization by constructing the intermediate result
|
| 1710 |
-
directly into the object being initialized; see
|
| 1711 |
-
[[class.temporary]], [[class.copy]].
|
| 1712 |
- Otherwise, if the source type is a (possibly cv-qualified) class type,
|
| 1713 |
conversion functions are considered. The applicable conversion
|
| 1714 |
functions are enumerated ([[over.match.conv]]), and the best one is
|
| 1715 |
chosen through overload resolution ([[over.match]]). The user-defined
|
| 1716 |
conversion so selected is called to convert the initializer expression
|
|
@@ -1719,40 +2078,84 @@ defined.
|
|
| 1719 |
- Otherwise, the initial value of the object being initialized is the
|
| 1720 |
(possibly converted) value of the initializer expression. Standard
|
| 1721 |
conversions (Clause [[conv]]) will be used, if necessary, to convert
|
| 1722 |
the initializer expression to the cv-unqualified version of the
|
| 1723 |
destination type; no user-defined conversions are considered. If the
|
| 1724 |
-
conversion cannot be done, the initialization is ill-formed.
|
| 1725 |
-
|
| 1726 |
-
|
|
|
|
|
|
|
|
|
|
| 1727 |
``` cpp
|
| 1728 |
int a;
|
| 1729 |
const int b = a;
|
| 1730 |
int c = b;
|
| 1731 |
```
|
| 1732 |
|
|
|
|
|
|
|
| 1733 |
An *initializer-clause* followed by an ellipsis is a pack expansion (
|
| 1734 |
[[temp.variadic]]).
|
| 1735 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1736 |
### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
|
| 1737 |
|
| 1738 |
-
An *aggregate* is an array or a class (Clause [[class]]) with
|
| 1739 |
-
user-provided constructors ([[class.ctor]]), no private or protected
|
| 1740 |
-
non-static data members (Clause [[class.access]]), no base classes
|
| 1741 |
-
(Clause [[class.derived]]), and no virtual functions (
|
| 1742 |
-
[[class.virtual]]).
|
| 1743 |
|
| 1744 |
-
|
| 1745 |
-
|
| 1746 |
-
|
| 1747 |
-
|
| 1748 |
-
|
| 1749 |
-
|
| 1750 |
-
|
| 1751 |
-
|
| 1752 |
-
|
| 1753 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1754 |
|
| 1755 |
``` cpp
|
| 1756 |
struct A {
|
| 1757 |
int x;
|
| 1758 |
struct B {
|
|
@@ -1762,29 +2165,79 @@ struct A {
|
|
| 1762 |
} a = { 1, { 2, 3 } };
|
| 1763 |
```
|
| 1764 |
|
| 1765 |
initializes `a.x` with 1, `a.b.i` with 2, `a.b.j` with 3.
|
| 1766 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1767 |
An aggregate that is a class can also be initialized with a single
|
| 1768 |
expression not enclosed in braces, as described in [[dcl.init]].
|
| 1769 |
|
| 1770 |
-
An array of unknown
|
| 1771 |
*initializer-list* containing `n` *initializer-clause*s, where `n` shall
|
| 1772 |
-
be greater than zero, is defined as having `n` elements
|
| 1773 |
[[dcl.array]]).
|
| 1774 |
|
|
|
|
|
|
|
| 1775 |
``` cpp
|
| 1776 |
int x[] = { 1, 3, 5 };
|
| 1777 |
```
|
| 1778 |
|
| 1779 |
declares and initializes `x` as a one-dimensional array that has three
|
| 1780 |
elements since no size was specified and there are three initializers.
|
|
|
|
|
|
|
|
|
|
| 1781 |
An empty initializer list `{}` shall not be used as the
|
| 1782 |
-
*initializer-clause
|
| 1783 |
|
| 1784 |
-
|
| 1785 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1786 |
|
| 1787 |
``` cpp
|
| 1788 |
struct A {
|
| 1789 |
int i;
|
| 1790 |
static int s;
|
|
@@ -1794,27 +2247,47 @@ struct A {
|
|
| 1794 |
} a = { 1, 2, 3 };
|
| 1795 |
```
|
| 1796 |
|
| 1797 |
Here, the second initializer 2 initializes `a.j` and not the static data
|
| 1798 |
member `A::s`, and the third initializer 3 initializes `a.k` and not the
|
| 1799 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1800 |
|
| 1801 |
An *initializer-list* is ill-formed if the number of
|
| 1802 |
-
*initializer-clause*s exceeds the number of
|
| 1803 |
-
|
|
|
|
| 1804 |
|
| 1805 |
``` cpp
|
| 1806 |
char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
|
| 1807 |
```
|
| 1808 |
|
| 1809 |
is ill-formed.
|
| 1810 |
|
|
|
|
|
|
|
| 1811 |
If there are fewer *initializer-clause*s in the list than there are
|
| 1812 |
-
|
| 1813 |
-
|
| 1814 |
-
|
| 1815 |
-
[[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1816 |
|
| 1817 |
``` cpp
|
| 1818 |
struct S { int a; const char* b; int c; int d = b[a]; };
|
| 1819 |
S ss = { 1, "asdf" };
|
| 1820 |
```
|
|
@@ -1829,17 +2302,39 @@ X a[] = { 1, 2, 3, 4, 5, 6 };
|
|
| 1829 |
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
|
| 1830 |
```
|
| 1831 |
|
| 1832 |
`a` and `b` have the same value
|
| 1833 |
|
| 1834 |
-
|
| 1835 |
-
|
| 1836 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1837 |
*initializer-list* for an object of type `C` unless the
|
| 1838 |
-
*initializer-clause*s for all
|
| 1839 |
omitted.
|
| 1840 |
|
|
|
|
|
|
|
| 1841 |
``` cpp
|
| 1842 |
struct S { } s;
|
| 1843 |
struct A {
|
| 1844 |
S s1;
|
| 1845 |
int i1;
|
|
@@ -1853,17 +2348,18 @@ struct A {
|
|
| 1853 |
s, // Required initialization
|
| 1854 |
0
|
| 1855 |
}; // Initialization not required for A::s3 because A::i3 is also not initialized
|
| 1856 |
```
|
| 1857 |
|
| 1858 |
-
|
| 1859 |
-
reference type uninitialized, the program is ill-formed.
|
| 1860 |
|
| 1861 |
When initializing a multi-dimensional array, the *initializer-clause*s
|
| 1862 |
initialize the elements with the last (rightmost) index of the array
|
| 1863 |
varying the fastest ([[dcl.array]]).
|
| 1864 |
|
|
|
|
|
|
|
| 1865 |
``` cpp
|
| 1866 |
int x[2][2] = { 3, 1, 4, 2 };
|
| 1867 |
```
|
| 1868 |
|
| 1869 |
initializes `x[0][0]` to `3`, `x[0][1]` to `1`, `x[1][0]` to `4`, and
|
|
@@ -1876,20 +2372,24 @@ float y[4][3] = {
|
|
| 1876 |
```
|
| 1877 |
|
| 1878 |
initializes the first column of `y` (regarded as a two-dimensional
|
| 1879 |
array) and leaves the rest zero.
|
| 1880 |
|
|
|
|
|
|
|
| 1881 |
Braces can be elided in an *initializer-list* as follows. If the
|
| 1882 |
*initializer-list* begins with a left brace, then the succeeding
|
| 1883 |
-
comma-separated list of *initializer-clause*s initializes the
|
| 1884 |
-
a subaggregate; it is erroneous for there to be more
|
| 1885 |
-
*initializer-clause*s than
|
| 1886 |
for a subaggregate does not begin with a left brace, then only enough
|
| 1887 |
-
*initializer-clause*s from the list are taken to initialize the
|
| 1888 |
of the subaggregate; any remaining *initializer-clause*s are left to
|
| 1889 |
-
initialize the next
|
| 1890 |
-
subaggregate is
|
|
|
|
|
|
|
| 1891 |
|
| 1892 |
``` cpp
|
| 1893 |
float y[4][3] = {
|
| 1894 |
{ 1, 3, 5 },
|
| 1895 |
{ 2, 4, 6 },
|
|
@@ -1915,19 +2415,24 @@ float y[4][3] = {
|
|
| 1915 |
|
| 1916 |
The initializer for `y` begins with a left brace, but the one for `y[0]`
|
| 1917 |
does not, therefore three elements from the list are used. Likewise the
|
| 1918 |
next three are taken successively for `y[1]` and `y[2]`.
|
| 1919 |
|
|
|
|
|
|
|
| 1920 |
All implicit type conversions (Clause [[conv]]) are considered when
|
| 1921 |
-
initializing the
|
| 1922 |
-
|
| 1923 |
-
initialized. Otherwise, if the
|
| 1924 |
elision is assumed and the *assignment-expression* is considered for the
|
| 1925 |
-
initialization of the first
|
| 1926 |
-
|
| 1927 |
-
|
| 1928 |
-
|
|
|
|
|
|
|
|
|
|
| 1929 |
|
| 1930 |
``` cpp
|
| 1931 |
struct A {
|
| 1932 |
int i;
|
| 1933 |
operator int();
|
|
@@ -1942,34 +2447,41 @@ B b = { 4, a, a };
|
|
| 1942 |
|
| 1943 |
Braces are elided around the *initializer-clause* for `b.a1.i`. `b.a1.i`
|
| 1944 |
is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
|
| 1945 |
initialized with whatever `a.operator int()` returns.
|
| 1946 |
|
| 1947 |
-
|
| 1948 |
-
type with a user-provided constructor ([[class.ctor]]). Initialization
|
| 1949 |
-
of these aggregate objects is described in [[class.expl.init]].
|
| 1950 |
|
| 1951 |
-
|
| 1952 |
-
|
| 1953 |
-
[[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1954 |
|
| 1955 |
When a union is initialized with a brace-enclosed initializer, the
|
| 1956 |
braces shall only contain an *initializer-clause* for the first
|
| 1957 |
non-static data member of the union.
|
| 1958 |
|
|
|
|
|
|
|
| 1959 |
``` cpp
|
| 1960 |
union u { int a; const char* b; };
|
| 1961 |
u a = { 1 };
|
| 1962 |
u b = a;
|
| 1963 |
u c = 1; // error
|
| 1964 |
u d = { 0, "asdf" }; // error
|
| 1965 |
u e = { "asdf" }; // error
|
| 1966 |
```
|
| 1967 |
|
| 1968 |
-
|
| 1969 |
-
|
| 1970 |
-
|
|
|
|
|
|
|
| 1971 |
|
| 1972 |
### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
|
| 1973 |
|
| 1974 |
An array of narrow character type ([[basic.fundamental]]), `char16_t`
|
| 1975 |
array, `char32_t` array, or `wchar_t` array can be initialized by a
|
|
@@ -1977,38 +2489,47 @@ narrow string literal, `char16_t` string literal, `char32_t` string
|
|
| 1977 |
literal, or wide string literal, respectively, or by an
|
| 1978 |
appropriately-typed string literal enclosed in braces ([[lex.string]]).
|
| 1979 |
Successive characters of the value of the string literal initialize the
|
| 1980 |
elements of the array.
|
| 1981 |
|
|
|
|
|
|
|
| 1982 |
``` cpp
|
| 1983 |
char msg[] = "Syntax error on line %s\n";
|
| 1984 |
```
|
| 1985 |
|
| 1986 |
shows a character array whose members are initialized with a
|
| 1987 |
*string-literal*. Note that because `'\n'` is a single character and
|
| 1988 |
because a trailing `'\0'` is appended, `sizeof(msg)` is `25`.
|
| 1989 |
|
|
|
|
|
|
|
| 1990 |
There shall not be more initializers than there are array elements.
|
| 1991 |
|
|
|
|
|
|
|
| 1992 |
``` cpp
|
| 1993 |
char cv[4] = "asdf"; // error
|
| 1994 |
```
|
| 1995 |
|
| 1996 |
is ill-formed since there is no space for the implied trailing `'\0'`.
|
| 1997 |
|
|
|
|
|
|
|
| 1998 |
If there are fewer initializers than there are array elements, each
|
| 1999 |
element not explicitly initialized shall be zero-initialized (
|
| 2000 |
[[dcl.init]]).
|
| 2001 |
|
| 2002 |
### References <a id="dcl.init.ref">[[dcl.init.ref]]</a>
|
| 2003 |
|
| 2004 |
-
A variable declared
|
| 2005 |
-
|
| 2006 |
-
|
|
|
|
| 2007 |
|
| 2008 |
``` cpp
|
| 2009 |
-
int g(int);
|
| 2010 |
void f() {
|
| 2011 |
int i;
|
| 2012 |
int& r = i; // r refers to i
|
| 2013 |
r = 1; // the value of i becomes 1
|
| 2014 |
int* p = &r; // p points to i
|
|
@@ -2019,56 +2540,75 @@ void f() {
|
|
| 2019 |
int (&ra)[3] = a; // ra refers to the array a
|
| 2020 |
ra[1] = i; // modifies a[1]
|
| 2021 |
}
|
| 2022 |
```
|
| 2023 |
|
|
|
|
|
|
|
| 2024 |
A reference cannot be changed to refer to another object after
|
| 2025 |
-
initialization.
|
| 2026 |
-
|
| 2027 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2028 |
|
| 2029 |
The initializer can be omitted for a reference only in a parameter
|
| 2030 |
declaration ([[dcl.fct]]), in the declaration of a function return
|
| 2031 |
type, in the declaration of a class member within its class definition (
|
| 2032 |
[[class.mem]]), and where the `extern` specifier is explicitly used.
|
| 2033 |
|
|
|
|
|
|
|
| 2034 |
``` cpp
|
| 2035 |
int& r1; // error: initializer missing
|
| 2036 |
extern int& r2; // OK
|
| 2037 |
```
|
| 2038 |
|
| 2039 |
-
|
| 2040 |
-
|
| 2041 |
-
|
| 2042 |
-
|
| 2043 |
-
|
| 2044 |
-
|
| 2045 |
-
|
| 2046 |
-
|
| 2047 |
-
|
| 2048 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2049 |
|
| 2050 |
A reference to type “*cv1* `T1`” is initialized by an expression of type
|
| 2051 |
“*cv2* `T2`” as follows:
|
| 2052 |
|
| 2053 |
- If the reference is an lvalue reference and the initializer expression
|
| 2054 |
-
- is an lvalue (but is not a bit-field), and “ `T1`” is
|
| 2055 |
-
reference-compatible with “ `T2`
|
| 2056 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 2057 |
reference-related to `T2`, and can be converted to an lvalue of type
|
| 2058 |
-
“ `T3`
|
| 2059 |
-
(this conversion is selected by enumerating the
|
| 2060 |
-
conversion functions ([[over.match.ref]]) and choosing
|
| 2061 |
-
through overload resolution ([[over.match]])),
|
| 2062 |
|
| 2063 |
then the reference is bound to the initializer expression lvalue in
|
| 2064 |
the first case and to the lvalue result of the conversion in the
|
| 2065 |
second case (or, in either case, to the appropriate base class
|
| 2066 |
-
subobject of the object).
|
|
|
|
| 2067 |
array-to-pointer ([[conv.array]]), and function-to-pointer (
|
| 2068 |
[[conv.func]]) standard conversions are not needed, and therefore are
|
| 2069 |
-
suppressed, when such direct bindings to lvalues are
|
|
|
|
|
|
|
| 2070 |
``` cpp
|
| 2071 |
double d = 2.0;
|
| 2072 |
double& rd = d; // rd refers to d
|
| 2073 |
const double& rcd = d; // rcd refers to d
|
| 2074 |
|
|
@@ -2076,36 +2616,39 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
|
|
| 2076 |
struct B : A { operator int&(); } b;
|
| 2077 |
A& ra = b; // ra refers to A subobject in b
|
| 2078 |
const A& rca = b; // rca refers to A subobject in b
|
| 2079 |
int& ir = B(); // ir refers to the result of B::operator int&
|
| 2080 |
```
|
|
|
|
|
|
|
| 2081 |
- Otherwise, the reference shall be an lvalue reference to a
|
| 2082 |
non-volatile const type (i.e., *cv1* shall be `const`), or the
|
| 2083 |
reference shall be an rvalue reference.
|
|
|
|
| 2084 |
``` cpp
|
| 2085 |
double& rd2 = 2.0; // error: not an lvalue and reference not const
|
| 2086 |
int i = 2;
|
| 2087 |
double& rd3 = i; // error: type mismatch and reference not const
|
| 2088 |
```
|
| 2089 |
|
|
|
|
| 2090 |
- If the initializer expression
|
| 2091 |
-
- is an
|
| 2092 |
-
|
| 2093 |
-
“*cv2* `T2`”, or
|
| 2094 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 2095 |
-
reference-related to `T2`, and can be converted to an
|
| 2096 |
-
|
| 2097 |
-
|
| 2098 |
-
[[over.match.ref]]),
|
| 2099 |
|
| 2100 |
-
then the
|
| 2101 |
-
|
| 2102 |
-
|
| 2103 |
-
|
| 2104 |
-
|
| 2105 |
-
|
| 2106 |
-
|
|
|
|
| 2107 |
``` cpp
|
| 2108 |
struct A { };
|
| 2109 |
struct B : A { } b;
|
| 2110 |
extern B f();
|
| 2111 |
const A& rca2 = f(); // bound to the A subobject of the B rvalue.
|
|
@@ -2116,59 +2659,70 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
|
|
| 2116 |
} x;
|
| 2117 |
const A& r = x; // bound to the A subobject of the result of the conversion
|
| 2118 |
int i2 = 42;
|
| 2119 |
int&& rri = static_cast<int&&>(i2); // bound directly to i2
|
| 2120 |
B&& rrb = x; // bound directly to the result of operator B
|
| 2121 |
-
int&& rri2 = X(); // error: lvalue-to-rvalue conversion applied to the
|
| 2122 |
-
// result of operator int&
|
| 2123 |
```
|
|
|
|
|
|
|
| 2124 |
- Otherwise:
|
| 2125 |
-
- If `T1` is a class type
|
| 2126 |
-
|
| 2127 |
-
`T1`” by
|
| 2128 |
-
[[over.match.copy]]
|
|
|
|
| 2129 |
corresponding non-reference copy-initialization would be
|
| 2130 |
ill-formed. The result of the call to the conversion function, as
|
| 2131 |
described for the non-reference copy-initialization, is then used
|
| 2132 |
-
to direct-initialize the reference.
|
| 2133 |
-
|
| 2134 |
-
|
| 2135 |
-
-
|
| 2136 |
-
|
| 2137 |
-
|
| 2138 |
|
| 2139 |
If `T1` is reference-related to `T2`:
|
| 2140 |
- *cv1* shall be the same cv-qualification as, or greater
|
| 2141 |
cv-qualification than, *cv2*; and
|
| 2142 |
- if the reference is an rvalue reference, the initializer
|
| 2143 |
expression shall not be an lvalue.
|
| 2144 |
|
|
|
|
| 2145 |
``` cpp
|
| 2146 |
struct Banana { };
|
| 2147 |
struct Enigma { operator const Banana(); };
|
|
|
|
| 2148 |
void enigmatic() {
|
| 2149 |
typedef const Banana ConstBanana;
|
| 2150 |
Banana &&banana1 = ConstBanana(); // ill-formed
|
| 2151 |
Banana &&banana2 = Enigma(); // ill-formed
|
|
|
|
| 2152 |
}
|
| 2153 |
|
| 2154 |
const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
|
| 2155 |
double&& rrd = 2; // rrd refers to temporary with value 2.0
|
| 2156 |
const volatile int cvi = 1;
|
| 2157 |
-
const int& r2 = cvi; // error:
|
|
|
|
|
|
|
|
|
|
| 2158 |
double d2 = 1.0;
|
| 2159 |
-
double&& rrd2 = d2; // error:
|
|
|
|
|
|
|
| 2160 |
int i3 = 2;
|
| 2161 |
double&& rrd3 = i3; // rrd3 refers to temporary with value 2.0
|
| 2162 |
```
|
| 2163 |
|
| 2164 |
-
|
| 2165 |
-
temporary from the initializer expression), the reference is said to
|
| 2166 |
-
*bind directly* to the initializer expression.
|
| 2167 |
|
| 2168 |
-
|
| 2169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2170 |
|
| 2171 |
### List-initialization <a id="dcl.init.list">[[dcl.init.list]]</a>
|
| 2172 |
|
| 2173 |
*List-initialization* is initialization of an object or reference from a
|
| 2174 |
*braced-init-list*. Such an initializer is called an *initializer list*,
|
|
@@ -2176,24 +2730,30 @@ and the comma-separated *initializer-clause*s of the list are called the
|
|
| 2176 |
*elements* of the initializer list. An initializer list may be empty.
|
| 2177 |
List-initialization can occur in direct-initialization or
|
| 2178 |
copy-initialization contexts; list-initialization in a
|
| 2179 |
direct-initialization context is called *direct-list-initialization* and
|
| 2180 |
list-initialization in a copy-initialization context is called
|
| 2181 |
-
*copy-list-initialization*.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2182 |
|
| 2183 |
- as the initializer in a variable definition ([[dcl.init]])
|
| 2184 |
-
- as the initializer in a new
|
| 2185 |
- in a return statement ([[stmt.return]])
|
| 2186 |
- as a *for-range-initializer* ([[stmt.iter]])
|
| 2187 |
- as a function argument ([[expr.call]])
|
| 2188 |
- as a subscript ([[expr.sub]])
|
| 2189 |
- as an argument to a constructor invocation ([[dcl.init]],
|
| 2190 |
[[expr.type.conv]])
|
| 2191 |
- as an initializer for a non-static data member ([[class.mem]])
|
| 2192 |
- in a *mem-initializer* ([[class.base.init]])
|
| 2193 |
- on the right-hand side of an assignment ([[expr.ass]])
|
| 2194 |
|
|
|
|
|
|
|
| 2195 |
``` cpp
|
| 2196 |
int a = {1};
|
| 2197 |
std::complex<double> z{1,2};
|
| 2198 |
new std::vector<std::string>{"once", "upon", "a", "time"}; // 4 string elements
|
| 2199 |
f( {"Nicholas","Annemarie"} ); // pass list of two elements
|
|
@@ -2201,30 +2761,48 @@ return { "Norah" }; // return list of one element
|
|
| 2201 |
int* e {}; // initialization to zero / null pointer
|
| 2202 |
x = double{1}; // explicitly construct a double
|
| 2203 |
std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
|
| 2204 |
```
|
| 2205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2206 |
A constructor is an *initializer-list constructor* if its first
|
| 2207 |
parameter is of type `std::initializer_list<E>` or reference to possibly
|
| 2208 |
cv-qualified `std::initializer_list<E>` for some type `E`, and either
|
| 2209 |
there are no other parameters or else all other parameters have default
|
| 2210 |
-
arguments ([[dcl.fct.default]]).
|
| 2211 |
-
|
| 2212 |
-
[
|
| 2213 |
-
|
| 2214 |
-
|
| 2215 |
-
|
| 2216 |
-
|
| 2217 |
-
|
| 2218 |
-
|
| 2219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2220 |
|
| 2221 |
List-initialization of an object or reference of type `T` is defined as
|
| 2222 |
follows:
|
| 2223 |
|
| 2224 |
-
- If `T` is an aggregate
|
| 2225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2226 |
``` cpp
|
| 2227 |
double ad[] = { 1, 2.0 }; // OK
|
| 2228 |
int ai[] = { 1, 2.0 }; // error: narrowing
|
| 2229 |
|
| 2230 |
struct S2 {
|
|
@@ -2233,22 +2811,22 @@ follows:
|
|
| 2233 |
};
|
| 2234 |
S2 s21 = { 1, 2, 3.0 }; // OK
|
| 2235 |
S2 s22 { 1.0, 2, 3 }; // error: narrowing
|
| 2236 |
S2 s23 { }; // OK: default to 0,0,0
|
| 2237 |
```
|
|
|
|
|
|
|
| 2238 |
- Otherwise, if the initializer list has no elements and `T` is a class
|
| 2239 |
type with a default constructor, the object is value-initialized.
|
| 2240 |
-
- Otherwise, if `T` is a specialization of `std::initializer_list<E>`,
|
| 2241 |
-
|
| 2242 |
-
and used to initialize the object according to the rules for
|
| 2243 |
-
initialization of an object from a class of the same type (
|
| 2244 |
-
[[dcl.init]]).
|
| 2245 |
- Otherwise, if `T` is a class type, constructors are considered. The
|
| 2246 |
applicable constructors are enumerated and the best one is chosen
|
| 2247 |
through overload resolution ([[over.match]], [[over.match.list]]).
|
| 2248 |
If a narrowing conversion (see below) is required to convert any of
|
| 2249 |
the arguments, the program is ill-formed.
|
|
|
|
| 2250 |
``` cpp
|
| 2251 |
struct S {
|
| 2252 |
S(std::initializer_list<double>); // #1
|
| 2253 |
S(std::initializer_list<int>); // #2
|
| 2254 |
S(); // #3
|
|
@@ -2257,17 +2835,21 @@ follows:
|
|
| 2257 |
S s1 = { 1.0, 2.0, 3.0 }; // invoke #1
|
| 2258 |
S s2 = { 1, 2, 3 }; // invoke #2
|
| 2259 |
S s3 = { }; // invoke #3
|
| 2260 |
```
|
| 2261 |
|
|
|
|
|
|
|
| 2262 |
``` cpp
|
| 2263 |
struct Map {
|
| 2264 |
Map(std::initializer_list<std::pair<std::string,int>>);
|
| 2265 |
};
|
| 2266 |
Map ship = {{"Sophie",14}, {"Surprise",28}};
|
| 2267 |
```
|
| 2268 |
|
|
|
|
|
|
|
| 2269 |
``` cpp
|
| 2270 |
struct S {
|
| 2271 |
// no initializer-list constructors
|
| 2272 |
S(int, double, double); // #1
|
| 2273 |
S(); // #2
|
|
@@ -2275,25 +2857,61 @@ follows:
|
|
| 2275 |
};
|
| 2276 |
S s1 = { 1, 2, 3.0 }; // OK: invoke #1
|
| 2277 |
S s2 { 1.0, 2, 3 }; // error: narrowing
|
| 2278 |
S s3 { }; // OK: invoke #2
|
| 2279 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2280 |
- Otherwise, if the initializer list has a single element of type `E`
|
| 2281 |
and either `T` is not a reference type or its referenced type is
|
| 2282 |
reference-related to `E`, the object or reference is initialized from
|
| 2283 |
-
that element
|
| 2284 |
-
|
|
|
|
|
|
|
|
|
|
| 2285 |
``` cpp
|
| 2286 |
int x1 {2}; // OK
|
| 2287 |
int x2 {2.0}; // error: narrowing
|
| 2288 |
```
|
| 2289 |
-
|
| 2290 |
-
|
| 2291 |
-
|
| 2292 |
-
|
| 2293 |
-
|
| 2294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2295 |
``` cpp
|
| 2296 |
struct S {
|
| 2297 |
S(std::initializer_list<double>); // #1
|
| 2298 |
S(const std::string&); // #2
|
| 2299 |
// ...
|
|
@@ -2303,16 +2921,22 @@ follows:
|
|
| 2303 |
S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
|
| 2304 |
const int& i1 = { 1 }; // OK
|
| 2305 |
const int& i2 = { 1.1 }; // error: narrowing
|
| 2306 |
const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
|
| 2307 |
```
|
|
|
|
|
|
|
| 2308 |
- Otherwise, if the initializer list has no elements, the object is
|
| 2309 |
value-initialized.
|
|
|
|
| 2310 |
``` cpp
|
| 2311 |
int** pp {}; // initialized to null pointer
|
| 2312 |
```
|
|
|
|
|
|
|
| 2313 |
- Otherwise, the program is ill-formed.
|
|
|
|
| 2314 |
``` cpp
|
| 2315 |
struct A { int i; int j; };
|
| 2316 |
A a1 { 1, 2 }; // aggregate initialization
|
| 2317 |
A a2 { 1.2 }; // error: narrowing
|
| 2318 |
struct B {
|
|
@@ -2328,32 +2952,42 @@ follows:
|
|
| 2328 |
|
| 2329 |
int j { 1 }; // initialize to 1
|
| 2330 |
int k { }; // initialize to 0
|
| 2331 |
```
|
| 2332 |
|
|
|
|
|
|
|
| 2333 |
Within the *initializer-list* of a *braced-init-list*, the
|
| 2334 |
*initializer-clause*s, including any that result from pack expansions (
|
| 2335 |
[[temp.variadic]]), are evaluated in the order in which they appear.
|
| 2336 |
That is, every value computation and side effect associated with a given
|
| 2337 |
*initializer-clause* is sequenced before every value computation and
|
| 2338 |
side effect associated with any *initializer-clause* that follows it in
|
| 2339 |
-
the comma-separated list of the *initializer-list*.
|
| 2340 |
-
|
| 2341 |
-
|
| 2342 |
-
|
| 2343 |
-
|
|
|
|
|
|
|
| 2344 |
|
| 2345 |
An object of type `std::initializer_list<E>` is constructed from an
|
| 2346 |
-
initializer list as if the implementation
|
| 2347 |
-
|
| 2348 |
-
initializer list. Each element of that array
|
| 2349 |
-
the corresponding element of the initializer
|
| 2350 |
-
`std::initializer_list<E>` object is constructed to refer
|
| 2351 |
-
|
| 2352 |
-
|
| 2353 |
-
|
| 2354 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2355 |
|
| 2356 |
``` cpp
|
| 2357 |
struct X {
|
| 2358 |
X(std::initializer_list<double> v);
|
| 2359 |
};
|
|
@@ -2369,15 +3003,19 @@ X x(std::initializer_list<double>(__a, __a+3));
|
|
| 2369 |
```
|
| 2370 |
|
| 2371 |
assuming that the implementation can construct an `initializer_list`
|
| 2372 |
object with a pair of pointers.
|
| 2373 |
|
|
|
|
|
|
|
| 2374 |
The array has the same lifetime as any other temporary object (
|
| 2375 |
[[class.temporary]]), except that initializing an `initializer_list`
|
| 2376 |
object from the array extends the lifetime of the array exactly like
|
| 2377 |
binding a reference to a temporary.
|
| 2378 |
|
|
|
|
|
|
|
| 2379 |
``` cpp
|
| 2380 |
typedef std::complex<double> cmplx;
|
| 2381 |
std::vector<cmplx> v1 = { 1, 2, 3 };
|
| 2382 |
|
| 2383 |
void f() {
|
|
@@ -2385,24 +3023,27 @@ void f() {
|
|
| 2385 |
std::initializer_list<int> i3 = { 1, 2, 3 };
|
| 2386 |
}
|
| 2387 |
|
| 2388 |
struct A {
|
| 2389 |
std::initializer_list<int> i4;
|
| 2390 |
-
A() : i4{ 1, 2, 3 } {} //
|
| 2391 |
};
|
| 2392 |
```
|
| 2393 |
|
| 2394 |
For `v1` and `v2`, the `initializer_list` object is a parameter in a
|
| 2395 |
function call, so the array created for `{ 1, 2, 3 }` has
|
| 2396 |
full-expression lifetime. For `i3`, the `initializer_list` object is a
|
| 2397 |
variable, so the array persists for the lifetime of the variable. For
|
| 2398 |
-
`i4`, the `initializer_list` object is initialized in
|
| 2399 |
-
*ctor-initializer*
|
| 2400 |
-
|
| 2401 |
-
|
| 2402 |
-
|
| 2403 |
-
|
|
|
|
|
|
|
|
|
|
| 2404 |
|
| 2405 |
A *narrowing conversion* is an implicit conversion
|
| 2406 |
|
| 2407 |
- from a floating-point type to an integer type, or
|
| 2408 |
- from `long double` to `double` or `float`, or from `double` to
|
|
@@ -2416,12 +3057,14 @@ A *narrowing conversion* is an implicit conversion
|
|
| 2416 |
- from an integer type or unscoped enumeration type to an integer type
|
| 2417 |
that cannot represent all the values of the original type, except
|
| 2418 |
where the source is a constant expression whose value after integral
|
| 2419 |
promotions will fit into the target type.
|
| 2420 |
|
| 2421 |
-
As indicated above, such conversions are not allowed at the
|
| 2422 |
-
list-initializations.
|
|
|
|
|
|
|
| 2423 |
|
| 2424 |
``` cpp
|
| 2425 |
int x = 999; // x is not a constant expression
|
| 2426 |
const int y = 999;
|
| 2427 |
const int z = 99;
|
|
@@ -2440,34 +3083,41 @@ float f2 { 7 }; // OK: 7 can be exactly represented as a float
|
|
| 2440 |
int f(int);
|
| 2441 |
int a[] =
|
| 2442 |
{ 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level
|
| 2443 |
```
|
| 2444 |
|
|
|
|
|
|
|
| 2445 |
<!-- Link reference definitions -->
|
|
|
|
| 2446 |
[basic.compound]: basic.md#basic.compound
|
| 2447 |
[basic.def]: basic.md#basic.def
|
| 2448 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 2449 |
[basic.fundamental]: basic.md#basic.fundamental
|
| 2450 |
[basic.life]: basic.md#basic.life
|
| 2451 |
[basic.link]: basic.md#basic.link
|
| 2452 |
[basic.lookup]: basic.md#basic.lookup
|
| 2453 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
|
|
|
| 2454 |
[basic.lookup.elab]: basic.md#basic.lookup.elab
|
| 2455 |
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
| 2456 |
[basic.lookup.udir]: basic.md#basic.lookup.udir
|
| 2457 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 2458 |
[basic.lval]: basic.md#basic.lval
|
| 2459 |
[basic.namespace]: #basic.namespace
|
| 2460 |
[basic.scope]: basic.md#basic.scope
|
| 2461 |
[basic.scope.block]: basic.md#basic.scope.block
|
|
|
|
| 2462 |
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
| 2463 |
[basic.scope.pdecl]: basic.md#basic.scope.pdecl
|
| 2464 |
[basic.scope.proto]: basic.md#basic.scope.proto
|
| 2465 |
[basic.start]: basic.md#basic.start
|
| 2466 |
-
[basic.start.
|
|
|
|
| 2467 |
[basic.stc]: basic.md#basic.stc
|
| 2468 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
|
|
|
| 2469 |
[basic.stc.static]: basic.md#basic.stc.static
|
| 2470 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 2471 |
[basic.type.qualifier]: basic.md#basic.type.qualifier
|
| 2472 |
[basic.types]: basic.md#basic.types
|
| 2473 |
[class]: class.md#class
|
|
@@ -2477,43 +3127,49 @@ int a[] =
|
|
| 2477 |
[class.conv]: special.md#class.conv
|
| 2478 |
[class.conv.ctor]: special.md#class.conv.ctor
|
| 2479 |
[class.conv.fct]: special.md#class.conv.fct
|
| 2480 |
[class.copy]: special.md#class.copy
|
| 2481 |
[class.ctor]: special.md#class.ctor
|
| 2482 |
-
[class.derived]: class.md#class.derived
|
| 2483 |
[class.dtor]: special.md#class.dtor
|
| 2484 |
[class.expl.init]: special.md#class.expl.init
|
| 2485 |
[class.friend]: class.md#class.friend
|
| 2486 |
-
[class.inhctor]: special.md#class.inhctor
|
| 2487 |
[class.init]: special.md#class.init
|
| 2488 |
[class.mem]: class.md#class.mem
|
| 2489 |
[class.member.lookup]: class.md#class.member.lookup
|
| 2490 |
[class.mfct]: class.md#class.mfct
|
|
|
|
| 2491 |
[class.name]: class.md#class.name
|
| 2492 |
[class.qual]: basic.md#class.qual
|
| 2493 |
[class.static]: class.md#class.static
|
| 2494 |
[class.static.data]: class.md#class.static.data
|
| 2495 |
[class.temporary]: special.md#class.temporary
|
| 2496 |
-
[class.this]: class.md#class.this
|
| 2497 |
[class.union]: class.md#class.union
|
|
|
|
| 2498 |
[class.virtual]: class.md#class.virtual
|
| 2499 |
[conv]: conv.md#conv
|
| 2500 |
[conv.array]: conv.md#conv.array
|
| 2501 |
[conv.func]: conv.md#conv.func
|
| 2502 |
[conv.integral]: conv.md#conv.integral
|
| 2503 |
[conv.lval]: conv.md#conv.lval
|
| 2504 |
[conv.prom]: conv.md#conv.prom
|
| 2505 |
[conv.ptr]: conv.md#conv.ptr
|
|
|
|
|
|
|
|
|
|
| 2506 |
[dcl.align]: #dcl.align
|
| 2507 |
[dcl.ambig.res]: #dcl.ambig.res
|
| 2508 |
[dcl.array]: #dcl.array
|
| 2509 |
[dcl.asm]: #dcl.asm
|
| 2510 |
[dcl.attr]: #dcl.attr
|
| 2511 |
[dcl.attr.depend]: #dcl.attr.depend
|
| 2512 |
[dcl.attr.deprecated]: #dcl.attr.deprecated
|
|
|
|
| 2513 |
[dcl.attr.grammar]: #dcl.attr.grammar
|
|
|
|
| 2514 |
[dcl.attr.noreturn]: #dcl.attr.noreturn
|
|
|
|
| 2515 |
[dcl.constexpr]: #dcl.constexpr
|
| 2516 |
[dcl.dcl]: #dcl.dcl
|
| 2517 |
[dcl.decl]: #dcl.decl
|
| 2518 |
[dcl.enum]: #dcl.enum
|
| 2519 |
[dcl.fct]: #dcl.fct
|
|
@@ -2527,25 +3183,28 @@ int a[] =
|
|
| 2527 |
[dcl.init]: #dcl.init
|
| 2528 |
[dcl.init.aggr]: #dcl.init.aggr
|
| 2529 |
[dcl.init.list]: #dcl.init.list
|
| 2530 |
[dcl.init.ref]: #dcl.init.ref
|
| 2531 |
[dcl.init.string]: #dcl.init.string
|
|
|
|
| 2532 |
[dcl.link]: #dcl.link
|
| 2533 |
[dcl.meaning]: #dcl.meaning
|
| 2534 |
[dcl.mptr]: #dcl.mptr
|
| 2535 |
[dcl.name]: #dcl.name
|
| 2536 |
[dcl.ptr]: #dcl.ptr
|
| 2537 |
[dcl.ref]: #dcl.ref
|
| 2538 |
[dcl.spec]: #dcl.spec
|
| 2539 |
[dcl.spec.auto]: #dcl.spec.auto
|
| 2540 |
[dcl.stc]: #dcl.stc
|
|
|
|
| 2541 |
[dcl.type]: #dcl.type
|
|
|
|
|
|
|
| 2542 |
[dcl.type.cv]: #dcl.type.cv
|
| 2543 |
[dcl.type.elab]: #dcl.type.elab
|
| 2544 |
[dcl.type.simple]: #dcl.type.simple
|
| 2545 |
[dcl.typedef]: #dcl.typedef
|
| 2546 |
-
[depr.register]: future.md#depr.register
|
| 2547 |
[except.handle]: except.md#except.handle
|
| 2548 |
[except.spec]: except.md#except.spec
|
| 2549 |
[except.throw]: except.md#except.throw
|
| 2550 |
[expr]: expr.md#expr
|
| 2551 |
[expr.alignof]: expr.md#expr.alignof
|
|
@@ -2556,18 +3215,18 @@ int a[] =
|
|
| 2556 |
[expr.cond]: expr.md#expr.cond
|
| 2557 |
[expr.const]: expr.md#expr.const
|
| 2558 |
[expr.const.cast]: expr.md#expr.const.cast
|
| 2559 |
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 2560 |
[expr.new]: expr.md#expr.new
|
| 2561 |
-
[expr.prim.lambda]: expr.md#expr.prim.lambda
|
|
|
|
| 2562 |
[expr.ref]: expr.md#expr.ref
|
| 2563 |
[expr.static.cast]: expr.md#expr.static.cast
|
| 2564 |
[expr.sub]: expr.md#expr.sub
|
| 2565 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 2566 |
[expr.unary]: expr.md#expr.unary
|
| 2567 |
[expr.unary.op]: expr.md#expr.unary.op
|
| 2568 |
-
[global.names]: library.md#global.names
|
| 2569 |
[intro.compliance]: intro.md#intro.compliance
|
| 2570 |
[intro.execution]: intro.md#intro.execution
|
| 2571 |
[intro.multithread]: intro.md#intro.multithread
|
| 2572 |
[lex.charset]: lex.md#lex.charset
|
| 2573 |
[lex.digraph]: lex.md#lex.digraph
|
|
@@ -2581,30 +3240,34 @@ int a[] =
|
|
| 2581 |
[namespace.udecl]: #namespace.udecl
|
| 2582 |
[namespace.udir]: #namespace.udir
|
| 2583 |
[namespace.unnamed]: #namespace.unnamed
|
| 2584 |
[over]: over.md#over
|
| 2585 |
[over.match]: over.md#over.match
|
|
|
|
| 2586 |
[over.match.conv]: over.md#over.match.conv
|
| 2587 |
[over.match.copy]: over.md#over.match.copy
|
| 2588 |
[over.match.ctor]: over.md#over.match.ctor
|
| 2589 |
[over.match.list]: over.md#over.match.list
|
| 2590 |
[over.match.ref]: over.md#over.match.ref
|
| 2591 |
[over.oper]: over.md#over.oper
|
| 2592 |
[over.sub]: over.md#over.sub
|
| 2593 |
[stmt.ambig]: stmt.md#stmt.ambig
|
| 2594 |
-
[stmt.block]: stmt.md#stmt.block
|
| 2595 |
[stmt.dcl]: stmt.md#stmt.dcl
|
| 2596 |
-
[stmt.
|
|
|
|
| 2597 |
[stmt.iter]: stmt.md#stmt.iter
|
|
|
|
| 2598 |
[stmt.return]: stmt.md#stmt.return
|
| 2599 |
[stmt.select]: stmt.md#stmt.select
|
| 2600 |
[stmt.stmt]: stmt.md#stmt.stmt
|
|
|
|
| 2601 |
[support.runtime]: language.md#support.runtime
|
| 2602 |
[tab:simple.type.specifiers]: #tab:simple.type.specifiers
|
| 2603 |
[temp]: temp.md#temp
|
| 2604 |
[temp.arg.type]: temp.md#temp.arg.type
|
| 2605 |
[temp.class.spec]: temp.md#temp.class.spec
|
|
|
|
| 2606 |
[temp.deduct.call]: temp.md#temp.deduct.call
|
| 2607 |
[temp.dep]: temp.md#temp.dep
|
| 2608 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 2609 |
[temp.explicit]: temp.md#temp.explicit
|
| 2610 |
[temp.inst]: temp.md#temp.inst
|
|
@@ -2615,11 +3278,11 @@ int a[] =
|
|
| 2615 |
[temp.spec]: temp.md#temp.spec
|
| 2616 |
[temp.variadic]: temp.md#temp.variadic
|
| 2617 |
|
| 2618 |
[^1]: The “implicit int” rule of C is no longer supported.
|
| 2619 |
|
| 2620 |
-
[^2]: The inline keyword has no effect on the linkage of a function.
|
| 2621 |
|
| 2622 |
[^3]: There is no special provision for a *decl-specifier-seq* that
|
| 2623 |
lacks a *type-specifier* or that has a *type-specifier* that only
|
| 2624 |
specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
|
| 2625 |
supported.
|
|
@@ -2627,88 +3290,46 @@ int a[] =
|
|
| 2627 |
[^4]: This set of values is used to define promotion and conversion
|
| 2628 |
semantics for the enumeration type. It does not preclude an
|
| 2629 |
expression of enumeration type from having a value that falls
|
| 2630 |
outside this range.
|
| 2631 |
|
| 2632 |
-
[^5]:
|
| 2633 |
-
linkage, they are effectively qualified by a name unique to their
|
| 2634 |
-
translation unit and therefore can never be seen from any other
|
| 2635 |
-
translation unit.
|
| 2636 |
-
|
| 2637 |
-
[^6]: this implies that the name of the class or function is
|
| 2638 |
unqualified.
|
| 2639 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2640 |
[^7]: During name lookup in a class hierarchy, some ambiguities may be
|
| 2641 |
resolved by considering whether one member hides the other along
|
| 2642 |
some paths ([[class.member.lookup]]). There is no such
|
| 2643 |
disambiguation when considering the set of names found as a result
|
| 2644 |
of following *using-directive*s.
|
| 2645 |
|
| 2646 |
-
[^8]:
|
| 2647 |
-
the corresponding sequence of declarations each with a single
|
| 2648 |
-
declarator. That is
|
| 2649 |
-
|
| 2650 |
-
`T D1, D2, ... Dn;`
|
| 2651 |
-
|
| 2652 |
-
is usually equivalent to
|
| 2653 |
-
|
| 2654 |
-
`T D1; T D2; ... T Dn;`
|
| 2655 |
-
|
| 2656 |
-
where `T` is a *decl-specifier-seq* and each `Di` is an
|
| 2657 |
-
*init-declarator*. An exception occurs when a name introduced by one
|
| 2658 |
-
of the *declarator*s hides a type name used by the
|
| 2659 |
-
*decl-specifiers*, so that when the same *decl-specifiers* are used
|
| 2660 |
-
in a subsequent declaration, they do not have the same meaning, as
|
| 2661 |
-
in
|
| 2662 |
-
|
| 2663 |
-
`struct S { ... };`
|
| 2664 |
-
`S S, T; \textrm{// declare two instances of \tcode{struct S}}`
|
| 2665 |
-
|
| 2666 |
-
which is not equivalent to
|
| 2667 |
-
|
| 2668 |
-
`struct S { ... };`
|
| 2669 |
-
`S S;`
|
| 2670 |
-
`S T; \textrm{// error}`
|
| 2671 |
-
|
| 2672 |
-
Another exception occurs when `T` is `auto` ([[dcl.spec.auto]]),
|
| 2673 |
-
for example:
|
| 2674 |
-
|
| 2675 |
-
`auto i = 1, j = 2.0; \textrm{// error: deduced types for \tcode{i} and \tcode{j} do not match}`
|
| 2676 |
-
as opposed to
|
| 2677 |
-
`auto i = 1; \textrm{// OK: \tcode{i} deduced to have type \tcode{int}}`
|
| 2678 |
-
`auto j = 2.0; \textrm{// OK: \tcode{j} deduced to have type \tcode{double}}`
|
| 2679 |
-
|
| 2680 |
-
[^9]: As indicated by syntax, cv-qualifiers are a significant component
|
| 2681 |
in function return types.
|
| 2682 |
|
| 2683 |
-
[^
|
| 2684 |
-
to array of unknown bound of `T`” and where means any sequence of
|
| 2685 |
-
“pointer to” and “array of” derived declarator types. This exclusion
|
| 2686 |
-
applies to the parameters of the function, and if a parameter is a
|
| 2687 |
-
pointer to function or pointer to member function then to its
|
| 2688 |
-
parameters also, etc.
|
| 2689 |
-
|
| 2690 |
-
[^11]: One can explicitly disambiguate the parse either by introducing a
|
| 2691 |
comma (so the ellipsis will be parsed as part of the
|
| 2692 |
*parameter-declaration-clause*) or by introducing a name for the
|
| 2693 |
parameter (so the ellipsis will be parsed as part of the
|
| 2694 |
*declarator-id*).
|
| 2695 |
|
| 2696 |
-
[^
|
| 2697 |
declarations of pointers to functions, references to functions, or
|
| 2698 |
`typedef` declarations.
|
| 2699 |
|
| 2700 |
-
[^
|
| 2701 |
variables with names that are reserved to the implementation (
|
| 2702 |
-
[[
|
| 2703 |
[[basic.def.odr]]), its string value need not be present in the
|
| 2704 |
program image.
|
| 2705 |
|
| 2706 |
-
[^
|
| 2707 |
whose value is `0` to a pointer type results in a null pointer
|
| 2708 |
value.
|
| 2709 |
|
| 2710 |
-
[^
|
| 2711 |
nonetheless C++does not have zero length arrays.
|
| 2712 |
|
| 2713 |
-
[^
|
| 2714 |
returning a reference type.
|
|
|
|
| 25 |
to) and `()` (function returning). Initial values can also be specified
|
| 26 |
in a declarator; initializers are discussed in [[dcl.init]] and
|
| 27 |
[[class.init]].
|
| 28 |
|
| 29 |
Each *init-declarator* in a declaration is analyzed separately as if it
|
| 30 |
+
was in a declaration by itself.
|
| 31 |
+
|
| 32 |
+
[*Note 1*:
|
| 33 |
+
|
| 34 |
+
A declaration with several declarators is usually equivalent to the
|
| 35 |
+
corresponding sequence of declarations each with a single declarator.
|
| 36 |
+
That is
|
| 37 |
+
|
| 38 |
+
``` cpp
|
| 39 |
+
T D1, D2, ... Dn;
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
is usually equivalent to
|
| 43 |
+
|
| 44 |
+
``` cpp
|
| 45 |
+
T D1; T D2; ... T Dn;
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
where `T` is a *decl-specifier-seq* and each `Di` is an
|
| 49 |
+
*init-declarator*. One exception is when a name introduced by one of the
|
| 50 |
+
*declarator*s hides a type name used by the *decl-specifier*s, so that
|
| 51 |
+
when the same *decl-specifier*s are used in a subsequent declaration,
|
| 52 |
+
they do not have the same meaning, as in
|
| 53 |
+
|
| 54 |
+
``` cpp
|
| 55 |
+
struct S { ... };
|
| 56 |
+
S S, T; // declare two instances of struct S
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
which is not equivalent to
|
| 60 |
+
|
| 61 |
+
``` cpp
|
| 62 |
+
struct S { ... };
|
| 63 |
+
S S;
|
| 64 |
+
S T; // error
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
Another exception is when `T` is `auto` ([[dcl.spec.auto]]), for
|
| 68 |
+
example:
|
| 69 |
+
|
| 70 |
+
``` cpp
|
| 71 |
+
auto i = 1, j = 2.0; // error: deduced types for i and j do not match
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
as opposed to
|
| 75 |
+
|
| 76 |
+
``` cpp
|
| 77 |
+
auto i = 1; // OK: i deduced to have type int
|
| 78 |
+
auto j = 2.0; // OK: j deduced to have type double
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
— *end note*]
|
| 82 |
|
| 83 |
Declarators have the syntax
|
| 84 |
|
| 85 |
``` bnf
|
| 86 |
declarator:
|
|
|
|
| 103 |
```
|
| 104 |
|
| 105 |
``` bnf
|
| 106 |
parameters-and-qualifiers:
|
| 107 |
'(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 108 |
+
ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 109 |
```
|
| 110 |
|
| 111 |
``` bnf
|
| 112 |
trailing-return-type:
|
| 113 |
+
'->' type-id
|
| 114 |
```
|
| 115 |
|
| 116 |
``` bnf
|
| 117 |
ptr-operator:
|
| 118 |
'*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ
|
|
|
|
| 141 |
``` bnf
|
| 142 |
declarator-id:
|
| 143 |
'...'ₒₚₜ id-expression
|
| 144 |
```
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
## Type names <a id="dcl.name">[[dcl.name]]</a>
|
| 147 |
|
| 148 |
To specify type conversions explicitly, and as an argument of `sizeof`,
|
| 149 |
`alignof`, `new`, or `typeid`, the name of a type shall be specified.
|
| 150 |
This can be done with a *type-id*, which is syntactically a declaration
|
|
|
|
| 154 |
``` bnf
|
| 155 |
type-id:
|
| 156 |
type-specifier-seq abstract-declaratorₒₚₜ
|
| 157 |
```
|
| 158 |
|
| 159 |
+
``` bnf
|
| 160 |
+
defining-type-id:
|
| 161 |
+
defining-type-specifier-seq abstract-declaratorₒₚₜ
|
| 162 |
+
```
|
| 163 |
+
|
| 164 |
``` bnf
|
| 165 |
abstract-declarator:
|
| 166 |
ptr-abstract-declarator
|
| 167 |
noptr-abstract-declaratorₒₚₜ parameters-and-qualifiers trailing-return-type
|
| 168 |
abstract-pack-declarator
|
|
|
|
| 197 |
It is possible to identify uniquely the location in the
|
| 198 |
*abstract-declarator* where the identifier would appear if the
|
| 199 |
construction were a declarator in a declaration. The named type is then
|
| 200 |
the same as the type of the hypothetical identifier.
|
| 201 |
|
| 202 |
+
[*Example 1*:
|
| 203 |
+
|
| 204 |
``` cpp
|
| 205 |
int // int i
|
| 206 |
int * // int *pi
|
| 207 |
int *[3] // int *p[3]
|
| 208 |
int (*)[3] // int (*p3i)[3]
|
| 209 |
int *() // int *f()
|
| 210 |
int (*)(double) // int (*pf)(double)
|
| 211 |
```
|
| 212 |
|
| 213 |
+
name respectively the types “`int`”, “pointer to `int`”, “array of 3
|
| 214 |
+
pointers to `int`”, “pointer to array of 3 `int`”, “function of (no
|
| 215 |
+
parameters) returning pointer to `int`”, and “pointer to a function of
|
| 216 |
+
(`double`) returning `int`”.
|
| 217 |
|
| 218 |
+
— *end example*]
|
| 219 |
+
|
| 220 |
+
A type can also be named (often more easily) by using a `typedef` (
|
| 221 |
[[dcl.typedef]]).
|
| 222 |
|
| 223 |
## Ambiguity resolution <a id="dcl.ambig.res">[[dcl.ambig.res]]</a>
|
| 224 |
|
| 225 |
The ambiguity arising from the similarity between a function-style cast
|
|
|
|
| 227 |
context of a declaration. In that context, the choice is between a
|
| 228 |
function declaration with a redundant set of parentheses around a
|
| 229 |
parameter name and an object declaration with a function-style cast as
|
| 230 |
the initializer. Just as for the ambiguities mentioned in
|
| 231 |
[[stmt.ambig]], the resolution is to consider any construct that could
|
| 232 |
+
possibly be a declaration a declaration.
|
| 233 |
+
|
| 234 |
+
[*Note 1*: A declaration can be explicitly disambiguated by adding
|
| 235 |
+
parentheses around the argument. The ambiguity can be avoided by use of
|
| 236 |
+
copy-initialization or list-initialization syntax, or by use of a
|
| 237 |
+
non-function-style cast. — *end note*]
|
| 238 |
+
|
| 239 |
+
[*Example 1*:
|
| 240 |
|
| 241 |
``` cpp
|
| 242 |
struct S {
|
| 243 |
S(int);
|
| 244 |
};
|
| 245 |
|
| 246 |
void foo(double a) {
|
| 247 |
S w(int(a)); // function declaration
|
| 248 |
S x(int()); // function declaration
|
| 249 |
+
S y((int(a))); // object declaration
|
| 250 |
S y((int)a); // object declaration
|
| 251 |
S z = int(a); // object declaration
|
| 252 |
}
|
| 253 |
```
|
| 254 |
|
| 255 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
+
An ambiguity can arise from the similarity between a function-style cast
|
| 258 |
+
and a *type-id*. The resolution is that any construct that could
|
| 259 |
+
possibly be a *type-id* in its syntactic context shall be considered a
|
| 260 |
+
*type-id*.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
+
[*Example 2*:
|
| 263 |
|
| 264 |
``` cpp
|
| 265 |
+
template <class T> struct X {};
|
| 266 |
+
template <int N> struct Y {};
|
| 267 |
+
X<int()> a; // type-id
|
| 268 |
+
X<int(1)> b; // expression (ill-formed)
|
| 269 |
+
Y<int()> c; // type-id (ill-formed)
|
| 270 |
+
Y<int(1)> d; // expression
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
+
void foo(signed char a) {
|
|
|
|
|
|
|
| 273 |
sizeof(int()); // type-id (ill-formed)
|
| 274 |
+
sizeof(int(a)); // expression
|
| 275 |
+
sizeof(int(unsigned(a))); // type-id (ill-formed)
|
| 276 |
+
|
| 277 |
+
(int())+1; // type-id (ill-formed)
|
| 278 |
+
(int(a))+1; // expression
|
| 279 |
+
(int(unsigned(a)))+1; // type-id (ill-formed)
|
| 280 |
}
|
| 281 |
```
|
| 282 |
|
| 283 |
+
— *end example*]
|
| 284 |
|
| 285 |
+
Another ambiguity arises in a *parameter-declaration-clause* when a
|
| 286 |
+
*type-name* is nested in parentheses. In this case, the choice is
|
| 287 |
+
between the declaration of a parameter of type pointer to function and
|
| 288 |
+
the declaration of a parameter with redundant parentheses around the
|
| 289 |
+
*declarator-id*. The resolution is to consider the *type-name* as a
|
| 290 |
+
*simple-type-specifier* rather than a *declarator-id*.
|
| 291 |
|
| 292 |
+
[*Example 3*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
``` cpp
|
| 295 |
class C { };
|
| 296 |
void f(int(C)) { } // void f(int(*fp)(C c)) { }
|
| 297 |
+
// not: void f(int C) { }
|
| 298 |
|
| 299 |
int g(C);
|
| 300 |
|
| 301 |
void foo() {
|
| 302 |
f(1); // error: cannot convert 1 to function pointer
|
|
|
|
| 310 |
class C { };
|
| 311 |
void h(int *(C[10])); // void h(int *(*_fp)(C _parm[10]));
|
| 312 |
// not: void h(int *C[10]);
|
| 313 |
```
|
| 314 |
|
| 315 |
+
— *end example*]
|
| 316 |
+
|
| 317 |
## Meaning of declarators <a id="dcl.meaning">[[dcl.meaning]]</a>
|
| 318 |
|
| 319 |
+
A declarator contains exactly one *declarator-id*; it names the
|
| 320 |
+
identifier that is declared. An *unqualified-id* occurring in a
|
| 321 |
+
*declarator-id* shall be a simple *identifier* except for the
|
| 322 |
+
declaration of some special functions ([[class.ctor]], [[class.conv]],
|
| 323 |
+
[[class.dtor]], [[over.oper]]) and for the declaration of template
|
| 324 |
+
specializations or partial specializations ([[temp.spec]]). When the
|
| 325 |
+
*declarator-id* is qualified, the declaration shall refer to a
|
| 326 |
+
previously declared member of the class or namespace to which the
|
| 327 |
+
qualifier refers (or, in the case of a namespace, of an element of the
|
| 328 |
+
inline namespace set of that namespace ([[namespace.def]])) or to a
|
| 329 |
+
specialization thereof; the member shall not merely have been introduced
|
| 330 |
+
by a *using-declaration* in the scope of the class or namespace
|
| 331 |
+
nominated by the *nested-name-specifier* of the *declarator-id*. The
|
| 332 |
+
*nested-name-specifier* of a qualified *declarator-id* shall not begin
|
| 333 |
+
with a *decltype-specifier*.
|
| 334 |
+
|
| 335 |
+
[*Note 1*: If the qualifier is the global `::` scope resolution
|
| 336 |
+
operator, the *declarator-id* refers to a name declared in the global
|
| 337 |
+
namespace scope. — *end note*]
|
| 338 |
+
|
| 339 |
The optional *attribute-specifier-seq* following a *declarator-id*
|
| 340 |
appertains to the entity that is declared.
|
| 341 |
|
| 342 |
+
A `static`, `thread_local`, `extern`, `mutable`, `friend`, `inline`,
|
| 343 |
+
`virtual`, `constexpr`, `explicit`, or `typedef` specifier applies
|
| 344 |
+
directly to each *declarator-id* in an *init-declarator-list* or
|
| 345 |
+
*member-declarator-list*; the type specified for each *declarator-id*
|
| 346 |
+
depends on both the *decl-specifier-seq* and its *declarator*.
|
| 347 |
|
| 348 |
Thus, a declaration of a particular identifier has the form
|
| 349 |
|
| 350 |
``` cpp
|
| 351 |
T D
|
| 352 |
```
|
| 353 |
|
| 354 |
+
where `T` is of the form *attribute-specifier-seq*ₒₚₜ
|
| 355 |
*decl-specifier-seq* and `D` is a declarator. Following is a recursive
|
| 356 |
procedure for determining the type specified for the contained
|
| 357 |
*declarator-id* by such a declaration.
|
| 358 |
|
| 359 |
First, the *decl-specifier-seq* determines a type. In a declaration
|
| 360 |
|
| 361 |
``` cpp
|
| 362 |
T D
|
| 363 |
```
|
| 364 |
|
| 365 |
+
the *decl-specifier-seq* `T` determines the type `T`.
|
| 366 |
+
|
| 367 |
+
[*Example 1*:
|
| 368 |
+
|
| 369 |
+
In the declaration
|
| 370 |
|
| 371 |
``` cpp
|
| 372 |
int unsigned i;
|
| 373 |
```
|
| 374 |
|
| 375 |
the type specifiers `int` `unsigned` determine the type “`unsigned int`”
|
| 376 |
([[dcl.type.simple]]).
|
| 377 |
|
| 378 |
+
— *end example*]
|
| 379 |
+
|
| 380 |
+
In a declaration *attribute-specifier-seq*ₒₚₜ `T` `D` where `D` is an
|
| 381 |
unadorned identifier the type of this identifier is “`T`”.
|
| 382 |
|
| 383 |
In a declaration `T` `D` where `D` has the form
|
| 384 |
|
| 385 |
``` bnf
|
| 386 |
+
'(' 'D1' ')'
|
| 387 |
```
|
| 388 |
|
| 389 |
the type of the contained *declarator-id* is the same as that of the
|
| 390 |
contained *declarator-id* in the declaration
|
| 391 |
|
|
|
|
| 402 |
|
| 403 |
``` bnf
|
| 404 |
'*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
| 405 |
```
|
| 406 |
|
| 407 |
+
and the type of the identifier in the declaration `T` `D1` is
|
| 408 |
+
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 409 |
+
`D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
|
| 410 |
+
`T`”. The *cv-qualifier*s apply to the pointer and not to the object
|
| 411 |
+
pointed to. Similarly, the optional *attribute-specifier-seq* (
|
| 412 |
[[dcl.attr.grammar]]) appertains to the pointer and not to the object
|
| 413 |
pointed to.
|
| 414 |
|
| 415 |
+
[*Example 1*:
|
| 416 |
+
|
| 417 |
+
The declarations
|
| 418 |
|
| 419 |
``` cpp
|
| 420 |
const int ci = 10, *pc = &ci, *const cpc = pc, **ppc;
|
| 421 |
int i, *p, *const cp = &i;
|
| 422 |
```
|
|
|
|
| 453 |
Each is unacceptable because it would either change the value of an
|
| 454 |
object declared `const` or allow it to be changed through a
|
| 455 |
cv-unqualified pointer later, for example:
|
| 456 |
|
| 457 |
``` cpp
|
| 458 |
+
*ppc = &ci; // OK, but would make p point to ci because of previous error
|
|
|
|
| 459 |
*p = 5; // clobber ci
|
| 460 |
```
|
| 461 |
|
| 462 |
+
— *end example*]
|
| 463 |
+
|
| 464 |
See also [[expr.ass]] and [[dcl.init]].
|
| 465 |
|
| 466 |
+
[*Note 1*: Forming a pointer to reference type is ill-formed; see
|
| 467 |
+
[[dcl.ref]]. Forming a function pointer type is ill-formed if the
|
| 468 |
+
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 469 |
+
[[dcl.fct]]. Since the address of a bit-field ([[class.bit]]) cannot be
|
| 470 |
+
taken, a pointer can never point to a bit-field. — *end note*]
|
| 471 |
|
| 472 |
### References <a id="dcl.ref">[[dcl.ref]]</a>
|
| 473 |
|
| 474 |
In a declaration `T` `D` where `D` has either of the forms
|
| 475 |
|
| 476 |
``` bnf
|
| 477 |
'&' attribute-specifier-seqₒₚₜ 'D1'
|
| 478 |
'&&' attribute-specifier-seqₒₚₜ 'D1'
|
| 479 |
```
|
| 480 |
|
| 481 |
+
and the type of the identifier in the declaration `T` `D1` is
|
| 482 |
+
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 483 |
+
`D` is “*derived-declarator-type-list* reference to `T`”. The optional
|
| 484 |
+
*attribute-specifier-seq* appertains to the reference type. Cv-qualified
|
| 485 |
+
references are ill-formed except when the cv-qualifiers are introduced
|
| 486 |
+
through the use of a *typedef-name* ([[dcl.typedef]], [[temp.param]])
|
| 487 |
+
or *decltype-specifier* ([[dcl.type.simple]]), in which case the
|
| 488 |
+
cv-qualifiers are ignored.
|
| 489 |
+
|
| 490 |
+
[*Example 1*:
|
| 491 |
|
| 492 |
``` cpp
|
| 493 |
typedef int& A;
|
| 494 |
const A aref = 3; // ill-formed; lvalue reference to non-const initialized with rvalue
|
| 495 |
```
|
| 496 |
|
| 497 |
The type of `aref` is “lvalue reference to `int`”, not “lvalue reference
|
| 498 |
+
to `const int`”.
|
| 499 |
+
|
| 500 |
+
— *end example*]
|
| 501 |
+
|
| 502 |
+
[*Note 1*: A reference can be thought of as a name of an
|
| 503 |
+
object. — *end note*]
|
| 504 |
+
|
| 505 |
+
A declarator that specifies the type “reference to cv `void`” is
|
| 506 |
ill-formed.
|
| 507 |
|
| 508 |
A reference type that is declared using `&` is called an *lvalue
|
| 509 |
reference*, and a reference type that is declared using `&&` is called
|
| 510 |
an *rvalue reference*. Lvalue references and rvalue references are
|
| 511 |
distinct types. Except where explicitly noted, they are semantically
|
| 512 |
equivalent and commonly referred to as references.
|
| 513 |
|
| 514 |
+
[*Example 2*:
|
| 515 |
+
|
| 516 |
``` cpp
|
| 517 |
void f(double& a) { a += 3.14; }
|
| 518 |
// ...
|
| 519 |
double d = 0;
|
| 520 |
f(d);
|
|
|
|
| 555 |
```
|
| 556 |
|
| 557 |
declares `p` to be a reference to a pointer to `link` so `h(q)` will
|
| 558 |
leave `q` with the value zero. See also [[dcl.init.ref]].
|
| 559 |
|
| 560 |
+
— *end example*]
|
| 561 |
+
|
| 562 |
It is unspecified whether or not a reference requires storage (
|
| 563 |
[[basic.stc]]).
|
| 564 |
|
| 565 |
There shall be no references to references, no arrays of references, and
|
| 566 |
no pointers to references. The declaration of a reference shall contain
|
| 567 |
an *initializer* ([[dcl.init.ref]]) except when the declaration
|
| 568 |
contains an explicit `extern` specifier ([[dcl.stc]]), is a class
|
| 569 |
member ([[class.mem]]) declaration within a class definition, or is the
|
| 570 |
declaration of a parameter or a return type ([[dcl.fct]]); see
|
| 571 |
[[basic.def]]. A reference shall be initialized to refer to a valid
|
| 572 |
+
object or function.
|
| 573 |
+
|
| 574 |
+
[*Note 2*: In particular, a null reference cannot exist in a
|
| 575 |
well-defined program, because the only way to create such a reference
|
| 576 |
would be to bind it to the “object” obtained by indirection through a
|
| 577 |
null pointer, which causes undefined behavior. As described in
|
| 578 |
+
[[class.bit]], a reference cannot be bound directly to a
|
| 579 |
+
bit-field. — *end note*]
|
| 580 |
|
| 581 |
If a *typedef-name* ([[dcl.typedef]], [[temp.param]]) or a
|
| 582 |
*decltype-specifier* ([[dcl.type.simple]]) denotes a type `TR` that is
|
| 583 |
a reference to a type `T`, an attempt to create the type “lvalue
|
| 584 |
reference to cv `TR`” creates the type “lvalue reference to `T`”, while
|
| 585 |
an attempt to create the type “rvalue reference to cv `TR`” creates the
|
| 586 |
type `TR`.
|
| 587 |
|
| 588 |
+
[*Note 3*: This rule is known as reference collapsing. — *end note*]
|
| 589 |
+
|
| 590 |
+
[*Example 3*:
|
| 591 |
+
|
| 592 |
``` cpp
|
| 593 |
int i;
|
| 594 |
typedef int& LRI;
|
| 595 |
typedef int&& RRI;
|
| 596 |
|
|
|
|
| 603 |
|
| 604 |
decltype(r2)& r6 = i; // r6 has the type int&
|
| 605 |
decltype(r2)&& r7 = i; // r7 has the type int&
|
| 606 |
```
|
| 607 |
|
| 608 |
+
— *end example*]
|
| 609 |
+
|
| 610 |
+
[*Note 4*: Forming a reference to function type is ill-formed if the
|
| 611 |
+
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 612 |
+
[[dcl.fct]]. — *end note*]
|
| 613 |
|
| 614 |
### Pointers to members <a id="dcl.mptr">[[dcl.mptr]]</a>
|
| 615 |
|
| 616 |
In a declaration `T` `D` where `D` has the form
|
| 617 |
|
| 618 |
``` bnf
|
| 619 |
+
nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
| 620 |
```
|
| 621 |
|
| 622 |
and the *nested-name-specifier* denotes a class, and the type of the
|
| 623 |
+
identifier in the declaration `T` `D1` is
|
| 624 |
+
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 625 |
+
`D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
|
| 626 |
+
member of class *nested-name-specifier* of type `T`”. The optional
|
| 627 |
+
*attribute-specifier-seq* ([[dcl.attr.grammar]]) appertains to the
|
| 628 |
+
pointer-to-member.
|
| 629 |
+
|
| 630 |
+
[*Example 1*:
|
| 631 |
|
| 632 |
``` cpp
|
| 633 |
struct X {
|
| 634 |
void f(int);
|
| 635 |
int a;
|
|
|
|
| 651 |
type. `pmi` and `pmf` can be used like this:
|
| 652 |
|
| 653 |
``` cpp
|
| 654 |
X obj;
|
| 655 |
// ...
|
| 656 |
+
obj.*pmi = 7; // assign 7 to an integer member of obj
|
| 657 |
+
(obj.*pmf)(7); // call a function member of obj with the argument 7
|
|
|
|
|
|
|
| 658 |
```
|
| 659 |
|
| 660 |
+
— *end example*]
|
| 661 |
+
|
| 662 |
A pointer to member shall not point to a static member of a class (
|
| 663 |
+
[[class.static]]), a member with reference type, or “cv `void`”.
|
| 664 |
|
| 665 |
+
[*Note 1*: See also [[expr.unary]] and [[expr.mptr.oper]]. The type
|
| 666 |
+
“pointer to member” is distinct from the type “pointer”, that is, a
|
| 667 |
+
pointer to member is declared only by the pointer to member declarator
|
| 668 |
+
syntax, and never by the pointer declarator syntax. There is no
|
| 669 |
+
“reference-to-member” type in C++. — *end note*]
|
| 670 |
|
| 671 |
### Arrays <a id="dcl.array">[[dcl.array]]</a>
|
| 672 |
|
| 673 |
In a declaration `T` `D` where `D` has the form
|
| 674 |
|
|
|
|
| 677 |
```
|
| 678 |
|
| 679 |
and the type of the identifier in the declaration `T` `D1` is
|
| 680 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 681 |
`D` is an array type; if the type of the identifier of `D` contains the
|
| 682 |
+
`auto` *type-specifier*, the program is ill-formed. `T` is called the
|
| 683 |
+
array *element type*; this type shall not be a reference type,
|
| 684 |
+
cv `void`, a function type or an abstract class type. If the
|
| 685 |
+
*constant-expression* ([[expr.const]]) is present, it shall be a
|
| 686 |
converted constant expression of type `std::size_t` and its value shall
|
| 687 |
be greater than zero. The constant expression specifies the *bound* of
|
| 688 |
(number of elements in) the array. If the value of the constant
|
| 689 |
expression is `N`, the array has `N` elements numbered `0` to `N-1`, and
|
| 690 |
+
the type of the identifier of `D` is “*derived-declarator-type-list*
|
| 691 |
+
array of `N` `T`”. An object of array type contains a contiguously
|
| 692 |
+
allocated non-empty set of `N` subobjects of type `T`. Except as noted
|
| 693 |
+
below, if the constant expression is omitted, the type of the identifier
|
| 694 |
+
of `D` is “*derived-declarator-type-list* array of unknown bound of
|
| 695 |
+
`T`”, an incomplete object type. The type
|
| 696 |
+
“*derived-declarator-type-list* array of `N` `T`” is a different type
|
| 697 |
+
from the type “*derived-declarator-type-list* array of unknown bound of
|
| 698 |
+
`T`”, see [[basic.types]]. Any type of the form “*cv-qualifier-seq*
|
| 699 |
+
array of `N` `T`” is adjusted to “array of `N` *cv-qualifier-seq* `T`”,
|
| 700 |
+
and similarly for “array of unknown bound of `T`”. The optional
|
| 701 |
+
*attribute-specifier-seq* appertains to the array.
|
| 702 |
+
|
| 703 |
+
[*Example 1*:
|
| 704 |
|
| 705 |
``` cpp
|
| 706 |
typedef int A[5], AA[2][3];
|
| 707 |
typedef const A CA; // type is ``array of 5 const int''
|
| 708 |
typedef const AA CAA; // type is ``array of 2 array of 3 const int''
|
| 709 |
```
|
| 710 |
|
| 711 |
+
— *end example*]
|
| 712 |
+
|
| 713 |
+
[*Note 1*: An “array of `N` *cv-qualifier-seq* `T`” has cv-qualified
|
| 714 |
+
type; see [[basic.type.qualifier]]. — *end note*]
|
| 715 |
|
| 716 |
An array can be constructed from one of the fundamental types (except
|
| 717 |
`void`), from a pointer, from a pointer to member, from a class, from an
|
| 718 |
enumeration type, or from another array.
|
| 719 |
|
| 720 |
When several “array of” specifications are adjacent, a multidimensional
|
| 721 |
+
array type is created; only the first of the constant expressions that
|
| 722 |
specify the bounds of the arrays may be omitted. In addition to
|
| 723 |
declarations in which an incomplete object type is allowed, an array
|
| 724 |
bound may be omitted in some cases in the declaration of a function
|
| 725 |
parameter ([[dcl.fct]]). An array bound may also be omitted when the
|
| 726 |
+
declarator is followed by an *initializer* ([[dcl.init]]) or when a
|
| 727 |
+
declarator for a static data member is followed by a
|
| 728 |
+
*brace-or-equal-initializer* ([[class.mem]]). In both cases the bound
|
| 729 |
+
is calculated from the number of initial elements (say, `N`) supplied (
|
| 730 |
+
[[dcl.init.aggr]]), and the type of the identifier of `D` is “array of
|
| 731 |
+
`N` `T`”. Furthermore, if there is a preceding declaration of the entity
|
| 732 |
+
in the same scope in which the bound was specified, an omitted array
|
| 733 |
+
bound is taken to be the same as in that earlier declaration, and
|
| 734 |
+
similarly for the definition of a static data member of a class.
|
| 735 |
+
|
| 736 |
+
[*Example 2*:
|
| 737 |
|
| 738 |
``` cpp
|
| 739 |
float fa[17], *afp[17];
|
| 740 |
```
|
| 741 |
|
|
|
|
| 765 |
extern int x[];
|
| 766 |
int i = sizeof(x); // error: incomplete object type
|
| 767 |
}
|
| 768 |
```
|
| 769 |
|
| 770 |
+
— *end example*]
|
|
|
|
|
|
|
| 771 |
|
| 772 |
+
[*Note 2*: Conversions affecting expressions of array type are
|
| 773 |
+
described in [[conv.array]]. Objects of array types cannot be modified,
|
| 774 |
+
see [[basic.lval]]. — *end note*]
|
| 775 |
+
|
| 776 |
+
[*Note 3*: Except where it has been declared for a class (
|
| 777 |
+
[[over.sub]]), the subscript operator `[]` is interpreted in such a way
|
| 778 |
+
that `E1[E2]` is identical to `*((E1)+(E2))` ([[expr.sub]]). Because of
|
| 779 |
+
the conversion rules that apply to `+`, if `E1` is an array and `E2` an
|
| 780 |
+
integer, then `E1[E2]` refers to the `E2`-th member of `E1`. Therefore,
|
| 781 |
+
despite its asymmetric appearance, subscripting is a commutative
|
| 782 |
+
operation. — *end note*]
|
| 783 |
+
|
| 784 |
+
[*Note 4*:
|
| 785 |
|
| 786 |
A consistent rule is followed for multidimensional arrays. If `E` is an
|
| 787 |
*n*-dimensional array of rank i × j × … × k, then `E` appearing in an
|
| 788 |
expression that is subject to the array-to-pointer conversion (
|
| 789 |
[[conv.array]]) is converted to a pointer to an (n-1)-dimensional array
|
| 790 |
with rank j × … × k. If the `*` operator, either explicitly or
|
| 791 |
implicitly as a result of subscripting, is applied to this pointer, the
|
| 792 |
result is the pointed-to (n-1)-dimensional array, which itself is
|
| 793 |
immediately converted into a pointer.
|
| 794 |
|
| 795 |
+
[*Example 3*:
|
| 796 |
+
|
| 797 |
+
Consider
|
| 798 |
|
| 799 |
``` cpp
|
| 800 |
int x[3][5];
|
| 801 |
```
|
| 802 |
|
|
|
|
| 809 |
namely five integer objects. The results are added and indirection
|
| 810 |
applied to yield an array (of five integers), which in turn is converted
|
| 811 |
to a pointer to the first of the integers. If there is another subscript
|
| 812 |
the same argument applies again; this time the result is an integer.
|
| 813 |
|
| 814 |
+
— *end example*]
|
| 815 |
+
|
| 816 |
+
— *end note*]
|
| 817 |
+
|
| 818 |
+
[*Note 5*: It follows from all this that arrays in C++are stored
|
| 819 |
+
row-wise (last subscript varies fastest) and that the first subscript in
|
| 820 |
+
the declaration helps determine the amount of storage consumed by an
|
| 821 |
+
array but plays no other part in subscript calculations. — *end note*]
|
| 822 |
|
| 823 |
### Functions <a id="dcl.fct">[[dcl.fct]]</a>
|
| 824 |
|
| 825 |
In a declaration `T` `D` where `D` has the form
|
| 826 |
|
| 827 |
``` bnf
|
| 828 |
'D1 (' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 829 |
+
ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 830 |
```
|
| 831 |
|
| 832 |
and the type of the contained *declarator-id* in the declaration `T`
|
| 833 |
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 834 |
+
*declarator-id* in `D` is “*derived-declarator-type-list* `noexcept`
|
| 835 |
+
function of (*parameter-declaration-clause*) *cv-qualifier-seq*ₒₚₜ
|
| 836 |
+
*ref-qualifier*ₒₚₜ returning `T`”, where the optional `noexcept` is
|
| 837 |
+
present if and only if the exception specification ([[except.spec]]) is
|
| 838 |
+
non-throwing. The optional *attribute-specifier-seq* appertains to the
|
| 839 |
+
function type.
|
| 840 |
|
| 841 |
In a declaration `T` `D` where `D` has the form
|
| 842 |
|
| 843 |
``` bnf
|
| 844 |
'D1 (' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 845 |
+
ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-type
|
| 846 |
```
|
| 847 |
|
| 848 |
and the type of the contained *declarator-id* in the declaration `T`
|
| 849 |
`D1` is “*derived-declarator-type-list* `T`”, `T` shall be the single
|
| 850 |
*type-specifier* `auto`. The type of the *declarator-id* in `D` is
|
| 851 |
+
“*derived-declarator-type-list* `noexcept` function of
|
| 852 |
+
(*parameter-declaration-clause*) *cv-qualifier-seq**ref-qualifier*
|
| 853 |
+
returning `U`”, where `U` is the type specified by the
|
| 854 |
+
*trailing-return-type*, and where the optional `noexcept` is present if
|
| 855 |
+
and only if the exception specification is non-throwing. The optional
|
| 856 |
*attribute-specifier-seq* appertains to the function type.
|
| 857 |
|
| 858 |
+
A type of either form is a *function type*.[^8]
|
| 859 |
|
| 860 |
``` bnf
|
| 861 |
parameter-declaration-clause:
|
| 862 |
+
parameter-declaration-listₒₚₜ '...'ₒₚₜ
|
| 863 |
+
parameter-declaration-list ', ...'
|
| 864 |
```
|
| 865 |
|
| 866 |
``` bnf
|
| 867 |
parameter-declaration-list:
|
| 868 |
parameter-declaration
|
|
|
|
| 879 |
|
| 880 |
The optional *attribute-specifier-seq* in a *parameter-declaration*
|
| 881 |
appertains to the parameter.
|
| 882 |
|
| 883 |
The *parameter-declaration-clause* determines the arguments that can be
|
| 884 |
+
specified, and their processing, when the function is called.
|
| 885 |
+
|
| 886 |
+
[*Note 1*: The *parameter-declaration-clause* is used to convert the
|
| 887 |
+
arguments specified on the function call; see
|
| 888 |
+
[[expr.call]]. — *end note*]
|
| 889 |
+
|
| 890 |
+
If the *parameter-declaration-clause* is empty, the function takes no
|
| 891 |
arguments. A parameter list consisting of a single unnamed parameter of
|
| 892 |
non-dependent type `void` is equivalent to an empty parameter list.
|
| 893 |
Except for this special case, a parameter shall not have type *cv*
|
| 894 |
`void`. If the *parameter-declaration-clause* terminates with an
|
| 895 |
ellipsis or a function parameter pack ([[temp.variadic]]), the number
|
| 896 |
of arguments shall be equal to or greater than the number of parameters
|
| 897 |
that do not have a default argument and are not function parameter
|
| 898 |
+
packs. Where syntactically correct and where “`...`” is not part of an
|
| 899 |
+
*abstract-declarator*, “`, ...`” is synonymous with “`...`”.
|
| 900 |
+
|
| 901 |
+
[*Example 1*:
|
| 902 |
+
|
| 903 |
+
The declaration
|
| 904 |
|
| 905 |
``` cpp
|
| 906 |
int printf(const char*, ...);
|
| 907 |
```
|
| 908 |
|
|
|
|
| 913 |
printf("hello world");
|
| 914 |
printf("a=%d b=%d", a, b);
|
| 915 |
```
|
| 916 |
|
| 917 |
However, the first argument must be of a type that can be converted to a
|
| 918 |
+
`const` `char*`
|
| 919 |
+
|
| 920 |
+
— *end example*]
|
| 921 |
+
|
| 922 |
+
[*Note 2*: The standard header `<cstdarg>` contains a mechanism for
|
| 923 |
accessing arguments passed using the ellipsis (see [[expr.call]] and
|
| 924 |
+
[[support.runtime]]). — *end note*]
|
| 925 |
|
| 926 |
A single name can be used for several different functions in a single
|
| 927 |
scope; this is function overloading (Clause [[over]]). All declarations
|
| 928 |
for a function shall agree exactly in both the return type and the
|
| 929 |
parameter-type-list. The type of a function is determined using the
|
| 930 |
following rules. The type of each parameter (including function
|
| 931 |
parameter packs) is determined from its own *decl-specifier-seq* and
|
| 932 |
*declarator*. After determining the type of each parameter, any
|
| 933 |
+
parameter of type “array of `T`” or of function type `T` is adjusted to
|
| 934 |
+
be “pointer to `T`”. After producing the list of parameter types, any
|
| 935 |
+
top-level *cv-qualifier*s modifying a parameter type are deleted when
|
| 936 |
+
forming the function type. The resulting list of transformed parameter
|
| 937 |
+
types and the presence or absence of the ellipsis or a function
|
| 938 |
+
parameter pack is the function’s *parameter-type-list*.
|
| 939 |
+
|
| 940 |
+
[*Note 3*: This transformation does not affect the types of the
|
| 941 |
+
parameters. For example, `int(*)(const int p, decltype(p)*)` and
|
| 942 |
+
`int(*)(int, const int*)` are identical types. — *end note*]
|
| 943 |
|
| 944 |
A function type with a *cv-qualifier-seq* or a *ref-qualifier*
|
| 945 |
(including a type named by *typedef-name* ([[dcl.typedef]],
|
| 946 |
[[temp.param]])) shall appear only as:
|
| 947 |
|
|
|
|
| 952 |
- the *type-id* in the default argument of a *type-parameter* (
|
| 953 |
[[temp.param]]), or
|
| 954 |
- the *type-id* of a *template-argument* for a *type-parameter* (
|
| 955 |
[[temp.arg.type]]).
|
| 956 |
|
| 957 |
+
[*Example 2*:
|
| 958 |
+
|
| 959 |
``` cpp
|
| 960 |
typedef int FIC(int) const;
|
| 961 |
FIC f; // ill-formed: does not declare a member function
|
| 962 |
struct S {
|
| 963 |
FIC f; // OK
|
| 964 |
};
|
| 965 |
FIC S::*pm = &S::f; // OK
|
| 966 |
```
|
| 967 |
|
| 968 |
+
— *end example*]
|
| 969 |
+
|
| 970 |
The effect of a *cv-qualifier-seq* in a function declarator is not the
|
| 971 |
same as adding cv-qualification on top of the function type. In the
|
| 972 |
+
latter case, the cv-qualifiers are ignored.
|
| 973 |
+
|
| 974 |
+
[*Note 4*: A function type that has a *cv-qualifier-seq* is not a
|
| 975 |
+
cv-qualified type; there are no cv-qualified function
|
| 976 |
+
types. — *end note*]
|
| 977 |
+
|
| 978 |
+
[*Example 3*:
|
| 979 |
|
| 980 |
``` cpp
|
| 981 |
typedef void F();
|
| 982 |
struct S {
|
| 983 |
const F f; // OK: equivalent to: void f();
|
| 984 |
};
|
| 985 |
```
|
| 986 |
|
| 987 |
+
— *end example*]
|
| 988 |
+
|
| 989 |
+
The return type, the parameter-type-list, the *ref-qualifier*, the
|
| 990 |
+
*cv-qualifier-seq*, and the exception specification, but not the default
|
| 991 |
+
arguments ([[dcl.fct.default]]), are part of the function type.
|
| 992 |
+
|
| 993 |
+
[*Note 5*: Function types are checked during the assignments and
|
| 994 |
initializations of pointers to functions, references to functions, and
|
| 995 |
+
pointers to member functions. — *end note*]
|
| 996 |
|
| 997 |
+
[*Example 4*:
|
| 998 |
+
|
| 999 |
+
The declaration
|
| 1000 |
|
| 1001 |
``` cpp
|
| 1002 |
int fseek(FILE*, long, int);
|
| 1003 |
```
|
| 1004 |
|
| 1005 |
declares a function taking three arguments of the specified types, and
|
| 1006 |
returning `int` ([[dcl.type]]).
|
| 1007 |
|
| 1008 |
+
— *end example*]
|
| 1009 |
+
|
| 1010 |
+
Functions shall not have a return type of type array or function,
|
| 1011 |
+
although they may have a return type of type pointer or reference to
|
| 1012 |
+
such things. There shall be no arrays of functions, although there can
|
| 1013 |
+
be arrays of pointers to functions.
|
| 1014 |
|
| 1015 |
Types shall not be defined in return or parameter types. The type of a
|
| 1016 |
parameter or the return type for a function definition shall not be an
|
| 1017 |
+
incomplete (possibly cv-qualified) class type in the context of the
|
| 1018 |
+
function definition unless the function is deleted (
|
| 1019 |
+
[[dcl.fct.def.delete]]).
|
|
|
|
| 1020 |
|
| 1021 |
A typedef of function type may be used to declare a function but shall
|
| 1022 |
not be used to define a function ([[dcl.fct.def]]).
|
| 1023 |
|
| 1024 |
+
[*Example 5*:
|
| 1025 |
+
|
| 1026 |
``` cpp
|
| 1027 |
typedef void F();
|
| 1028 |
F fv; // OK: equivalent to void fv();
|
| 1029 |
F fv { } // ill-formed
|
| 1030 |
void fv() { } // OK: definition of fv
|
| 1031 |
```
|
| 1032 |
|
| 1033 |
+
— *end example*]
|
| 1034 |
+
|
| 1035 |
An identifier can optionally be provided as a parameter name; if present
|
| 1036 |
+
in a function definition ([[dcl.fct.def]]), it names a parameter.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1037 |
|
| 1038 |
+
[*Note 6*: In particular, parameter names are also optional in function
|
| 1039 |
+
definitions and names used for a parameter in different declarations and
|
| 1040 |
+
the definition of a function need not be the same. If a parameter name
|
| 1041 |
+
is present in a function declaration that is not a definition, it cannot
|
| 1042 |
+
be used outside of its function declarator because that is the extent of
|
| 1043 |
+
its potential scope ([[basic.scope.proto]]). — *end note*]
|
| 1044 |
+
|
| 1045 |
+
[*Example 6*:
|
| 1046 |
+
|
| 1047 |
+
The declaration
|
| 1048 |
|
| 1049 |
``` cpp
|
| 1050 |
int i,
|
| 1051 |
*pi,
|
| 1052 |
f(),
|
|
|
|
| 1066 |
suggests, and the same construction in an expression requires, the
|
| 1067 |
calling of a function `fpi`, and then using indirection through the
|
| 1068 |
(pointer) result to yield an integer. In the declarator
|
| 1069 |
`(*pif)(const char*, const char*)`, the extra parentheses are necessary
|
| 1070 |
to indicate that indirection through a pointer to a function yields a
|
| 1071 |
+
function, which is then called.
|
| 1072 |
+
|
| 1073 |
+
— *end example*]
|
| 1074 |
+
|
| 1075 |
+
[*Note 7*:
|
| 1076 |
+
|
| 1077 |
+
Typedefs and *trailing-return-type*s are sometimes convenient when the
|
| 1078 |
+
return type of a function is complex. For example, the function `fpif`
|
| 1079 |
+
above could have been declared
|
| 1080 |
|
| 1081 |
``` cpp
|
| 1082 |
typedef int IFUNC(int);
|
| 1083 |
IFUNC* fpif(int);
|
| 1084 |
```
|
|
|
|
| 1100 |
|
| 1101 |
``` cpp
|
| 1102 |
template <class T, class U> decltype((*(T*)0) + (*(U*)0)) add(T t, U u);
|
| 1103 |
```
|
| 1104 |
|
| 1105 |
+
— *end note*]
|
| 1106 |
+
|
| 1107 |
A *non-template function* is a function that is not a function template
|
| 1108 |
+
specialization.
|
| 1109 |
+
|
| 1110 |
+
[*Note 8*: A function template is not a function. — *end note*]
|
| 1111 |
|
| 1112 |
A *declarator-id* or *abstract-declarator* containing an ellipsis shall
|
| 1113 |
only be used in a *parameter-declaration*. Such a
|
| 1114 |
*parameter-declaration* is a parameter pack ([[temp.variadic]]). When
|
| 1115 |
it is part of a *parameter-declaration-clause*, the parameter pack is a
|
| 1116 |
+
function parameter pack ([[temp.variadic]]).
|
| 1117 |
+
|
| 1118 |
+
[*Note 9*: Otherwise, the *parameter-declaration* is part of a
|
| 1119 |
+
*template-parameter-list* and the parameter pack is a template parameter
|
| 1120 |
+
pack; see [[temp.param]]. — *end note*]
|
| 1121 |
+
|
| 1122 |
+
A function parameter pack is a pack expansion ([[temp.variadic]]).
|
| 1123 |
+
|
| 1124 |
+
[*Example 7*:
|
| 1125 |
|
| 1126 |
``` cpp
|
| 1127 |
template<typename... T> void f(T (* ...t)(int, int));
|
| 1128 |
|
| 1129 |
int add(int, int);
|
|
|
|
| 1132 |
void g() {
|
| 1133 |
f(add, subtract);
|
| 1134 |
}
|
| 1135 |
```
|
| 1136 |
|
| 1137 |
+
— *end example*]
|
| 1138 |
+
|
| 1139 |
There is a syntactic ambiguity when an ellipsis occurs at the end of a
|
| 1140 |
*parameter-declaration-clause* without a preceding comma. In this case,
|
| 1141 |
the ellipsis is parsed as part of the *abstract-declarator* if the type
|
| 1142 |
of the parameter either names a template parameter pack that has not
|
| 1143 |
been expanded or contains `auto`; otherwise, it is parsed as part of the
|
| 1144 |
+
*parameter-declaration-clause*.[^9]
|
| 1145 |
|
| 1146 |
### Default arguments <a id="dcl.fct.default">[[dcl.fct.default]]</a>
|
| 1147 |
|
| 1148 |
If an *initializer-clause* is specified in a *parameter-declaration*
|
| 1149 |
this *initializer-clause* is used as a default argument. Default
|
| 1150 |
arguments will be used in calls where trailing arguments are missing.
|
| 1151 |
|
| 1152 |
+
[*Example 1*:
|
| 1153 |
+
|
| 1154 |
+
The declaration
|
| 1155 |
|
| 1156 |
``` cpp
|
| 1157 |
void point(int = 3, int = 4);
|
| 1158 |
```
|
| 1159 |
|
|
|
|
| 1165 |
```
|
| 1166 |
|
| 1167 |
The last two calls are equivalent to `point(1,4)` and `point(3,4)`,
|
| 1168 |
respectively.
|
| 1169 |
|
| 1170 |
+
— *end example*]
|
| 1171 |
+
|
| 1172 |
A default argument shall be specified only in the
|
| 1173 |
+
*parameter-declaration-clause* of a function declaration or
|
| 1174 |
+
*lambda-declarator* or in a *template-parameter* ([[temp.param]]); in
|
| 1175 |
+
the latter case, the *initializer-clause* shall be an
|
| 1176 |
+
*assignment-expression*. A default argument shall not be specified for a
|
| 1177 |
+
parameter pack. If it is specified in a *parameter-declaration-clause*,
|
| 1178 |
+
it shall not occur within a *declarator* or *abstract-declarator* of a
|
| 1179 |
+
*parameter-declaration*.[^10]
|
| 1180 |
|
| 1181 |
For non-template functions, default arguments can be added in later
|
| 1182 |
declarations of a function in the same scope. Declarations in different
|
| 1183 |
scopes have completely distinct sets of default arguments. That is,
|
| 1184 |
declarations in inner scopes do not acquire default arguments from
|
|
|
|
| 1187 |
argument shall have a default argument supplied in this or a previous
|
| 1188 |
declaration or shall be a function parameter pack. A default argument
|
| 1189 |
shall not be redefined by a later declaration (not even to the same
|
| 1190 |
value).
|
| 1191 |
|
| 1192 |
+
[*Example 2*:
|
| 1193 |
+
|
| 1194 |
``` cpp
|
| 1195 |
void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow
|
| 1196 |
// a parameter with a default argument
|
| 1197 |
void f(int, int);
|
| 1198 |
void f(int, int = 7);
|
| 1199 |
void h() {
|
| 1200 |
f(3); // OK, calls f(3, 7)
|
| 1201 |
+
void f(int = 1, int); // error: does not use default from surrounding scope
|
|
|
|
| 1202 |
}
|
| 1203 |
void m() {
|
| 1204 |
void f(int, int); // has no defaults
|
| 1205 |
f(4); // error: wrong number of arguments
|
| 1206 |
void f(int, int = 5); // OK
|
| 1207 |
f(4); // OK, calls f(4, 5);
|
| 1208 |
+
void f(int, int = 5); // error: cannot redefine, even to same value
|
|
|
|
| 1209 |
}
|
| 1210 |
void n() {
|
| 1211 |
f(6); // OK, calls f(6, 7)
|
| 1212 |
}
|
| 1213 |
```
|
| 1214 |
|
| 1215 |
+
— *end example*]
|
| 1216 |
+
|
| 1217 |
For a given inline function defined in different translation units, the
|
| 1218 |
accumulated sets of default arguments at the end of the translation
|
| 1219 |
units shall be the same; see [[basic.def.odr]]. If a friend declaration
|
| 1220 |
specifies a default argument expression, that declaration shall be a
|
| 1221 |
definition and shall be the only declaration of the function or function
|
|
|
|
| 1226 |
the copy-initialization semantics ([[dcl.init]]). The names in the
|
| 1227 |
default argument are bound, and the semantic constraints are checked, at
|
| 1228 |
the point where the default argument appears. Name lookup and checking
|
| 1229 |
of semantic constraints for default arguments in function templates and
|
| 1230 |
in member functions of class templates are performed as described in
|
| 1231 |
+
[[temp.inst]].
|
| 1232 |
+
|
| 1233 |
+
[*Example 3*:
|
| 1234 |
+
|
| 1235 |
+
In the following code, `g` will be called with the value `f(2)`:
|
| 1236 |
|
| 1237 |
``` cpp
|
| 1238 |
int a = 1;
|
| 1239 |
int f(int);
|
| 1240 |
int g(int x = f(a)); // default argument: f(::a)
|
|
|
|
| 1246 |
g(); // g(f(::a))
|
| 1247 |
}
|
| 1248 |
}
|
| 1249 |
```
|
| 1250 |
|
| 1251 |
+
— *end example*]
|
| 1252 |
+
|
| 1253 |
+
[*Note 1*: In member function declarations, names in default arguments
|
| 1254 |
+
are looked up as described in [[basic.lookup.unqual]]. Access checking
|
| 1255 |
+
applies to names in default arguments as described in Clause
|
| 1256 |
+
[[class.access]]. — *end note*]
|
| 1257 |
|
| 1258 |
Except for member functions of class templates, the default arguments in
|
| 1259 |
a member function definition that appears outside of the class
|
| 1260 |
definition are added to the set of default arguments provided by the
|
| 1261 |
member function declaration in the class definition; the program is
|
|
|
|
| 1263 |
constructor, or copy or move assignment operator ([[class.copy]]) is so
|
| 1264 |
declared. Default arguments for a member function of a class template
|
| 1265 |
shall be specified on the initial declaration of the member function
|
| 1266 |
within the class template.
|
| 1267 |
|
| 1268 |
+
[*Example 4*:
|
| 1269 |
+
|
| 1270 |
``` cpp
|
| 1271 |
class C {
|
| 1272 |
void f(int i = 3);
|
| 1273 |
void g(int i, int j = 99);
|
| 1274 |
};
|
| 1275 |
|
| 1276 |
+
void C::f(int i = 3) {} // error: default argument already specified in class scope
|
| 1277 |
+
void C::g(int i = 88, int j) {} // in this translation unit, C::g can be called with no argument
|
|
|
|
|
|
|
| 1278 |
```
|
| 1279 |
|
| 1280 |
+
— *end example*]
|
| 1281 |
+
|
| 1282 |
+
A local variable shall not appear as a potentially-evaluated expression
|
| 1283 |
+
in a default argument.
|
| 1284 |
+
|
| 1285 |
+
[*Example 5*:
|
| 1286 |
|
| 1287 |
``` cpp
|
| 1288 |
void f() {
|
| 1289 |
int i;
|
| 1290 |
extern void g(int x = i); // error
|
| 1291 |
+
extern void h(int x = sizeof(i)); // OK
|
| 1292 |
// ...
|
| 1293 |
}
|
| 1294 |
```
|
| 1295 |
|
| 1296 |
+
— *end example*]
|
| 1297 |
+
|
| 1298 |
+
[*Note 2*:
|
| 1299 |
+
|
| 1300 |
+
The keyword `this` may not appear in a default argument of a member
|
| 1301 |
+
function; see [[expr.prim.this]].
|
| 1302 |
+
|
| 1303 |
+
[*Example 6*:
|
| 1304 |
|
| 1305 |
``` cpp
|
| 1306 |
class A {
|
| 1307 |
void f(A* p = this) { } // error
|
| 1308 |
};
|
| 1309 |
```
|
| 1310 |
|
| 1311 |
+
— *end example*]
|
| 1312 |
+
|
| 1313 |
+
— *end note*]
|
| 1314 |
+
|
| 1315 |
A default argument is evaluated each time the function is called with no
|
| 1316 |
+
argument for the corresponding parameter. A parameter shall not appear
|
| 1317 |
+
as a potentially-evaluated expression in a default argument. Parameters
|
| 1318 |
+
of a function declared before a default argument are in scope and can
|
| 1319 |
+
hide namespace and class member names.
|
| 1320 |
+
|
| 1321 |
+
[*Example 7*:
|
| 1322 |
|
| 1323 |
``` cpp
|
| 1324 |
int a;
|
| 1325 |
+
int f(int a, int b = a); // error: parameter a used as default argument
|
|
|
|
| 1326 |
typedef int I;
|
| 1327 |
int g(float I, int b = I(2)); // error: parameter I found
|
| 1328 |
+
int h(int a, int b = sizeof(a)); // OK, unevaluated operand
|
|
|
|
| 1329 |
```
|
| 1330 |
|
| 1331 |
+
— *end example*]
|
| 1332 |
+
|
| 1333 |
+
A non-static member shall not appear in a default argument unless it
|
| 1334 |
+
appears as the *id-expression* of a class member access expression (
|
| 1335 |
+
[[expr.ref]]) or unless it is used to form a pointer to member (
|
| 1336 |
+
[[expr.unary.op]]).
|
| 1337 |
+
|
| 1338 |
+
[*Example 8*:
|
| 1339 |
+
|
| 1340 |
+
The declaration of `X::mem1()` in the following example is ill-formed
|
| 1341 |
+
because no object is supplied for the non-static member `X::a` used as
|
| 1342 |
+
an initializer.
|
| 1343 |
|
| 1344 |
``` cpp
|
| 1345 |
int b;
|
| 1346 |
class X {
|
| 1347 |
int a;
|
| 1348 |
+
int mem1(int i = a); // error: non-static member a used as default argument
|
|
|
|
| 1349 |
int mem2(int i = b); // OK; use X::b
|
| 1350 |
static int b;
|
| 1351 |
};
|
| 1352 |
```
|
| 1353 |
|
| 1354 |
The declaration of `X::mem2()` is meaningful, however, since no object
|
| 1355 |
is needed to access the static member `X::b`. Classes, objects, and
|
| 1356 |
+
members are described in Clause [[class]].
|
| 1357 |
+
|
| 1358 |
+
— *end example*]
|
| 1359 |
+
|
| 1360 |
+
A default argument is not part of the type of a function.
|
| 1361 |
+
|
| 1362 |
+
[*Example 9*:
|
| 1363 |
|
| 1364 |
``` cpp
|
| 1365 |
int f(int = 0);
|
| 1366 |
|
| 1367 |
void h() {
|
|
|
|
| 1371 |
|
| 1372 |
int (*p1)(int) = &f;
|
| 1373 |
int (*p2)() = &f; // error: type mismatch
|
| 1374 |
```
|
| 1375 |
|
| 1376 |
+
— *end example*]
|
| 1377 |
+
|
| 1378 |
When a declaration of a function is introduced by way of a
|
| 1379 |
*using-declaration* ([[namespace.udecl]]), any default argument
|
| 1380 |
information associated with the declaration is made known as well. If
|
| 1381 |
the function is redeclared thereafter in the namespace with additional
|
| 1382 |
default arguments, the additional arguments are also known at any point
|
|
|
|
| 1386 |
in the declaration of the virtual function determined by the static type
|
| 1387 |
of the pointer or reference denoting the object. An overriding function
|
| 1388 |
in a derived class does not acquire default arguments from the function
|
| 1389 |
it overrides.
|
| 1390 |
|
| 1391 |
+
[*Example 10*:
|
| 1392 |
+
|
| 1393 |
``` cpp
|
| 1394 |
struct A {
|
| 1395 |
virtual void f(int a = 7);
|
| 1396 |
};
|
| 1397 |
struct B : public A {
|
|
|
|
| 1403 |
pa->f(); // OK, calls pa->B::f(7)
|
| 1404 |
pb->f(); // error: wrong number of arguments for B::f()
|
| 1405 |
}
|
| 1406 |
```
|
| 1407 |
|
| 1408 |
+
— *end example*]
|
| 1409 |
+
|
| 1410 |
## Function definitions <a id="dcl.fct.def">[[dcl.fct.def]]</a>
|
| 1411 |
|
| 1412 |
### In general <a id="dcl.fct.def.general">[[dcl.fct.def.general]]</a>
|
| 1413 |
|
| 1414 |
Function definitions have the form
|
|
|
|
| 1430 |
as a reference to the non-terminal *function-body*. The optional
|
| 1431 |
*attribute-specifier-seq* in a *function-definition* appertains to the
|
| 1432 |
function. A *virt-specifier-seq* can be part of a *function-definition*
|
| 1433 |
only if it is a *member-declaration* ([[class.mem]]).
|
| 1434 |
|
| 1435 |
+
In a *function-definition*, either `void` *declarator* `;` or
|
| 1436 |
+
*declarator* `;` shall be a well-formed function declaration as
|
| 1437 |
+
described in [[dcl.fct]]. A function shall be defined only in namespace
|
| 1438 |
+
or class scope.
|
| 1439 |
|
| 1440 |
+
[*Example 1*:
|
|
|
|
| 1441 |
|
| 1442 |
+
A simple example of a complete function definition is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1443 |
|
| 1444 |
``` cpp
|
| 1445 |
int max(int a, int b, int c) {
|
| 1446 |
int m = (a > b) ? a : b;
|
| 1447 |
return (m > c) ? m : c;
|
|
|
|
| 1449 |
```
|
| 1450 |
|
| 1451 |
Here `int` is the *decl-specifier-seq*; `max(int` `a,` `int` `b,` `int`
|
| 1452 |
`c)` is the *declarator*; `{ /* ... */ }` is the *function-body*.
|
| 1453 |
|
| 1454 |
+
— *end example*]
|
| 1455 |
+
|
| 1456 |
A *ctor-initializer* is used only in a constructor; see [[class.ctor]]
|
| 1457 |
and [[class.init]].
|
| 1458 |
|
| 1459 |
+
[*Note 1*: A *cv-qualifier-seq* affects the type of `this` in the body
|
| 1460 |
+
of a member function; see [[dcl.ref]]. — *end note*]
|
| 1461 |
+
|
| 1462 |
+
[*Note 2*:
|
| 1463 |
|
| 1464 |
Unused parameters need not be named. For example,
|
| 1465 |
|
| 1466 |
``` cpp
|
| 1467 |
void print(int a, int) {
|
| 1468 |
std::printf("a = %d\n",a);
|
| 1469 |
}
|
| 1470 |
```
|
| 1471 |
|
| 1472 |
+
— *end note*]
|
| 1473 |
+
|
| 1474 |
In the *function-body*, a *function-local predefined variable* denotes a
|
| 1475 |
block-scope object of static storage duration that is implicitly defined
|
| 1476 |
(see [[basic.scope.block]]).
|
| 1477 |
|
| 1478 |
The function-local predefined variable `__func__` is defined as if a
|
|
|
|
| 1482 |
static const char __func__[] = "function-name";
|
| 1483 |
```
|
| 1484 |
|
| 1485 |
had been provided, where *function-name* is an *implementation-defined*
|
| 1486 |
string. It is unspecified whether such a variable has an address
|
| 1487 |
+
distinct from that of any other object in the program.[^11]
|
| 1488 |
+
|
| 1489 |
+
[*Example 2*:
|
| 1490 |
|
| 1491 |
``` cpp
|
| 1492 |
struct S {
|
| 1493 |
S() : s(__func__) { } // OK
|
| 1494 |
const char* s;
|
| 1495 |
};
|
| 1496 |
void f(const char* s = __func__); // error: __func__ is undeclared
|
| 1497 |
```
|
| 1498 |
|
| 1499 |
+
— *end example*]
|
| 1500 |
+
|
| 1501 |
### Explicitly-defaulted functions <a id="dcl.fct.def.default">[[dcl.fct.def.default]]</a>
|
| 1502 |
|
| 1503 |
A function definition of the form:
|
| 1504 |
|
| 1505 |
``` bnf
|
|
|
|
| 1515 |
copy assignment operator, the parameter type may be “reference to
|
| 1516 |
non-const `T`”, where `T` is the name of the member function’s class)
|
| 1517 |
as if it had been implicitly declared, and
|
| 1518 |
- not have default arguments.
|
| 1519 |
|
| 1520 |
+
An explicitly-defaulted function that is not defined as deleted may be
|
| 1521 |
+
declared `constexpr` only if it would have been implicitly declared as
|
| 1522 |
+
`constexpr`. If a function is explicitly defaulted on its first
|
| 1523 |
+
declaration, it is implicitly considered to be `constexpr` if the
|
| 1524 |
+
implicit declaration would be.
|
| 1525 |
|
| 1526 |
+
If a function that is explicitly defaulted is declared with a
|
| 1527 |
+
*noexcept-specifier* that does not produce the same exception
|
| 1528 |
+
specification as the implicit declaration ([[except.spec]]), then
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1529 |
|
| 1530 |
- if the function is explicitly defaulted on its first declaration, it
|
| 1531 |
is defined as deleted;
|
| 1532 |
- otherwise, the program is ill-formed.
|
| 1533 |
|
| 1534 |
+
[*Example 1*:
|
| 1535 |
+
|
| 1536 |
``` cpp
|
| 1537 |
struct S {
|
| 1538 |
constexpr S() = default; // ill-formed: implicit S() is not constexpr
|
| 1539 |
S(int a = 0) = default; // ill-formed: default argument
|
| 1540 |
void operator=(const S&) = default; // ill-formed: non-matching return type
|
| 1541 |
+
~S() noexcept(false) = default; // deleted: exception specification does not match
|
| 1542 |
private:
|
| 1543 |
int i;
|
| 1544 |
S(S&); // OK: private copy constructor
|
| 1545 |
};
|
| 1546 |
S::S(S&) = default; // OK: defines copy constructor
|
| 1547 |
```
|
| 1548 |
|
| 1549 |
+
— *end example*]
|
| 1550 |
+
|
| 1551 |
Explicitly-defaulted functions and implicitly-declared functions are
|
| 1552 |
collectively called *defaulted* functions, and the implementation shall
|
| 1553 |
provide implicit definitions for them ([[class.ctor]] [[class.dtor]],
|
| 1554 |
[[class.copy]]), which might mean defining them as deleted. A function
|
| 1555 |
is *user-provided* if it is user-declared and not explicitly defaulted
|
| 1556 |
or deleted on its first declaration. A user-provided
|
| 1557 |
explicitly-defaulted function (i.e., explicitly defaulted after its
|
| 1558 |
first declaration) is defined at the point where it is explicitly
|
| 1559 |
defaulted; if such a function is implicitly defined as deleted, the
|
| 1560 |
+
program is ill-formed.
|
| 1561 |
+
|
| 1562 |
+
[*Note 1*: Declaring a function as defaulted after its first
|
| 1563 |
declaration can provide efficient execution and concise definition while
|
| 1564 |
+
enabling a stable binary interface to an evolving code
|
| 1565 |
+
base. — *end note*]
|
| 1566 |
+
|
| 1567 |
+
[*Example 2*:
|
| 1568 |
|
| 1569 |
``` cpp
|
| 1570 |
struct trivial {
|
| 1571 |
trivial() = default;
|
| 1572 |
trivial(const trivial&) = default;
|
|
|
|
| 1580 |
nontrivial1();
|
| 1581 |
};
|
| 1582 |
nontrivial1::nontrivial1() = default; // not first declaration
|
| 1583 |
```
|
| 1584 |
|
| 1585 |
+
— *end example*]
|
| 1586 |
+
|
| 1587 |
### Deleted definitions <a id="dcl.fct.def.delete">[[dcl.fct.def.delete]]</a>
|
| 1588 |
|
| 1589 |
A function definition of the form:
|
| 1590 |
|
| 1591 |
``` bnf
|
|
|
|
| 1594 |
|
| 1595 |
is called a *deleted definition*. A function with a deleted definition
|
| 1596 |
is also called a *deleted function*.
|
| 1597 |
|
| 1598 |
A program that refers to a deleted function implicitly or explicitly,
|
| 1599 |
+
other than to declare it, is ill-formed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1600 |
|
| 1601 |
+
[*Note 1*: This includes calling the function implicitly or explicitly
|
| 1602 |
+
and forming a pointer or pointer-to-member to the function. It applies
|
| 1603 |
+
even for references in expressions that are not potentially-evaluated.
|
| 1604 |
+
If a function is overloaded, it is referenced only if the function is
|
| 1605 |
+
selected by overload resolution. The implicit odr-use (
|
| 1606 |
+
[[basic.def.odr]]) of a virtual function does not, by itself, constitute
|
| 1607 |
+
a reference. — *end note*]
|
| 1608 |
+
|
| 1609 |
+
[*Example 1*:
|
| 1610 |
+
|
| 1611 |
+
One can enforce non-default-initialization and non-integral
|
| 1612 |
initialization with
|
| 1613 |
|
| 1614 |
``` cpp
|
| 1615 |
struct onlydouble {
|
| 1616 |
onlydouble() = delete; // OK, but redundant
|
| 1617 |
onlydouble(std::intmax_t) = delete;
|
| 1618 |
onlydouble(double);
|
| 1619 |
};
|
| 1620 |
```
|
| 1621 |
|
| 1622 |
+
— *end example*]
|
| 1623 |
+
|
| 1624 |
+
[*Example 2*:
|
| 1625 |
+
|
| 1626 |
+
One can prevent use of a class in certain *new-expression*s by using
|
| 1627 |
deleted definitions of a user-declared `operator new` for that class.
|
| 1628 |
|
| 1629 |
``` cpp
|
| 1630 |
struct sometype {
|
| 1631 |
void* operator new(std::size_t) = delete;
|
|
|
|
| 1633 |
};
|
| 1634 |
sometype* p = new sometype; // error, deleted class operator new
|
| 1635 |
sometype* q = new sometype[3]; // error, deleted class operator new[]
|
| 1636 |
```
|
| 1637 |
|
| 1638 |
+
— *end example*]
|
| 1639 |
+
|
| 1640 |
+
[*Example 3*:
|
| 1641 |
+
|
| 1642 |
One can make a class uncopyable, i.e. move-only, by using deleted
|
| 1643 |
definitions of the copy constructor and copy assignment operator, and
|
| 1644 |
then providing defaulted definitions of the move constructor and move
|
| 1645 |
assignment operator.
|
| 1646 |
|
|
|
|
| 1655 |
};
|
| 1656 |
moveonly* p;
|
| 1657 |
moveonly q(*p); // error, deleted copy constructor
|
| 1658 |
```
|
| 1659 |
|
| 1660 |
+
— *end example*]
|
| 1661 |
+
|
| 1662 |
+
A deleted function is implicitly an inline function ([[dcl.inline]]).
|
| 1663 |
+
|
| 1664 |
+
[*Note 2*: The one-definition rule ([[basic.def.odr]]) applies to
|
| 1665 |
+
deleted definitions. — *end note*]
|
| 1666 |
+
|
| 1667 |
+
A deleted definition of a function shall be the first declaration of the
|
| 1668 |
+
function or, for an explicit specialization of a function template, the
|
| 1669 |
+
first declaration of that specialization. An implicitly declared
|
| 1670 |
+
allocation or deallocation function ([[basic.stc.dynamic]]) shall not
|
| 1671 |
+
be defined as deleted.
|
| 1672 |
+
|
| 1673 |
+
[*Example 4*:
|
| 1674 |
|
| 1675 |
``` cpp
|
| 1676 |
struct sometype {
|
| 1677 |
sometype();
|
| 1678 |
};
|
| 1679 |
sometype::sometype() = delete; // ill-formed; not first declaration
|
| 1680 |
```
|
| 1681 |
|
| 1682 |
+
— *end example*]
|
| 1683 |
+
|
| 1684 |
+
## Structured binding declarations <a id="dcl.struct.bind">[[dcl.struct.bind]]</a>
|
| 1685 |
+
|
| 1686 |
+
A structured binding declaration introduces the *identifier*s `v`₀,
|
| 1687 |
+
`v`₁, `v`₂, ... of the *identifier-list* as names (
|
| 1688 |
+
[[basic.scope.declarative]]), called *structured binding*s. Let cv
|
| 1689 |
+
denote the *cv-qualifier*s in the *decl-specifier-seq*. First, a
|
| 1690 |
+
variable with a unique name `e` is introduced. If the
|
| 1691 |
+
*assignment-expression* in the *initializer* has array type `A` and no
|
| 1692 |
+
*ref-qualifier* is present, `e` has type cv `A` and each element is
|
| 1693 |
+
copy-initialized or direct-initialized from the corresponding element of
|
| 1694 |
+
the *assignment-expression* as specified by the form of the
|
| 1695 |
+
*initializer*. Otherwise, `e` is defined as-if by
|
| 1696 |
+
|
| 1697 |
+
``` bnf
|
| 1698 |
+
attribute-specifier-seqₒₚₜ decl-specifier-seq ref-qualifierₒₚₜ 'e' initializer ';'
|
| 1699 |
+
```
|
| 1700 |
+
|
| 1701 |
+
where the declaration is never interpreted as a function declaration and
|
| 1702 |
+
the parts of the declaration other than the *declarator-id* are taken
|
| 1703 |
+
from the corresponding structured binding declaration. The type of the
|
| 1704 |
+
*id-expression* `e` is called `E`.
|
| 1705 |
+
|
| 1706 |
+
[*Note 1*: `E` is never a reference type (Clause
|
| 1707 |
+
[[expr]]). — *end note*]
|
| 1708 |
+
|
| 1709 |
+
If `E` is an array type with element type `T`, the number of elements in
|
| 1710 |
+
the *identifier-list* shall be equal to the number of elements of `E`.
|
| 1711 |
+
Each `v`ᵢ is the name of an lvalue that refers to the element i of the
|
| 1712 |
+
array and whose type is `T`; the referenced type is `T`.
|
| 1713 |
+
|
| 1714 |
+
[*Note 2*: The top-level cv-qualifiers of `T` are cv. — *end note*]
|
| 1715 |
+
|
| 1716 |
+
[*Example 1*:
|
| 1717 |
+
|
| 1718 |
+
``` cpp
|
| 1719 |
+
auto f() -> int(&)[2];
|
| 1720 |
+
auto [ x, y ] = f(); // x and y refer to elements in a copy of the array return value
|
| 1721 |
+
auto& [ xr, yr ] = f(); // xr and yr refer to elements in the array referred to by f's return value
|
| 1722 |
+
```
|
| 1723 |
+
|
| 1724 |
+
— *end example*]
|
| 1725 |
+
|
| 1726 |
+
Otherwise, if the *qualified-id* `std::tuple_size<E>` names a complete
|
| 1727 |
+
type, the expression `std::tuple_size<E>::value` shall be a well-formed
|
| 1728 |
+
integral constant expression and the number of elements in the
|
| 1729 |
+
*identifier-list* shall be equal to the value of that expression. The
|
| 1730 |
+
*unqualified-id* `get` is looked up in the scope of `E` by class member
|
| 1731 |
+
access lookup ([[basic.lookup.classref]]), and if that finds at least
|
| 1732 |
+
one declaration, the initializer is `e.get<i>()`. Otherwise, the
|
| 1733 |
+
initializer is `get<i>(e)`, where `get` is looked up in the associated
|
| 1734 |
+
namespaces ([[basic.lookup.argdep]]). In either case, `get<i>` is
|
| 1735 |
+
interpreted as a *template-id*.
|
| 1736 |
+
|
| 1737 |
+
[*Note 3*: Ordinary unqualified lookup ([[basic.lookup.unqual]]) is
|
| 1738 |
+
not performed. — *end note*]
|
| 1739 |
+
|
| 1740 |
+
In either case, `e` is an lvalue if the type of the entity `e` is an
|
| 1741 |
+
lvalue reference and an xvalue otherwise. Given the type `Tᵢ` designated
|
| 1742 |
+
by `std::tuple_element<i, E>::type`, each `v`ᵢ is a variable of type
|
| 1743 |
+
“reference to `Tᵢ`” initialized with the initializer, where the
|
| 1744 |
+
reference is an lvalue reference if the initializer is an lvalue and an
|
| 1745 |
+
rvalue reference otherwise; the referenced type is `Tᵢ`.
|
| 1746 |
+
|
| 1747 |
+
Otherwise, all of `E`’s non-static data members shall be public direct
|
| 1748 |
+
members of `E` or of the same unambiguous public base class of `E`, `E`
|
| 1749 |
+
shall not have an anonymous union member, and the number of elements in
|
| 1750 |
+
the *identifier-list* shall be equal to the number of non-static data
|
| 1751 |
+
members of `E`. Designating the non-static data members of `E` as `m`₀,
|
| 1752 |
+
`m`₁, `m`₂, ... (in declaration order), each `v`ᵢ is the name of an
|
| 1753 |
+
lvalue that refers to the member `m`ᵢ of `e` and whose type is cv `Tᵢ`,
|
| 1754 |
+
where `Tᵢ` is the declared type of that member; the referenced type is
|
| 1755 |
+
cv `Tᵢ`. The lvalue is a bit-field if that member is a bit-field.
|
| 1756 |
+
|
| 1757 |
+
[*Example 2*:
|
| 1758 |
+
|
| 1759 |
+
``` cpp
|
| 1760 |
+
struct S { int x1 : 2; volatile double y1; };
|
| 1761 |
+
S f();
|
| 1762 |
+
const auto [ x, y ] = f();
|
| 1763 |
+
```
|
| 1764 |
+
|
| 1765 |
+
The type of the *id-expression* `x` is “`const int`”, the type of the
|
| 1766 |
+
*id-expression* `y` is “`const volatile double`”.
|
| 1767 |
+
|
| 1768 |
+
— *end example*]
|
| 1769 |
+
|
| 1770 |
## Initializers <a id="dcl.init">[[dcl.init]]</a>
|
| 1771 |
|
| 1772 |
A declarator can specify an initial value for the identifier being
|
| 1773 |
declared. The identifier designates a variable being initialized. The
|
| 1774 |
process of initialization described in the remainder of [[dcl.init]]
|
| 1775 |
applies also to initializations specified by other syntactic contexts,
|
| 1776 |
+
such as the initialization of function parameters ([[expr.call]]) or
|
| 1777 |
+
the initialization of return values ([[stmt.return]]).
|
|
|
|
| 1778 |
|
| 1779 |
``` bnf
|
| 1780 |
initializer:
|
| 1781 |
brace-or-equal-initializer
|
| 1782 |
'(' expression-list ')'
|
|
|
|
| 1804 |
braced-init-list:
|
| 1805 |
'{' initializer-list ','ₒₚₜ '}'
|
| 1806 |
'{' '}'
|
| 1807 |
```
|
| 1808 |
|
| 1809 |
+
``` bnf
|
| 1810 |
+
expr-or-braced-init-list:
|
| 1811 |
+
expression
|
| 1812 |
+
braced-init-list
|
| 1813 |
+
```
|
| 1814 |
+
|
| 1815 |
Except for objects declared with the `constexpr` specifier, for which
|
| 1816 |
see [[dcl.constexpr]], an *initializer* in the definition of a variable
|
| 1817 |
can consist of arbitrary expressions involving literals and previously
|
| 1818 |
declared variables and functions, regardless of the variable’s storage
|
| 1819 |
duration.
|
| 1820 |
|
| 1821 |
+
[*Example 1*:
|
| 1822 |
+
|
| 1823 |
``` cpp
|
| 1824 |
int f(int);
|
| 1825 |
int a = 2;
|
| 1826 |
int b = f(a);
|
| 1827 |
int c(b);
|
| 1828 |
```
|
| 1829 |
|
| 1830 |
+
— *end example*]
|
| 1831 |
|
| 1832 |
+
[*Note 1*: Default arguments are more restricted; see
|
| 1833 |
+
[[dcl.fct.default]]. — *end note*]
|
| 1834 |
+
|
| 1835 |
+
[*Note 2*: The order of initialization of variables with static storage
|
| 1836 |
+
duration is described in [[basic.start]] and
|
| 1837 |
+
[[stmt.dcl]]. — *end note*]
|
| 1838 |
|
| 1839 |
A declaration of a block-scope variable with external or internal
|
| 1840 |
linkage that has an *initializer* is ill-formed.
|
| 1841 |
|
| 1842 |
To *zero-initialize* an object or reference of type `T` means:
|
| 1843 |
|
| 1844 |
- if `T` is a scalar type ([[basic.types]]), the object is initialized
|
| 1845 |
to the value obtained by converting the integer literal `0` (zero) to
|
| 1846 |
+
`T`;[^12]
|
| 1847 |
- if `T` is a (possibly cv-qualified) non-union class type, each
|
| 1848 |
+
non-static data member, each non-virtual base class subobject, and, if
|
| 1849 |
+
the object is not a base class subobject, each virtual base class
|
| 1850 |
+
subobject is zero-initialized and padding is initialized to zero bits;
|
| 1851 |
- if `T` is a (possibly cv-qualified) union type, the object’s first
|
| 1852 |
non-static named data member is zero-initialized and padding is
|
| 1853 |
initialized to zero bits;
|
| 1854 |
- if `T` is an array type, each element is zero-initialized;
|
| 1855 |
- if `T` is a reference type, no initialization is performed.
|
| 1856 |
|
| 1857 |
To *default-initialize* an object of type `T` means:
|
| 1858 |
|
| 1859 |
+
- If `T` is a (possibly cv-qualified) class type (Clause [[class]]),
|
| 1860 |
+
constructors are considered. The applicable constructors are
|
| 1861 |
+
enumerated ([[over.match.ctor]]), and the best one for the
|
| 1862 |
+
*initializer* `()` is chosen through overload resolution (
|
| 1863 |
+
[[over.match]]). The constructor thus selected is called, with an
|
| 1864 |
+
empty argument list, to initialize the object.
|
| 1865 |
+
- If `T` is an array type, each element is default-initialized.
|
| 1866 |
+
- Otherwise, no initialization is performed.
|
| 1867 |
|
| 1868 |
+
A class type `T` is *const-default-constructible* if
|
| 1869 |
+
default-initialization of `T` would invoke a user-provided constructor
|
| 1870 |
+
of `T` (not inherited from a base class) or if
|
| 1871 |
+
|
| 1872 |
+
- each direct non-variant non-static data member `M` of `T` has a
|
| 1873 |
+
default member initializer or, if `M` is of class type `X` (or array
|
| 1874 |
+
thereof), `X` is const-default-constructible,
|
| 1875 |
+
- if `T` is a union with at least one non-static data member, exactly
|
| 1876 |
+
one variant member has a default member initializer,
|
| 1877 |
+
- if `T` is not a union, for each anonymous union member with at least
|
| 1878 |
+
one non-static data member (if any), exactly one non-static data
|
| 1879 |
+
member has a default member initializer, and
|
| 1880 |
+
- each potentially constructed base class of `T` is
|
| 1881 |
+
const-default-constructible.
|
| 1882 |
+
|
| 1883 |
+
If a program calls for the default-initialization of an object of a
|
| 1884 |
+
const-qualified type `T`, `T` shall be a const-default-constructible
|
| 1885 |
+
class type or array thereof.
|
| 1886 |
|
| 1887 |
To *value-initialize* an object of type `T` means:
|
| 1888 |
|
| 1889 |
- if `T` is a (possibly cv-qualified) class type (Clause [[class]])
|
| 1890 |
with either no default constructor ([[class.ctor]]) or a default
|
|
|
|
| 1896 |
and if `T` has a non-trivial default constructor, the object is
|
| 1897 |
default-initialized;
|
| 1898 |
- if `T` is an array type, then each element is value-initialized;
|
| 1899 |
- otherwise, the object is zero-initialized.
|
| 1900 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1901 |
A program that calls for default-initialization or value-initialization
|
| 1902 |
of an entity of reference type is ill-formed.
|
| 1903 |
|
| 1904 |
+
[*Note 3*: Every object of static storage duration is zero-initialized
|
| 1905 |
+
at program startup before any other initialization takes place. In some
|
| 1906 |
+
cases, additional initialization is done later. — *end note*]
|
| 1907 |
|
| 1908 |
An object whose initializer is an empty set of parentheses, i.e., `()`,
|
| 1909 |
shall be value-initialized.
|
| 1910 |
|
| 1911 |
+
[*Note 4*:
|
| 1912 |
+
|
| 1913 |
Since `()` is not permitted by the syntax for *initializer*,
|
| 1914 |
|
| 1915 |
``` cpp
|
| 1916 |
X a();
|
| 1917 |
```
|
|
|
|
| 1919 |
is not the declaration of an object of class `X`, but the declaration of
|
| 1920 |
a function taking no argument and returning an `X`. The form `()` is
|
| 1921 |
permitted in certain other initialization contexts ([[expr.new]],
|
| 1922 |
[[expr.type.conv]], [[class.base.init]]).
|
| 1923 |
|
| 1924 |
+
— *end note*]
|
| 1925 |
+
|
| 1926 |
If no initializer is specified for an object, the object is
|
| 1927 |
default-initialized. When storage for an object with automatic or
|
| 1928 |
dynamic storage duration is obtained, the object has an *indeterminate
|
| 1929 |
value*, and if no initialization is performed for the object, that
|
| 1930 |
object retains an indeterminate value until that value is replaced (
|
| 1931 |
+
[[expr.ass]]).
|
| 1932 |
+
|
| 1933 |
+
[*Note 5*: Objects with static or thread storage duration are
|
| 1934 |
+
zero-initialized, see [[basic.start.static]]. — *end note*]
|
| 1935 |
+
|
| 1936 |
+
If an indeterminate value is produced by an evaluation, the behavior is
|
| 1937 |
+
undefined except in the following cases:
|
| 1938 |
|
| 1939 |
- If an indeterminate value of unsigned narrow character type (
|
| 1940 |
+
[[basic.fundamental]]) or `std::byte` type ([[cstddef.syn]]) is
|
| 1941 |
+
produced by the evaluation of:
|
| 1942 |
- the second or third operand of a conditional expression (
|
| 1943 |
[[expr.cond]]),
|
| 1944 |
- the right operand of a comma expression ([[expr.comma]]),
|
| 1945 |
+
- the operand of a cast or conversion ([[conv.integral]],
|
| 1946 |
+
[[expr.type.conv]], [[expr.static.cast]], [[expr.cast]]) to an
|
| 1947 |
+
unsigned narrow character type or `std::byte` type (
|
| 1948 |
+
[[cstddef.syn]]), or
|
| 1949 |
- a discarded-value expression (Clause [[expr]]),
|
| 1950 |
|
| 1951 |
then the result of the operation is an indeterminate value.
|
| 1952 |
+
- If an indeterminate value of unsigned narrow character type or
|
| 1953 |
+
`std::byte` type is produced by the evaluation of the right operand of
|
| 1954 |
+
a simple assignment operator ([[expr.ass]]) whose first operand is an
|
| 1955 |
+
lvalue of unsigned narrow character type or `std::byte` type, an
|
| 1956 |
+
indeterminate value replaces the value of the object referred to by
|
| 1957 |
+
the left operand.
|
| 1958 |
- If an indeterminate value of unsigned narrow character type is
|
| 1959 |
produced by the evaluation of the initialization expression when
|
| 1960 |
initializing an object of unsigned narrow character type, that object
|
| 1961 |
is initialized to an indeterminate value.
|
| 1962 |
+
- If an indeterminate value of unsigned narrow character type or
|
| 1963 |
+
`std::byte` type is produced by the evaluation of the initialization
|
| 1964 |
+
expression when initializing an object of `std::byte` type, that
|
| 1965 |
+
object is initialized to an indeterminate value.
|
| 1966 |
+
|
| 1967 |
+
[*Example 2*:
|
| 1968 |
|
| 1969 |
``` cpp
|
| 1970 |
int f(bool b) {
|
| 1971 |
unsigned char c;
|
| 1972 |
unsigned char d = c; // OK, d has an indeterminate value
|
| 1973 |
int e = d; // undefined behavior
|
| 1974 |
return b ? d : 0; // undefined behavior if b is true
|
| 1975 |
}
|
| 1976 |
```
|
| 1977 |
|
| 1978 |
+
— *end example*]
|
| 1979 |
+
|
| 1980 |
An initializer for a static member is in the scope of the member’s
|
| 1981 |
class.
|
| 1982 |
|
| 1983 |
+
[*Example 3*:
|
| 1984 |
+
|
| 1985 |
``` cpp
|
| 1986 |
int a;
|
| 1987 |
|
| 1988 |
struct X {
|
| 1989 |
static int a;
|
|
|
|
| 1992 |
|
| 1993 |
int X::a = 1;
|
| 1994 |
int X::b = a; // X::b = X::a
|
| 1995 |
```
|
| 1996 |
|
| 1997 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1998 |
|
| 1999 |
+
If the entity being initialized does not have class type, the
|
| 2000 |
+
*expression-list* in a parenthesized initializer shall be a single
|
| 2001 |
+
expression.
|
| 2002 |
|
| 2003 |
+
The initialization that occurs in the `=` form of a
|
| 2004 |
+
*brace-or-equal-initializer* or *condition* ([[stmt.select]]), as well
|
| 2005 |
+
as in argument passing, function return, throwing an exception (
|
|
|
|
|
|
|
| 2006 |
[[except.throw]]), handling an exception ([[except.handle]]), and
|
| 2007 |
+
aggregate member initialization ([[dcl.init.aggr]]), is called
|
| 2008 |
+
*copy-initialization*.
|
| 2009 |
+
|
| 2010 |
+
[*Note 6*: Copy-initialization may invoke a move (
|
| 2011 |
+
[[class.copy]]). — *end note*]
|
| 2012 |
|
| 2013 |
The initialization that occurs in the forms
|
| 2014 |
|
| 2015 |
``` cpp
|
| 2016 |
T x(a);
|
| 2017 |
T x{a};
|
| 2018 |
```
|
| 2019 |
|
| 2020 |
+
as well as in `new` expressions ([[expr.new]]), `static_cast`
|
| 2021 |
+
expressions ([[expr.static.cast]]), functional notation type
|
| 2022 |
+
conversions ([[expr.type.conv]]), *mem-initializer*s (
|
| 2023 |
+
[[class.base.init]]), and the *braced-init-list* form of a *condition*
|
| 2024 |
+
is called *direct-initialization*.
|
| 2025 |
|
| 2026 |
The semantics of initializers are as follows. The *destination type* is
|
| 2027 |
the type of the object or reference being initialized and the *source
|
| 2028 |
type* is the type of the initializer expression. If the initializer is
|
| 2029 |
not a single (possibly parenthesized) expression, the source type is not
|
| 2030 |
defined.
|
| 2031 |
|
| 2032 |
+
- If the initializer is a (non-parenthesized) *braced-init-list* or is
|
| 2033 |
+
`=` *braced-init-list*, the object or reference is list-initialized (
|
| 2034 |
+
[[dcl.init.list]]).
|
| 2035 |
- If the destination type is a reference type, see [[dcl.init.ref]].
|
| 2036 |
- If the destination type is an array of characters, an array of
|
| 2037 |
`char16_t`, an array of `char32_t`, or an array of `wchar_t`, and the
|
| 2038 |
initializer is a string literal, see [[dcl.init.string]].
|
| 2039 |
- If the initializer is `()`, the object is value-initialized.
|
| 2040 |
- Otherwise, if the destination type is an array, the program is
|
| 2041 |
ill-formed.
|
| 2042 |
- If the destination type is a (possibly cv-qualified) class type:
|
| 2043 |
+
- If the initializer expression is a prvalue and the cv-unqualified
|
| 2044 |
+
version of the source type is the same class as the class of the
|
| 2045 |
+
destination, the initializer expression is used to initialize the
|
| 2046 |
+
destination object. \[*Example 4*: `T x = T(T(T()));` calls the `T`
|
| 2047 |
+
default constructor to initialize `x`. — *end example*]
|
| 2048 |
+
- Otherwise, if the initialization is direct-initialization, or if it
|
| 2049 |
+
is copy-initialization where the cv-unqualified version of the
|
| 2050 |
+
source type is the same class as, or a derived class of, the class
|
| 2051 |
+
of the destination, constructors are considered. The applicable
|
| 2052 |
constructors are enumerated ([[over.match.ctor]]), and the best one
|
| 2053 |
is chosen through overload resolution ([[over.match]]). The
|
| 2054 |
constructor so selected is called to initialize the object, with the
|
| 2055 |
initializer expression or *expression-list* as its argument(s). If
|
| 2056 |
no constructor applies, or the overload resolution is ambiguous, the
|
|
|
|
| 2061 |
to a derived class thereof are enumerated as described in
|
| 2062 |
[[over.match.copy]], and the best one is chosen through overload
|
| 2063 |
resolution ([[over.match]]). If the conversion cannot be done or is
|
| 2064 |
ambiguous, the initialization is ill-formed. The function selected
|
| 2065 |
is called with the initializer expression as its argument; if the
|
| 2066 |
+
function is a constructor, the call is a prvalue of the
|
| 2067 |
+
cv-unqualified version of the destination type whose result object
|
| 2068 |
+
is initialized by the constructor. The call is used to
|
| 2069 |
+
direct-initialize, according to the rules above, the object that is
|
| 2070 |
+
the destination of the copy-initialization.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2071 |
- Otherwise, if the source type is a (possibly cv-qualified) class type,
|
| 2072 |
conversion functions are considered. The applicable conversion
|
| 2073 |
functions are enumerated ([[over.match.conv]]), and the best one is
|
| 2074 |
chosen through overload resolution ([[over.match]]). The user-defined
|
| 2075 |
conversion so selected is called to convert the initializer expression
|
|
|
|
| 2078 |
- Otherwise, the initial value of the object being initialized is the
|
| 2079 |
(possibly converted) value of the initializer expression. Standard
|
| 2080 |
conversions (Clause [[conv]]) will be used, if necessary, to convert
|
| 2081 |
the initializer expression to the cv-unqualified version of the
|
| 2082 |
destination type; no user-defined conversions are considered. If the
|
| 2083 |
+
conversion cannot be done, the initialization is ill-formed. When
|
| 2084 |
+
initializing a bit-field with a value that it cannot represent, the
|
| 2085 |
+
resulting value of the bit-field is *implementation-defined*.
|
| 2086 |
+
\[*Note 7*:
|
| 2087 |
+
An expression of type “*cv1* `T`” can initialize an object of type
|
| 2088 |
+
“*cv2* `T`” independently of the cv-qualifiers *cv1* and *cv2*.
|
| 2089 |
``` cpp
|
| 2090 |
int a;
|
| 2091 |
const int b = a;
|
| 2092 |
int c = b;
|
| 2093 |
```
|
| 2094 |
|
| 2095 |
+
— *end note*]
|
| 2096 |
+
|
| 2097 |
An *initializer-clause* followed by an ellipsis is a pack expansion (
|
| 2098 |
[[temp.variadic]]).
|
| 2099 |
|
| 2100 |
+
If the initializer is a parenthesized *expression-list*, the expressions
|
| 2101 |
+
are evaluated in the order specified for function calls (
|
| 2102 |
+
[[expr.call]]).
|
| 2103 |
+
|
| 2104 |
+
An object whose initialization has completed is deemed to be
|
| 2105 |
+
constructed, even if no constructor of the object’s class is invoked for
|
| 2106 |
+
the initialization.
|
| 2107 |
+
|
| 2108 |
+
[*Note 8*: Such an object might have been value-initialized or
|
| 2109 |
+
initialized by aggregate initialization ([[dcl.init.aggr]]) or by an
|
| 2110 |
+
inherited constructor ([[class.inhctor.init]]). — *end note*]
|
| 2111 |
+
|
| 2112 |
+
A declaration that specifies the initialization of a variable, whether
|
| 2113 |
+
from an explicit initializer or by default-initialization, is called the
|
| 2114 |
+
*initializing declaration* of that variable.
|
| 2115 |
+
|
| 2116 |
+
[*Note 9*: In most cases this is the defining declaration (
|
| 2117 |
+
[[basic.def]]) of the variable, but the initializing declaration of a
|
| 2118 |
+
non-inline static data member ([[class.static.data]]) might be the
|
| 2119 |
+
declaration within the class definition and not the definition at
|
| 2120 |
+
namespace scope. — *end note*]
|
| 2121 |
+
|
| 2122 |
### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
|
| 2123 |
|
| 2124 |
+
An *aggregate* is an array or a class (Clause [[class]]) with
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2125 |
|
| 2126 |
+
- no user-provided, `explicit`, or inherited constructors (
|
| 2127 |
+
[[class.ctor]]),
|
| 2128 |
+
- no private or protected non-static data members (Clause
|
| 2129 |
+
[[class.access]]),
|
| 2130 |
+
- no virtual functions ([[class.virtual]]), and
|
| 2131 |
+
- no virtual, private, or protected base classes ([[class.mi]]).
|
| 2132 |
+
|
| 2133 |
+
[*Note 1*: Aggregate initialization does not allow accessing protected
|
| 2134 |
+
and private base class’ members or constructors. — *end note*]
|
| 2135 |
+
|
| 2136 |
+
The *elements* of an aggregate are:
|
| 2137 |
+
|
| 2138 |
+
- for an array, the array elements in increasing subscript order, or
|
| 2139 |
+
- for a class, the direct base classes in declaration order, followed by
|
| 2140 |
+
the direct non-static data members ([[class.mem]]) that are not
|
| 2141 |
+
members of an anonymous union, in declaration order.
|
| 2142 |
+
|
| 2143 |
+
When an aggregate is initialized by an initializer list as specified in
|
| 2144 |
+
[[dcl.init.list]], the elements of the initializer list are taken as
|
| 2145 |
+
initializers for the elements of the aggregate, in order. Each element
|
| 2146 |
+
is copy-initialized from the corresponding *initializer-clause*. If the
|
| 2147 |
+
*initializer-clause* is an expression and a narrowing conversion (
|
| 2148 |
+
[[dcl.init.list]]) is required to convert the expression, the program is
|
| 2149 |
+
ill-formed.
|
| 2150 |
+
|
| 2151 |
+
[*Note 2*: If an *initializer-clause* is itself an initializer list,
|
| 2152 |
+
the element is list-initialized, which will result in a recursive
|
| 2153 |
+
application of the rules in this section if the element is an
|
| 2154 |
+
aggregate. — *end note*]
|
| 2155 |
+
|
| 2156 |
+
[*Example 1*:
|
| 2157 |
|
| 2158 |
``` cpp
|
| 2159 |
struct A {
|
| 2160 |
int x;
|
| 2161 |
struct B {
|
|
|
|
| 2165 |
} a = { 1, { 2, 3 } };
|
| 2166 |
```
|
| 2167 |
|
| 2168 |
initializes `a.x` with 1, `a.b.i` with 2, `a.b.j` with 3.
|
| 2169 |
|
| 2170 |
+
``` cpp
|
| 2171 |
+
struct base1 { int b1, b2 = 42; };
|
| 2172 |
+
struct base2 {
|
| 2173 |
+
base2() {
|
| 2174 |
+
b3 = 42;
|
| 2175 |
+
}
|
| 2176 |
+
int b3;
|
| 2177 |
+
};
|
| 2178 |
+
struct derived : base1, base2 {
|
| 2179 |
+
int d;
|
| 2180 |
+
};
|
| 2181 |
+
|
| 2182 |
+
derived d1{{1, 2}, {}, 4};
|
| 2183 |
+
derived d2{{}, {}, 4};
|
| 2184 |
+
```
|
| 2185 |
+
|
| 2186 |
+
initializes `d1.b1` with 1, `d1.b2` with 2, `d1.b3` with 42, `d1.d` with
|
| 2187 |
+
4, and `d2.b1` with 0, `d2.b2` with 42, `d2.b3` with 42, `d2.d` with 4.
|
| 2188 |
+
|
| 2189 |
+
— *end example*]
|
| 2190 |
+
|
| 2191 |
An aggregate that is a class can also be initialized with a single
|
| 2192 |
expression not enclosed in braces, as described in [[dcl.init]].
|
| 2193 |
|
| 2194 |
+
An array of unknown bound initialized with a brace-enclosed
|
| 2195 |
*initializer-list* containing `n` *initializer-clause*s, where `n` shall
|
| 2196 |
+
be greater than zero, is defined as having `n` elements (
|
| 2197 |
[[dcl.array]]).
|
| 2198 |
|
| 2199 |
+
[*Example 2*:
|
| 2200 |
+
|
| 2201 |
``` cpp
|
| 2202 |
int x[] = { 1, 3, 5 };
|
| 2203 |
```
|
| 2204 |
|
| 2205 |
declares and initializes `x` as a one-dimensional array that has three
|
| 2206 |
elements since no size was specified and there are three initializers.
|
| 2207 |
+
|
| 2208 |
+
— *end example*]
|
| 2209 |
+
|
| 2210 |
An empty initializer list `{}` shall not be used as the
|
| 2211 |
+
*initializer-clause* for an array of unknown bound.[^13]
|
| 2212 |
|
| 2213 |
+
[*Note 3*:
|
| 2214 |
+
|
| 2215 |
+
A default member initializer does not determine the bound for a member
|
| 2216 |
+
array of unknown bound. Since the default member initializer is ignored
|
| 2217 |
+
if a suitable *mem-initializer* is present ([[class.base.init]]), the
|
| 2218 |
+
default member initializer is not considered to initialize the array of
|
| 2219 |
+
unknown bound.
|
| 2220 |
+
|
| 2221 |
+
[*Example 3*:
|
| 2222 |
+
|
| 2223 |
+
``` cpp
|
| 2224 |
+
struct S {
|
| 2225 |
+
int y[] = { 0 }; // error: non-static data member of incomplete type
|
| 2226 |
+
};
|
| 2227 |
+
```
|
| 2228 |
+
|
| 2229 |
+
— *end example*]
|
| 2230 |
+
|
| 2231 |
+
— *end note*]
|
| 2232 |
+
|
| 2233 |
+
[*Note 4*:
|
| 2234 |
+
|
| 2235 |
+
Static data members and unnamed bit-fields are not considered elements
|
| 2236 |
+
of the aggregate.
|
| 2237 |
+
|
| 2238 |
+
[*Example 4*:
|
| 2239 |
|
| 2240 |
``` cpp
|
| 2241 |
struct A {
|
| 2242 |
int i;
|
| 2243 |
static int s;
|
|
|
|
| 2247 |
} a = { 1, 2, 3 };
|
| 2248 |
```
|
| 2249 |
|
| 2250 |
Here, the second initializer 2 initializes `a.j` and not the static data
|
| 2251 |
member `A::s`, and the third initializer 3 initializes `a.k` and not the
|
| 2252 |
+
unnamed bit-field before it.
|
| 2253 |
+
|
| 2254 |
+
— *end example*]
|
| 2255 |
+
|
| 2256 |
+
— *end note*]
|
| 2257 |
|
| 2258 |
An *initializer-list* is ill-formed if the number of
|
| 2259 |
+
*initializer-clause*s exceeds the number of elements to initialize.
|
| 2260 |
+
|
| 2261 |
+
[*Example 5*:
|
| 2262 |
|
| 2263 |
``` cpp
|
| 2264 |
char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
|
| 2265 |
```
|
| 2266 |
|
| 2267 |
is ill-formed.
|
| 2268 |
|
| 2269 |
+
— *end example*]
|
| 2270 |
+
|
| 2271 |
If there are fewer *initializer-clause*s in the list than there are
|
| 2272 |
+
elements in a non-union aggregate, then each element not explicitly
|
| 2273 |
+
initialized is initialized as follows:
|
| 2274 |
+
|
| 2275 |
+
- If the element has a default member initializer ([[class.mem]]), the
|
| 2276 |
+
element is initialized from that initializer.
|
| 2277 |
+
- Otherwise, if the element is not a reference, the element is
|
| 2278 |
+
copy-initialized from an empty initializer list ([[dcl.init.list]]).
|
| 2279 |
+
- Otherwise, the program is ill-formed.
|
| 2280 |
+
|
| 2281 |
+
If the aggregate is a union and the initializer list is empty, then
|
| 2282 |
+
|
| 2283 |
+
- if any variant member has a default member initializer, that member is
|
| 2284 |
+
initialized from its default member initializer;
|
| 2285 |
+
- otherwise, the first member of the union (if any) is copy-initialized
|
| 2286 |
+
from an empty initializer list.
|
| 2287 |
+
|
| 2288 |
+
[*Example 6*:
|
| 2289 |
|
| 2290 |
``` cpp
|
| 2291 |
struct S { int a; const char* b; int c; int d = b[a]; };
|
| 2292 |
S ss = { 1, "asdf" };
|
| 2293 |
```
|
|
|
|
| 2302 |
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
|
| 2303 |
```
|
| 2304 |
|
| 2305 |
`a` and `b` have the same value
|
| 2306 |
|
| 2307 |
+
— *end example*]
|
| 2308 |
+
|
| 2309 |
+
If a reference member is initialized from its default member initializer
|
| 2310 |
+
and a potentially-evaluated subexpression thereof is an aggregate
|
| 2311 |
+
initialization that would use that default member initializer, the
|
| 2312 |
+
program is ill-formed.
|
| 2313 |
+
|
| 2314 |
+
[*Example 7*:
|
| 2315 |
+
|
| 2316 |
+
``` cpp
|
| 2317 |
+
struct A;
|
| 2318 |
+
extern A a;
|
| 2319 |
+
struct A {
|
| 2320 |
+
const A& a1 { A{a,a} }; // OK
|
| 2321 |
+
const A& a2 { A{} }; // error
|
| 2322 |
+
};
|
| 2323 |
+
A a{a,a}; // OK
|
| 2324 |
+
```
|
| 2325 |
+
|
| 2326 |
+
— *end example*]
|
| 2327 |
+
|
| 2328 |
+
If an aggregate class `C` contains a subaggregate element `e` with no
|
| 2329 |
+
elements, the *initializer-clause* for `e` shall not be omitted from an
|
| 2330 |
*initializer-list* for an object of type `C` unless the
|
| 2331 |
+
*initializer-clause*s for all elements of `C` following `e` are also
|
| 2332 |
omitted.
|
| 2333 |
|
| 2334 |
+
[*Example 8*:
|
| 2335 |
+
|
| 2336 |
``` cpp
|
| 2337 |
struct S { } s;
|
| 2338 |
struct A {
|
| 2339 |
S s1;
|
| 2340 |
int i1;
|
|
|
|
| 2348 |
s, // Required initialization
|
| 2349 |
0
|
| 2350 |
}; // Initialization not required for A::s3 because A::i3 is also not initialized
|
| 2351 |
```
|
| 2352 |
|
| 2353 |
+
— *end example*]
|
|
|
|
| 2354 |
|
| 2355 |
When initializing a multi-dimensional array, the *initializer-clause*s
|
| 2356 |
initialize the elements with the last (rightmost) index of the array
|
| 2357 |
varying the fastest ([[dcl.array]]).
|
| 2358 |
|
| 2359 |
+
[*Example 9*:
|
| 2360 |
+
|
| 2361 |
``` cpp
|
| 2362 |
int x[2][2] = { 3, 1, 4, 2 };
|
| 2363 |
```
|
| 2364 |
|
| 2365 |
initializes `x[0][0]` to `3`, `x[0][1]` to `1`, `x[1][0]` to `4`, and
|
|
|
|
| 2372 |
```
|
| 2373 |
|
| 2374 |
initializes the first column of `y` (regarded as a two-dimensional
|
| 2375 |
array) and leaves the rest zero.
|
| 2376 |
|
| 2377 |
+
— *end example*]
|
| 2378 |
+
|
| 2379 |
Braces can be elided in an *initializer-list* as follows. If the
|
| 2380 |
*initializer-list* begins with a left brace, then the succeeding
|
| 2381 |
+
comma-separated list of *initializer-clause*s initializes the elements
|
| 2382 |
+
of a subaggregate; it is erroneous for there to be more
|
| 2383 |
+
*initializer-clause*s than elements. If, however, the *initializer-list*
|
| 2384 |
for a subaggregate does not begin with a left brace, then only enough
|
| 2385 |
+
*initializer-clause*s from the list are taken to initialize the elements
|
| 2386 |
of the subaggregate; any remaining *initializer-clause*s are left to
|
| 2387 |
+
initialize the next element of the aggregate of which the current
|
| 2388 |
+
subaggregate is an element.
|
| 2389 |
+
|
| 2390 |
+
[*Example 10*:
|
| 2391 |
|
| 2392 |
``` cpp
|
| 2393 |
float y[4][3] = {
|
| 2394 |
{ 1, 3, 5 },
|
| 2395 |
{ 2, 4, 6 },
|
|
|
|
| 2415 |
|
| 2416 |
The initializer for `y` begins with a left brace, but the one for `y[0]`
|
| 2417 |
does not, therefore three elements from the list are used. Likewise the
|
| 2418 |
next three are taken successively for `y[1]` and `y[2]`.
|
| 2419 |
|
| 2420 |
+
— *end example*]
|
| 2421 |
+
|
| 2422 |
All implicit type conversions (Clause [[conv]]) are considered when
|
| 2423 |
+
initializing the element with an *assignment-expression*. If the
|
| 2424 |
+
*assignment-expression* can initialize an element, the element is
|
| 2425 |
+
initialized. Otherwise, if the element is itself a subaggregate, brace
|
| 2426 |
elision is assumed and the *assignment-expression* is considered for the
|
| 2427 |
+
initialization of the first element of the subaggregate.
|
| 2428 |
+
|
| 2429 |
+
[*Note 5*: As specified above, brace elision cannot apply to
|
| 2430 |
+
subaggregates with no elements; an *initializer-clause* for the entire
|
| 2431 |
+
subobject is required. — *end note*]
|
| 2432 |
+
|
| 2433 |
+
[*Example 11*:
|
| 2434 |
|
| 2435 |
``` cpp
|
| 2436 |
struct A {
|
| 2437 |
int i;
|
| 2438 |
operator int();
|
|
|
|
| 2447 |
|
| 2448 |
Braces are elided around the *initializer-clause* for `b.a1.i`. `b.a1.i`
|
| 2449 |
is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
|
| 2450 |
initialized with whatever `a.operator int()` returns.
|
| 2451 |
|
| 2452 |
+
— *end example*]
|
|
|
|
|
|
|
| 2453 |
|
| 2454 |
+
[*Note 6*: An aggregate array or an aggregate class may contain
|
| 2455 |
+
elements of a class type with a user-provided constructor (
|
| 2456 |
+
[[class.ctor]]). Initialization of these aggregate objects is described
|
| 2457 |
+
in [[class.expl.init]]. — *end note*]
|
| 2458 |
+
|
| 2459 |
+
[*Note 7*: Whether the initialization of aggregates with static storage
|
| 2460 |
+
duration is static or dynamic is specified in [[basic.start.static]],
|
| 2461 |
+
[[basic.start.dynamic]], and [[stmt.dcl]]. — *end note*]
|
| 2462 |
|
| 2463 |
When a union is initialized with a brace-enclosed initializer, the
|
| 2464 |
braces shall only contain an *initializer-clause* for the first
|
| 2465 |
non-static data member of the union.
|
| 2466 |
|
| 2467 |
+
[*Example 12*:
|
| 2468 |
+
|
| 2469 |
``` cpp
|
| 2470 |
union u { int a; const char* b; };
|
| 2471 |
u a = { 1 };
|
| 2472 |
u b = a;
|
| 2473 |
u c = 1; // error
|
| 2474 |
u d = { 0, "asdf" }; // error
|
| 2475 |
u e = { "asdf" }; // error
|
| 2476 |
```
|
| 2477 |
|
| 2478 |
+
— *end example*]
|
| 2479 |
+
|
| 2480 |
+
[*Note 8*: As described above, the braces around the
|
| 2481 |
+
*initializer-clause* for a union member can be omitted if the union is a
|
| 2482 |
+
member of another aggregate. — *end note*]
|
| 2483 |
|
| 2484 |
### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
|
| 2485 |
|
| 2486 |
An array of narrow character type ([[basic.fundamental]]), `char16_t`
|
| 2487 |
array, `char32_t` array, or `wchar_t` array can be initialized by a
|
|
|
|
| 2489 |
literal, or wide string literal, respectively, or by an
|
| 2490 |
appropriately-typed string literal enclosed in braces ([[lex.string]]).
|
| 2491 |
Successive characters of the value of the string literal initialize the
|
| 2492 |
elements of the array.
|
| 2493 |
|
| 2494 |
+
[*Example 1*:
|
| 2495 |
+
|
| 2496 |
``` cpp
|
| 2497 |
char msg[] = "Syntax error on line %s\n";
|
| 2498 |
```
|
| 2499 |
|
| 2500 |
shows a character array whose members are initialized with a
|
| 2501 |
*string-literal*. Note that because `'\n'` is a single character and
|
| 2502 |
because a trailing `'\0'` is appended, `sizeof(msg)` is `25`.
|
| 2503 |
|
| 2504 |
+
— *end example*]
|
| 2505 |
+
|
| 2506 |
There shall not be more initializers than there are array elements.
|
| 2507 |
|
| 2508 |
+
[*Example 2*:
|
| 2509 |
+
|
| 2510 |
``` cpp
|
| 2511 |
char cv[4] = "asdf"; // error
|
| 2512 |
```
|
| 2513 |
|
| 2514 |
is ill-formed since there is no space for the implied trailing `'\0'`.
|
| 2515 |
|
| 2516 |
+
— *end example*]
|
| 2517 |
+
|
| 2518 |
If there are fewer initializers than there are array elements, each
|
| 2519 |
element not explicitly initialized shall be zero-initialized (
|
| 2520 |
[[dcl.init]]).
|
| 2521 |
|
| 2522 |
### References <a id="dcl.init.ref">[[dcl.init.ref]]</a>
|
| 2523 |
|
| 2524 |
+
A variable whose declared type is “reference to type `T`” ([[dcl.ref]])
|
| 2525 |
+
shall be initialized.
|
| 2526 |
+
|
| 2527 |
+
[*Example 1*:
|
| 2528 |
|
| 2529 |
``` cpp
|
| 2530 |
+
int g(int) noexcept;
|
| 2531 |
void f() {
|
| 2532 |
int i;
|
| 2533 |
int& r = i; // r refers to i
|
| 2534 |
r = 1; // the value of i becomes 1
|
| 2535 |
int* p = &r; // p points to i
|
|
|
|
| 2540 |
int (&ra)[3] = a; // ra refers to the array a
|
| 2541 |
ra[1] = i; // modifies a[1]
|
| 2542 |
}
|
| 2543 |
```
|
| 2544 |
|
| 2545 |
+
— *end example*]
|
| 2546 |
+
|
| 2547 |
A reference cannot be changed to refer to another object after
|
| 2548 |
+
initialization.
|
| 2549 |
+
|
| 2550 |
+
[*Note 1*: Assignment to a reference assigns to the object referred to
|
| 2551 |
+
by the reference ([[expr.ass]]). — *end note*]
|
| 2552 |
+
|
| 2553 |
+
Argument passing ([[expr.call]]) and function value return (
|
| 2554 |
+
[[stmt.return]]) are initializations.
|
| 2555 |
|
| 2556 |
The initializer can be omitted for a reference only in a parameter
|
| 2557 |
declaration ([[dcl.fct]]), in the declaration of a function return
|
| 2558 |
type, in the declaration of a class member within its class definition (
|
| 2559 |
[[class.mem]]), and where the `extern` specifier is explicitly used.
|
| 2560 |
|
| 2561 |
+
[*Example 2*:
|
| 2562 |
+
|
| 2563 |
``` cpp
|
| 2564 |
int& r1; // error: initializer missing
|
| 2565 |
extern int& r2; // OK
|
| 2566 |
```
|
| 2567 |
|
| 2568 |
+
— *end example*]
|
| 2569 |
+
|
| 2570 |
+
Given types “*cv1* `T1`” and “*cv2* `T2`”, “*cv1* `T1`” is
|
| 2571 |
+
*reference-related* to “*cv2* `T2`” if `T1` is the same type as `T2`, or
|
| 2572 |
+
`T1` is a base class of `T2`. “*cv1* `T1`” is *reference-compatible*
|
| 2573 |
+
with “*cv2* `T2`” if
|
| 2574 |
+
|
| 2575 |
+
- `T1` is reference-related to `T2`, or
|
| 2576 |
+
- `T2` is “`noexcept` function” and `T1` is “function”, where the
|
| 2577 |
+
function types are otherwise the same,
|
| 2578 |
+
|
| 2579 |
+
and *cv1* is the same cv-qualification as, or greater cv-qualification
|
| 2580 |
+
than, *cv2*. In all cases where the reference-related or
|
| 2581 |
+
reference-compatible relationship of two types is used to establish the
|
| 2582 |
+
validity of a reference binding, and `T1` is a base class of `T2`, a
|
| 2583 |
+
program that necessitates such a binding is ill-formed if `T1` is an
|
| 2584 |
+
inaccessible (Clause [[class.access]]) or ambiguous (
|
| 2585 |
+
[[class.member.lookup]]) base class of `T2`.
|
| 2586 |
|
| 2587 |
A reference to type “*cv1* `T1`” is initialized by an expression of type
|
| 2588 |
“*cv2* `T2`” as follows:
|
| 2589 |
|
| 2590 |
- If the reference is an lvalue reference and the initializer expression
|
| 2591 |
+
- is an lvalue (but is not a bit-field), and “*cv1* `T1`” is
|
| 2592 |
+
reference-compatible with “*cv2* `T2`”, or
|
| 2593 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 2594 |
reference-related to `T2`, and can be converted to an lvalue of type
|
| 2595 |
+
“*cv3* `T3`”, where “*cv1* `T1`” is reference-compatible with “*cv3*
|
| 2596 |
+
`T3`”[^14] (this conversion is selected by enumerating the
|
| 2597 |
+
applicable conversion functions ([[over.match.ref]]) and choosing
|
| 2598 |
+
the best one through overload resolution ([[over.match]])),
|
| 2599 |
|
| 2600 |
then the reference is bound to the initializer expression lvalue in
|
| 2601 |
the first case and to the lvalue result of the conversion in the
|
| 2602 |
second case (or, in either case, to the appropriate base class
|
| 2603 |
+
subobject of the object).
|
| 2604 |
+
\[*Note 2*: The usual lvalue-to-rvalue ([[conv.lval]]),
|
| 2605 |
array-to-pointer ([[conv.array]]), and function-to-pointer (
|
| 2606 |
[[conv.func]]) standard conversions are not needed, and therefore are
|
| 2607 |
+
suppressed, when such direct bindings to lvalues are
|
| 2608 |
+
done. — *end note*]
|
| 2609 |
+
\[*Example 3*:
|
| 2610 |
``` cpp
|
| 2611 |
double d = 2.0;
|
| 2612 |
double& rd = d; // rd refers to d
|
| 2613 |
const double& rcd = d; // rcd refers to d
|
| 2614 |
|
|
|
|
| 2616 |
struct B : A { operator int&(); } b;
|
| 2617 |
A& ra = b; // ra refers to A subobject in b
|
| 2618 |
const A& rca = b; // rca refers to A subobject in b
|
| 2619 |
int& ir = B(); // ir refers to the result of B::operator int&
|
| 2620 |
```
|
| 2621 |
+
|
| 2622 |
+
— *end example*]
|
| 2623 |
- Otherwise, the reference shall be an lvalue reference to a
|
| 2624 |
non-volatile const type (i.e., *cv1* shall be `const`), or the
|
| 2625 |
reference shall be an rvalue reference.
|
| 2626 |
+
\[*Example 4*:
|
| 2627 |
``` cpp
|
| 2628 |
double& rd2 = 2.0; // error: not an lvalue and reference not const
|
| 2629 |
int i = 2;
|
| 2630 |
double& rd3 = i; // error: type mismatch and reference not const
|
| 2631 |
```
|
| 2632 |
|
| 2633 |
+
— *end example*]
|
| 2634 |
- If the initializer expression
|
| 2635 |
+
- is an rvalue (but not a bit-field) or function lvalue and “*cv1*
|
| 2636 |
+
`T1`” is reference-compatible with “*cv2* `T2`”, or
|
|
|
|
| 2637 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 2638 |
+
reference-related to `T2`, and can be converted to an rvalue or
|
| 2639 |
+
function lvalue of type “*cv3* `T3`”, where “*cv1* `T1`” is
|
| 2640 |
+
reference-compatible with “*cv3* `T3`” (see [[over.match.ref]]),
|
|
|
|
| 2641 |
|
| 2642 |
+
then the value of the initializer expression in the first case and
|
| 2643 |
+
the result of the conversion in the second case is called the
|
| 2644 |
+
converted initializer. If the converted initializer is a prvalue,
|
| 2645 |
+
its type `T4` is adjusted to type “*cv1* `T4`” ([[conv.qual]]) and
|
| 2646 |
+
the temporary materialization conversion ([[conv.rval]]) is
|
| 2647 |
+
applied. In any case, the reference is bound to the resulting
|
| 2648 |
+
glvalue (or to an appropriate base class subobject).
|
| 2649 |
+
\[*Example 5*:
|
| 2650 |
``` cpp
|
| 2651 |
struct A { };
|
| 2652 |
struct B : A { } b;
|
| 2653 |
extern B f();
|
| 2654 |
const A& rca2 = f(); // bound to the A subobject of the B rvalue.
|
|
|
|
| 2659 |
} x;
|
| 2660 |
const A& r = x; // bound to the A subobject of the result of the conversion
|
| 2661 |
int i2 = 42;
|
| 2662 |
int&& rri = static_cast<int&&>(i2); // bound directly to i2
|
| 2663 |
B&& rrb = x; // bound directly to the result of operator B
|
|
|
|
|
|
|
| 2664 |
```
|
| 2665 |
+
|
| 2666 |
+
— *end example*]
|
| 2667 |
- Otherwise:
|
| 2668 |
+
- If `T1` or `T2` is a class type and `T1` is not reference-related
|
| 2669 |
+
to `T2`, user-defined conversions are considered using the rules
|
| 2670 |
+
for copy-initialization of an object of type “*cv1* `T1`” by
|
| 2671 |
+
user-defined conversion ([[dcl.init]], [[over.match.copy]],
|
| 2672 |
+
[[over.match.conv]]); the program is ill-formed if the
|
| 2673 |
corresponding non-reference copy-initialization would be
|
| 2674 |
ill-formed. The result of the call to the conversion function, as
|
| 2675 |
described for the non-reference copy-initialization, is then used
|
| 2676 |
+
to direct-initialize the reference. For this
|
| 2677 |
+
direct-initialization, user-defined conversions are not
|
| 2678 |
+
considered.
|
| 2679 |
+
- Otherwise, the initializer expression is implicitly converted to a
|
| 2680 |
+
prvalue of type “*cv1* `T1`”. The temporary materialization
|
| 2681 |
+
conversion is applied and the reference is bound to the result.
|
| 2682 |
|
| 2683 |
If `T1` is reference-related to `T2`:
|
| 2684 |
- *cv1* shall be the same cv-qualification as, or greater
|
| 2685 |
cv-qualification than, *cv2*; and
|
| 2686 |
- if the reference is an rvalue reference, the initializer
|
| 2687 |
expression shall not be an lvalue.
|
| 2688 |
|
| 2689 |
+
\[*Example 6*:
|
| 2690 |
``` cpp
|
| 2691 |
struct Banana { };
|
| 2692 |
struct Enigma { operator const Banana(); };
|
| 2693 |
+
struct Alaska { operator Banana&(); };
|
| 2694 |
void enigmatic() {
|
| 2695 |
typedef const Banana ConstBanana;
|
| 2696 |
Banana &&banana1 = ConstBanana(); // ill-formed
|
| 2697 |
Banana &&banana2 = Enigma(); // ill-formed
|
| 2698 |
+
Banana &&banana3 = Alaska(); // ill-formed
|
| 2699 |
}
|
| 2700 |
|
| 2701 |
const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
|
| 2702 |
double&& rrd = 2; // rrd refers to temporary with value 2.0
|
| 2703 |
const volatile int cvi = 1;
|
| 2704 |
+
const int& r2 = cvi; // error: cv-qualifier dropped
|
| 2705 |
+
struct A { operator volatile int&(); } a;
|
| 2706 |
+
const int& r3 = a; // error: cv-qualifier dropped
|
| 2707 |
+
// from result of conversion function
|
| 2708 |
double d2 = 1.0;
|
| 2709 |
+
double&& rrd2 = d2; // error: initializer is lvalue of related type
|
| 2710 |
+
struct X { operator int&(); };
|
| 2711 |
+
int&& rri2 = X(); // error: result of conversion function is lvalue of related type
|
| 2712 |
int i3 = 2;
|
| 2713 |
double&& rrd3 = i3; // rrd3 refers to temporary with value 2.0
|
| 2714 |
```
|
| 2715 |
|
| 2716 |
+
— *end example*]
|
|
|
|
|
|
|
| 2717 |
|
| 2718 |
+
In all cases except the last (i.e., implicitly converting the
|
| 2719 |
+
initializer expression to the underlying type of the reference), the
|
| 2720 |
+
reference is said to *bind directly* to the initializer expression.
|
| 2721 |
+
|
| 2722 |
+
[*Note 3*: [[class.temporary]] describes the lifetime of temporaries
|
| 2723 |
+
bound to references. — *end note*]
|
| 2724 |
|
| 2725 |
### List-initialization <a id="dcl.init.list">[[dcl.init.list]]</a>
|
| 2726 |
|
| 2727 |
*List-initialization* is initialization of an object or reference from a
|
| 2728 |
*braced-init-list*. Such an initializer is called an *initializer list*,
|
|
|
|
| 2730 |
*elements* of the initializer list. An initializer list may be empty.
|
| 2731 |
List-initialization can occur in direct-initialization or
|
| 2732 |
copy-initialization contexts; list-initialization in a
|
| 2733 |
direct-initialization context is called *direct-list-initialization* and
|
| 2734 |
list-initialization in a copy-initialization context is called
|
| 2735 |
+
*copy-list-initialization*.
|
| 2736 |
+
|
| 2737 |
+
[*Note 1*:
|
| 2738 |
+
|
| 2739 |
+
List-initialization can be used
|
| 2740 |
|
| 2741 |
- as the initializer in a variable definition ([[dcl.init]])
|
| 2742 |
+
- as the initializer in a *new-expression* ([[expr.new]])
|
| 2743 |
- in a return statement ([[stmt.return]])
|
| 2744 |
- as a *for-range-initializer* ([[stmt.iter]])
|
| 2745 |
- as a function argument ([[expr.call]])
|
| 2746 |
- as a subscript ([[expr.sub]])
|
| 2747 |
- as an argument to a constructor invocation ([[dcl.init]],
|
| 2748 |
[[expr.type.conv]])
|
| 2749 |
- as an initializer for a non-static data member ([[class.mem]])
|
| 2750 |
- in a *mem-initializer* ([[class.base.init]])
|
| 2751 |
- on the right-hand side of an assignment ([[expr.ass]])
|
| 2752 |
|
| 2753 |
+
[*Example 1*:
|
| 2754 |
+
|
| 2755 |
``` cpp
|
| 2756 |
int a = {1};
|
| 2757 |
std::complex<double> z{1,2};
|
| 2758 |
new std::vector<std::string>{"once", "upon", "a", "time"}; // 4 string elements
|
| 2759 |
f( {"Nicholas","Annemarie"} ); // pass list of two elements
|
|
|
|
| 2761 |
int* e {}; // initialization to zero / null pointer
|
| 2762 |
x = double{1}; // explicitly construct a double
|
| 2763 |
std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
|
| 2764 |
```
|
| 2765 |
|
| 2766 |
+
— *end example*]
|
| 2767 |
+
|
| 2768 |
+
— *end note*]
|
| 2769 |
+
|
| 2770 |
A constructor is an *initializer-list constructor* if its first
|
| 2771 |
parameter is of type `std::initializer_list<E>` or reference to possibly
|
| 2772 |
cv-qualified `std::initializer_list<E>` for some type `E`, and either
|
| 2773 |
there are no other parameters or else all other parameters have default
|
| 2774 |
+
arguments ([[dcl.fct.default]]).
|
| 2775 |
+
|
| 2776 |
+
[*Note 2*: Initializer-list constructors are favored over other
|
| 2777 |
+
constructors in list-initialization ([[over.match.list]]). Passing an
|
| 2778 |
+
initializer list as the argument to the constructor template
|
| 2779 |
+
`template<class T> C(T)` of a class `C` does not create an
|
| 2780 |
+
initializer-list constructor, because an initializer list argument
|
| 2781 |
+
causes the corresponding parameter to be a non-deduced context (
|
| 2782 |
+
[[temp.deduct.call]]). — *end note*]
|
| 2783 |
+
|
| 2784 |
+
The template `std::initializer_list` is not predefined; if the header
|
| 2785 |
+
`<initializer_list>` is not included prior to a use of
|
| 2786 |
+
`std::initializer_list` — even an implicit use in which the type is not
|
| 2787 |
+
named ([[dcl.spec.auto]]) — the program is ill-formed.
|
| 2788 |
|
| 2789 |
List-initialization of an object or reference of type `T` is defined as
|
| 2790 |
follows:
|
| 2791 |
|
| 2792 |
+
- If `T` is an aggregate class and the initializer list has a single
|
| 2793 |
+
element of type *cv* `U`, where `U` is `T` or a class derived from
|
| 2794 |
+
`T`, the object is initialized from that element (by
|
| 2795 |
+
copy-initialization for copy-list-initialization, or by
|
| 2796 |
+
direct-initialization for direct-list-initialization).
|
| 2797 |
+
- Otherwise, if `T` is a character array and the initializer list has a
|
| 2798 |
+
single element that is an appropriately-typed string literal (
|
| 2799 |
+
[[dcl.init.string]]), initialization is performed as described in that
|
| 2800 |
+
section.
|
| 2801 |
+
- Otherwise, if `T` is an aggregate, aggregate initialization is
|
| 2802 |
+
performed ([[dcl.init.aggr]]).
|
| 2803 |
+
\[*Example 2*:
|
| 2804 |
``` cpp
|
| 2805 |
double ad[] = { 1, 2.0 }; // OK
|
| 2806 |
int ai[] = { 1, 2.0 }; // error: narrowing
|
| 2807 |
|
| 2808 |
struct S2 {
|
|
|
|
| 2811 |
};
|
| 2812 |
S2 s21 = { 1, 2, 3.0 }; // OK
|
| 2813 |
S2 s22 { 1.0, 2, 3 }; // error: narrowing
|
| 2814 |
S2 s23 { }; // OK: default to 0,0,0
|
| 2815 |
```
|
| 2816 |
+
|
| 2817 |
+
— *end example*]
|
| 2818 |
- Otherwise, if the initializer list has no elements and `T` is a class
|
| 2819 |
type with a default constructor, the object is value-initialized.
|
| 2820 |
+
- Otherwise, if `T` is a specialization of `std::initializer_list<E>`,
|
| 2821 |
+
the object is constructed as described below.
|
|
|
|
|
|
|
|
|
|
| 2822 |
- Otherwise, if `T` is a class type, constructors are considered. The
|
| 2823 |
applicable constructors are enumerated and the best one is chosen
|
| 2824 |
through overload resolution ([[over.match]], [[over.match.list]]).
|
| 2825 |
If a narrowing conversion (see below) is required to convert any of
|
| 2826 |
the arguments, the program is ill-formed.
|
| 2827 |
+
\[*Example 3*:
|
| 2828 |
``` cpp
|
| 2829 |
struct S {
|
| 2830 |
S(std::initializer_list<double>); // #1
|
| 2831 |
S(std::initializer_list<int>); // #2
|
| 2832 |
S(); // #3
|
|
|
|
| 2835 |
S s1 = { 1.0, 2.0, 3.0 }; // invoke #1
|
| 2836 |
S s2 = { 1, 2, 3 }; // invoke #2
|
| 2837 |
S s3 = { }; // invoke #3
|
| 2838 |
```
|
| 2839 |
|
| 2840 |
+
— *end example*]
|
| 2841 |
+
\[*Example 4*:
|
| 2842 |
``` cpp
|
| 2843 |
struct Map {
|
| 2844 |
Map(std::initializer_list<std::pair<std::string,int>>);
|
| 2845 |
};
|
| 2846 |
Map ship = {{"Sophie",14}, {"Surprise",28}};
|
| 2847 |
```
|
| 2848 |
|
| 2849 |
+
— *end example*]
|
| 2850 |
+
\[*Example 5*:
|
| 2851 |
``` cpp
|
| 2852 |
struct S {
|
| 2853 |
// no initializer-list constructors
|
| 2854 |
S(int, double, double); // #1
|
| 2855 |
S(); // #2
|
|
|
|
| 2857 |
};
|
| 2858 |
S s1 = { 1, 2, 3.0 }; // OK: invoke #1
|
| 2859 |
S s2 { 1.0, 2, 3 }; // error: narrowing
|
| 2860 |
S s3 { }; // OK: invoke #2
|
| 2861 |
```
|
| 2862 |
+
|
| 2863 |
+
— *end example*]
|
| 2864 |
+
- Otherwise, if `T` is an enumeration with a fixed underlying type (
|
| 2865 |
+
[[dcl.enum]]), the *initializer-list* has a single element `v`, and
|
| 2866 |
+
the initialization is direct-list-initialization, the object is
|
| 2867 |
+
initialized with the value `T(v)` ([[expr.type.conv]]); if a
|
| 2868 |
+
narrowing conversion is required to convert `v` to the underlying type
|
| 2869 |
+
of `T`, the program is ill-formed.
|
| 2870 |
+
\[*Example 6*:
|
| 2871 |
+
``` cpp
|
| 2872 |
+
enum byte : unsigned char { };
|
| 2873 |
+
byte b { 42 }; // OK
|
| 2874 |
+
byte c = { 42 }; // error
|
| 2875 |
+
byte d = byte{ 42 }; // OK; same value as b
|
| 2876 |
+
byte e { -1 }; // error
|
| 2877 |
+
|
| 2878 |
+
struct A { byte b; };
|
| 2879 |
+
A a1 = { { 42 } }; // error
|
| 2880 |
+
A a2 = { byte{ 42 } }; // OK
|
| 2881 |
+
|
| 2882 |
+
void f(byte);
|
| 2883 |
+
f({ 42 }); // error
|
| 2884 |
+
|
| 2885 |
+
enum class Handle : uint32_t { Invalid = 0 };
|
| 2886 |
+
Handle h { 42 }; // OK
|
| 2887 |
+
```
|
| 2888 |
+
|
| 2889 |
+
— *end example*]
|
| 2890 |
- Otherwise, if the initializer list has a single element of type `E`
|
| 2891 |
and either `T` is not a reference type or its referenced type is
|
| 2892 |
reference-related to `E`, the object or reference is initialized from
|
| 2893 |
+
that element (by copy-initialization for copy-list-initialization, or
|
| 2894 |
+
by direct-initialization for direct-list-initialization); if a
|
| 2895 |
+
narrowing conversion (see below) is required to convert the element to
|
| 2896 |
+
`T`, the program is ill-formed.
|
| 2897 |
+
\[*Example 7*:
|
| 2898 |
``` cpp
|
| 2899 |
int x1 {2}; // OK
|
| 2900 |
int x2 {2.0}; // error: narrowing
|
| 2901 |
```
|
| 2902 |
+
|
| 2903 |
+
— *end example*]
|
| 2904 |
+
- Otherwise, if `T` is a reference type, a prvalue of the type
|
| 2905 |
+
referenced by `T` is generated. The prvalue initializes its result
|
| 2906 |
+
object by copy-list-initialization or direct-list-initialization,
|
| 2907 |
+
depending on the kind of initialization for the reference. The prvalue
|
| 2908 |
+
is then used to direct-initialize the reference.
|
| 2909 |
+
\[*Note 3*: As usual, the binding will fail and the program is
|
| 2910 |
+
ill-formed if the reference type is an lvalue reference to a non-const
|
| 2911 |
+
type. — *end note*]
|
| 2912 |
+
\[*Example 8*:
|
| 2913 |
``` cpp
|
| 2914 |
struct S {
|
| 2915 |
S(std::initializer_list<double>); // #1
|
| 2916 |
S(const std::string&); // #2
|
| 2917 |
// ...
|
|
|
|
| 2921 |
S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
|
| 2922 |
const int& i1 = { 1 }; // OK
|
| 2923 |
const int& i2 = { 1.1 }; // error: narrowing
|
| 2924 |
const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
|
| 2925 |
```
|
| 2926 |
+
|
| 2927 |
+
— *end example*]
|
| 2928 |
- Otherwise, if the initializer list has no elements, the object is
|
| 2929 |
value-initialized.
|
| 2930 |
+
\[*Example 9*:
|
| 2931 |
``` cpp
|
| 2932 |
int** pp {}; // initialized to null pointer
|
| 2933 |
```
|
| 2934 |
+
|
| 2935 |
+
— *end example*]
|
| 2936 |
- Otherwise, the program is ill-formed.
|
| 2937 |
+
\[*Example 10*:
|
| 2938 |
``` cpp
|
| 2939 |
struct A { int i; int j; };
|
| 2940 |
A a1 { 1, 2 }; // aggregate initialization
|
| 2941 |
A a2 { 1.2 }; // error: narrowing
|
| 2942 |
struct B {
|
|
|
|
| 2952 |
|
| 2953 |
int j { 1 }; // initialize to 1
|
| 2954 |
int k { }; // initialize to 0
|
| 2955 |
```
|
| 2956 |
|
| 2957 |
+
— *end example*]
|
| 2958 |
+
|
| 2959 |
Within the *initializer-list* of a *braced-init-list*, the
|
| 2960 |
*initializer-clause*s, including any that result from pack expansions (
|
| 2961 |
[[temp.variadic]]), are evaluated in the order in which they appear.
|
| 2962 |
That is, every value computation and side effect associated with a given
|
| 2963 |
*initializer-clause* is sequenced before every value computation and
|
| 2964 |
side effect associated with any *initializer-clause* that follows it in
|
| 2965 |
+
the comma-separated list of the *initializer-list*.
|
| 2966 |
+
|
| 2967 |
+
[*Note 4*: This evaluation ordering holds regardless of the semantics
|
| 2968 |
+
of the initialization; for example, it applies when the elements of the
|
| 2969 |
+
*initializer-list* are interpreted as arguments of a constructor call,
|
| 2970 |
+
even though ordinarily there are no sequencing constraints on the
|
| 2971 |
+
arguments of a call. — *end note*]
|
| 2972 |
|
| 2973 |
An object of type `std::initializer_list<E>` is constructed from an
|
| 2974 |
+
initializer list as if the implementation generated and materialized (
|
| 2975 |
+
[[conv.rval]]) a prvalue of type “array of N `const E`”, where N is the
|
| 2976 |
+
number of elements in the initializer list. Each element of that array
|
| 2977 |
+
is copy-initialized with the corresponding element of the initializer
|
| 2978 |
+
list, and the `std::initializer_list<E>` object is constructed to refer
|
| 2979 |
+
to that array.
|
| 2980 |
+
|
| 2981 |
+
[*Note 5*: A constructor or conversion function selected for the copy
|
| 2982 |
+
shall be accessible (Clause [[class.access]]) in the context of the
|
| 2983 |
+
initializer list. — *end note*]
|
| 2984 |
+
|
| 2985 |
+
If a narrowing conversion is required to initialize any of the elements,
|
| 2986 |
+
the program is ill-formed.
|
| 2987 |
+
|
| 2988 |
+
[*Example 11*:
|
| 2989 |
|
| 2990 |
``` cpp
|
| 2991 |
struct X {
|
| 2992 |
X(std::initializer_list<double> v);
|
| 2993 |
};
|
|
|
|
| 3003 |
```
|
| 3004 |
|
| 3005 |
assuming that the implementation can construct an `initializer_list`
|
| 3006 |
object with a pair of pointers.
|
| 3007 |
|
| 3008 |
+
— *end example*]
|
| 3009 |
+
|
| 3010 |
The array has the same lifetime as any other temporary object (
|
| 3011 |
[[class.temporary]]), except that initializing an `initializer_list`
|
| 3012 |
object from the array extends the lifetime of the array exactly like
|
| 3013 |
binding a reference to a temporary.
|
| 3014 |
|
| 3015 |
+
[*Example 12*:
|
| 3016 |
+
|
| 3017 |
``` cpp
|
| 3018 |
typedef std::complex<double> cmplx;
|
| 3019 |
std::vector<cmplx> v1 = { 1, 2, 3 };
|
| 3020 |
|
| 3021 |
void f() {
|
|
|
|
| 3023 |
std::initializer_list<int> i3 = { 1, 2, 3 };
|
| 3024 |
}
|
| 3025 |
|
| 3026 |
struct A {
|
| 3027 |
std::initializer_list<int> i4;
|
| 3028 |
+
A() : i4{ 1, 2, 3 } {} // ill-formed, would create a dangling reference
|
| 3029 |
};
|
| 3030 |
```
|
| 3031 |
|
| 3032 |
For `v1` and `v2`, the `initializer_list` object is a parameter in a
|
| 3033 |
function call, so the array created for `{ 1, 2, 3 }` has
|
| 3034 |
full-expression lifetime. For `i3`, the `initializer_list` object is a
|
| 3035 |
variable, so the array persists for the lifetime of the variable. For
|
| 3036 |
+
`i4`, the `initializer_list` object is initialized in the constructor’s
|
| 3037 |
+
*ctor-initializer* as if by binding a temporary array to a reference
|
| 3038 |
+
member, so the program is ill-formed ([[class.base.init]]).
|
| 3039 |
+
|
| 3040 |
+
— *end example*]
|
| 3041 |
+
|
| 3042 |
+
[*Note 6*: The implementation is free to allocate the array in
|
| 3043 |
+
read-only memory if an explicit array with the same initializer could be
|
| 3044 |
+
so allocated. — *end note*]
|
| 3045 |
|
| 3046 |
A *narrowing conversion* is an implicit conversion
|
| 3047 |
|
| 3048 |
- from a floating-point type to an integer type, or
|
| 3049 |
- from `long double` to `double` or `float`, or from `double` to
|
|
|
|
| 3057 |
- from an integer type or unscoped enumeration type to an integer type
|
| 3058 |
that cannot represent all the values of the original type, except
|
| 3059 |
where the source is a constant expression whose value after integral
|
| 3060 |
promotions will fit into the target type.
|
| 3061 |
|
| 3062 |
+
[*Note 7*: As indicated above, such conversions are not allowed at the
|
| 3063 |
+
top level in list-initializations. — *end note*]
|
| 3064 |
+
|
| 3065 |
+
[*Example 13*:
|
| 3066 |
|
| 3067 |
``` cpp
|
| 3068 |
int x = 999; // x is not a constant expression
|
| 3069 |
const int y = 999;
|
| 3070 |
const int z = 99;
|
|
|
|
| 3083 |
int f(int);
|
| 3084 |
int a[] =
|
| 3085 |
{ 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level
|
| 3086 |
```
|
| 3087 |
|
| 3088 |
+
— *end example*]
|
| 3089 |
+
|
| 3090 |
<!-- Link reference definitions -->
|
| 3091 |
+
[basic.align]: basic.md#basic.align
|
| 3092 |
[basic.compound]: basic.md#basic.compound
|
| 3093 |
[basic.def]: basic.md#basic.def
|
| 3094 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 3095 |
[basic.fundamental]: basic.md#basic.fundamental
|
| 3096 |
[basic.life]: basic.md#basic.life
|
| 3097 |
[basic.link]: basic.md#basic.link
|
| 3098 |
[basic.lookup]: basic.md#basic.lookup
|
| 3099 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 3100 |
+
[basic.lookup.classref]: basic.md#basic.lookup.classref
|
| 3101 |
[basic.lookup.elab]: basic.md#basic.lookup.elab
|
| 3102 |
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
| 3103 |
[basic.lookup.udir]: basic.md#basic.lookup.udir
|
| 3104 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 3105 |
[basic.lval]: basic.md#basic.lval
|
| 3106 |
[basic.namespace]: #basic.namespace
|
| 3107 |
[basic.scope]: basic.md#basic.scope
|
| 3108 |
[basic.scope.block]: basic.md#basic.scope.block
|
| 3109 |
+
[basic.scope.declarative]: basic.md#basic.scope.declarative
|
| 3110 |
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
| 3111 |
[basic.scope.pdecl]: basic.md#basic.scope.pdecl
|
| 3112 |
[basic.scope.proto]: basic.md#basic.scope.proto
|
| 3113 |
[basic.start]: basic.md#basic.start
|
| 3114 |
+
[basic.start.dynamic]: basic.md#basic.start.dynamic
|
| 3115 |
+
[basic.start.static]: basic.md#basic.start.static
|
| 3116 |
[basic.stc]: basic.md#basic.stc
|
| 3117 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
| 3118 |
+
[basic.stc.dynamic]: basic.md#basic.stc.dynamic
|
| 3119 |
[basic.stc.static]: basic.md#basic.stc.static
|
| 3120 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 3121 |
[basic.type.qualifier]: basic.md#basic.type.qualifier
|
| 3122 |
[basic.types]: basic.md#basic.types
|
| 3123 |
[class]: class.md#class
|
|
|
|
| 3127 |
[class.conv]: special.md#class.conv
|
| 3128 |
[class.conv.ctor]: special.md#class.conv.ctor
|
| 3129 |
[class.conv.fct]: special.md#class.conv.fct
|
| 3130 |
[class.copy]: special.md#class.copy
|
| 3131 |
[class.ctor]: special.md#class.ctor
|
|
|
|
| 3132 |
[class.dtor]: special.md#class.dtor
|
| 3133 |
[class.expl.init]: special.md#class.expl.init
|
| 3134 |
[class.friend]: class.md#class.friend
|
| 3135 |
+
[class.inhctor.init]: special.md#class.inhctor.init
|
| 3136 |
[class.init]: special.md#class.init
|
| 3137 |
[class.mem]: class.md#class.mem
|
| 3138 |
[class.member.lookup]: class.md#class.member.lookup
|
| 3139 |
[class.mfct]: class.md#class.mfct
|
| 3140 |
+
[class.mi]: class.md#class.mi
|
| 3141 |
[class.name]: class.md#class.name
|
| 3142 |
[class.qual]: basic.md#class.qual
|
| 3143 |
[class.static]: class.md#class.static
|
| 3144 |
[class.static.data]: class.md#class.static.data
|
| 3145 |
[class.temporary]: special.md#class.temporary
|
|
|
|
| 3146 |
[class.union]: class.md#class.union
|
| 3147 |
+
[class.union.anon]: class.md#class.union.anon
|
| 3148 |
[class.virtual]: class.md#class.virtual
|
| 3149 |
[conv]: conv.md#conv
|
| 3150 |
[conv.array]: conv.md#conv.array
|
| 3151 |
[conv.func]: conv.md#conv.func
|
| 3152 |
[conv.integral]: conv.md#conv.integral
|
| 3153 |
[conv.lval]: conv.md#conv.lval
|
| 3154 |
[conv.prom]: conv.md#conv.prom
|
| 3155 |
[conv.ptr]: conv.md#conv.ptr
|
| 3156 |
+
[conv.qual]: conv.md#conv.qual
|
| 3157 |
+
[conv.rval]: conv.md#conv.rval
|
| 3158 |
+
[cstddef.syn]: language.md#cstddef.syn
|
| 3159 |
[dcl.align]: #dcl.align
|
| 3160 |
[dcl.ambig.res]: #dcl.ambig.res
|
| 3161 |
[dcl.array]: #dcl.array
|
| 3162 |
[dcl.asm]: #dcl.asm
|
| 3163 |
[dcl.attr]: #dcl.attr
|
| 3164 |
[dcl.attr.depend]: #dcl.attr.depend
|
| 3165 |
[dcl.attr.deprecated]: #dcl.attr.deprecated
|
| 3166 |
+
[dcl.attr.fallthrough]: #dcl.attr.fallthrough
|
| 3167 |
[dcl.attr.grammar]: #dcl.attr.grammar
|
| 3168 |
+
[dcl.attr.nodiscard]: #dcl.attr.nodiscard
|
| 3169 |
[dcl.attr.noreturn]: #dcl.attr.noreturn
|
| 3170 |
+
[dcl.attr.unused]: #dcl.attr.unused
|
| 3171 |
[dcl.constexpr]: #dcl.constexpr
|
| 3172 |
[dcl.dcl]: #dcl.dcl
|
| 3173 |
[dcl.decl]: #dcl.decl
|
| 3174 |
[dcl.enum]: #dcl.enum
|
| 3175 |
[dcl.fct]: #dcl.fct
|
|
|
|
| 3183 |
[dcl.init]: #dcl.init
|
| 3184 |
[dcl.init.aggr]: #dcl.init.aggr
|
| 3185 |
[dcl.init.list]: #dcl.init.list
|
| 3186 |
[dcl.init.ref]: #dcl.init.ref
|
| 3187 |
[dcl.init.string]: #dcl.init.string
|
| 3188 |
+
[dcl.inline]: #dcl.inline
|
| 3189 |
[dcl.link]: #dcl.link
|
| 3190 |
[dcl.meaning]: #dcl.meaning
|
| 3191 |
[dcl.mptr]: #dcl.mptr
|
| 3192 |
[dcl.name]: #dcl.name
|
| 3193 |
[dcl.ptr]: #dcl.ptr
|
| 3194 |
[dcl.ref]: #dcl.ref
|
| 3195 |
[dcl.spec]: #dcl.spec
|
| 3196 |
[dcl.spec.auto]: #dcl.spec.auto
|
| 3197 |
[dcl.stc]: #dcl.stc
|
| 3198 |
+
[dcl.struct.bind]: #dcl.struct.bind
|
| 3199 |
[dcl.type]: #dcl.type
|
| 3200 |
+
[dcl.type.auto.deduct]: #dcl.type.auto.deduct
|
| 3201 |
+
[dcl.type.class.deduct]: #dcl.type.class.deduct
|
| 3202 |
[dcl.type.cv]: #dcl.type.cv
|
| 3203 |
[dcl.type.elab]: #dcl.type.elab
|
| 3204 |
[dcl.type.simple]: #dcl.type.simple
|
| 3205 |
[dcl.typedef]: #dcl.typedef
|
|
|
|
| 3206 |
[except.handle]: except.md#except.handle
|
| 3207 |
[except.spec]: except.md#except.spec
|
| 3208 |
[except.throw]: except.md#except.throw
|
| 3209 |
[expr]: expr.md#expr
|
| 3210 |
[expr.alignof]: expr.md#expr.alignof
|
|
|
|
| 3215 |
[expr.cond]: expr.md#expr.cond
|
| 3216 |
[expr.const]: expr.md#expr.const
|
| 3217 |
[expr.const.cast]: expr.md#expr.const.cast
|
| 3218 |
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 3219 |
[expr.new]: expr.md#expr.new
|
| 3220 |
+
[expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
|
| 3221 |
+
[expr.prim.this]: expr.md#expr.prim.this
|
| 3222 |
[expr.ref]: expr.md#expr.ref
|
| 3223 |
[expr.static.cast]: expr.md#expr.static.cast
|
| 3224 |
[expr.sub]: expr.md#expr.sub
|
| 3225 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 3226 |
[expr.unary]: expr.md#expr.unary
|
| 3227 |
[expr.unary.op]: expr.md#expr.unary.op
|
|
|
|
| 3228 |
[intro.compliance]: intro.md#intro.compliance
|
| 3229 |
[intro.execution]: intro.md#intro.execution
|
| 3230 |
[intro.multithread]: intro.md#intro.multithread
|
| 3231 |
[lex.charset]: lex.md#lex.charset
|
| 3232 |
[lex.digraph]: lex.md#lex.digraph
|
|
|
|
| 3240 |
[namespace.udecl]: #namespace.udecl
|
| 3241 |
[namespace.udir]: #namespace.udir
|
| 3242 |
[namespace.unnamed]: #namespace.unnamed
|
| 3243 |
[over]: over.md#over
|
| 3244 |
[over.match]: over.md#over.match
|
| 3245 |
+
[over.match.class.deduct]: over.md#over.match.class.deduct
|
| 3246 |
[over.match.conv]: over.md#over.match.conv
|
| 3247 |
[over.match.copy]: over.md#over.match.copy
|
| 3248 |
[over.match.ctor]: over.md#over.match.ctor
|
| 3249 |
[over.match.list]: over.md#over.match.list
|
| 3250 |
[over.match.ref]: over.md#over.match.ref
|
| 3251 |
[over.oper]: over.md#over.oper
|
| 3252 |
[over.sub]: over.md#over.sub
|
| 3253 |
[stmt.ambig]: stmt.md#stmt.ambig
|
|
|
|
| 3254 |
[stmt.dcl]: stmt.md#stmt.dcl
|
| 3255 |
+
[stmt.expr]: stmt.md#stmt.expr
|
| 3256 |
+
[stmt.if]: stmt.md#stmt.if
|
| 3257 |
[stmt.iter]: stmt.md#stmt.iter
|
| 3258 |
+
[stmt.label]: stmt.md#stmt.label
|
| 3259 |
[stmt.return]: stmt.md#stmt.return
|
| 3260 |
[stmt.select]: stmt.md#stmt.select
|
| 3261 |
[stmt.stmt]: stmt.md#stmt.stmt
|
| 3262 |
+
[stmt.switch]: stmt.md#stmt.switch
|
| 3263 |
[support.runtime]: language.md#support.runtime
|
| 3264 |
[tab:simple.type.specifiers]: #tab:simple.type.specifiers
|
| 3265 |
[temp]: temp.md#temp
|
| 3266 |
[temp.arg.type]: temp.md#temp.arg.type
|
| 3267 |
[temp.class.spec]: temp.md#temp.class.spec
|
| 3268 |
+
[temp.deduct]: temp.md#temp.deduct
|
| 3269 |
[temp.deduct.call]: temp.md#temp.deduct.call
|
| 3270 |
[temp.dep]: temp.md#temp.dep
|
| 3271 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 3272 |
[temp.explicit]: temp.md#temp.explicit
|
| 3273 |
[temp.inst]: temp.md#temp.inst
|
|
|
|
| 3278 |
[temp.spec]: temp.md#temp.spec
|
| 3279 |
[temp.variadic]: temp.md#temp.variadic
|
| 3280 |
|
| 3281 |
[^1]: The “implicit int” rule of C is no longer supported.
|
| 3282 |
|
| 3283 |
+
[^2]: The `inline` keyword has no effect on the linkage of a function.
|
| 3284 |
|
| 3285 |
[^3]: There is no special provision for a *decl-specifier-seq* that
|
| 3286 |
lacks a *type-specifier* or that has a *type-specifier* that only
|
| 3287 |
specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
|
| 3288 |
supported.
|
|
|
|
| 3290 |
[^4]: This set of values is used to define promotion and conversion
|
| 3291 |
semantics for the enumeration type. It does not preclude an
|
| 3292 |
expression of enumeration type from having a value that falls
|
| 3293 |
outside this range.
|
| 3294 |
|
| 3295 |
+
[^5]: this implies that the name of the class or function is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3296 |
unqualified.
|
| 3297 |
|
| 3298 |
+
[^6]: A *using-declaration* with more than one *using-declarator* is
|
| 3299 |
+
equivalent to a corresponding sequence of *using-declaration*s with
|
| 3300 |
+
one *using-declarator* each.
|
| 3301 |
+
|
| 3302 |
[^7]: During name lookup in a class hierarchy, some ambiguities may be
|
| 3303 |
resolved by considering whether one member hides the other along
|
| 3304 |
some paths ([[class.member.lookup]]). There is no such
|
| 3305 |
disambiguation when considering the set of names found as a result
|
| 3306 |
of following *using-directive*s.
|
| 3307 |
|
| 3308 |
+
[^8]: As indicated by syntax, cv-qualifiers are a significant component
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3309 |
in function return types.
|
| 3310 |
|
| 3311 |
+
[^9]: One can explicitly disambiguate the parse either by introducing a
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3312 |
comma (so the ellipsis will be parsed as part of the
|
| 3313 |
*parameter-declaration-clause*) or by introducing a name for the
|
| 3314 |
parameter (so the ellipsis will be parsed as part of the
|
| 3315 |
*declarator-id*).
|
| 3316 |
|
| 3317 |
+
[^10]: This means that default arguments cannot appear, for example, in
|
| 3318 |
declarations of pointers to functions, references to functions, or
|
| 3319 |
`typedef` declarations.
|
| 3320 |
|
| 3321 |
+
[^11]: Implementations are permitted to provide additional predefined
|
| 3322 |
variables with names that are reserved to the implementation (
|
| 3323 |
+
[[lex.name]]). If a predefined variable is not odr-used (
|
| 3324 |
[[basic.def.odr]]), its string value need not be present in the
|
| 3325 |
program image.
|
| 3326 |
|
| 3327 |
+
[^12]: As specified in [[conv.ptr]], converting an integer literal
|
| 3328 |
whose value is `0` to a pointer type results in a null pointer
|
| 3329 |
value.
|
| 3330 |
|
| 3331 |
+
[^13]: The syntax provides for empty *initializer-list*s, but
|
| 3332 |
nonetheless C++does not have zero length arrays.
|
| 3333 |
|
| 3334 |
+
[^14]: This requires a conversion function ([[class.conv.fct]])
|
| 3335 |
returning a reference type.
|