- tmp/tmpkppd7vkf/{from.md → to.md} +335 -237
tmp/tmpkppd7vkf/{from.md → to.md}
RENAMED
|
@@ -1,17 +1,17 @@
|
|
| 1 |
-
## Meaning of declarators <a id="dcl.meaning">[[dcl.meaning]]</a>
|
| 2 |
|
| 3 |
A declarator contains exactly one *declarator-id*; it names the
|
| 4 |
identifier that is declared. An *unqualified-id* occurring in a
|
| 5 |
*declarator-id* shall be a simple *identifier* except for the
|
| 6 |
declaration of some special functions ([[class.ctor]], [[class.conv]],
|
| 7 |
[[class.dtor]], [[over.oper]]) and for the declaration of template
|
| 8 |
-
specializations or partial specializations
|
| 9 |
*declarator-id* is qualified, the declaration shall refer to a
|
| 10 |
previously declared member of the class or namespace to which the
|
| 11 |
qualifier refers (or, in the case of a namespace, of an element of the
|
| 12 |
-
inline namespace set of that namespace
|
| 13 |
specialization thereof; the member shall not merely have been introduced
|
| 14 |
by a *using-declaration* in the scope of the class or namespace
|
| 15 |
nominated by the *nested-name-specifier* of the *declarator-id*. The
|
| 16 |
*nested-name-specifier* of a qualified *declarator-id* shall not begin
|
| 17 |
with a *decltype-specifier*.
|
|
@@ -22,14 +22,15 @@ namespace scope. — *end note*]
|
|
| 22 |
|
| 23 |
The optional *attribute-specifier-seq* following a *declarator-id*
|
| 24 |
appertains to the entity that is declared.
|
| 25 |
|
| 26 |
A `static`, `thread_local`, `extern`, `mutable`, `friend`, `inline`,
|
| 27 |
-
`virtual`, `constexpr`,
|
| 28 |
-
directly to each *declarator-id* in an
|
| 29 |
-
*member-declarator-list*; the type specified
|
| 30 |
-
depends on both the *decl-specifier-seq* and
|
|
|
|
| 31 |
|
| 32 |
Thus, a declaration of a particular identifier has the form
|
| 33 |
|
| 34 |
``` cpp
|
| 35 |
T D
|
|
@@ -55,11 +56,11 @@ In the declaration
|
|
| 55 |
``` cpp
|
| 56 |
int unsigned i;
|
| 57 |
```
|
| 58 |
|
| 59 |
the type specifiers `int` `unsigned` determine the type “`unsigned int`”
|
| 60 |
-
|
| 61 |
|
| 62 |
— *end example*]
|
| 63 |
|
| 64 |
In a declaration *attribute-specifier-seq*ₒₚₜ `T` `D` where `D` is an
|
| 65 |
unadorned identifier the type of this identifier is “`T`”.
|
|
@@ -78,11 +79,11 @@ T D1
|
|
| 78 |
```
|
| 79 |
|
| 80 |
Parentheses do not alter the type of the embedded *declarator-id*, but
|
| 81 |
they can alter the binding of complex declarators.
|
| 82 |
|
| 83 |
-
### Pointers <a id="dcl.ptr">[[dcl.ptr]]</a>
|
| 84 |
|
| 85 |
In a declaration `T` `D` where `D` has the form
|
| 86 |
|
| 87 |
``` bnf
|
| 88 |
'*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
|
@@ -90,12 +91,12 @@ In a declaration `T` `D` where `D` has the form
|
|
| 90 |
|
| 91 |
and the type of the identifier in the declaration `T` `D1` is
|
| 92 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 93 |
`D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
|
| 94 |
`T`”. The *cv-qualifier*s apply to the pointer and not to the object
|
| 95 |
-
pointed to. Similarly, the optional *attribute-specifier-seq*
|
| 96 |
-
[[dcl.attr.grammar]]
|
| 97 |
pointed to.
|
| 98 |
|
| 99 |
[*Example 1*:
|
| 100 |
|
| 101 |
The declarations
|
|
@@ -148,14 +149,14 @@ cv-unqualified pointer later, for example:
|
|
| 148 |
See also [[expr.ass]] and [[dcl.init]].
|
| 149 |
|
| 150 |
[*Note 1*: Forming a pointer to reference type is ill-formed; see
|
| 151 |
[[dcl.ref]]. Forming a function pointer type is ill-formed if the
|
| 152 |
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 153 |
-
[[dcl.fct]]. Since the address of a bit-field
|
| 154 |
taken, a pointer can never point to a bit-field. — *end note*]
|
| 155 |
|
| 156 |
-
### References <a id="dcl.ref">[[dcl.ref]]</a>
|
| 157 |
|
| 158 |
In a declaration `T` `D` where `D` has either of the forms
|
| 159 |
|
| 160 |
``` bnf
|
| 161 |
'&' attribute-specifier-seqₒₚₜ 'D1'
|
|
@@ -166,18 +167,18 @@ and the type of the identifier in the declaration `T` `D1` is
|
|
| 166 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 167 |
`D` is “*derived-declarator-type-list* reference to `T`”. The optional
|
| 168 |
*attribute-specifier-seq* appertains to the reference type. Cv-qualified
|
| 169 |
references are ill-formed except when the cv-qualifiers are introduced
|
| 170 |
through the use of a *typedef-name* ([[dcl.typedef]], [[temp.param]])
|
| 171 |
-
or *decltype-specifier*
|
| 172 |
cv-qualifiers are ignored.
|
| 173 |
|
| 174 |
[*Example 1*:
|
| 175 |
|
| 176 |
``` cpp
|
| 177 |
typedef int& A;
|
| 178 |
-
const A aref = 3; //
|
| 179 |
```
|
| 180 |
|
| 181 |
The type of `aref` is “lvalue reference to `int`”, not “lvalue reference
|
| 182 |
to `const int`”.
|
| 183 |
|
|
@@ -241,19 +242,19 @@ void k() {
|
|
| 241 |
declares `p` to be a reference to a pointer to `link` so `h(q)` will
|
| 242 |
leave `q` with the value zero. See also [[dcl.init.ref]].
|
| 243 |
|
| 244 |
— *end example*]
|
| 245 |
|
| 246 |
-
It is unspecified whether or not a reference requires storage
|
| 247 |
-
[[basic.stc]]
|
| 248 |
|
| 249 |
There shall be no references to references, no arrays of references, and
|
| 250 |
no pointers to references. The declaration of a reference shall contain
|
| 251 |
-
an *initializer*
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
declaration of a parameter or a return type
|
| 255 |
[[basic.def]]. A reference shall be initialized to refer to a valid
|
| 256 |
object or function.
|
| 257 |
|
| 258 |
[*Note 2*: In particular, a null reference cannot exist in a
|
| 259 |
well-defined program, because the only way to create such a reference
|
|
@@ -261,15 +262,14 @@ would be to bind it to the “object” obtained by indirection through a
|
|
| 261 |
null pointer, which causes undefined behavior. As described in
|
| 262 |
[[class.bit]], a reference cannot be bound directly to a
|
| 263 |
bit-field. — *end note*]
|
| 264 |
|
| 265 |
If a *typedef-name* ([[dcl.typedef]], [[temp.param]]) or a
|
| 266 |
-
*decltype-specifier*
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
type `TR`.
|
| 271 |
|
| 272 |
[*Note 3*: This rule is known as reference collapsing. — *end note*]
|
| 273 |
|
| 274 |
[*Example 3*:
|
| 275 |
|
|
@@ -293,11 +293,11 @@ decltype(r2)&& r7 = i; // r7 has the type int&
|
|
| 293 |
|
| 294 |
[*Note 4*: Forming a reference to function type is ill-formed if the
|
| 295 |
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 296 |
[[dcl.fct]]. — *end note*]
|
| 297 |
|
| 298 |
-
### Pointers to members <a id="dcl.mptr">[[dcl.mptr]]</a>
|
| 299 |
|
| 300 |
In a declaration `T` `D` where `D` has the form
|
| 301 |
|
| 302 |
``` bnf
|
| 303 |
nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
|
@@ -306,11 +306,11 @@ nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒ
|
|
| 306 |
and the *nested-name-specifier* denotes a class, and the type of the
|
| 307 |
identifier in the declaration `T` `D1` is
|
| 308 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 309 |
`D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
|
| 310 |
member of class *nested-name-specifier* of type `T`”. The optional
|
| 311 |
-
*attribute-specifier-seq*
|
| 312 |
pointer-to-member.
|
| 313 |
|
| 314 |
[*Example 1*:
|
| 315 |
|
| 316 |
``` cpp
|
|
@@ -341,102 +341,107 @@ obj.*pmi = 7; // assign 7 to an integer member of obj
|
|
| 341 |
(obj.*pmf)(7); // call a function member of obj with the argument 7
|
| 342 |
```
|
| 343 |
|
| 344 |
— *end example*]
|
| 345 |
|
| 346 |
-
A pointer to member shall not point to a static member of a class
|
| 347 |
-
[[class.static]]
|
| 348 |
|
| 349 |
[*Note 1*: See also [[expr.unary]] and [[expr.mptr.oper]]. The type
|
| 350 |
“pointer to member” is distinct from the type “pointer”, that is, a
|
| 351 |
-
pointer to member is declared only by the pointer
|
| 352 |
syntax, and never by the pointer declarator syntax. There is no
|
| 353 |
“reference-to-member” type in C++. — *end note*]
|
| 354 |
|
| 355 |
-
### Arrays <a id="dcl.array">[[dcl.array]]</a>
|
| 356 |
|
| 357 |
In a declaration `T` `D` where `D` has the form
|
| 358 |
|
| 359 |
``` bnf
|
| 360 |
-
'D1 [' constant-expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
|
| 361 |
```
|
| 362 |
|
| 363 |
-
and the type of the
|
| 364 |
-
“*derived-declarator-type-list* `T`”,
|
| 365 |
-
`D` is
|
| 366 |
-
`
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
`
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
|
| 387 |
[*Example 1*:
|
| 388 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
``` cpp
|
| 390 |
typedef int A[5], AA[2][3];
|
| 391 |
typedef const A CA; // type is ``array of 5 const int''
|
| 392 |
typedef const AA CAA; // type is ``array of 2 array of 3 const int''
|
| 393 |
```
|
| 394 |
|
| 395 |
— *end example*]
|
| 396 |
|
| 397 |
-
[*Note
|
| 398 |
type; see [[basic.type.qualifier]]. — *end note*]
|
| 399 |
|
| 400 |
-
An
|
| 401 |
-
|
| 402 |
-
|
| 403 |
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
is calculated from the number of initial elements (say, `N`) supplied (
|
| 414 |
-
[[dcl.init.aggr]]), and the type of the identifier of `D` is “array of
|
| 415 |
-
`N` `T`”. Furthermore, if there is a preceding declaration of the entity
|
| 416 |
-
in the same scope in which the bound was specified, an omitted array
|
| 417 |
-
bound is taken to be the same as in that earlier declaration, and
|
| 418 |
-
similarly for the definition of a static data member of a class.
|
| 419 |
|
| 420 |
-
|
|
|
|
|
|
|
|
|
|
| 421 |
|
| 422 |
-
|
| 423 |
-
float fa[17], *afp[17];
|
| 424 |
-
```
|
| 425 |
-
|
| 426 |
-
declares an array of `float` numbers and an array of pointers to `float`
|
| 427 |
-
numbers. For another example,
|
| 428 |
-
|
| 429 |
-
``` cpp
|
| 430 |
-
static int x3d[3][5][7];
|
| 431 |
-
```
|
| 432 |
-
|
| 433 |
-
declares a static three-dimensional array of integers, with rank
|
| 434 |
-
3 × 5 × 7. In complete detail, `x3d` is an array of three items; each
|
| 435 |
-
item is an array of five arrays; each of the latter arrays is an array
|
| 436 |
-
of seven integers. Any of the expressions `x3d`, `x3d[i]`, `x3d[i][j]`,
|
| 437 |
-
`x3d[i][j][k]` can reasonably appear in an expression. Finally,
|
| 438 |
|
| 439 |
``` cpp
|
| 440 |
extern int x[10];
|
| 441 |
struct S {
|
| 442 |
static int y[10];
|
|
@@ -451,102 +456,106 @@ void f() {
|
|
| 451 |
}
|
| 452 |
```
|
| 453 |
|
| 454 |
— *end example*]
|
| 455 |
|
| 456 |
-
[*Note
|
| 457 |
-
described in [[conv.array]]. Objects of array types cannot be modified,
|
| 458 |
-
see [[basic.lval]]. — *end note*]
|
| 459 |
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
the conversion rules that apply to `+`, if `E1` is an array and `E2` an
|
| 464 |
-
integer, then `E1[E2]` refers to the `E2`-th member of `E1`. Therefore,
|
| 465 |
-
despite its asymmetric appearance, subscripting is a commutative
|
| 466 |
-
operation. — *end note*]
|
| 467 |
|
| 468 |
-
[*
|
| 469 |
-
|
| 470 |
-
A consistent rule is followed for multidimensional arrays. If `E` is an
|
| 471 |
-
*n*-dimensional array of rank i × j × … × k, then `E` appearing in an
|
| 472 |
-
expression that is subject to the array-to-pointer conversion (
|
| 473 |
-
[[conv.array]]) is converted to a pointer to an (n-1)-dimensional array
|
| 474 |
-
with rank j × … × k. If the `*` operator, either explicitly or
|
| 475 |
-
implicitly as a result of subscripting, is applied to this pointer, the
|
| 476 |
-
result is the pointed-to (n-1)-dimensional array, which itself is
|
| 477 |
-
immediately converted into a pointer.
|
| 478 |
-
|
| 479 |
-
[*Example 3*:
|
| 480 |
-
|
| 481 |
-
Consider
|
| 482 |
|
| 483 |
``` cpp
|
| 484 |
-
int
|
| 485 |
```
|
| 486 |
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 497 |
|
| 498 |
— *end example*]
|
| 499 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
— *end note*]
|
| 501 |
|
| 502 |
-
[*Note
|
| 503 |
-
|
| 504 |
-
the declaration helps determine the amount of storage consumed by an
|
| 505 |
-
array but plays no other part in subscript calculations. — *end note*]
|
| 506 |
|
| 507 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 508 |
|
| 509 |
In a declaration `T` `D` where `D` has the form
|
| 510 |
|
| 511 |
``` bnf
|
| 512 |
-
'D1 (' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 513 |
ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 514 |
```
|
| 515 |
|
| 516 |
and the type of the contained *declarator-id* in the declaration `T`
|
| 517 |
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 518 |
-
*declarator-id* in `D` is “*derived-declarator-type-list* `noexcept`
|
| 519 |
-
function of
|
| 520 |
-
*ref-qualifier*ₒₚₜ returning `T`”, where
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 524 |
|
| 525 |
In a declaration `T` `D` where `D` has the form
|
| 526 |
|
| 527 |
``` bnf
|
| 528 |
-
'D1 (' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 529 |
ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-type
|
| 530 |
```
|
| 531 |
|
| 532 |
and the type of the contained *declarator-id* in the declaration `T`
|
| 533 |
`D1` is “*derived-declarator-type-list* `T`”, `T` shall be the single
|
| 534 |
*type-specifier* `auto`. The type of the *declarator-id* in `D` is
|
| 535 |
-
“*derived-declarator-type-list* `noexcept`
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
*trailing-return-type*, and where the optional `noexcept` is present if
|
| 539 |
-
and only if the exception specification is non-throwing. The optional
|
| 540 |
-
*attribute-specifier-seq* appertains to the function type.
|
| 541 |
|
| 542 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 543 |
|
| 544 |
``` bnf
|
| 545 |
parameter-declaration-clause:
|
| 546 |
parameter-declaration-listₒₚₜ '...'ₒₚₜ
|
| 547 |
-
parameter-declaration-list ', ...'
|
| 548 |
```
|
| 549 |
|
| 550 |
``` bnf
|
| 551 |
parameter-declaration-list:
|
| 552 |
parameter-declaration
|
|
@@ -572,17 +581,18 @@ arguments specified on the function call; see
|
|
| 572 |
[[expr.call]]. — *end note*]
|
| 573 |
|
| 574 |
If the *parameter-declaration-clause* is empty, the function takes no
|
| 575 |
arguments. A parameter list consisting of a single unnamed parameter of
|
| 576 |
non-dependent type `void` is equivalent to an empty parameter list.
|
| 577 |
-
Except for this special case, a parameter shall not have type
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
|
|
|
| 584 |
|
| 585 |
[*Example 1*:
|
| 586 |
|
| 587 |
The declaration
|
| 588 |
|
|
@@ -597,31 +607,28 @@ arguments.
|
|
| 597 |
printf("hello world");
|
| 598 |
printf("a=%d b=%d", a, b);
|
| 599 |
```
|
| 600 |
|
| 601 |
However, the first argument must be of a type that can be converted to a
|
| 602 |
-
`const` `char*`
|
| 603 |
|
| 604 |
— *end example*]
|
| 605 |
|
| 606 |
[*Note 2*: The standard header `<cstdarg>` contains a mechanism for
|
| 607 |
accessing arguments passed using the ellipsis (see [[expr.call]] and
|
| 608 |
[[support.runtime]]). — *end note*]
|
| 609 |
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
forming the function type. The resulting list of transformed parameter
|
| 621 |
-
types and the presence or absence of the ellipsis or a function
|
| 622 |
-
parameter pack is the function’s *parameter-type-list*.
|
| 623 |
|
| 624 |
[*Note 3*: This transformation does not affect the types of the
|
| 625 |
parameters. For example, `int(*)(const int p, decltype(p)*)` and
|
| 626 |
`int(*)(int, const int*)` are identical types. — *end note*]
|
| 627 |
|
|
@@ -631,20 +638,20 @@ A function type with a *cv-qualifier-seq* or a *ref-qualifier*
|
|
| 631 |
|
| 632 |
- the function type for a non-static member function,
|
| 633 |
- the function type to which a pointer to member refers,
|
| 634 |
- the top-level function type of a function typedef declaration or
|
| 635 |
*alias-declaration*,
|
| 636 |
-
- the *type-id* in the default argument of a *type-parameter*
|
| 637 |
-
[[temp.param]]
|
| 638 |
-
- the *type-id* of a *template-argument* for a *type-parameter*
|
| 639 |
-
[[temp.arg.type]]
|
| 640 |
|
| 641 |
[*Example 2*:
|
| 642 |
|
| 643 |
``` cpp
|
| 644 |
typedef int FIC(int) const;
|
| 645 |
-
FIC f; //
|
| 646 |
struct S {
|
| 647 |
FIC f; // OK
|
| 648 |
};
|
| 649 |
FIC S::*pm = &S::f; // OK
|
| 650 |
```
|
|
@@ -670,11 +677,12 @@ struct S {
|
|
| 670 |
|
| 671 |
— *end example*]
|
| 672 |
|
| 673 |
The return type, the parameter-type-list, the *ref-qualifier*, the
|
| 674 |
*cv-qualifier-seq*, and the exception specification, but not the default
|
| 675 |
-
arguments
|
|
|
|
| 676 |
|
| 677 |
[*Note 5*: Function types are checked during the assignments and
|
| 678 |
initializations of pointers to functions, references to functions, and
|
| 679 |
pointers to member functions. — *end note*]
|
| 680 |
|
|
@@ -685,48 +693,52 @@ The declaration
|
|
| 685 |
``` cpp
|
| 686 |
int fseek(FILE*, long, int);
|
| 687 |
```
|
| 688 |
|
| 689 |
declares a function taking three arguments of the specified types, and
|
| 690 |
-
returning `int`
|
| 691 |
|
| 692 |
— *end example*]
|
| 693 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 694 |
Functions shall not have a return type of type array or function,
|
| 695 |
although they may have a return type of type pointer or reference to
|
| 696 |
such things. There shall be no arrays of functions, although there can
|
| 697 |
be arrays of pointers to functions.
|
| 698 |
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
[[dcl.fct.def.delete]]).
|
| 704 |
|
| 705 |
A typedef of function type may be used to declare a function but shall
|
| 706 |
-
not be used to define a function
|
| 707 |
|
| 708 |
[*Example 5*:
|
| 709 |
|
| 710 |
``` cpp
|
| 711 |
typedef void F();
|
| 712 |
F fv; // OK: equivalent to void fv();
|
| 713 |
-
F fv { } //
|
| 714 |
void fv() { } // OK: definition of fv
|
| 715 |
```
|
| 716 |
|
| 717 |
— *end example*]
|
| 718 |
|
| 719 |
An identifier can optionally be provided as a parameter name; if present
|
| 720 |
-
in a function definition
|
| 721 |
|
| 722 |
[*Note 6*: In particular, parameter names are also optional in function
|
| 723 |
definitions and names used for a parameter in different declarations and
|
| 724 |
the definition of a function need not be the same. If a parameter name
|
| 725 |
is present in a function declaration that is not a definition, it cannot
|
| 726 |
be used outside of its function declarator because that is the extent of
|
| 727 |
-
its potential scope
|
| 728 |
|
| 729 |
[*Example 6*:
|
| 730 |
|
| 731 |
The declaration
|
| 732 |
|
|
@@ -791,23 +803,100 @@ template <class T, class U> decltype((*(T*)0) + (*(U*)0)) add(T t, U u);
|
|
| 791 |
A *non-template function* is a function that is not a function template
|
| 792 |
specialization.
|
| 793 |
|
| 794 |
[*Note 8*: A function template is not a function. — *end note*]
|
| 795 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 796 |
A *declarator-id* or *abstract-declarator* containing an ellipsis shall
|
| 797 |
-
only be used in a *parameter-declaration*.
|
| 798 |
-
*parameter-declaration*
|
| 799 |
-
|
| 800 |
-
|
|
|
|
|
|
|
| 801 |
|
| 802 |
-
[*
|
| 803 |
-
*template-parameter-list* and the parameter pack is a template parameter
|
| 804 |
-
pack; see [[temp.param]]. — *end note*]
|
| 805 |
-
|
| 806 |
-
A function parameter pack is a pack expansion ([[temp.variadic]]).
|
| 807 |
-
|
| 808 |
-
[*Example 7*:
|
| 809 |
|
| 810 |
``` cpp
|
| 811 |
template<typename... T> void f(T (* ...t)(int, int));
|
| 812 |
|
| 813 |
int add(int, int);
|
|
@@ -823,17 +912,19 @@ void g() {
|
|
| 823 |
There is a syntactic ambiguity when an ellipsis occurs at the end of a
|
| 824 |
*parameter-declaration-clause* without a preceding comma. In this case,
|
| 825 |
the ellipsis is parsed as part of the *abstract-declarator* if the type
|
| 826 |
of the parameter either names a template parameter pack that has not
|
| 827 |
been expanded or contains `auto`; otherwise, it is parsed as part of the
|
| 828 |
-
*parameter-declaration-clause*.[^
|
| 829 |
|
| 830 |
-
### Default arguments <a id="dcl.fct.default">[[dcl.fct.default]]</a>
|
| 831 |
|
| 832 |
If an *initializer-clause* is specified in a *parameter-declaration*
|
| 833 |
-
this *initializer-clause* is used as a default argument.
|
| 834 |
-
|
|
|
|
|
|
|
| 835 |
|
| 836 |
[*Example 1*:
|
| 837 |
|
| 838 |
The declaration
|
| 839 |
|
|
@@ -853,27 +944,30 @@ respectively.
|
|
| 853 |
|
| 854 |
— *end example*]
|
| 855 |
|
| 856 |
A default argument shall be specified only in the
|
| 857 |
*parameter-declaration-clause* of a function declaration or
|
| 858 |
-
*lambda-declarator* or in a *template-parameter*
|
| 859 |
-
|
| 860 |
*assignment-expression*. A default argument shall not be specified for a
|
| 861 |
-
parameter pack. If it is specified
|
| 862 |
-
it shall not occur within a
|
| 863 |
-
*parameter-declaration*.[^
|
| 864 |
|
| 865 |
For non-template functions, default arguments can be added in later
|
| 866 |
declarations of a function in the same scope. Declarations in different
|
| 867 |
scopes have completely distinct sets of default arguments. That is,
|
| 868 |
declarations in inner scopes do not acquire default arguments from
|
| 869 |
declarations in outer scopes, and vice versa. In a given function
|
| 870 |
declaration, each parameter subsequent to a parameter with a default
|
| 871 |
argument shall have a default argument supplied in this or a previous
|
| 872 |
-
declaration
|
| 873 |
-
shall
|
| 874 |
-
|
|
|
|
|
|
|
|
|
|
| 875 |
|
| 876 |
[*Example 2*:
|
| 877 |
|
| 878 |
``` cpp
|
| 879 |
void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow
|
|
@@ -892,28 +986,32 @@ void m() {
|
|
| 892 |
void f(int, int = 5); // error: cannot redefine, even to same value
|
| 893 |
}
|
| 894 |
void n() {
|
| 895 |
f(6); // OK, calls f(6, 7)
|
| 896 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 897 |
```
|
| 898 |
|
| 899 |
— *end example*]
|
| 900 |
|
| 901 |
For a given inline function defined in different translation units, the
|
| 902 |
accumulated sets of default arguments at the end of the translation
|
| 903 |
-
units shall be the same;
|
| 904 |
-
specifies a default argument expression, that declaration
|
| 905 |
-
definition and shall be the only declaration of the function
|
| 906 |
-
template in the translation unit.
|
| 907 |
|
| 908 |
The default argument has the same semantic constraints as the
|
| 909 |
initializer in a declaration of a variable of the parameter type, using
|
| 910 |
-
the copy-initialization semantics
|
| 911 |
-
|
| 912 |
-
|
| 913 |
-
|
| 914 |
-
|
| 915 |
[[temp.inst]].
|
| 916 |
|
| 917 |
[*Example 3*:
|
| 918 |
|
| 919 |
In the following code, `g` will be called with the value `f(2)`:
|
|
@@ -932,24 +1030,24 @@ void h() {
|
|
| 932 |
}
|
| 933 |
```
|
| 934 |
|
| 935 |
— *end example*]
|
| 936 |
|
| 937 |
-
[*Note
|
| 938 |
are looked up as described in [[basic.lookup.unqual]]. Access checking
|
| 939 |
-
applies to names in default arguments as described in
|
| 940 |
[[class.access]]. — *end note*]
|
| 941 |
|
| 942 |
Except for member functions of class templates, the default arguments in
|
| 943 |
a member function definition that appears outside of the class
|
| 944 |
definition are added to the set of default arguments provided by the
|
| 945 |
member function declaration in the class definition; the program is
|
| 946 |
-
ill-formed if a default constructor
|
| 947 |
-
constructor, or copy or move assignment operator
|
| 948 |
-
declared. Default arguments for a member
|
| 949 |
-
shall be specified on the initial
|
| 950 |
-
within the class template.
|
| 951 |
|
| 952 |
[*Example 4*:
|
| 953 |
|
| 954 |
``` cpp
|
| 955 |
class C {
|
|
@@ -961,12 +1059,12 @@ void C::f(int i = 3) {} // error: default argument already specified in
|
|
| 961 |
void C::g(int i = 88, int j) {} // in this translation unit, C::g can be called with no argument
|
| 962 |
```
|
| 963 |
|
| 964 |
— *end example*]
|
| 965 |
|
| 966 |
-
A local variable
|
| 967 |
-
|
| 968 |
|
| 969 |
[*Example 5*:
|
| 970 |
|
| 971 |
``` cpp
|
| 972 |
void f() {
|
|
@@ -977,11 +1075,11 @@ void f() {
|
|
| 977 |
}
|
| 978 |
```
|
| 979 |
|
| 980 |
— *end example*]
|
| 981 |
|
| 982 |
-
[*Note
|
| 983 |
|
| 984 |
The keyword `this` may not appear in a default argument of a member
|
| 985 |
function; see [[expr.prim.this]].
|
| 986 |
|
| 987 |
[*Example 6*:
|
|
@@ -1013,13 +1111,13 @@ int h(int a, int b = sizeof(a)); // OK, unevaluated operand
|
|
| 1013 |
```
|
| 1014 |
|
| 1015 |
— *end example*]
|
| 1016 |
|
| 1017 |
A non-static member shall not appear in a default argument unless it
|
| 1018 |
-
appears as the *id-expression* of a class member access expression
|
| 1019 |
-
[[expr.ref]]
|
| 1020 |
-
[[expr.unary.op]]
|
| 1021 |
|
| 1022 |
[*Example 8*:
|
| 1023 |
|
| 1024 |
The declaration of `X::mem1()` in the following example is ill-formed
|
| 1025 |
because no object is supplied for the non-static member `X::a` used as
|
|
@@ -1035,11 +1133,11 @@ class X {
|
|
| 1035 |
};
|
| 1036 |
```
|
| 1037 |
|
| 1038 |
The declaration of `X::mem2()` is meaningful, however, since no object
|
| 1039 |
is needed to access the static member `X::b`. Classes, objects, and
|
| 1040 |
-
members are described in
|
| 1041 |
|
| 1042 |
— *end example*]
|
| 1043 |
|
| 1044 |
A default argument is not part of the type of a function.
|
| 1045 |
|
|
@@ -1058,21 +1156,21 @@ int (*p2)() = &f; // error: type mismatch
|
|
| 1058 |
```
|
| 1059 |
|
| 1060 |
— *end example*]
|
| 1061 |
|
| 1062 |
When a declaration of a function is introduced by way of a
|
| 1063 |
-
*using-declaration*
|
| 1064 |
information associated with the declaration is made known as well. If
|
| 1065 |
the function is redeclared thereafter in the namespace with additional
|
| 1066 |
default arguments, the additional arguments are also known at any point
|
| 1067 |
following the redeclaration where the *using-declaration* is in scope.
|
| 1068 |
|
| 1069 |
-
A virtual function call
|
| 1070 |
-
|
| 1071 |
-
|
| 1072 |
-
|
| 1073 |
-
|
| 1074 |
|
| 1075 |
[*Example 10*:
|
| 1076 |
|
| 1077 |
``` cpp
|
| 1078 |
struct A {
|
|
|
|
| 1 |
+
### Meaning of declarators <a id="dcl.meaning">[[dcl.meaning]]</a>
|
| 2 |
|
| 3 |
A declarator contains exactly one *declarator-id*; it names the
|
| 4 |
identifier that is declared. An *unqualified-id* occurring in a
|
| 5 |
*declarator-id* shall be a simple *identifier* except for the
|
| 6 |
declaration of some special functions ([[class.ctor]], [[class.conv]],
|
| 7 |
[[class.dtor]], [[over.oper]]) and for the declaration of template
|
| 8 |
+
specializations or partial specializations [[temp.spec]]. When the
|
| 9 |
*declarator-id* is qualified, the declaration shall refer to a
|
| 10 |
previously declared member of the class or namespace to which the
|
| 11 |
qualifier refers (or, in the case of a namespace, of an element of the
|
| 12 |
+
inline namespace set of that namespace [[namespace.def]]) or to a
|
| 13 |
specialization thereof; the member shall not merely have been introduced
|
| 14 |
by a *using-declaration* in the scope of the class or namespace
|
| 15 |
nominated by the *nested-name-specifier* of the *declarator-id*. The
|
| 16 |
*nested-name-specifier* of a qualified *declarator-id* shall not begin
|
| 17 |
with a *decltype-specifier*.
|
|
|
|
| 22 |
|
| 23 |
The optional *attribute-specifier-seq* following a *declarator-id*
|
| 24 |
appertains to the entity that is declared.
|
| 25 |
|
| 26 |
A `static`, `thread_local`, `extern`, `mutable`, `friend`, `inline`,
|
| 27 |
+
`virtual`, `constexpr`, or `typedef` specifier or an
|
| 28 |
+
*explicit-specifier* applies directly to each *declarator-id* in an
|
| 29 |
+
*init-declarator-list* or *member-declarator-list*; the type specified
|
| 30 |
+
for each *declarator-id* depends on both the *decl-specifier-seq* and
|
| 31 |
+
its *declarator*.
|
| 32 |
|
| 33 |
Thus, a declaration of a particular identifier has the form
|
| 34 |
|
| 35 |
``` cpp
|
| 36 |
T D
|
|
|
|
| 56 |
``` cpp
|
| 57 |
int unsigned i;
|
| 58 |
```
|
| 59 |
|
| 60 |
the type specifiers `int` `unsigned` determine the type “`unsigned int`”
|
| 61 |
+
[[dcl.type.simple]].
|
| 62 |
|
| 63 |
— *end example*]
|
| 64 |
|
| 65 |
In a declaration *attribute-specifier-seq*ₒₚₜ `T` `D` where `D` is an
|
| 66 |
unadorned identifier the type of this identifier is “`T`”.
|
|
|
|
| 79 |
```
|
| 80 |
|
| 81 |
Parentheses do not alter the type of the embedded *declarator-id*, but
|
| 82 |
they can alter the binding of complex declarators.
|
| 83 |
|
| 84 |
+
#### Pointers <a id="dcl.ptr">[[dcl.ptr]]</a>
|
| 85 |
|
| 86 |
In a declaration `T` `D` where `D` has the form
|
| 87 |
|
| 88 |
``` bnf
|
| 89 |
'*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
|
|
|
| 91 |
|
| 92 |
and the type of the identifier in the declaration `T` `D1` is
|
| 93 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 94 |
`D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
|
| 95 |
`T`”. The *cv-qualifier*s apply to the pointer and not to the object
|
| 96 |
+
pointed to. Similarly, the optional *attribute-specifier-seq*
|
| 97 |
+
[[dcl.attr.grammar]] appertains to the pointer and not to the object
|
| 98 |
pointed to.
|
| 99 |
|
| 100 |
[*Example 1*:
|
| 101 |
|
| 102 |
The declarations
|
|
|
|
| 149 |
See also [[expr.ass]] and [[dcl.init]].
|
| 150 |
|
| 151 |
[*Note 1*: Forming a pointer to reference type is ill-formed; see
|
| 152 |
[[dcl.ref]]. Forming a function pointer type is ill-formed if the
|
| 153 |
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 154 |
+
[[dcl.fct]]. Since the address of a bit-field [[class.bit]] cannot be
|
| 155 |
taken, a pointer can never point to a bit-field. — *end note*]
|
| 156 |
|
| 157 |
+
#### References <a id="dcl.ref">[[dcl.ref]]</a>
|
| 158 |
|
| 159 |
In a declaration `T` `D` where `D` has either of the forms
|
| 160 |
|
| 161 |
``` bnf
|
| 162 |
'&' attribute-specifier-seqₒₚₜ 'D1'
|
|
|
|
| 167 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 168 |
`D` is “*derived-declarator-type-list* reference to `T`”. The optional
|
| 169 |
*attribute-specifier-seq* appertains to the reference type. Cv-qualified
|
| 170 |
references are ill-formed except when the cv-qualifiers are introduced
|
| 171 |
through the use of a *typedef-name* ([[dcl.typedef]], [[temp.param]])
|
| 172 |
+
or *decltype-specifier* [[dcl.type.simple]], in which case the
|
| 173 |
cv-qualifiers are ignored.
|
| 174 |
|
| 175 |
[*Example 1*:
|
| 176 |
|
| 177 |
``` cpp
|
| 178 |
typedef int& A;
|
| 179 |
+
const A aref = 3; // error: lvalue reference to non-const initialized with rvalue
|
| 180 |
```
|
| 181 |
|
| 182 |
The type of `aref` is “lvalue reference to `int`”, not “lvalue reference
|
| 183 |
to `const int`”.
|
| 184 |
|
|
|
|
| 242 |
declares `p` to be a reference to a pointer to `link` so `h(q)` will
|
| 243 |
leave `q` with the value zero. See also [[dcl.init.ref]].
|
| 244 |
|
| 245 |
— *end example*]
|
| 246 |
|
| 247 |
+
It is unspecified whether or not a reference requires storage
|
| 248 |
+
[[basic.stc]].
|
| 249 |
|
| 250 |
There shall be no references to references, no arrays of references, and
|
| 251 |
no pointers to references. The declaration of a reference shall contain
|
| 252 |
+
an *initializer* [[dcl.init.ref]] except when the declaration contains
|
| 253 |
+
an explicit `extern` specifier [[dcl.stc]], is a class member
|
| 254 |
+
[[class.mem]] declaration within a class definition, or is the
|
| 255 |
+
declaration of a parameter or a return type [[dcl.fct]]; see
|
| 256 |
[[basic.def]]. A reference shall be initialized to refer to a valid
|
| 257 |
object or function.
|
| 258 |
|
| 259 |
[*Note 2*: In particular, a null reference cannot exist in a
|
| 260 |
well-defined program, because the only way to create such a reference
|
|
|
|
| 262 |
null pointer, which causes undefined behavior. As described in
|
| 263 |
[[class.bit]], a reference cannot be bound directly to a
|
| 264 |
bit-field. — *end note*]
|
| 265 |
|
| 266 |
If a *typedef-name* ([[dcl.typedef]], [[temp.param]]) or a
|
| 267 |
+
*decltype-specifier* [[dcl.type.simple]] denotes a type `TR` that is a
|
| 268 |
+
reference to a type `T`, an attempt to create the type “lvalue reference
|
| 269 |
+
to cv `TR`” creates the type “lvalue reference to `T`”, while an attempt
|
| 270 |
+
to create the type “rvalue reference to cv `TR`” creates the type `TR`.
|
|
|
|
| 271 |
|
| 272 |
[*Note 3*: This rule is known as reference collapsing. — *end note*]
|
| 273 |
|
| 274 |
[*Example 3*:
|
| 275 |
|
|
|
|
| 293 |
|
| 294 |
[*Note 4*: Forming a reference to function type is ill-formed if the
|
| 295 |
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 296 |
[[dcl.fct]]. — *end note*]
|
| 297 |
|
| 298 |
+
#### Pointers to members <a id="dcl.mptr">[[dcl.mptr]]</a>
|
| 299 |
|
| 300 |
In a declaration `T` `D` where `D` has the form
|
| 301 |
|
| 302 |
``` bnf
|
| 303 |
nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
|
|
|
| 306 |
and the *nested-name-specifier* denotes a class, and the type of the
|
| 307 |
identifier in the declaration `T` `D1` is
|
| 308 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 309 |
`D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
|
| 310 |
member of class *nested-name-specifier* of type `T`”. The optional
|
| 311 |
+
*attribute-specifier-seq* [[dcl.attr.grammar]] appertains to the
|
| 312 |
pointer-to-member.
|
| 313 |
|
| 314 |
[*Example 1*:
|
| 315 |
|
| 316 |
``` cpp
|
|
|
|
| 341 |
(obj.*pmf)(7); // call a function member of obj with the argument 7
|
| 342 |
```
|
| 343 |
|
| 344 |
— *end example*]
|
| 345 |
|
| 346 |
+
A pointer to member shall not point to a static member of a class
|
| 347 |
+
[[class.static]], a member with reference type, or “cv `void`”.
|
| 348 |
|
| 349 |
[*Note 1*: See also [[expr.unary]] and [[expr.mptr.oper]]. The type
|
| 350 |
“pointer to member” is distinct from the type “pointer”, that is, a
|
| 351 |
+
pointer to member is declared only by the pointer-to-member declarator
|
| 352 |
syntax, and never by the pointer declarator syntax. There is no
|
| 353 |
“reference-to-member” type in C++. — *end note*]
|
| 354 |
|
| 355 |
+
#### Arrays <a id="dcl.array">[[dcl.array]]</a>
|
| 356 |
|
| 357 |
In a declaration `T` `D` where `D` has the form
|
| 358 |
|
| 359 |
``` bnf
|
| 360 |
+
'D1' '[' constant-expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
|
| 361 |
```
|
| 362 |
|
| 363 |
+
and the type of the contained *declarator-id* in the declaration `T`
|
| 364 |
+
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 365 |
+
*declarator-id* in `D` is “*derived-declarator-type-list* array of `N`
|
| 366 |
+
`T`”. The *constant-expression* shall be a converted constant expression
|
| 367 |
+
of type `std::size_t` [[expr.const]]. Its value `N` specifies the *array
|
| 368 |
+
bound*, i.e., the number of elements in the array; `N` shall be greater
|
| 369 |
+
than zero.
|
| 370 |
+
|
| 371 |
+
In a declaration `T` `D` where `D` has the form
|
| 372 |
+
|
| 373 |
+
``` bnf
|
| 374 |
+
'D1 [ ]' attribute-specifier-seqₒₚₜ
|
| 375 |
+
```
|
| 376 |
+
|
| 377 |
+
and the type of the contained *declarator-id* in the declaration `T`
|
| 378 |
+
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 379 |
+
*declarator-id* in `D` is “*derived-declarator-type-list* array of
|
| 380 |
+
unknown bound of `T`”, except as specified below.
|
| 381 |
+
|
| 382 |
+
A type of the form “array of `N` `U`” or “array of unknown bound of `U`”
|
| 383 |
+
is an *array type*. The optional *attribute-specifier-seq* appertains to
|
| 384 |
+
the array type.
|
| 385 |
+
|
| 386 |
+
`U` is called the array *element type*; this type shall not be a
|
| 387 |
+
placeholder type [[dcl.spec.auto]], a reference type, a function type,
|
| 388 |
+
an array of unknown bound, or cv `void`.
|
| 389 |
+
|
| 390 |
+
[*Note 1*: An array can be constructed from one of the fundamental
|
| 391 |
+
types (except `void`), from a pointer, from a pointer to member, from a
|
| 392 |
+
class, from an enumeration type, or from an array of known
|
| 393 |
+
bound. — *end note*]
|
| 394 |
|
| 395 |
[*Example 1*:
|
| 396 |
|
| 397 |
+
``` cpp
|
| 398 |
+
float fa[17], *afp[17];
|
| 399 |
+
```
|
| 400 |
+
|
| 401 |
+
declares an array of `float` numbers and an array of pointers to `float`
|
| 402 |
+
numbers.
|
| 403 |
+
|
| 404 |
+
— *end example*]
|
| 405 |
+
|
| 406 |
+
Any type of the form “*cv-qualifier-seq* array of `N` `U`” is adjusted
|
| 407 |
+
to “array of `N` *cv-qualifier-seq* `U`”, and similarly for “array of
|
| 408 |
+
unknown bound of `U`”.
|
| 409 |
+
|
| 410 |
+
[*Example 2*:
|
| 411 |
+
|
| 412 |
``` cpp
|
| 413 |
typedef int A[5], AA[2][3];
|
| 414 |
typedef const A CA; // type is ``array of 5 const int''
|
| 415 |
typedef const AA CAA; // type is ``array of 2 array of 3 const int''
|
| 416 |
```
|
| 417 |
|
| 418 |
— *end example*]
|
| 419 |
|
| 420 |
+
[*Note 2*: An “array of `N` *cv-qualifier-seq* `U`” has cv-qualified
|
| 421 |
type; see [[basic.type.qualifier]]. — *end note*]
|
| 422 |
|
| 423 |
+
An object of type “array of `N` `U`” contains a contiguously allocated
|
| 424 |
+
non-empty set of `N` subobjects of type `U`, known as the *elements* of
|
| 425 |
+
the array, and numbered `0` to `N-1`.
|
| 426 |
|
| 427 |
+
In addition to declarations in which an incomplete object type is
|
| 428 |
+
allowed, an array bound may be omitted in some cases in the declaration
|
| 429 |
+
of a function parameter [[dcl.fct]]. An array bound may also be omitted
|
| 430 |
+
when an object (but not a non-static data member) of array type is
|
| 431 |
+
initialized and the declarator is followed by an initializer (
|
| 432 |
+
[[dcl.init]], [[class.mem]], [[expr.type.conv]], [[expr.new]]). In these
|
| 433 |
+
cases, the array bound is calculated from the number of initial elements
|
| 434 |
+
(say, `N`) supplied [[dcl.init.aggr]], and the type of the array is
|
| 435 |
+
“array of `N` `U`”.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
|
| 437 |
+
Furthermore, if there is a preceding declaration of the entity in the
|
| 438 |
+
same scope in which the bound was specified, an omitted array bound is
|
| 439 |
+
taken to be the same as in that earlier declaration, and similarly for
|
| 440 |
+
the definition of a static data member of a class.
|
| 441 |
|
| 442 |
+
[*Example 3*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
|
| 444 |
``` cpp
|
| 445 |
extern int x[10];
|
| 446 |
struct S {
|
| 447 |
static int y[10];
|
|
|
|
| 456 |
}
|
| 457 |
```
|
| 458 |
|
| 459 |
— *end example*]
|
| 460 |
|
| 461 |
+
[*Note 3*:
|
|
|
|
|
|
|
| 462 |
|
| 463 |
+
When several “array of” specifications are adjacent, a multidimensional
|
| 464 |
+
array type is created; only the first of the constant expressions that
|
| 465 |
+
specify the bounds of the arrays may be omitted.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
|
| 467 |
+
[*Example 4*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
|
| 469 |
``` cpp
|
| 470 |
+
int x3d[3][5][7];
|
| 471 |
```
|
| 472 |
|
| 473 |
+
declares an array of three elements, each of which is an array of five
|
| 474 |
+
elements, each of which is an array of seven integers. The overall array
|
| 475 |
+
can be viewed as a three-dimensional array of integers, with rank
|
| 476 |
+
3 × 5 × 7. Any of the expressions `x3d`, `x3d[i]`, `x3d[i][j]`,
|
| 477 |
+
`x3d[i][j][k]` can reasonably appear in an expression. The expression
|
| 478 |
+
`x3d[i]` is equivalent to `*(x3d + i)`; in that expression, `x3d` is
|
| 479 |
+
subject to the array-to-pointer conversion [[conv.array]] and is first
|
| 480 |
+
converted to a pointer to a 2-dimensional array with rank 5 × 7 that
|
| 481 |
+
points to the first element of `x3d`. Then `i` is added, which on
|
| 482 |
+
typical implementations involves multiplying `i` by the length of the
|
| 483 |
+
object to which the pointer points, which is `sizeof(int)`× 5 × 7. The
|
| 484 |
+
result of the addition and indirection is an lvalue denoting the `i`ᵗʰ
|
| 485 |
+
array element of `x3d` (an array of five arrays of seven integers). If
|
| 486 |
+
there is another subscript, the same argument applies again, so
|
| 487 |
+
`x3d[i][j]` is an lvalue denoting the `j`ᵗʰ array element of the `i`ᵗʰ
|
| 488 |
+
array element of `x3d` (an array of seven integers), and `x3d[i][j][k]`
|
| 489 |
+
is an lvalue denoting the `k`ᵗʰ array element of the `j`ᵗʰ array element
|
| 490 |
+
of the `i`ᵗʰ array element of `x3d` (an integer).
|
| 491 |
|
| 492 |
— *end example*]
|
| 493 |
|
| 494 |
+
The first subscript in the declaration helps determine the amount of
|
| 495 |
+
storage consumed by an array but plays no other part in subscript
|
| 496 |
+
calculations.
|
| 497 |
+
|
| 498 |
— *end note*]
|
| 499 |
|
| 500 |
+
[*Note 4*: Conversions affecting expressions of array type are
|
| 501 |
+
described in [[conv.array]]. — *end note*]
|
|
|
|
|
|
|
| 502 |
|
| 503 |
+
[*Note 5*: The subscript operator can be overloaded for a class
|
| 504 |
+
[[over.sub]]. For the operator’s built-in meaning, see
|
| 505 |
+
[[expr.sub]]. — *end note*]
|
| 506 |
+
|
| 507 |
+
#### Functions <a id="dcl.fct">[[dcl.fct]]</a>
|
| 508 |
|
| 509 |
In a declaration `T` `D` where `D` has the form
|
| 510 |
|
| 511 |
``` bnf
|
| 512 |
+
'D1' '(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 513 |
ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 514 |
```
|
| 515 |
|
| 516 |
and the type of the contained *declarator-id* in the declaration `T`
|
| 517 |
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 518 |
+
*declarator-id* in `D` is “*derived-declarator-type-list* `noexcept`ₒₚₜ
|
| 519 |
+
function of parameter-type-list *cv-qualifier-seq*ₒₚₜ
|
| 520 |
+
*ref-qualifier*ₒₚₜ returning `T`”, where
|
| 521 |
+
|
| 522 |
+
- the parameter-type-list is derived from the
|
| 523 |
+
*parameter-declaration-clause* as described below and
|
| 524 |
+
- the optional `noexcept` is present if and only if the exception
|
| 525 |
+
specification [[except.spec]] is non-throwing.
|
| 526 |
+
|
| 527 |
+
The optional *attribute-specifier-seq* appertains to the function type.
|
| 528 |
|
| 529 |
In a declaration `T` `D` where `D` has the form
|
| 530 |
|
| 531 |
``` bnf
|
| 532 |
+
'D1' '(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 533 |
ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-type
|
| 534 |
```
|
| 535 |
|
| 536 |
and the type of the contained *declarator-id* in the declaration `T`
|
| 537 |
`D1` is “*derived-declarator-type-list* `T`”, `T` shall be the single
|
| 538 |
*type-specifier* `auto`. The type of the *declarator-id* in `D` is
|
| 539 |
+
“*derived-declarator-type-list* `noexcept`ₒₚₜ function of
|
| 540 |
+
parameter-type-list *cv-qualifier-seq*ₒₚₜ *ref-qualifier*ₒₚₜ returning
|
| 541 |
+
`U`”, where
|
|
|
|
|
|
|
|
|
|
| 542 |
|
| 543 |
+
- the parameter-type-list is derived from the
|
| 544 |
+
*parameter-declaration-clause* as described below,
|
| 545 |
+
- `U` is the type specified by the *trailing-return-type*, and
|
| 546 |
+
- the optional `noexcept` is present if and only if the exception
|
| 547 |
+
specification is non-throwing.
|
| 548 |
+
|
| 549 |
+
The optional *attribute-specifier-seq* appertains to the function type.
|
| 550 |
+
|
| 551 |
+
A type of either form is a *function type*.[^2]
|
| 552 |
|
| 553 |
``` bnf
|
| 554 |
parameter-declaration-clause:
|
| 555 |
parameter-declaration-listₒₚₜ '...'ₒₚₜ
|
| 556 |
+
parameter-declaration-list ',' '...'
|
| 557 |
```
|
| 558 |
|
| 559 |
``` bnf
|
| 560 |
parameter-declaration-list:
|
| 561 |
parameter-declaration
|
|
|
|
| 581 |
[[expr.call]]. — *end note*]
|
| 582 |
|
| 583 |
If the *parameter-declaration-clause* is empty, the function takes no
|
| 584 |
arguments. A parameter list consisting of a single unnamed parameter of
|
| 585 |
non-dependent type `void` is equivalent to an empty parameter list.
|
| 586 |
+
Except for this special case, a parameter shall not have type cv `void`.
|
| 587 |
+
A parameter with volatile-qualified type is deprecated; see
|
| 588 |
+
[[depr.volatile.type]]. If the *parameter-declaration-clause* terminates
|
| 589 |
+
with an ellipsis or a function parameter pack [[temp.variadic]], the
|
| 590 |
+
number of arguments shall be equal to or greater than the number of
|
| 591 |
+
parameters that do not have a default argument and are not function
|
| 592 |
+
parameter packs. Where syntactically correct and where “`...`” is not
|
| 593 |
+
part of an *abstract-declarator*, “`, ...`” is synonymous with “`...`”.
|
| 594 |
|
| 595 |
[*Example 1*:
|
| 596 |
|
| 597 |
The declaration
|
| 598 |
|
|
|
|
| 607 |
printf("hello world");
|
| 608 |
printf("a=%d b=%d", a, b);
|
| 609 |
```
|
| 610 |
|
| 611 |
However, the first argument must be of a type that can be converted to a
|
| 612 |
+
`const` `char*`.
|
| 613 |
|
| 614 |
— *end example*]
|
| 615 |
|
| 616 |
[*Note 2*: The standard header `<cstdarg>` contains a mechanism for
|
| 617 |
accessing arguments passed using the ellipsis (see [[expr.call]] and
|
| 618 |
[[support.runtime]]). — *end note*]
|
| 619 |
|
| 620 |
+
The type of a function is determined using the following rules. The type
|
| 621 |
+
of each parameter (including function parameter packs) is determined
|
| 622 |
+
from its own *decl-specifier-seq* and *declarator*. After determining
|
| 623 |
+
the type of each parameter, any parameter of type “array of `T`” or of
|
| 624 |
+
function type `T` is adjusted to be “pointer to `T`”. After producing
|
| 625 |
+
the list of parameter types, any top-level *cv-qualifier*s modifying a
|
| 626 |
+
parameter type are deleted when forming the function type. The resulting
|
| 627 |
+
list of transformed parameter types and the presence or absence of the
|
| 628 |
+
ellipsis or a function parameter pack is the function’s
|
| 629 |
+
*parameter-type-list*.
|
|
|
|
|
|
|
|
|
|
| 630 |
|
| 631 |
[*Note 3*: This transformation does not affect the types of the
|
| 632 |
parameters. For example, `int(*)(const int p, decltype(p)*)` and
|
| 633 |
`int(*)(int, const int*)` are identical types. — *end note*]
|
| 634 |
|
|
|
|
| 638 |
|
| 639 |
- the function type for a non-static member function,
|
| 640 |
- the function type to which a pointer to member refers,
|
| 641 |
- the top-level function type of a function typedef declaration or
|
| 642 |
*alias-declaration*,
|
| 643 |
+
- the *type-id* in the default argument of a *type-parameter*
|
| 644 |
+
[[temp.param]], or
|
| 645 |
+
- the *type-id* of a *template-argument* for a *type-parameter*
|
| 646 |
+
[[temp.arg.type]].
|
| 647 |
|
| 648 |
[*Example 2*:
|
| 649 |
|
| 650 |
``` cpp
|
| 651 |
typedef int FIC(int) const;
|
| 652 |
+
FIC f; // error: does not declare a member function
|
| 653 |
struct S {
|
| 654 |
FIC f; // OK
|
| 655 |
};
|
| 656 |
FIC S::*pm = &S::f; // OK
|
| 657 |
```
|
|
|
|
| 677 |
|
| 678 |
— *end example*]
|
| 679 |
|
| 680 |
The return type, the parameter-type-list, the *ref-qualifier*, the
|
| 681 |
*cv-qualifier-seq*, and the exception specification, but not the default
|
| 682 |
+
arguments [[dcl.fct.default]] or the trailing *requires-clause*
|
| 683 |
+
[[dcl.decl]], are part of the function type.
|
| 684 |
|
| 685 |
[*Note 5*: Function types are checked during the assignments and
|
| 686 |
initializations of pointers to functions, references to functions, and
|
| 687 |
pointers to member functions. — *end note*]
|
| 688 |
|
|
|
|
| 693 |
``` cpp
|
| 694 |
int fseek(FILE*, long, int);
|
| 695 |
```
|
| 696 |
|
| 697 |
declares a function taking three arguments of the specified types, and
|
| 698 |
+
returning `int` [[dcl.type]].
|
| 699 |
|
| 700 |
— *end example*]
|
| 701 |
|
| 702 |
+
A single name can be used for several different functions in a single
|
| 703 |
+
scope; this is function overloading [[over]]. All declarations for a
|
| 704 |
+
function shall have equivalent return types, parameter-type-lists, and
|
| 705 |
+
*requires-clause*s [[temp.over.link]].
|
| 706 |
+
|
| 707 |
Functions shall not have a return type of type array or function,
|
| 708 |
although they may have a return type of type pointer or reference to
|
| 709 |
such things. There shall be no arrays of functions, although there can
|
| 710 |
be arrays of pointers to functions.
|
| 711 |
|
| 712 |
+
A volatile-qualified return type is deprecated; see
|
| 713 |
+
[[depr.volatile.type]].
|
| 714 |
+
|
| 715 |
+
Types shall not be defined in return or parameter types.
|
|
|
|
| 716 |
|
| 717 |
A typedef of function type may be used to declare a function but shall
|
| 718 |
+
not be used to define a function [[dcl.fct.def]].
|
| 719 |
|
| 720 |
[*Example 5*:
|
| 721 |
|
| 722 |
``` cpp
|
| 723 |
typedef void F();
|
| 724 |
F fv; // OK: equivalent to void fv();
|
| 725 |
+
F fv { } // error
|
| 726 |
void fv() { } // OK: definition of fv
|
| 727 |
```
|
| 728 |
|
| 729 |
— *end example*]
|
| 730 |
|
| 731 |
An identifier can optionally be provided as a parameter name; if present
|
| 732 |
+
in a function definition [[dcl.fct.def]], it names a parameter.
|
| 733 |
|
| 734 |
[*Note 6*: In particular, parameter names are also optional in function
|
| 735 |
definitions and names used for a parameter in different declarations and
|
| 736 |
the definition of a function need not be the same. If a parameter name
|
| 737 |
is present in a function declaration that is not a definition, it cannot
|
| 738 |
be used outside of its function declarator because that is the extent of
|
| 739 |
+
its potential scope [[basic.scope.param]]. — *end note*]
|
| 740 |
|
| 741 |
[*Example 6*:
|
| 742 |
|
| 743 |
The declaration
|
| 744 |
|
|
|
|
| 803 |
A *non-template function* is a function that is not a function template
|
| 804 |
specialization.
|
| 805 |
|
| 806 |
[*Note 8*: A function template is not a function. — *end note*]
|
| 807 |
|
| 808 |
+
An *abbreviated function template* is a function declaration that has
|
| 809 |
+
one or more generic parameter type placeholders [[dcl.spec.auto]]. An
|
| 810 |
+
abbreviated function template is equivalent to a function template
|
| 811 |
+
[[temp.fct]] whose *template-parameter-list* includes one invented type
|
| 812 |
+
*template-parameter* for each generic parameter type placeholder of the
|
| 813 |
+
function declaration, in order of appearance. For a
|
| 814 |
+
*placeholder-type-specifier* of the form `auto`, the invented parameter
|
| 815 |
+
is an unconstrained *type-parameter*. For a *placeholder-type-specifier*
|
| 816 |
+
of the form *type-constraint* `auto`, the invented parameter is a
|
| 817 |
+
*type-parameter* with that *type-constraint*. The invented type
|
| 818 |
+
*template-parameter* is a template parameter pack if the corresponding
|
| 819 |
+
*parameter-declaration* declares a function parameter pack [[dcl.fct]].
|
| 820 |
+
If the placeholder contains `decltype(auto)`, the program is ill-formed.
|
| 821 |
+
The adjusted function parameters of an abbreviated function template are
|
| 822 |
+
derived from the *parameter-declaration-clause* by replacing each
|
| 823 |
+
occurrence of a placeholder with the name of the corresponding invented
|
| 824 |
+
*template-parameter*.
|
| 825 |
+
|
| 826 |
+
[*Example 7*:
|
| 827 |
+
|
| 828 |
+
``` cpp
|
| 829 |
+
template<typename T> concept C1 = /* ... */;
|
| 830 |
+
template<typename T> concept C2 = /* ... */;
|
| 831 |
+
template<typename... Ts> concept C3 = /* ... */;
|
| 832 |
+
|
| 833 |
+
void g1(const C1 auto*, C2 auto&);
|
| 834 |
+
void g2(C1 auto&...);
|
| 835 |
+
void g3(C3 auto...);
|
| 836 |
+
void g4(C3 auto);
|
| 837 |
+
```
|
| 838 |
+
|
| 839 |
+
These declarations are functionally equivalent (but not equivalent) to
|
| 840 |
+
the following declarations.
|
| 841 |
+
|
| 842 |
+
``` cpp
|
| 843 |
+
template<C1 T, C2 U> void g1(const T*, U&);
|
| 844 |
+
template<C1... Ts> void g2(Ts&...);
|
| 845 |
+
template<C3... Ts> void g3(Ts...);
|
| 846 |
+
template<C3 T> void g4(T);
|
| 847 |
+
```
|
| 848 |
+
|
| 849 |
+
Abbreviated function templates can be specialized like all function
|
| 850 |
+
templates.
|
| 851 |
+
|
| 852 |
+
``` cpp
|
| 853 |
+
template<> void g1<int>(const int*, const double&); // OK, specialization of g1<int, const double>
|
| 854 |
+
```
|
| 855 |
+
|
| 856 |
+
— *end example*]
|
| 857 |
+
|
| 858 |
+
An abbreviated function template can have a *template-head*. The
|
| 859 |
+
invented *template-parameters* are appended to the
|
| 860 |
+
*template-parameter-list* after the explicitly declared
|
| 861 |
+
*template-parameters*.
|
| 862 |
+
|
| 863 |
+
[*Example 8*:
|
| 864 |
+
|
| 865 |
+
``` cpp
|
| 866 |
+
template<typename> concept C = /* ... */;
|
| 867 |
+
|
| 868 |
+
template <typename T, C U>
|
| 869 |
+
void g(T x, U y, C auto z);
|
| 870 |
+
```
|
| 871 |
+
|
| 872 |
+
This is functionally equivalent to each of the following two
|
| 873 |
+
declarations.
|
| 874 |
+
|
| 875 |
+
``` cpp
|
| 876 |
+
template<typename T, C U, C W>
|
| 877 |
+
void g(T x, U y, W z);
|
| 878 |
+
|
| 879 |
+
template<typename T, typename U, typename W>
|
| 880 |
+
requires C<U> && C<W>
|
| 881 |
+
void g(T x, U y, W z);
|
| 882 |
+
```
|
| 883 |
+
|
| 884 |
+
— *end example*]
|
| 885 |
+
|
| 886 |
+
A function declaration at block scope shall not declare an abbreviated
|
| 887 |
+
function template.
|
| 888 |
+
|
| 889 |
A *declarator-id* or *abstract-declarator* containing an ellipsis shall
|
| 890 |
+
only be used in a *parameter-declaration*. When it is part of a
|
| 891 |
+
*parameter-declaration-clause*, the *parameter-declaration* declares a
|
| 892 |
+
function parameter pack [[temp.variadic]]. Otherwise, the
|
| 893 |
+
*parameter-declaration* is part of a *template-parameter-list* and
|
| 894 |
+
declares a template parameter pack; see [[temp.param]]. A function
|
| 895 |
+
parameter pack is a pack expansion [[temp.variadic]].
|
| 896 |
|
| 897 |
+
[*Example 9*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 898 |
|
| 899 |
``` cpp
|
| 900 |
template<typename... T> void f(T (* ...t)(int, int));
|
| 901 |
|
| 902 |
int add(int, int);
|
|
|
|
| 912 |
There is a syntactic ambiguity when an ellipsis occurs at the end of a
|
| 913 |
*parameter-declaration-clause* without a preceding comma. In this case,
|
| 914 |
the ellipsis is parsed as part of the *abstract-declarator* if the type
|
| 915 |
of the parameter either names a template parameter pack that has not
|
| 916 |
been expanded or contains `auto`; otherwise, it is parsed as part of the
|
| 917 |
+
*parameter-declaration-clause*.[^3]
|
| 918 |
|
| 919 |
+
#### Default arguments <a id="dcl.fct.default">[[dcl.fct.default]]</a>
|
| 920 |
|
| 921 |
If an *initializer-clause* is specified in a *parameter-declaration*
|
| 922 |
+
this *initializer-clause* is used as a default argument.
|
| 923 |
+
|
| 924 |
+
[*Note 1*: Default arguments will be used in calls where trailing
|
| 925 |
+
arguments are missing [[expr.call]]. — *end note*]
|
| 926 |
|
| 927 |
[*Example 1*:
|
| 928 |
|
| 929 |
The declaration
|
| 930 |
|
|
|
|
| 944 |
|
| 945 |
— *end example*]
|
| 946 |
|
| 947 |
A default argument shall be specified only in the
|
| 948 |
*parameter-declaration-clause* of a function declaration or
|
| 949 |
+
*lambda-declarator* or in a *template-parameter* [[temp.param]]; in the
|
| 950 |
+
latter case, the *initializer-clause* shall be an
|
| 951 |
*assignment-expression*. A default argument shall not be specified for a
|
| 952 |
+
template parameter pack or a function parameter pack. If it is specified
|
| 953 |
+
in a *parameter-declaration-clause*, it shall not occur within a
|
| 954 |
+
*declarator* or *abstract-declarator* of a *parameter-declaration*.[^4]
|
| 955 |
|
| 956 |
For non-template functions, default arguments can be added in later
|
| 957 |
declarations of a function in the same scope. Declarations in different
|
| 958 |
scopes have completely distinct sets of default arguments. That is,
|
| 959 |
declarations in inner scopes do not acquire default arguments from
|
| 960 |
declarations in outer scopes, and vice versa. In a given function
|
| 961 |
declaration, each parameter subsequent to a parameter with a default
|
| 962 |
argument shall have a default argument supplied in this or a previous
|
| 963 |
+
declaration, unless the parameter was expanded from a parameter pack, or
|
| 964 |
+
shall be a function parameter pack.
|
| 965 |
+
|
| 966 |
+
[*Note 2*: A default argument cannot be redefined by a later
|
| 967 |
+
declaration (not even to the same value)
|
| 968 |
+
[[basic.def.odr]]. — *end note*]
|
| 969 |
|
| 970 |
[*Example 2*:
|
| 971 |
|
| 972 |
``` cpp
|
| 973 |
void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow
|
|
|
|
| 986 |
void f(int, int = 5); // error: cannot redefine, even to same value
|
| 987 |
}
|
| 988 |
void n() {
|
| 989 |
f(6); // OK, calls f(6, 7)
|
| 990 |
}
|
| 991 |
+
template<class ... T> struct C {
|
| 992 |
+
void f(int n = 0, T...);
|
| 993 |
+
};
|
| 994 |
+
C<int> c; // OK, instantiates declaration void C::f(int n = 0, int)
|
| 995 |
```
|
| 996 |
|
| 997 |
— *end example*]
|
| 998 |
|
| 999 |
For a given inline function defined in different translation units, the
|
| 1000 |
accumulated sets of default arguments at the end of the translation
|
| 1001 |
+
units shall be the same; no diagnostic is required. If a friend
|
| 1002 |
+
declaration specifies a default argument expression, that declaration
|
| 1003 |
+
shall be a definition and shall be the only declaration of the function
|
| 1004 |
+
or function template in the translation unit.
|
| 1005 |
|
| 1006 |
The default argument has the same semantic constraints as the
|
| 1007 |
initializer in a declaration of a variable of the parameter type, using
|
| 1008 |
+
the copy-initialization semantics [[dcl.init]]. The names in the default
|
| 1009 |
+
argument are bound, and the semantic constraints are checked, at the
|
| 1010 |
+
point where the default argument appears. Name lookup and checking of
|
| 1011 |
+
semantic constraints for default arguments in function templates and in
|
| 1012 |
+
member functions of class templates are performed as described in
|
| 1013 |
[[temp.inst]].
|
| 1014 |
|
| 1015 |
[*Example 3*:
|
| 1016 |
|
| 1017 |
In the following code, `g` will be called with the value `f(2)`:
|
|
|
|
| 1030 |
}
|
| 1031 |
```
|
| 1032 |
|
| 1033 |
— *end example*]
|
| 1034 |
|
| 1035 |
+
[*Note 3*: In member function declarations, names in default arguments
|
| 1036 |
are looked up as described in [[basic.lookup.unqual]]. Access checking
|
| 1037 |
+
applies to names in default arguments as described in
|
| 1038 |
[[class.access]]. — *end note*]
|
| 1039 |
|
| 1040 |
Except for member functions of class templates, the default arguments in
|
| 1041 |
a member function definition that appears outside of the class
|
| 1042 |
definition are added to the set of default arguments provided by the
|
| 1043 |
member function declaration in the class definition; the program is
|
| 1044 |
+
ill-formed if a default constructor [[class.default.ctor]], copy or move
|
| 1045 |
+
constructor [[class.copy.ctor]], or copy or move assignment operator
|
| 1046 |
+
[[class.copy.assign]] is so declared. Default arguments for a member
|
| 1047 |
+
function of a class template shall be specified on the initial
|
| 1048 |
+
declaration of the member function within the class template.
|
| 1049 |
|
| 1050 |
[*Example 4*:
|
| 1051 |
|
| 1052 |
``` cpp
|
| 1053 |
class C {
|
|
|
|
| 1059 |
void C::g(int i = 88, int j) {} // in this translation unit, C::g can be called with no argument
|
| 1060 |
```
|
| 1061 |
|
| 1062 |
— *end example*]
|
| 1063 |
|
| 1064 |
+
[*Note 4*: A local variable cannot be odr-used [[basic.def.odr]] in a
|
| 1065 |
+
default argument. — *end note*]
|
| 1066 |
|
| 1067 |
[*Example 5*:
|
| 1068 |
|
| 1069 |
``` cpp
|
| 1070 |
void f() {
|
|
|
|
| 1075 |
}
|
| 1076 |
```
|
| 1077 |
|
| 1078 |
— *end example*]
|
| 1079 |
|
| 1080 |
+
[*Note 5*:
|
| 1081 |
|
| 1082 |
The keyword `this` may not appear in a default argument of a member
|
| 1083 |
function; see [[expr.prim.this]].
|
| 1084 |
|
| 1085 |
[*Example 6*:
|
|
|
|
| 1111 |
```
|
| 1112 |
|
| 1113 |
— *end example*]
|
| 1114 |
|
| 1115 |
A non-static member shall not appear in a default argument unless it
|
| 1116 |
+
appears as the *id-expression* of a class member access expression
|
| 1117 |
+
[[expr.ref]] or unless it is used to form a pointer to member
|
| 1118 |
+
[[expr.unary.op]].
|
| 1119 |
|
| 1120 |
[*Example 8*:
|
| 1121 |
|
| 1122 |
The declaration of `X::mem1()` in the following example is ill-formed
|
| 1123 |
because no object is supplied for the non-static member `X::a` used as
|
|
|
|
| 1133 |
};
|
| 1134 |
```
|
| 1135 |
|
| 1136 |
The declaration of `X::mem2()` is meaningful, however, since no object
|
| 1137 |
is needed to access the static member `X::b`. Classes, objects, and
|
| 1138 |
+
members are described in [[class]].
|
| 1139 |
|
| 1140 |
— *end example*]
|
| 1141 |
|
| 1142 |
A default argument is not part of the type of a function.
|
| 1143 |
|
|
|
|
| 1156 |
```
|
| 1157 |
|
| 1158 |
— *end example*]
|
| 1159 |
|
| 1160 |
When a declaration of a function is introduced by way of a
|
| 1161 |
+
*using-declaration* [[namespace.udecl]], any default argument
|
| 1162 |
information associated with the declaration is made known as well. If
|
| 1163 |
the function is redeclared thereafter in the namespace with additional
|
| 1164 |
default arguments, the additional arguments are also known at any point
|
| 1165 |
following the redeclaration where the *using-declaration* is in scope.
|
| 1166 |
|
| 1167 |
+
A virtual function call [[class.virtual]] uses the default arguments in
|
| 1168 |
+
the declaration of the virtual function determined by the static type of
|
| 1169 |
+
the pointer or reference denoting the object. An overriding function in
|
| 1170 |
+
a derived class does not acquire default arguments from the function it
|
| 1171 |
+
overrides.
|
| 1172 |
|
| 1173 |
[*Example 10*:
|
| 1174 |
|
| 1175 |
``` cpp
|
| 1176 |
struct A {
|