- tmp/tmpna7yauhj/{from.md → to.md} +341 -164
tmp/tmpna7yauhj/{from.md → to.md}
RENAMED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
## Declarators <a id="dcl.decl">[[dcl.decl]]</a>
|
| 2 |
|
|
|
|
|
|
|
| 3 |
A declarator declares a single variable, function, or type, within a
|
| 4 |
-
declaration. The *init-declarator-list* appearing in a
|
| 5 |
-
comma-separated sequence of declarators, each
|
| 6 |
-
initializer.
|
| 7 |
|
| 8 |
``` bnf
|
| 9 |
init-declarator-list:
|
| 10 |
init-declarator
|
| 11 |
init-declarator-list ',' init-declarator
|
|
@@ -15,28 +17,31 @@ init-declarator-list:
|
|
| 15 |
init-declarator:
|
| 16 |
declarator initializerₒₚₜ
|
| 17 |
declarator requires-clause
|
| 18 |
```
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
A declaration with several declarators is usually equivalent to the
|
| 36 |
corresponding sequence of declarations each with a single declarator.
|
| 37 |
-
That is
|
| 38 |
|
| 39 |
``` cpp
|
| 40 |
T D1, D2, ... Dn;
|
| 41 |
```
|
| 42 |
|
|
@@ -45,14 +50,14 @@ is usually equivalent to
|
|
| 45 |
``` cpp
|
| 46 |
T D1; T D2; ... T Dn;
|
| 47 |
```
|
| 48 |
|
| 49 |
where `T` is a *decl-specifier-seq* and each `Di` is an
|
| 50 |
-
*init-declarator*. One exception is when a name
|
| 51 |
-
*declarator*s hides a type name used by the
|
| 52 |
-
when the same *decl-specifier*s are used in a
|
| 53 |
-
they do not have the same meaning, as in
|
| 54 |
|
| 55 |
``` cpp
|
| 56 |
struct S { ... };
|
| 57 |
S S, T; // declare two instances of struct S
|
| 58 |
```
|
|
@@ -72,19 +77,19 @@ 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
|
| 78 |
-
auto j = 2.0; // OK
|
| 79 |
```
|
| 80 |
|
| 81 |
— *end note*]
|
| 82 |
|
| 83 |
-
The optional *requires-clause*
|
| 84 |
*member-declarator* shall be present only if the declarator declares a
|
| 85 |
-
templated function [[
|
| 86 |
*requires-clause* is called the *trailing *requires-clause**. The
|
| 87 |
trailing *requires-clause* introduces the *constraint-expression* that
|
| 88 |
results from interpreting its *constraint-logical-or-expression* as a
|
| 89 |
*constraint-expression*.
|
| 90 |
|
|
@@ -246,16 +251,17 @@ A type can also be named (often more easily) by using a `typedef`
|
|
| 246 |
|
| 247 |
### Ambiguity resolution <a id="dcl.ambig.res">[[dcl.ambig.res]]</a>
|
| 248 |
|
| 249 |
The ambiguity arising from the similarity between a function-style cast
|
| 250 |
and a declaration mentioned in [[stmt.ambig]] can also occur in the
|
| 251 |
-
context of a declaration. In that context, the choice is between
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
[[stmt.ambig]], the resolution is to consider any
|
| 256 |
-
|
|
|
|
| 257 |
|
| 258 |
[*Note 1*: A declaration can be explicitly disambiguated by adding
|
| 259 |
parentheses around the argument. The ambiguity can be avoided by use of
|
| 260 |
copy-initialization or list-initialization syntax, or by use of a
|
| 261 |
non-function-style cast. — *end note*]
|
|
@@ -338,41 +344,137 @@ void h(int *(C[10])); // void h(int *(*_fp)(C _parm[10]));
|
|
| 338 |
|
| 339 |
— *end example*]
|
| 340 |
|
| 341 |
### Meaning of declarators <a id="dcl.meaning">[[dcl.meaning]]</a>
|
| 342 |
|
| 343 |
-
|
| 344 |
-
identifier that is declared. An *unqualified-id* occurring in a
|
| 345 |
-
*declarator-id* shall be a simple *identifier* except for the
|
| 346 |
-
declaration of some special functions ([[class.ctor]], [[class.conv]],
|
| 347 |
-
[[class.dtor]], [[over.oper]]) and for the declaration of template
|
| 348 |
-
specializations or partial specializations [[temp.spec]]. When the
|
| 349 |
-
*declarator-id* is qualified, the declaration shall refer to a
|
| 350 |
-
previously declared member of the class or namespace to which the
|
| 351 |
-
qualifier refers (or, in the case of a namespace, of an element of the
|
| 352 |
-
inline namespace set of that namespace [[namespace.def]]) or to a
|
| 353 |
-
specialization thereof; the member shall not merely have been introduced
|
| 354 |
-
by a *using-declaration* in the scope of the class or namespace
|
| 355 |
-
nominated by the *nested-name-specifier* of the *declarator-id*. The
|
| 356 |
-
*nested-name-specifier* of a qualified *declarator-id* shall not begin
|
| 357 |
-
with a *decltype-specifier*.
|
| 358 |
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
|
| 363 |
The optional *attribute-specifier-seq* following a *declarator-id*
|
| 364 |
appertains to the entity that is declared.
|
| 365 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
A `static`, `thread_local`, `extern`, `mutable`, `friend`, `inline`,
|
| 367 |
-
`virtual`, `constexpr`, or `typedef` specifier
|
| 368 |
-
*explicit-specifier* applies directly to each *declarator-id* in
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
its *declarator*.
|
| 372 |
|
| 373 |
-
Thus,
|
| 374 |
|
| 375 |
``` cpp
|
| 376 |
T D
|
| 377 |
```
|
| 378 |
|
|
@@ -387,11 +489,11 @@ First, the *decl-specifier-seq* determines a type. In a declaration
|
|
| 387 |
T D
|
| 388 |
```
|
| 389 |
|
| 390 |
the *decl-specifier-seq* `T` determines the type `T`.
|
| 391 |
|
| 392 |
-
[*Example
|
| 393 |
|
| 394 |
In the declaration
|
| 395 |
|
| 396 |
``` cpp
|
| 397 |
int unsigned i;
|
|
@@ -401,11 +503,11 @@ the type specifiers `int` `unsigned` determine the type “`unsigned int`”
|
|
| 401 |
[[dcl.type.simple]].
|
| 402 |
|
| 403 |
— *end example*]
|
| 404 |
|
| 405 |
In a declaration *attribute-specifier-seq*ₒₚₜ `T` `D` where `D` is an
|
| 406 |
-
unadorned
|
| 407 |
|
| 408 |
In a declaration `T` `D` where `D` has the form
|
| 409 |
|
| 410 |
``` bnf
|
| 411 |
'(' 'D1' ')'
|
|
@@ -427,17 +529,17 @@ In a declaration `T` `D` where `D` has the form
|
|
| 427 |
|
| 428 |
``` bnf
|
| 429 |
'*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
| 430 |
```
|
| 431 |
|
| 432 |
-
and the type of the
|
| 433 |
-
“*derived-declarator-type-list* `T`”,
|
| 434 |
-
`D` is “*derived-declarator-type-list*
|
| 435 |
-
`T`”. The *cv-qualifier*s apply to the
|
| 436 |
-
pointed to. Similarly, the optional
|
| 437 |
-
[[dcl.attr.grammar]] appertains to the pointer
|
| 438 |
-
pointed to.
|
| 439 |
|
| 440 |
[*Example 1*:
|
| 441 |
|
| 442 |
The declarations
|
| 443 |
|
|
@@ -501,18 +603,18 @@ In a declaration `T` `D` where `D` has either of the forms
|
|
| 501 |
``` bnf
|
| 502 |
'&' attribute-specifier-seqₒₚₜ 'D1'
|
| 503 |
'&&' attribute-specifier-seqₒₚₜ 'D1'
|
| 504 |
```
|
| 505 |
|
| 506 |
-
and the type of the
|
| 507 |
-
“*derived-declarator-type-list* `T`”,
|
| 508 |
-
`D` is “*derived-declarator-type-list* reference to
|
| 509 |
-
*attribute-specifier-seq* appertains to the reference
|
| 510 |
-
references are ill-formed except when the
|
| 511 |
-
through the use of a *typedef-name*
|
| 512 |
-
|
| 513 |
-
cv-qualifiers are ignored.
|
| 514 |
|
| 515 |
[*Example 1*:
|
| 516 |
|
| 517 |
``` cpp
|
| 518 |
typedef int& A;
|
|
@@ -601,12 +703,12 @@ well-defined program, because the only way to create such a reference
|
|
| 601 |
would be to bind it to the “object” obtained by indirection through a
|
| 602 |
null pointer, which causes undefined behavior. As described in
|
| 603 |
[[class.bit]], a reference cannot be bound directly to a
|
| 604 |
bit-field. — *end note*]
|
| 605 |
|
| 606 |
-
If a *typedef-name*
|
| 607 |
-
*decltype-specifier* [[dcl.type.
|
| 608 |
reference to a type `T`, an attempt to create the type “lvalue reference
|
| 609 |
to cv `TR`” creates the type “lvalue reference to `T`”, while an attempt
|
| 610 |
to create the type “rvalue reference to cv `TR`” creates the type `TR`.
|
| 611 |
|
| 612 |
[*Note 3*: This rule is known as reference collapsing. — *end note*]
|
|
@@ -635,19 +737,22 @@ decltype(r2)&& r7 = i; // r7 has the type int&
|
|
| 635 |
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 636 |
[[dcl.fct]]. — *end note*]
|
| 637 |
|
| 638 |
#### Pointers to members <a id="dcl.mptr">[[dcl.mptr]]</a>
|
| 639 |
|
|
|
|
|
|
|
|
|
|
| 640 |
In a declaration `T` `D` where `D` has the form
|
| 641 |
|
| 642 |
``` bnf
|
| 643 |
nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
| 644 |
```
|
| 645 |
|
| 646 |
and the *nested-name-specifier* denotes a class, and the type of the
|
| 647 |
-
|
| 648 |
-
“*derived-declarator-type-list* `T`”,
|
| 649 |
`D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
|
| 650 |
member of class *nested-name-specifier* of type `T`”. The optional
|
| 651 |
*attribute-specifier-seq* [[dcl.attr.grammar]] appertains to the
|
| 652 |
pointer-to-member.
|
| 653 |
|
|
@@ -722,12 +827,12 @@ unknown bound of `T`”, except as specified below.
|
|
| 722 |
A type of the form “array of `N` `U`” or “array of unknown bound of `U`”
|
| 723 |
is an *array type*. The optional *attribute-specifier-seq* appertains to
|
| 724 |
the array type.
|
| 725 |
|
| 726 |
`U` is called the array *element type*; this type shall not be a
|
| 727 |
-
|
| 728 |
-
|
| 729 |
|
| 730 |
[*Note 1*: An array can be constructed from one of the fundamental
|
| 731 |
types (except `void`), from a pointer, from a pointer to member, from a
|
| 732 |
class, from an enumeration type, or from an array of known
|
| 733 |
bound. — *end note*]
|
|
@@ -758,39 +863,39 @@ typedef const AA CAA; // type is ``array of 2 array of 3 const int''
|
|
| 758 |
— *end example*]
|
| 759 |
|
| 760 |
[*Note 2*: An “array of `N` *cv-qualifier-seq* `U`” has cv-qualified
|
| 761 |
type; see [[basic.type.qualifier]]. — *end note*]
|
| 762 |
|
| 763 |
-
An object of type “array of `N` `U`”
|
| 764 |
-
non-empty set of `N` subobjects of type `U`, known as the
|
| 765 |
-
the array, and numbered `0` to `N-1`.
|
| 766 |
|
| 767 |
In addition to declarations in which an incomplete object type is
|
| 768 |
allowed, an array bound may be omitted in some cases in the declaration
|
| 769 |
of a function parameter [[dcl.fct]]. An array bound may also be omitted
|
| 770 |
when an object (but not a non-static data member) of array type is
|
| 771 |
-
initialized and the declarator is followed by an initializer
|
| 772 |
-
[[dcl.init]], [[class.mem]], [[expr.type.conv]], [[expr.new]]
|
| 773 |
cases, the array bound is calculated from the number of initial elements
|
| 774 |
(say, `N`) supplied [[dcl.init.aggr]], and the type of the array is
|
| 775 |
“array of `N` `U`”.
|
| 776 |
|
| 777 |
-
Furthermore, if there is a
|
| 778 |
-
same scope in which the bound was specified, an omitted
|
| 779 |
-
taken to be the same as in that earlier declaration, and
|
| 780 |
-
the definition of a static data member of a class.
|
| 781 |
|
| 782 |
[*Example 3*:
|
| 783 |
|
| 784 |
``` cpp
|
| 785 |
extern int x[10];
|
| 786 |
struct S {
|
| 787 |
static int y[10];
|
| 788 |
};
|
| 789 |
|
| 790 |
-
int x[]; // OK
|
| 791 |
-
int S::y[]; // OK
|
| 792 |
|
| 793 |
void f() {
|
| 794 |
extern int x[];
|
| 795 |
int i = sizeof(x); // error: incomplete object type
|
| 796 |
}
|
|
@@ -800,11 +905,11 @@ void f() {
|
|
| 800 |
|
| 801 |
[*Note 3*:
|
| 802 |
|
| 803 |
When several “array of” specifications are adjacent, a multidimensional
|
| 804 |
array type is created; only the first of the constant expressions that
|
| 805 |
-
specify the bounds of the arrays
|
| 806 |
|
| 807 |
[*Example 4*:
|
| 808 |
|
| 809 |
``` cpp
|
| 810 |
int x3d[3][5][7];
|
|
@@ -902,13 +1007,13 @@ parameter-declaration-list:
|
|
| 902 |
parameter-declaration-list ',' parameter-declaration
|
| 903 |
```
|
| 904 |
|
| 905 |
``` bnf
|
| 906 |
parameter-declaration:
|
| 907 |
-
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator
|
| 908 |
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator '=' initializer-clause
|
| 909 |
-
attribute-specifier-seqₒₚₜ decl-specifier-seq abstract-declaratorₒₚₜ
|
| 910 |
attribute-specifier-seqₒₚₜ decl-specifier-seq abstract-declaratorₒₚₜ '=' initializer-clause
|
| 911 |
```
|
| 912 |
|
| 913 |
The optional *attribute-specifier-seq* in a *parameter-declaration*
|
| 914 |
appertains to the parameter.
|
|
@@ -957,12 +1062,12 @@ However, the first argument must be of a type that can be converted to a
|
|
| 957 |
accessing arguments passed using the ellipsis (see [[expr.call]] and
|
| 958 |
[[support.runtime]]). — *end note*]
|
| 959 |
|
| 960 |
The type of a function is determined using the following rules. The type
|
| 961 |
of each parameter (including function parameter packs) is determined
|
| 962 |
-
from its own *
|
| 963 |
-
|
| 964 |
function type `T` is adjusted to be “pointer to `T`”. After producing
|
| 965 |
the list of parameter types, any top-level *cv-qualifier*s modifying a
|
| 966 |
parameter type are deleted when forming the function type. The resulting
|
| 967 |
list of transformed parameter types and the presence or absence of the
|
| 968 |
ellipsis or a function parameter pack is the function’s
|
|
@@ -970,24 +1075,94 @@ ellipsis or a function parameter pack is the function’s
|
|
| 970 |
|
| 971 |
[*Note 3*: This transformation does not affect the types of the
|
| 972 |
parameters. For example, `int(*)(const int p, decltype(p)*)` and
|
| 973 |
`int(*)(int, const int*)` are identical types. — *end note*]
|
| 974 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 975 |
A function type with a *cv-qualifier-seq* or a *ref-qualifier*
|
| 976 |
-
(including a type named by *typedef-name*
|
| 977 |
-
[[temp.param]])
|
| 978 |
|
| 979 |
- the function type for a non-static member function,
|
| 980 |
- the function type to which a pointer to member refers,
|
| 981 |
- the top-level function type of a function typedef declaration or
|
| 982 |
*alias-declaration*,
|
| 983 |
- the *type-id* in the default argument of a *type-parameter*
|
| 984 |
[[temp.param]], or
|
| 985 |
- the *type-id* of a *template-argument* for a *type-parameter*
|
| 986 |
[[temp.arg.type]].
|
| 987 |
|
| 988 |
-
[*Example
|
| 989 |
|
| 990 |
``` cpp
|
| 991 |
typedef int FIC(int) const;
|
| 992 |
FIC f; // error: does not declare a member function
|
| 993 |
struct S {
|
|
@@ -1000,35 +1175,35 @@ FIC S::*pm = &S::f; // OK
|
|
| 1000 |
|
| 1001 |
The effect of a *cv-qualifier-seq* in a function declarator is not the
|
| 1002 |
same as adding cv-qualification on top of the function type. In the
|
| 1003 |
latter case, the cv-qualifiers are ignored.
|
| 1004 |
|
| 1005 |
-
[*Note
|
| 1006 |
cv-qualified type; there are no cv-qualified function
|
| 1007 |
types. — *end note*]
|
| 1008 |
|
| 1009 |
-
[*Example
|
| 1010 |
|
| 1011 |
``` cpp
|
| 1012 |
typedef void F();
|
| 1013 |
struct S {
|
| 1014 |
-
const F f; // OK
|
| 1015 |
};
|
| 1016 |
```
|
| 1017 |
|
| 1018 |
— *end example*]
|
| 1019 |
|
| 1020 |
The return type, the parameter-type-list, the *ref-qualifier*, the
|
| 1021 |
*cv-qualifier-seq*, and the exception specification, but not the default
|
| 1022 |
arguments [[dcl.fct.default]] or the trailing *requires-clause*
|
| 1023 |
[[dcl.decl]], are part of the function type.
|
| 1024 |
|
| 1025 |
-
[*Note
|
| 1026 |
initializations of pointers to functions, references to functions, and
|
| 1027 |
pointers to member functions. — *end note*]
|
| 1028 |
|
| 1029 |
-
[*Example
|
| 1030 |
|
| 1031 |
The declaration
|
| 1032 |
|
| 1033 |
``` cpp
|
| 1034 |
int fseek(FILE*, long, int);
|
|
@@ -1037,50 +1212,46 @@ int fseek(FILE*, long, int);
|
|
| 1037 |
declares a function taking three arguments of the specified types, and
|
| 1038 |
returning `int` [[dcl.type]].
|
| 1039 |
|
| 1040 |
— *end example*]
|
| 1041 |
|
| 1042 |
-
A single name can be used for several different functions in
|
| 1043 |
-
scope; this is function overloading [[over]].
|
| 1044 |
-
function shall have equivalent return types, parameter-type-lists, and
|
| 1045 |
-
*requires-clause*s [[temp.over.link]].
|
| 1046 |
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
|
|
|
|
| 1051 |
|
| 1052 |
A volatile-qualified return type is deprecated; see
|
| 1053 |
[[depr.volatile.type]].
|
| 1054 |
|
| 1055 |
Types shall not be defined in return or parameter types.
|
| 1056 |
|
| 1057 |
A typedef of function type may be used to declare a function but shall
|
| 1058 |
not be used to define a function [[dcl.fct.def]].
|
| 1059 |
|
| 1060 |
-
[*Example
|
| 1061 |
|
| 1062 |
``` cpp
|
| 1063 |
typedef void F();
|
| 1064 |
-
F fv; // OK
|
| 1065 |
F fv { } // error
|
| 1066 |
-
void fv() { } // OK
|
| 1067 |
```
|
| 1068 |
|
| 1069 |
— *end example*]
|
| 1070 |
|
| 1071 |
An identifier can optionally be provided as a parameter name; if present
|
| 1072 |
in a function definition [[dcl.fct.def]], it names a parameter.
|
| 1073 |
|
| 1074 |
-
[*Note
|
| 1075 |
definitions and names used for a parameter in different declarations and
|
| 1076 |
-
the definition of a function need not be the same.
|
| 1077 |
-
is present in a function declaration that is not a definition, it cannot
|
| 1078 |
-
be used outside of its function declarator because that is the extent of
|
| 1079 |
-
its potential scope [[basic.scope.param]]. — *end note*]
|
| 1080 |
|
| 1081 |
-
[*Example
|
| 1082 |
|
| 1083 |
The declaration
|
| 1084 |
|
| 1085 |
``` cpp
|
| 1086 |
int i,
|
|
@@ -1106,15 +1277,15 @@ calling of a function `fpi`, and then using indirection through the
|
|
| 1106 |
to indicate that indirection through a pointer to a function yields a
|
| 1107 |
function, which is then called.
|
| 1108 |
|
| 1109 |
— *end example*]
|
| 1110 |
|
| 1111 |
-
[*Note
|
| 1112 |
|
| 1113 |
Typedefs and *trailing-return-type*s are sometimes convenient when the
|
| 1114 |
return type of a function is complex. For example, the function `fpif`
|
| 1115 |
-
above
|
| 1116 |
|
| 1117 |
``` cpp
|
| 1118 |
typedef int IFUNC(int);
|
| 1119 |
IFUNC* fpif(int);
|
| 1120 |
```
|
|
@@ -1141,11 +1312,11 @@ template <class T, class U> decltype((*(T*)0) + (*(U*)0)) add(T t, U u);
|
|
| 1141 |
— *end note*]
|
| 1142 |
|
| 1143 |
A *non-template function* is a function that is not a function template
|
| 1144 |
specialization.
|
| 1145 |
|
| 1146 |
-
[*Note
|
| 1147 |
|
| 1148 |
An *abbreviated function template* is a function declaration that has
|
| 1149 |
one or more generic parameter type placeholders [[dcl.spec.auto]]. An
|
| 1150 |
abbreviated function template is equivalent to a function template
|
| 1151 |
[[temp.fct]] whose *template-parameter-list* includes one invented type
|
|
@@ -1154,18 +1325,18 @@ function declaration, in order of appearance. For a
|
|
| 1154 |
*placeholder-type-specifier* of the form `auto`, the invented parameter
|
| 1155 |
is an unconstrained *type-parameter*. For a *placeholder-type-specifier*
|
| 1156 |
of the form *type-constraint* `auto`, the invented parameter is a
|
| 1157 |
*type-parameter* with that *type-constraint*. The invented type
|
| 1158 |
*template-parameter* is a template parameter pack if the corresponding
|
| 1159 |
-
*parameter-declaration* declares a function parameter pack
|
| 1160 |
-
|
| 1161 |
-
|
| 1162 |
derived from the *parameter-declaration-clause* by replacing each
|
| 1163 |
occurrence of a placeholder with the name of the corresponding invented
|
| 1164 |
*template-parameter*.
|
| 1165 |
|
| 1166 |
-
[*Example
|
| 1167 |
|
| 1168 |
``` cpp
|
| 1169 |
template<typename T> concept C1 = /* ... */;
|
| 1170 |
template<typename T> concept C2 = /* ... */;
|
| 1171 |
template<typename... Ts> concept C3 = /* ... */;
|
|
@@ -1174,12 +1345,12 @@ void g1(const C1 auto*, C2 auto&);
|
|
| 1174 |
void g2(C1 auto&...);
|
| 1175 |
void g3(C3 auto...);
|
| 1176 |
void g4(C3 auto);
|
| 1177 |
```
|
| 1178 |
|
| 1179 |
-
|
| 1180 |
-
|
| 1181 |
|
| 1182 |
``` cpp
|
| 1183 |
template<C1 T, C2 U> void g1(const T*, U&);
|
| 1184 |
template<C1... Ts> void g2(Ts&...);
|
| 1185 |
template<C3... Ts> void g3(Ts...);
|
|
@@ -1194,15 +1365,15 @@ template<> void g1<int>(const int*, const double&); // OK, specialization of g1<
|
|
| 1194 |
```
|
| 1195 |
|
| 1196 |
— *end example*]
|
| 1197 |
|
| 1198 |
An abbreviated function template can have a *template-head*. The
|
| 1199 |
-
invented *template-
|
| 1200 |
*template-parameter-list* after the explicitly declared
|
| 1201 |
-
*template-
|
| 1202 |
|
| 1203 |
-
[*Example
|
| 1204 |
|
| 1205 |
``` cpp
|
| 1206 |
template<typename> concept C = /* ... */;
|
| 1207 |
|
| 1208 |
template <typename T, C U>
|
|
@@ -1232,11 +1403,11 @@ only be used in a *parameter-declaration*. When it is part of a
|
|
| 1232 |
function parameter pack [[temp.variadic]]. Otherwise, the
|
| 1233 |
*parameter-declaration* is part of a *template-parameter-list* and
|
| 1234 |
declares a template parameter pack; see [[temp.param]]. A function
|
| 1235 |
parameter pack is a pack expansion [[temp.variadic]].
|
| 1236 |
|
| 1237 |
-
[*Example
|
| 1238 |
|
| 1239 |
``` cpp
|
| 1240 |
template<typename... T> void f(T (* ...t)(int, int));
|
| 1241 |
|
| 1242 |
int add(int, int);
|
|
@@ -1292,18 +1463,18 @@ latter case, the *initializer-clause* shall be an
|
|
| 1292 |
template parameter pack or a function parameter pack. If it is specified
|
| 1293 |
in a *parameter-declaration-clause*, it shall not occur within a
|
| 1294 |
*declarator* or *abstract-declarator* of a *parameter-declaration*.[^4]
|
| 1295 |
|
| 1296 |
For non-template functions, default arguments can be added in later
|
| 1297 |
-
declarations of a function
|
| 1298 |
-
scopes have completely distinct sets of default
|
| 1299 |
-
declarations in inner scopes do not acquire default
|
| 1300 |
-
declarations in outer scopes, and vice versa. In a given
|
| 1301 |
-
declaration, each parameter subsequent to a parameter with a
|
| 1302 |
-
argument shall have a default argument supplied in this or a
|
| 1303 |
-
declaration, unless the parameter was expanded from a parameter
|
| 1304 |
-
shall be a function parameter pack.
|
| 1305 |
|
| 1306 |
[*Note 2*: A default argument cannot be redefined by a later
|
| 1307 |
declaration (not even to the same value)
|
| 1308 |
[[basic.def.odr]]. — *end note*]
|
| 1309 |
|
|
@@ -1337,22 +1508,26 @@ C<int> c; // OK, instantiates declaration void C::f(int n
|
|
| 1337 |
— *end example*]
|
| 1338 |
|
| 1339 |
For a given inline function defined in different translation units, the
|
| 1340 |
accumulated sets of default arguments at the end of the translation
|
| 1341 |
units shall be the same; no diagnostic is required. If a friend
|
| 1342 |
-
declaration specifies a default argument expression, that declaration
|
| 1343 |
-
shall be a definition and shall be
|
| 1344 |
-
or function template
|
|
|
|
| 1345 |
|
| 1346 |
The default argument has the same semantic constraints as the
|
| 1347 |
initializer in a declaration of a variable of the parameter type, using
|
| 1348 |
the copy-initialization semantics [[dcl.init]]. The names in the default
|
| 1349 |
-
argument are
|
| 1350 |
-
point where the default argument appears
|
| 1351 |
-
|
| 1352 |
-
|
| 1353 |
-
|
|
|
|
|
|
|
|
|
|
| 1354 |
|
| 1355 |
[*Example 3*:
|
| 1356 |
|
| 1357 |
In the following code, `g` will be called with the value `f(2)`:
|
| 1358 |
|
|
@@ -1370,14 +1545,13 @@ void h() {
|
|
| 1370 |
}
|
| 1371 |
```
|
| 1372 |
|
| 1373 |
— *end example*]
|
| 1374 |
|
| 1375 |
-
[*Note 3*:
|
| 1376 |
-
|
| 1377 |
-
|
| 1378 |
-
[[class.access]]. — *end note*]
|
| 1379 |
|
| 1380 |
Except for member functions of class templates, the default arguments in
|
| 1381 |
a member function definition that appears outside of the class
|
| 1382 |
definition are added to the set of default arguments provided by the
|
| 1383 |
member function declaration in the class definition; the program is
|
|
@@ -1394,16 +1568,16 @@ class C {
|
|
| 1394 |
void f(int i = 3);
|
| 1395 |
void g(int i, int j = 99);
|
| 1396 |
};
|
| 1397 |
|
| 1398 |
void C::f(int i = 3) {} // error: default argument already specified in class scope
|
| 1399 |
-
void C::g(int i = 88, int j) {} // in this translation unit, C::g can be called with no
|
| 1400 |
```
|
| 1401 |
|
| 1402 |
— *end example*]
|
| 1403 |
|
| 1404 |
-
[*Note 4*: A local variable cannot be odr-used [[
|
| 1405 |
default argument. — *end note*]
|
| 1406 |
|
| 1407 |
[*Example 5*:
|
| 1408 |
|
| 1409 |
``` cpp
|
|
@@ -1417,11 +1591,11 @@ void f() {
|
|
| 1417 |
|
| 1418 |
— *end example*]
|
| 1419 |
|
| 1420 |
[*Note 5*:
|
| 1421 |
|
| 1422 |
-
The keyword `this`
|
| 1423 |
function; see [[expr.prim.this]].
|
| 1424 |
|
| 1425 |
[*Example 6*:
|
| 1426 |
|
| 1427 |
``` cpp
|
|
@@ -1434,22 +1608,24 @@ class A {
|
|
| 1434 |
|
| 1435 |
— *end note*]
|
| 1436 |
|
| 1437 |
A default argument is evaluated each time the function is called with no
|
| 1438 |
argument for the corresponding parameter. A parameter shall not appear
|
| 1439 |
-
as a potentially-evaluated expression in a default argument.
|
| 1440 |
-
|
| 1441 |
-
|
|
|
|
|
|
|
| 1442 |
|
| 1443 |
[*Example 7*:
|
| 1444 |
|
| 1445 |
``` cpp
|
| 1446 |
int a;
|
| 1447 |
int f(int a, int b = a); // error: parameter a used as default argument
|
| 1448 |
typedef int I;
|
| 1449 |
int g(float I, int b = I(2)); // error: parameter I found
|
| 1450 |
-
int h(int a, int b = sizeof(a)); // OK, unevaluated operand
|
| 1451 |
```
|
| 1452 |
|
| 1453 |
— *end example*]
|
| 1454 |
|
| 1455 |
A non-static member shall not appear in a default argument unless it
|
|
@@ -1495,16 +1671,17 @@ int (*p1)(int) = &f;
|
|
| 1495 |
int (*p2)() = &f; // error: type mismatch
|
| 1496 |
```
|
| 1497 |
|
| 1498 |
— *end example*]
|
| 1499 |
|
| 1500 |
-
When a declaration of a function
|
| 1501 |
-
|
| 1502 |
-
|
| 1503 |
-
|
| 1504 |
-
|
| 1505 |
-
|
|
|
|
| 1506 |
|
| 1507 |
A virtual function call [[class.virtual]] uses the default arguments in
|
| 1508 |
the declaration of the virtual function determined by the static type of
|
| 1509 |
the pointer or reference denoting the object. An overriding function in
|
| 1510 |
a derived class does not acquire default arguments from the function it
|
|
|
|
| 1 |
## Declarators <a id="dcl.decl">[[dcl.decl]]</a>
|
| 2 |
|
| 3 |
+
### General <a id="dcl.decl.general">[[dcl.decl.general]]</a>
|
| 4 |
+
|
| 5 |
A declarator declares a single variable, function, or type, within a
|
| 6 |
+
declaration. The *init-declarator-list* appearing in a
|
| 7 |
+
*simple-declaration* is a comma-separated sequence of declarators, each
|
| 8 |
+
of which can have an initializer.
|
| 9 |
|
| 10 |
``` bnf
|
| 11 |
init-declarator-list:
|
| 12 |
init-declarator
|
| 13 |
init-declarator-list ',' init-declarator
|
|
|
|
| 17 |
init-declarator:
|
| 18 |
declarator initializerₒₚₜ
|
| 19 |
declarator requires-clause
|
| 20 |
```
|
| 21 |
|
| 22 |
+
In all contexts, a *declarator* is interpreted as given below. Where an
|
| 23 |
+
*abstract-declarator* can be used (or omitted) in place of a
|
| 24 |
+
*declarator* [[dcl.fct]], [[except.pre]], it is as if a unique
|
| 25 |
+
identifier were included in the appropriate place [[dcl.name]]. The
|
| 26 |
+
preceding specifiers indicate the type, storage class or other
|
| 27 |
+
properties of the entity or entities being declared. Each declarator
|
| 28 |
+
specifies one entity and (optionally) names it and/or modifies the type
|
| 29 |
+
of the specifiers with operators such as `*` (pointer to) and `()`
|
| 30 |
+
(function returning).
|
| 31 |
|
| 32 |
+
[*Note 1*: An *init-declarator* can also specify an initializer
|
| 33 |
+
[[dcl.init]]. — *end note*]
|
| 34 |
|
| 35 |
+
Each *init-declarator* or *member-declarator* in a declaration is
|
| 36 |
+
analyzed separately as if it were in a declaration by itself.
|
| 37 |
+
|
| 38 |
+
[*Note 2*:
|
| 39 |
|
| 40 |
A declaration with several declarators is usually equivalent to the
|
| 41 |
corresponding sequence of declarations each with a single declarator.
|
| 42 |
+
That is,
|
| 43 |
|
| 44 |
``` cpp
|
| 45 |
T D1, D2, ... Dn;
|
| 46 |
```
|
| 47 |
|
|
|
|
| 50 |
``` cpp
|
| 51 |
T D1; T D2; ... T Dn;
|
| 52 |
```
|
| 53 |
|
| 54 |
where `T` is a *decl-specifier-seq* and each `Di` is an
|
| 55 |
+
*init-declarator* or *member-declarator*. One exception is when a name
|
| 56 |
+
introduced by one of the *declarator*s hides a type name used by the
|
| 57 |
+
*decl-specifier*s, so that when the same *decl-specifier*s are used in a
|
| 58 |
+
subsequent declaration, they do not have the same meaning, as in
|
| 59 |
|
| 60 |
``` cpp
|
| 61 |
struct S { ... };
|
| 62 |
S S, T; // declare two instances of struct S
|
| 63 |
```
|
|
|
|
| 77 |
```
|
| 78 |
|
| 79 |
as opposed to
|
| 80 |
|
| 81 |
``` cpp
|
| 82 |
+
auto i = 1; // OK, i deduced to have type int
|
| 83 |
+
auto j = 2.0; // OK, j deduced to have type double
|
| 84 |
```
|
| 85 |
|
| 86 |
— *end note*]
|
| 87 |
|
| 88 |
+
The optional *requires-clause* in an *init-declarator* or
|
| 89 |
*member-declarator* shall be present only if the declarator declares a
|
| 90 |
+
templated function [[temp.pre]]. When present after a declarator, the
|
| 91 |
*requires-clause* is called the *trailing *requires-clause**. The
|
| 92 |
trailing *requires-clause* introduces the *constraint-expression* that
|
| 93 |
results from interpreting its *constraint-logical-or-expression* as a
|
| 94 |
*constraint-expression*.
|
| 95 |
|
|
|
|
| 251 |
|
| 252 |
### Ambiguity resolution <a id="dcl.ambig.res">[[dcl.ambig.res]]</a>
|
| 253 |
|
| 254 |
The ambiguity arising from the similarity between a function-style cast
|
| 255 |
and a declaration mentioned in [[stmt.ambig]] can also occur in the
|
| 256 |
+
context of a declaration. In that context, the choice is between an
|
| 257 |
+
object declaration with a function-style cast as the initializer and a
|
| 258 |
+
declaration involving a function declarator with a redundant set of
|
| 259 |
+
parentheses around a parameter name. Just as for the ambiguities
|
| 260 |
+
mentioned in [[stmt.ambig]], the resolution is to consider any
|
| 261 |
+
construct, such as the potential parameter declaration, that could
|
| 262 |
+
possibly be a declaration to be a declaration.
|
| 263 |
|
| 264 |
[*Note 1*: A declaration can be explicitly disambiguated by adding
|
| 265 |
parentheses around the argument. The ambiguity can be avoided by use of
|
| 266 |
copy-initialization or list-initialization syntax, or by use of a
|
| 267 |
non-function-style cast. — *end note*]
|
|
|
|
| 344 |
|
| 345 |
— *end example*]
|
| 346 |
|
| 347 |
### Meaning of declarators <a id="dcl.meaning">[[dcl.meaning]]</a>
|
| 348 |
|
| 349 |
+
#### General <a id="dcl.meaning.general">[[dcl.meaning.general]]</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
|
| 351 |
+
A declarator contains exactly one *declarator-id*; it names the entity
|
| 352 |
+
that is declared. If the *unqualified-id* occurring in a *declarator-id*
|
| 353 |
+
is a *template-id*, the declarator shall appear in the *declaration* of
|
| 354 |
+
a *template-declaration* [[temp.decls]], *explicit-specialization*
|
| 355 |
+
[[temp.expl.spec]], or *explicit-instantiation* [[temp.explicit]].
|
| 356 |
+
|
| 357 |
+
[*Note 1*: An *unqualified-id* that is not an *identifier* is used to
|
| 358 |
+
declare certain functions
|
| 359 |
+
[[class.conv.fct]], [[class.dtor]], [[over.oper]], [[over.literal]]. — *end note*]
|
| 360 |
|
| 361 |
The optional *attribute-specifier-seq* following a *declarator-id*
|
| 362 |
appertains to the entity that is declared.
|
| 363 |
|
| 364 |
+
If the declaration is a friend declaration:
|
| 365 |
+
|
| 366 |
+
- The *declarator* does not bind a name.
|
| 367 |
+
- If the *id-expression* E in the *declarator-id* of the *declarator* is
|
| 368 |
+
a *qualified-id* or a *template-id*:
|
| 369 |
+
- If the friend declaration is not a template declaration, then in the
|
| 370 |
+
lookup for the terminal name of E:
|
| 371 |
+
- if the *unqualified-id* in E is a *template-id*, all function
|
| 372 |
+
declarations are discarded;
|
| 373 |
+
- otherwise, if the *declarator* corresponds [[basic.scope.scope]]
|
| 374 |
+
to any declaration found of a non-template function, all function
|
| 375 |
+
template declarations are discarded;
|
| 376 |
+
- each remaining function template is replaced with the
|
| 377 |
+
specialization chosen by deduction from the friend declaration
|
| 378 |
+
[[temp.deduct.decl]] or discarded if deduction fails.
|
| 379 |
+
- The *declarator* shall correspond to one or more declarations found
|
| 380 |
+
by the lookup; they shall all have the same target scope, and the
|
| 381 |
+
target scope of the *declarator* is that scope.
|
| 382 |
+
- Otherwise, the terminal name of E is not looked up. The declaration’s
|
| 383 |
+
target scope is the innermost enclosing namespace scope; if the
|
| 384 |
+
declaration is contained by a block scope, the declaration shall
|
| 385 |
+
correspond to a reachable [[module.reach]] declaration that inhabits
|
| 386 |
+
the innermost block scope.
|
| 387 |
+
|
| 388 |
+
Otherwise:
|
| 389 |
+
|
| 390 |
+
- If the *id-expression* in the *declarator-id* of the *declarator* is a
|
| 391 |
+
*qualified-id* Q, let S be its lookup context [[basic.lookup.qual]];
|
| 392 |
+
the declaration shall inhabit a namespace scope.
|
| 393 |
+
- Otherwise, let S be the entity associated with the scope inhabited by
|
| 394 |
+
the *declarator*.
|
| 395 |
+
- If the *declarator* declares an explicit instantiation or a partial or
|
| 396 |
+
explicit specialization, the *declarator* does not bind a name. If it
|
| 397 |
+
declares a class member, the terminal name of the *declarator-id* is
|
| 398 |
+
not looked up; otherwise, only those lookup results that are nominable
|
| 399 |
+
in S are considered when identifying any function template
|
| 400 |
+
specialization being declared [[temp.deduct.decl]].
|
| 401 |
+
\[*Example 1*:
|
| 402 |
+
``` cpp
|
| 403 |
+
namespace N {
|
| 404 |
+
inline namespace O {
|
| 405 |
+
template<class T> void f(T); // #1
|
| 406 |
+
template<class T> void g(T) {}
|
| 407 |
+
}
|
| 408 |
+
namespace P {
|
| 409 |
+
template<class T> void f(T*); // #2, more specialized than #1
|
| 410 |
+
template<class> int g;
|
| 411 |
+
}
|
| 412 |
+
using P::f,P::g;
|
| 413 |
+
}
|
| 414 |
+
template<> void N::f(int*) {} // OK, #2 is not nominable
|
| 415 |
+
template void N::g(int); // error: lookup is ambiguous
|
| 416 |
+
```
|
| 417 |
+
|
| 418 |
+
— *end example*]
|
| 419 |
+
- Otherwise, the terminal name of the *declarator-id* is not looked up.
|
| 420 |
+
If it is a qualified name, the *declarator* shall correspond to one or
|
| 421 |
+
more declarations nominable in S; all the declarations shall have the
|
| 422 |
+
same target scope and the target scope of the *declarator* is that
|
| 423 |
+
scope.
|
| 424 |
+
\[*Example 2*:
|
| 425 |
+
``` cpp
|
| 426 |
+
namespace Q {
|
| 427 |
+
namespace V {
|
| 428 |
+
void f();
|
| 429 |
+
}
|
| 430 |
+
void V::f() { ... } // OK
|
| 431 |
+
void V::g() { ... } // error: g() is not yet a member of V
|
| 432 |
+
namespace V {
|
| 433 |
+
void g();
|
| 434 |
+
}
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
namespace R {
|
| 438 |
+
void Q::V::g() { ... } // error: R doesn't enclose Q
|
| 439 |
+
}
|
| 440 |
+
```
|
| 441 |
+
|
| 442 |
+
— *end example*]
|
| 443 |
+
- If the declaration inhabits a block scope S and declares a function
|
| 444 |
+
[[dcl.fct]] or uses the `extern` specifier, the declaration shall not
|
| 445 |
+
be attached to a named module [[module.unit]]; its target scope is the
|
| 446 |
+
innermost enclosing namespace scope, but the name is bound in S.
|
| 447 |
+
\[*Example 3*:
|
| 448 |
+
``` cpp
|
| 449 |
+
namespace X {
|
| 450 |
+
void p() {
|
| 451 |
+
q(); // error: q not yet declared
|
| 452 |
+
extern void q(); // q is a member of namespace X
|
| 453 |
+
extern void r(); // r is a member of namespace X
|
| 454 |
+
}
|
| 455 |
+
|
| 456 |
+
void middle() {
|
| 457 |
+
q(); // error: q not found
|
| 458 |
+
}
|
| 459 |
+
|
| 460 |
+
void q() { ... } // definition of X::q
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
void q() { ... } // some other, unrelated q
|
| 464 |
+
void X::r() { ... } // error: r cannot be declared by qualified-id
|
| 465 |
+
```
|
| 466 |
+
|
| 467 |
+
— *end example*]
|
| 468 |
+
|
| 469 |
A `static`, `thread_local`, `extern`, `mutable`, `friend`, `inline`,
|
| 470 |
+
`virtual`, `constexpr`, `consteval`, `constinit`, or `typedef` specifier
|
| 471 |
+
or an *explicit-specifier* applies directly to each *declarator-id* in a
|
| 472 |
+
declaration; the type specified for each *declarator-id* depends on both
|
| 473 |
+
the *decl-specifier-seq* and its *declarator*.
|
|
|
|
| 474 |
|
| 475 |
+
Thus, (for each *declarator*) a declaration has the form
|
| 476 |
|
| 477 |
``` cpp
|
| 478 |
T D
|
| 479 |
```
|
| 480 |
|
|
|
|
| 489 |
T D
|
| 490 |
```
|
| 491 |
|
| 492 |
the *decl-specifier-seq* `T` determines the type `T`.
|
| 493 |
|
| 494 |
+
[*Example 4*:
|
| 495 |
|
| 496 |
In the declaration
|
| 497 |
|
| 498 |
``` cpp
|
| 499 |
int unsigned i;
|
|
|
|
| 503 |
[[dcl.type.simple]].
|
| 504 |
|
| 505 |
— *end example*]
|
| 506 |
|
| 507 |
In a declaration *attribute-specifier-seq*ₒₚₜ `T` `D` where `D` is an
|
| 508 |
+
unadorned name, the type of the declared entity is “`T`”.
|
| 509 |
|
| 510 |
In a declaration `T` `D` where `D` has the form
|
| 511 |
|
| 512 |
``` bnf
|
| 513 |
'(' 'D1' ')'
|
|
|
|
| 529 |
|
| 530 |
``` bnf
|
| 531 |
'*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
| 532 |
```
|
| 533 |
|
| 534 |
+
and the type of the contained *declarator-id* in the declaration `T`
|
| 535 |
+
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 536 |
+
*declarator-id* in `D` is “*derived-declarator-type-list*
|
| 537 |
+
*cv-qualifier-seq* pointer to `T`”. The *cv-qualifier*s apply to the
|
| 538 |
+
pointer and not to the object pointed to. Similarly, the optional
|
| 539 |
+
*attribute-specifier-seq* [[dcl.attr.grammar]] appertains to the pointer
|
| 540 |
+
and not to the object pointed to.
|
| 541 |
|
| 542 |
[*Example 1*:
|
| 543 |
|
| 544 |
The declarations
|
| 545 |
|
|
|
|
| 603 |
``` bnf
|
| 604 |
'&' attribute-specifier-seqₒₚₜ 'D1'
|
| 605 |
'&&' attribute-specifier-seqₒₚₜ 'D1'
|
| 606 |
```
|
| 607 |
|
| 608 |
+
and the type of the contained *declarator-id* in the declaration `T`
|
| 609 |
+
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 610 |
+
*declarator-id* in `D` is “*derived-declarator-type-list* reference to
|
| 611 |
+
`T`”. The optional *attribute-specifier-seq* appertains to the reference
|
| 612 |
+
type. Cv-qualified references are ill-formed except when the
|
| 613 |
+
cv-qualifiers are introduced through the use of a *typedef-name*
|
| 614 |
+
[[dcl.typedef]], [[temp.param]] or *decltype-specifier*
|
| 615 |
+
[[dcl.type.decltype]], in which case the cv-qualifiers are ignored.
|
| 616 |
|
| 617 |
[*Example 1*:
|
| 618 |
|
| 619 |
``` cpp
|
| 620 |
typedef int& A;
|
|
|
|
| 703 |
would be to bind it to the “object” obtained by indirection through a
|
| 704 |
null pointer, which causes undefined behavior. As described in
|
| 705 |
[[class.bit]], a reference cannot be bound directly to a
|
| 706 |
bit-field. — *end note*]
|
| 707 |
|
| 708 |
+
If a *typedef-name* [[dcl.typedef]], [[temp.param]] or a
|
| 709 |
+
*decltype-specifier* [[dcl.type.decltype]] denotes a type `TR` that is a
|
| 710 |
reference to a type `T`, an attempt to create the type “lvalue reference
|
| 711 |
to cv `TR`” creates the type “lvalue reference to `T`”, while an attempt
|
| 712 |
to create the type “rvalue reference to cv `TR`” creates the type `TR`.
|
| 713 |
|
| 714 |
[*Note 3*: This rule is known as reference collapsing. — *end note*]
|
|
|
|
| 737 |
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 738 |
[[dcl.fct]]. — *end note*]
|
| 739 |
|
| 740 |
#### Pointers to members <a id="dcl.mptr">[[dcl.mptr]]</a>
|
| 741 |
|
| 742 |
+
The component names of a *ptr-operator* are those of its
|
| 743 |
+
*nested-name-specifier*, if any.
|
| 744 |
+
|
| 745 |
In a declaration `T` `D` where `D` has the form
|
| 746 |
|
| 747 |
``` bnf
|
| 748 |
nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
| 749 |
```
|
| 750 |
|
| 751 |
and the *nested-name-specifier* denotes a class, and the type of the
|
| 752 |
+
contained *declarator-id* in the declaration `T` `D1` is
|
| 753 |
+
“*derived-declarator-type-list* `T`”, the type of the *declarator-id* in
|
| 754 |
`D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
|
| 755 |
member of class *nested-name-specifier* of type `T`”. The optional
|
| 756 |
*attribute-specifier-seq* [[dcl.attr.grammar]] appertains to the
|
| 757 |
pointer-to-member.
|
| 758 |
|
|
|
|
| 827 |
A type of the form “array of `N` `U`” or “array of unknown bound of `U`”
|
| 828 |
is an *array type*. The optional *attribute-specifier-seq* appertains to
|
| 829 |
the array type.
|
| 830 |
|
| 831 |
`U` is called the array *element type*; this type shall not be a
|
| 832 |
+
reference type, a function type, an array of unknown bound, or
|
| 833 |
+
cv `void`.
|
| 834 |
|
| 835 |
[*Note 1*: An array can be constructed from one of the fundamental
|
| 836 |
types (except `void`), from a pointer, from a pointer to member, from a
|
| 837 |
class, from an enumeration type, or from an array of known
|
| 838 |
bound. — *end note*]
|
|
|
|
| 863 |
— *end example*]
|
| 864 |
|
| 865 |
[*Note 2*: An “array of `N` *cv-qualifier-seq* `U`” has cv-qualified
|
| 866 |
type; see [[basic.type.qualifier]]. — *end note*]
|
| 867 |
|
| 868 |
+
An object of type “array of `N` `U`” consists of a contiguously
|
| 869 |
+
allocated non-empty set of `N` subobjects of type `U`, known as the
|
| 870 |
+
*elements* of the array, and numbered `0` to `N-1`.
|
| 871 |
|
| 872 |
In addition to declarations in which an incomplete object type is
|
| 873 |
allowed, an array bound may be omitted in some cases in the declaration
|
| 874 |
of a function parameter [[dcl.fct]]. An array bound may also be omitted
|
| 875 |
when an object (but not a non-static data member) of array type is
|
| 876 |
+
initialized and the declarator is followed by an initializer
|
| 877 |
+
[[dcl.init]], [[class.mem]], [[expr.type.conv]], [[expr.new]]. In these
|
| 878 |
cases, the array bound is calculated from the number of initial elements
|
| 879 |
(say, `N`) supplied [[dcl.init.aggr]], and the type of the array is
|
| 880 |
“array of `N` `U`”.
|
| 881 |
|
| 882 |
+
Furthermore, if there is a reachable declaration of the entity that
|
| 883 |
+
inhabits the same scope in which the bound was specified, an omitted
|
| 884 |
+
array bound is taken to be the same as in that earlier declaration, and
|
| 885 |
+
similarly for the definition of a static data member of a class.
|
| 886 |
|
| 887 |
[*Example 3*:
|
| 888 |
|
| 889 |
``` cpp
|
| 890 |
extern int x[10];
|
| 891 |
struct S {
|
| 892 |
static int y[10];
|
| 893 |
};
|
| 894 |
|
| 895 |
+
int x[]; // OK, bound is 10
|
| 896 |
+
int S::y[]; // OK, bound is 10
|
| 897 |
|
| 898 |
void f() {
|
| 899 |
extern int x[];
|
| 900 |
int i = sizeof(x); // error: incomplete object type
|
| 901 |
}
|
|
|
|
| 905 |
|
| 906 |
[*Note 3*:
|
| 907 |
|
| 908 |
When several “array of” specifications are adjacent, a multidimensional
|
| 909 |
array type is created; only the first of the constant expressions that
|
| 910 |
+
specify the bounds of the arrays can be omitted.
|
| 911 |
|
| 912 |
[*Example 4*:
|
| 913 |
|
| 914 |
``` cpp
|
| 915 |
int x3d[3][5][7];
|
|
|
|
| 1007 |
parameter-declaration-list ',' parameter-declaration
|
| 1008 |
```
|
| 1009 |
|
| 1010 |
``` bnf
|
| 1011 |
parameter-declaration:
|
| 1012 |
+
attribute-specifier-seqₒₚₜ thisₒₚₜ decl-specifier-seq declarator
|
| 1013 |
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator '=' initializer-clause
|
| 1014 |
+
attribute-specifier-seqₒₚₜ thisₒₚₜ decl-specifier-seq abstract-declaratorₒₚₜ
|
| 1015 |
attribute-specifier-seqₒₚₜ decl-specifier-seq abstract-declaratorₒₚₜ '=' initializer-clause
|
| 1016 |
```
|
| 1017 |
|
| 1018 |
The optional *attribute-specifier-seq* in a *parameter-declaration*
|
| 1019 |
appertains to the parameter.
|
|
|
|
| 1062 |
accessing arguments passed using the ellipsis (see [[expr.call]] and
|
| 1063 |
[[support.runtime]]). — *end note*]
|
| 1064 |
|
| 1065 |
The type of a function is determined using the following rules. The type
|
| 1066 |
of each parameter (including function parameter packs) is determined
|
| 1067 |
+
from its own *parameter-declaration* [[dcl.decl]]. After determining the
|
| 1068 |
+
type of each parameter, any parameter of type “array of `T`” or of
|
| 1069 |
function type `T` is adjusted to be “pointer to `T`”. After producing
|
| 1070 |
the list of parameter types, any top-level *cv-qualifier*s modifying a
|
| 1071 |
parameter type are deleted when forming the function type. The resulting
|
| 1072 |
list of transformed parameter types and the presence or absence of the
|
| 1073 |
ellipsis or a function parameter pack is the function’s
|
|
|
|
| 1075 |
|
| 1076 |
[*Note 3*: This transformation does not affect the types of the
|
| 1077 |
parameters. For example, `int(*)(const int p, decltype(p)*)` and
|
| 1078 |
`int(*)(int, const int*)` are identical types. — *end note*]
|
| 1079 |
|
| 1080 |
+
[*Example 2*:
|
| 1081 |
+
|
| 1082 |
+
``` cpp
|
| 1083 |
+
void f(char*); // #1
|
| 1084 |
+
void f(char[]) {} // defines #1
|
| 1085 |
+
void f(const char*) {} // OK, another overload
|
| 1086 |
+
void f(char *const) {} // error: redefines #1
|
| 1087 |
+
|
| 1088 |
+
void g(char(*)[2]); // #2
|
| 1089 |
+
void g(char[3][2]) {} // defines #2
|
| 1090 |
+
void g(char[3][3]) {} // OK, another overload
|
| 1091 |
+
|
| 1092 |
+
void h(int x(const int)); // #3
|
| 1093 |
+
void h(int (*)(int)) {} // defines #3
|
| 1094 |
+
```
|
| 1095 |
+
|
| 1096 |
+
— *end example*]
|
| 1097 |
+
|
| 1098 |
+
An *explicit-object-parameter-declaration* is a *parameter-declaration*
|
| 1099 |
+
with a `this` specifier. An explicit-object-parameter-declaration shall
|
| 1100 |
+
appear only as the first *parameter-declaration* of a
|
| 1101 |
+
*parameter-declaration-list* of either:
|
| 1102 |
+
|
| 1103 |
+
- a *member-declarator* that declares a member function [[class.mem]],
|
| 1104 |
+
or
|
| 1105 |
+
- a *lambda-declarator* [[expr.prim.lambda]].
|
| 1106 |
+
|
| 1107 |
+
A *member-declarator* with an explicit-object-parameter-declaration
|
| 1108 |
+
shall not include a *ref-qualifier* or a *cv-qualifier-seq* and shall
|
| 1109 |
+
not be declared `static` or `virtual`.
|
| 1110 |
+
|
| 1111 |
+
[*Example 3*:
|
| 1112 |
+
|
| 1113 |
+
``` cpp
|
| 1114 |
+
struct C {
|
| 1115 |
+
void f(this C& self);
|
| 1116 |
+
template <typename Self> void g(this Self&& self, int);
|
| 1117 |
+
|
| 1118 |
+
void h(this C) const; // error: const not allowed here
|
| 1119 |
+
};
|
| 1120 |
+
|
| 1121 |
+
void test(C c) {
|
| 1122 |
+
c.f(); // OK, calls C::f
|
| 1123 |
+
c.g(42); // OK, calls C::g<C&>
|
| 1124 |
+
std::move(c).g(42); // OK, calls C::g<C>
|
| 1125 |
+
}
|
| 1126 |
+
```
|
| 1127 |
+
|
| 1128 |
+
— *end example*]
|
| 1129 |
+
|
| 1130 |
+
A function parameter declared with an
|
| 1131 |
+
explicit-object-parameter-declaration is an *explicit object parameter*.
|
| 1132 |
+
An explicit object parameter shall not be a function parameter pack
|
| 1133 |
+
[[temp.variadic]]. An *explicit object member function* is a non-static
|
| 1134 |
+
member function with an explicit object parameter. An
|
| 1135 |
+
*implicit object member function* is a non-static member function
|
| 1136 |
+
without an explicit object parameter.
|
| 1137 |
+
|
| 1138 |
+
The *object parameter* of a non-static member function is either the
|
| 1139 |
+
explicit object parameter or the implicit object parameter
|
| 1140 |
+
[[over.match.funcs]].
|
| 1141 |
+
|
| 1142 |
+
A *non-object parameter* is a function parameter that is not the
|
| 1143 |
+
explicit object parameter. The *non-object-parameter-type-list* of a
|
| 1144 |
+
member function is the parameter-type-list of that function with the
|
| 1145 |
+
explicit object parameter, if any, omitted.
|
| 1146 |
+
|
| 1147 |
+
[*Note 4*: The non-object-parameter-type-list consists of the adjusted
|
| 1148 |
+
types of all the non-object parameters. — *end note*]
|
| 1149 |
+
|
| 1150 |
A function type with a *cv-qualifier-seq* or a *ref-qualifier*
|
| 1151 |
+
(including a type named by *typedef-name*
|
| 1152 |
+
[[dcl.typedef]], [[temp.param]]) shall appear only as:
|
| 1153 |
|
| 1154 |
- the function type for a non-static member function,
|
| 1155 |
- the function type to which a pointer to member refers,
|
| 1156 |
- the top-level function type of a function typedef declaration or
|
| 1157 |
*alias-declaration*,
|
| 1158 |
- the *type-id* in the default argument of a *type-parameter*
|
| 1159 |
[[temp.param]], or
|
| 1160 |
- the *type-id* of a *template-argument* for a *type-parameter*
|
| 1161 |
[[temp.arg.type]].
|
| 1162 |
|
| 1163 |
+
[*Example 4*:
|
| 1164 |
|
| 1165 |
``` cpp
|
| 1166 |
typedef int FIC(int) const;
|
| 1167 |
FIC f; // error: does not declare a member function
|
| 1168 |
struct S {
|
|
|
|
| 1175 |
|
| 1176 |
The effect of a *cv-qualifier-seq* in a function declarator is not the
|
| 1177 |
same as adding cv-qualification on top of the function type. In the
|
| 1178 |
latter case, the cv-qualifiers are ignored.
|
| 1179 |
|
| 1180 |
+
[*Note 5*: A function type that has a *cv-qualifier-seq* is not a
|
| 1181 |
cv-qualified type; there are no cv-qualified function
|
| 1182 |
types. — *end note*]
|
| 1183 |
|
| 1184 |
+
[*Example 5*:
|
| 1185 |
|
| 1186 |
``` cpp
|
| 1187 |
typedef void F();
|
| 1188 |
struct S {
|
| 1189 |
+
const F f; // OK, equivalent to: void f();
|
| 1190 |
};
|
| 1191 |
```
|
| 1192 |
|
| 1193 |
— *end example*]
|
| 1194 |
|
| 1195 |
The return type, the parameter-type-list, the *ref-qualifier*, the
|
| 1196 |
*cv-qualifier-seq*, and the exception specification, but not the default
|
| 1197 |
arguments [[dcl.fct.default]] or the trailing *requires-clause*
|
| 1198 |
[[dcl.decl]], are part of the function type.
|
| 1199 |
|
| 1200 |
+
[*Note 6*: Function types are checked during the assignments and
|
| 1201 |
initializations of pointers to functions, references to functions, and
|
| 1202 |
pointers to member functions. — *end note*]
|
| 1203 |
|
| 1204 |
+
[*Example 6*:
|
| 1205 |
|
| 1206 |
The declaration
|
| 1207 |
|
| 1208 |
``` cpp
|
| 1209 |
int fseek(FILE*, long, int);
|
|
|
|
| 1212 |
declares a function taking three arguments of the specified types, and
|
| 1213 |
returning `int` [[dcl.type]].
|
| 1214 |
|
| 1215 |
— *end example*]
|
| 1216 |
|
| 1217 |
+
[*Note 7*: A single name can be used for several different functions in
|
| 1218 |
+
a single scope; this is function overloading [[over]]. — *end note*]
|
|
|
|
|
|
|
| 1219 |
|
| 1220 |
+
The return type shall be a non-array object type, a reference type, or
|
| 1221 |
+
cv `void`.
|
| 1222 |
+
|
| 1223 |
+
[*Note 8*: An array of placeholder type is considered an array
|
| 1224 |
+
type. — *end note*]
|
| 1225 |
|
| 1226 |
A volatile-qualified return type is deprecated; see
|
| 1227 |
[[depr.volatile.type]].
|
| 1228 |
|
| 1229 |
Types shall not be defined in return or parameter types.
|
| 1230 |
|
| 1231 |
A typedef of function type may be used to declare a function but shall
|
| 1232 |
not be used to define a function [[dcl.fct.def]].
|
| 1233 |
|
| 1234 |
+
[*Example 7*:
|
| 1235 |
|
| 1236 |
``` cpp
|
| 1237 |
typedef void F();
|
| 1238 |
+
F fv; // OK, equivalent to void fv();
|
| 1239 |
F fv { } // error
|
| 1240 |
+
void fv() { } // OK, definition of fv
|
| 1241 |
```
|
| 1242 |
|
| 1243 |
— *end example*]
|
| 1244 |
|
| 1245 |
An identifier can optionally be provided as a parameter name; if present
|
| 1246 |
in a function definition [[dcl.fct.def]], it names a parameter.
|
| 1247 |
|
| 1248 |
+
[*Note 9*: In particular, parameter names are also optional in function
|
| 1249 |
definitions and names used for a parameter in different declarations and
|
| 1250 |
+
the definition of a function need not be the same. — *end note*]
|
|
|
|
|
|
|
|
|
|
| 1251 |
|
| 1252 |
+
[*Example 8*:
|
| 1253 |
|
| 1254 |
The declaration
|
| 1255 |
|
| 1256 |
``` cpp
|
| 1257 |
int i,
|
|
|
|
| 1277 |
to indicate that indirection through a pointer to a function yields a
|
| 1278 |
function, which is then called.
|
| 1279 |
|
| 1280 |
— *end example*]
|
| 1281 |
|
| 1282 |
+
[*Note 10*:
|
| 1283 |
|
| 1284 |
Typedefs and *trailing-return-type*s are sometimes convenient when the
|
| 1285 |
return type of a function is complex. For example, the function `fpif`
|
| 1286 |
+
above can be declared
|
| 1287 |
|
| 1288 |
``` cpp
|
| 1289 |
typedef int IFUNC(int);
|
| 1290 |
IFUNC* fpif(int);
|
| 1291 |
```
|
|
|
|
| 1312 |
— *end note*]
|
| 1313 |
|
| 1314 |
A *non-template function* is a function that is not a function template
|
| 1315 |
specialization.
|
| 1316 |
|
| 1317 |
+
[*Note 11*: A function template is not a function. — *end note*]
|
| 1318 |
|
| 1319 |
An *abbreviated function template* is a function declaration that has
|
| 1320 |
one or more generic parameter type placeholders [[dcl.spec.auto]]. An
|
| 1321 |
abbreviated function template is equivalent to a function template
|
| 1322 |
[[temp.fct]] whose *template-parameter-list* includes one invented type
|
|
|
|
| 1325 |
*placeholder-type-specifier* of the form `auto`, the invented parameter
|
| 1326 |
is an unconstrained *type-parameter*. For a *placeholder-type-specifier*
|
| 1327 |
of the form *type-constraint* `auto`, the invented parameter is a
|
| 1328 |
*type-parameter* with that *type-constraint*. The invented type
|
| 1329 |
*template-parameter* is a template parameter pack if the corresponding
|
| 1330 |
+
*parameter-declaration* declares a function parameter pack. If the
|
| 1331 |
+
placeholder contains `decltype(auto)`, the program is ill-formed. The
|
| 1332 |
+
adjusted function parameters of an abbreviated function template are
|
| 1333 |
derived from the *parameter-declaration-clause* by replacing each
|
| 1334 |
occurrence of a placeholder with the name of the corresponding invented
|
| 1335 |
*template-parameter*.
|
| 1336 |
|
| 1337 |
+
[*Example 9*:
|
| 1338 |
|
| 1339 |
``` cpp
|
| 1340 |
template<typename T> concept C1 = /* ... */;
|
| 1341 |
template<typename T> concept C2 = /* ... */;
|
| 1342 |
template<typename... Ts> concept C3 = /* ... */;
|
|
|
|
| 1345 |
void g2(C1 auto&...);
|
| 1346 |
void g3(C3 auto...);
|
| 1347 |
void g4(C3 auto);
|
| 1348 |
```
|
| 1349 |
|
| 1350 |
+
The declarations above are functionally equivalent (but not equivalent)
|
| 1351 |
+
to their respective declarations below:
|
| 1352 |
|
| 1353 |
``` cpp
|
| 1354 |
template<C1 T, C2 U> void g1(const T*, U&);
|
| 1355 |
template<C1... Ts> void g2(Ts&...);
|
| 1356 |
template<C3... Ts> void g3(Ts...);
|
|
|
|
| 1365 |
```
|
| 1366 |
|
| 1367 |
— *end example*]
|
| 1368 |
|
| 1369 |
An abbreviated function template can have a *template-head*. The
|
| 1370 |
+
invented *template-parameter*s are appended to the
|
| 1371 |
*template-parameter-list* after the explicitly declared
|
| 1372 |
+
*template-parameter*s.
|
| 1373 |
|
| 1374 |
+
[*Example 10*:
|
| 1375 |
|
| 1376 |
``` cpp
|
| 1377 |
template<typename> concept C = /* ... */;
|
| 1378 |
|
| 1379 |
template <typename T, C U>
|
|
|
|
| 1403 |
function parameter pack [[temp.variadic]]. Otherwise, the
|
| 1404 |
*parameter-declaration* is part of a *template-parameter-list* and
|
| 1405 |
declares a template parameter pack; see [[temp.param]]. A function
|
| 1406 |
parameter pack is a pack expansion [[temp.variadic]].
|
| 1407 |
|
| 1408 |
+
[*Example 11*:
|
| 1409 |
|
| 1410 |
``` cpp
|
| 1411 |
template<typename... T> void f(T (* ...t)(int, int));
|
| 1412 |
|
| 1413 |
int add(int, int);
|
|
|
|
| 1463 |
template parameter pack or a function parameter pack. If it is specified
|
| 1464 |
in a *parameter-declaration-clause*, it shall not occur within a
|
| 1465 |
*declarator* or *abstract-declarator* of a *parameter-declaration*.[^4]
|
| 1466 |
|
| 1467 |
For non-template functions, default arguments can be added in later
|
| 1468 |
+
declarations of a function that inhabit the same scope. Declarations
|
| 1469 |
+
that inhabit different scopes have completely distinct sets of default
|
| 1470 |
+
arguments. That is, declarations in inner scopes do not acquire default
|
| 1471 |
+
arguments from declarations in outer scopes, and vice versa. In a given
|
| 1472 |
+
function declaration, each parameter subsequent to a parameter with a
|
| 1473 |
+
default argument shall have a default argument supplied in this or a
|
| 1474 |
+
previous declaration, unless the parameter was expanded from a parameter
|
| 1475 |
+
pack, or shall be a function parameter pack.
|
| 1476 |
|
| 1477 |
[*Note 2*: A default argument cannot be redefined by a later
|
| 1478 |
declaration (not even to the same value)
|
| 1479 |
[[basic.def.odr]]. — *end note*]
|
| 1480 |
|
|
|
|
| 1508 |
— *end example*]
|
| 1509 |
|
| 1510 |
For a given inline function defined in different translation units, the
|
| 1511 |
accumulated sets of default arguments at the end of the translation
|
| 1512 |
units shall be the same; no diagnostic is required. If a friend
|
| 1513 |
+
declaration D specifies a default argument expression, that declaration
|
| 1514 |
+
shall be a definition and there shall be no other declaration of the
|
| 1515 |
+
function or function template which is reachable from D or from which D
|
| 1516 |
+
is reachable.
|
| 1517 |
|
| 1518 |
The default argument has the same semantic constraints as the
|
| 1519 |
initializer in a declaration of a variable of the parameter type, using
|
| 1520 |
the copy-initialization semantics [[dcl.init]]. The names in the default
|
| 1521 |
+
argument are looked up, and the semantic constraints are checked, at the
|
| 1522 |
+
point where the default argument appears, except that an immediate
|
| 1523 |
+
invocation [[expr.const]] that is a potentially-evaluated subexpression
|
| 1524 |
+
[[intro.execution]] of the *initializer-clause* in a
|
| 1525 |
+
*parameter-declaration* is neither evaluated nor checked for whether it
|
| 1526 |
+
is a constant expression at that point. Name lookup and checking of
|
| 1527 |
+
semantic constraints for default arguments of templated functions are
|
| 1528 |
+
performed as described in [[temp.inst]].
|
| 1529 |
|
| 1530 |
[*Example 3*:
|
| 1531 |
|
| 1532 |
In the following code, `g` will be called with the value `f(2)`:
|
| 1533 |
|
|
|
|
| 1545 |
}
|
| 1546 |
```
|
| 1547 |
|
| 1548 |
— *end example*]
|
| 1549 |
|
| 1550 |
+
[*Note 3*: A default argument is a complete-class context
|
| 1551 |
+
[[class.mem]]. Access checking applies to names in default arguments as
|
| 1552 |
+
described in [[class.access]]. — *end note*]
|
|
|
|
| 1553 |
|
| 1554 |
Except for member functions of class templates, the default arguments in
|
| 1555 |
a member function definition that appears outside of the class
|
| 1556 |
definition are added to the set of default arguments provided by the
|
| 1557 |
member function declaration in the class definition; the program is
|
|
|
|
| 1568 |
void f(int i = 3);
|
| 1569 |
void g(int i, int j = 99);
|
| 1570 |
};
|
| 1571 |
|
| 1572 |
void C::f(int i = 3) {} // error: default argument already specified in class scope
|
| 1573 |
+
void C::g(int i = 88, int j) {} // in this translation unit, C::g can be called with no arguments
|
| 1574 |
```
|
| 1575 |
|
| 1576 |
— *end example*]
|
| 1577 |
|
| 1578 |
+
[*Note 4*: A local variable cannot be odr-used [[term.odr.use]] in a
|
| 1579 |
default argument. — *end note*]
|
| 1580 |
|
| 1581 |
[*Example 5*:
|
| 1582 |
|
| 1583 |
``` cpp
|
|
|
|
| 1591 |
|
| 1592 |
— *end example*]
|
| 1593 |
|
| 1594 |
[*Note 5*:
|
| 1595 |
|
| 1596 |
+
The keyword `this` cannot appear in a default argument of a member
|
| 1597 |
function; see [[expr.prim.this]].
|
| 1598 |
|
| 1599 |
[*Example 6*:
|
| 1600 |
|
| 1601 |
``` cpp
|
|
|
|
| 1608 |
|
| 1609 |
— *end note*]
|
| 1610 |
|
| 1611 |
A default argument is evaluated each time the function is called with no
|
| 1612 |
argument for the corresponding parameter. A parameter shall not appear
|
| 1613 |
+
as a potentially-evaluated expression in a default argument.
|
| 1614 |
+
|
| 1615 |
+
[*Note 6*: Parameters of a function declared before a default argument
|
| 1616 |
+
are in scope and can hide namespace and class member
|
| 1617 |
+
names. — *end note*]
|
| 1618 |
|
| 1619 |
[*Example 7*:
|
| 1620 |
|
| 1621 |
``` cpp
|
| 1622 |
int a;
|
| 1623 |
int f(int a, int b = a); // error: parameter a used as default argument
|
| 1624 |
typedef int I;
|
| 1625 |
int g(float I, int b = I(2)); // error: parameter I found
|
| 1626 |
+
int h(int a, int b = sizeof(a)); // OK, unevaluated operand[term.unevaluated.operand]
|
| 1627 |
```
|
| 1628 |
|
| 1629 |
— *end example*]
|
| 1630 |
|
| 1631 |
A non-static member shall not appear in a default argument unless it
|
|
|
|
| 1671 |
int (*p2)() = &f; // error: type mismatch
|
| 1672 |
```
|
| 1673 |
|
| 1674 |
— *end example*]
|
| 1675 |
|
| 1676 |
+
When an overload set contains a declaration of a function that inhabits
|
| 1677 |
+
a scope S, any default argument associated with any reachable
|
| 1678 |
+
declaration that inhabits S is available to the call.
|
| 1679 |
+
|
| 1680 |
+
[*Note 7*: The candidate might have been found through a
|
| 1681 |
+
*using-declarator* from which the declaration that provides the default
|
| 1682 |
+
argument is not reachable. — *end note*]
|
| 1683 |
|
| 1684 |
A virtual function call [[class.virtual]] uses the default arguments in
|
| 1685 |
the declaration of the virtual function determined by the static type of
|
| 1686 |
the pointer or reference denoting the object. An overriding function in
|
| 1687 |
a derived class does not acquire default arguments from the function it
|