- tmp/tmpvislfbtk/{from.md → to.md} +368 -212
tmp/tmpvislfbtk/{from.md → to.md}
RENAMED
|
@@ -1,67 +1,75 @@
|
|
| 1 |
## Meaning of declarators <a id="dcl.meaning">[[dcl.meaning]]</a>
|
| 2 |
|
| 3 |
-
A
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
qualifier is the global `::` scope resolution
|
| 20 |
-
*declarator-id* refers to a name declared in the global
|
|
|
|
|
|
|
| 21 |
The optional *attribute-specifier-seq* following a *declarator-id*
|
| 22 |
appertains to the entity that is declared.
|
| 23 |
|
| 24 |
-
A `static`, `thread_local`, `extern`, `
|
| 25 |
-
`
|
| 26 |
-
*declarator-id* in an *init-declarator-list*
|
| 27 |
-
|
| 28 |
-
*declarator*.
|
| 29 |
|
| 30 |
Thus, a declaration of a particular identifier has the form
|
| 31 |
|
| 32 |
``` cpp
|
| 33 |
T D
|
| 34 |
```
|
| 35 |
|
| 36 |
-
where `T` is of the form *attribute-specifier-seqₒₚₜ
|
| 37 |
*decl-specifier-seq* and `D` is a declarator. Following is a recursive
|
| 38 |
procedure for determining the type specified for the contained
|
| 39 |
*declarator-id* by such a declaration.
|
| 40 |
|
| 41 |
First, the *decl-specifier-seq* determines a type. In a declaration
|
| 42 |
|
| 43 |
``` cpp
|
| 44 |
T D
|
| 45 |
```
|
| 46 |
|
| 47 |
-
the *decl-specifier-seq* `T` determines the type `T`.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
``` cpp
|
| 50 |
int unsigned i;
|
| 51 |
```
|
| 52 |
|
| 53 |
the type specifiers `int` `unsigned` determine the type “`unsigned int`”
|
| 54 |
([[dcl.type.simple]]).
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
unadorned identifier the type of this identifier is “`T`”.
|
| 58 |
|
| 59 |
In a declaration `T` `D` where `D` has the form
|
| 60 |
|
| 61 |
``` bnf
|
| 62 |
-
( D1 )
|
| 63 |
```
|
| 64 |
|
| 65 |
the type of the contained *declarator-id* is the same as that of the
|
| 66 |
contained *declarator-id* in the declaration
|
| 67 |
|
|
@@ -78,18 +86,21 @@ In a declaration `T` `D` where `D` has the form
|
|
| 78 |
|
| 79 |
``` bnf
|
| 80 |
'*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
| 81 |
```
|
| 82 |
|
| 83 |
-
and the type of the identifier in the declaration `T` `D1` is
|
| 84 |
-
then the type of the identifier of
|
| 85 |
-
*cv-qualifier*
|
| 86 |
-
|
|
|
|
| 87 |
[[dcl.attr.grammar]]) appertains to the pointer and not to the object
|
| 88 |
pointed to.
|
| 89 |
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
|
| 92 |
``` cpp
|
| 93 |
const int ci = 10, *pc = &ci, *const cpc = pc, **ppc;
|
| 94 |
int i, *p, *const cp = &i;
|
| 95 |
```
|
|
@@ -126,56 +137,68 @@ ppc = &p; // error
|
|
| 126 |
Each is unacceptable because it would either change the value of an
|
| 127 |
object declared `const` or allow it to be changed through a
|
| 128 |
cv-unqualified pointer later, for example:
|
| 129 |
|
| 130 |
``` cpp
|
| 131 |
-
*ppc = &ci; // OK, but would make p point to ci
|
| 132 |
-
// ... because of previous error
|
| 133 |
*p = 5; // clobber ci
|
| 134 |
```
|
| 135 |
|
|
|
|
|
|
|
| 136 |
See also [[expr.ass]] and [[dcl.init]].
|
| 137 |
|
| 138 |
-
Forming a pointer to reference type is ill-formed; see
|
| 139 |
-
Forming a
|
| 140 |
-
has *cv-qualifier*s or a *ref-qualifier*; see
|
| 141 |
-
address of a bit-field ([[class.bit]]) cannot be
|
| 142 |
-
never point to a bit-field.
|
| 143 |
|
| 144 |
### References <a id="dcl.ref">[[dcl.ref]]</a>
|
| 145 |
|
| 146 |
In a declaration `T` `D` where `D` has either of the forms
|
| 147 |
|
| 148 |
``` bnf
|
| 149 |
'&' attribute-specifier-seqₒₚₜ 'D1'
|
| 150 |
'&&' attribute-specifier-seqₒₚₜ 'D1'
|
| 151 |
```
|
| 152 |
|
| 153 |
-
and the type of the identifier in the declaration `T` `D1` is
|
| 154 |
-
then the type of the identifier of
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
``` cpp
|
| 162 |
typedef int& A;
|
| 163 |
const A aref = 3; // ill-formed; lvalue reference to non-const initialized with rvalue
|
| 164 |
```
|
| 165 |
|
| 166 |
The type of `aref` is “lvalue reference to `int`”, not “lvalue reference
|
| 167 |
-
to `const int`”.
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
ill-formed.
|
| 170 |
|
| 171 |
A reference type that is declared using `&` is called an *lvalue
|
| 172 |
reference*, and a reference type that is declared using `&&` is called
|
| 173 |
an *rvalue reference*. Lvalue references and rvalue references are
|
| 174 |
distinct types. Except where explicitly noted, they are semantically
|
| 175 |
equivalent and commonly referred to as references.
|
| 176 |
|
|
|
|
|
|
|
| 177 |
``` cpp
|
| 178 |
void f(double& a) { a += 3.14; }
|
| 179 |
// ...
|
| 180 |
double d = 0;
|
| 181 |
f(d);
|
|
@@ -216,33 +239,42 @@ void k() {
|
|
| 216 |
```
|
| 217 |
|
| 218 |
declares `p` to be a reference to a pointer to `link` so `h(q)` will
|
| 219 |
leave `q` with the value zero. See also [[dcl.init.ref]].
|
| 220 |
|
|
|
|
|
|
|
| 221 |
It is unspecified whether or not a reference requires storage (
|
| 222 |
[[basic.stc]]).
|
| 223 |
|
| 224 |
There shall be no references to references, no arrays of references, and
|
| 225 |
no pointers to references. The declaration of a reference shall contain
|
| 226 |
an *initializer* ([[dcl.init.ref]]) except when the declaration
|
| 227 |
contains an explicit `extern` specifier ([[dcl.stc]]), is a class
|
| 228 |
member ([[class.mem]]) declaration within a class definition, or is the
|
| 229 |
declaration of a parameter or a return type ([[dcl.fct]]); see
|
| 230 |
[[basic.def]]. A reference shall be initialized to refer to a valid
|
| 231 |
-
object or function.
|
|
|
|
|
|
|
| 232 |
well-defined program, because the only way to create such a reference
|
| 233 |
would be to bind it to the “object” obtained by indirection through a
|
| 234 |
null pointer, which causes undefined behavior. As described in
|
| 235 |
-
[[class.bit]], a reference cannot be bound directly to a
|
|
|
|
| 236 |
|
| 237 |
If a *typedef-name* ([[dcl.typedef]], [[temp.param]]) or a
|
| 238 |
*decltype-specifier* ([[dcl.type.simple]]) denotes a type `TR` that is
|
| 239 |
a reference to a type `T`, an attempt to create the type “lvalue
|
| 240 |
reference to cv `TR`” creates the type “lvalue reference to `T`”, while
|
| 241 |
an attempt to create the type “rvalue reference to cv `TR`” creates the
|
| 242 |
type `TR`.
|
| 243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
``` cpp
|
| 245 |
int i;
|
| 246 |
typedef int& LRI;
|
| 247 |
typedef int&& RRI;
|
| 248 |
|
|
@@ -255,26 +287,33 @@ RRI&& r5 = 5; // r5 has the type int&&
|
|
| 255 |
|
| 256 |
decltype(r2)& r6 = i; // r6 has the type int&
|
| 257 |
decltype(r2)&& r7 = i; // r7 has the type int&
|
| 258 |
```
|
| 259 |
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
### Pointers to members <a id="dcl.mptr">[[dcl.mptr]]</a>
|
| 264 |
|
| 265 |
In a declaration `T` `D` where `D` has the form
|
| 266 |
|
| 267 |
``` bnf
|
| 268 |
-
nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ D1
|
| 269 |
```
|
| 270 |
|
| 271 |
and the *nested-name-specifier* denotes a class, and the type of the
|
| 272 |
-
identifier in the declaration `T` `D1` is
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
``` cpp
|
| 278 |
struct X {
|
| 279 |
void f(int);
|
| 280 |
int a;
|
|
@@ -296,24 +335,24 @@ declaration of `pmc` is well-formed even though `Y` is an incomplete
|
|
| 296 |
type. `pmi` and `pmf` can be used like this:
|
| 297 |
|
| 298 |
``` cpp
|
| 299 |
X obj;
|
| 300 |
// ...
|
| 301 |
-
obj.*pmi = 7; // assign 7 to an integer
|
| 302 |
-
|
| 303 |
-
(obj.*pmf)(7); // call a function member of obj
|
| 304 |
-
// with the argument 7
|
| 305 |
```
|
| 306 |
|
|
|
|
|
|
|
| 307 |
A pointer to member shall not point to a static member of a class (
|
| 308 |
-
[[class.static]]), a member with reference type, or “
|
| 309 |
|
| 310 |
-
See also [[expr.unary]] and [[expr.mptr.oper]]. The type
|
| 311 |
-
member” is distinct from the type “pointer”, that is, a
|
| 312 |
-
member is declared only by the pointer to member declarator
|
| 313 |
-
never by the pointer declarator syntax. There is no
|
| 314 |
-
“reference-to-member” type in C++.
|
| 315 |
|
| 316 |
### Arrays <a id="dcl.array">[[dcl.array]]</a>
|
| 317 |
|
| 318 |
In a declaration `T` `D` where `D` has the form
|
| 319 |
|
|
@@ -322,56 +361,65 @@ In a declaration `T` `D` where `D` has the form
|
|
| 322 |
```
|
| 323 |
|
| 324 |
and the type of the identifier in the declaration `T` `D1` is
|
| 325 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 326 |
`D` is an array type; if the type of the identifier of `D` contains the
|
| 327 |
-
`auto` , the program is ill-formed. `T` is called the
|
| 328 |
-
type*; this type shall not be a reference type,
|
| 329 |
-
cv
|
| 330 |
-
|
| 331 |
converted constant expression of type `std::size_t` and its value shall
|
| 332 |
be greater than zero. The constant expression specifies the *bound* of
|
| 333 |
(number of elements in) the array. If the value of the constant
|
| 334 |
expression is `N`, the array has `N` elements numbered `0` to `N-1`, and
|
| 335 |
-
the type of the identifier of `D` is “
|
| 336 |
-
array
|
| 337 |
-
subobjects of type `T`. Except as noted
|
| 338 |
-
expression is omitted, the type of the identifier
|
| 339 |
-
|
| 340 |
-
`
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
array
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
``` cpp
|
| 347 |
typedef int A[5], AA[2][3];
|
| 348 |
typedef const A CA; // type is ``array of 5 const int''
|
| 349 |
typedef const AA CAA; // type is ``array of 2 array of 3 const int''
|
| 350 |
```
|
| 351 |
|
| 352 |
-
|
| 353 |
-
|
|
|
|
|
|
|
| 354 |
|
| 355 |
An array can be constructed from one of the fundamental types (except
|
| 356 |
`void`), from a pointer, from a pointer to member, from a class, from an
|
| 357 |
enumeration type, or from another array.
|
| 358 |
|
| 359 |
When several “array of” specifications are adjacent, a multidimensional
|
| 360 |
-
array is created; only the first of the constant expressions that
|
| 361 |
specify the bounds of the arrays may be omitted. In addition to
|
| 362 |
declarations in which an incomplete object type is allowed, an array
|
| 363 |
bound may be omitted in some cases in the declaration of a function
|
| 364 |
parameter ([[dcl.fct]]). An array bound may also be omitted when the
|
| 365 |
-
declarator is followed by an *initializer*
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
|
|
|
|
|
|
|
|
|
| 373 |
|
| 374 |
``` cpp
|
| 375 |
float fa[17], *afp[17];
|
| 376 |
```
|
| 377 |
|
|
@@ -401,31 +449,38 @@ void f() {
|
|
| 401 |
extern int x[];
|
| 402 |
int i = sizeof(x); // error: incomplete object type
|
| 403 |
}
|
| 404 |
```
|
| 405 |
|
| 406 |
-
|
| 407 |
-
[[conv.array]]. Objects of array types cannot be modified, see
|
| 408 |
-
[[basic.lval]].
|
| 409 |
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
|
| 417 |
A consistent rule is followed for multidimensional arrays. If `E` is an
|
| 418 |
*n*-dimensional array of rank i × j × … × k, then `E` appearing in an
|
| 419 |
expression that is subject to the array-to-pointer conversion (
|
| 420 |
[[conv.array]]) is converted to a pointer to an (n-1)-dimensional array
|
| 421 |
with rank j × … × k. If the `*` operator, either explicitly or
|
| 422 |
implicitly as a result of subscripting, is applied to this pointer, the
|
| 423 |
result is the pointed-to (n-1)-dimensional array, which itself is
|
| 424 |
immediately converted into a pointer.
|
| 425 |
|
| 426 |
-
|
|
|
|
|
|
|
| 427 |
|
| 428 |
``` cpp
|
| 429 |
int x[3][5];
|
| 430 |
```
|
| 431 |
|
|
@@ -438,50 +493,60 @@ multiplying `i` by the length of the object to which the pointer points,
|
|
| 438 |
namely five integer objects. The results are added and indirection
|
| 439 |
applied to yield an array (of five integers), which in turn is converted
|
| 440 |
to a pointer to the first of the integers. If there is another subscript
|
| 441 |
the same argument applies again; this time the result is an integer.
|
| 442 |
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
|
| 448 |
### Functions <a id="dcl.fct">[[dcl.fct]]</a>
|
| 449 |
|
| 450 |
In a declaration `T` `D` where `D` has the form
|
| 451 |
|
| 452 |
``` bnf
|
| 453 |
'D1 (' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 454 |
-
ref-qualifierₒₚₜ
|
| 455 |
```
|
| 456 |
|
| 457 |
and the type of the contained *declarator-id* in the declaration `T`
|
| 458 |
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 459 |
-
*declarator-id* in `D` is “
|
| 460 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
|
| 462 |
In a declaration `T` `D` where `D` has the form
|
| 463 |
|
| 464 |
``` bnf
|
| 465 |
'D1 (' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 466 |
-
ref-qualifierₒₚₜ
|
| 467 |
```
|
| 468 |
|
| 469 |
and the type of the contained *declarator-id* in the declaration `T`
|
| 470 |
`D1` is “*derived-declarator-type-list* `T`”, `T` shall be the single
|
| 471 |
*type-specifier* `auto`. The type of the *declarator-id* in `D` is
|
| 472 |
-
“*derived-declarator-type-list* function of
|
| 473 |
-
(*parameter-declaration-clause*) *cv-qualifier-seq*
|
| 474 |
-
|
|
|
|
|
|
|
| 475 |
*attribute-specifier-seq* appertains to the function type.
|
| 476 |
|
| 477 |
-
A type of either form is a *function type*.[^
|
| 478 |
|
| 479 |
``` bnf
|
| 480 |
parameter-declaration-clause:
|
| 481 |
-
parameter-declaration-listₒₚₜ ...ₒₚₜ
|
| 482 |
-
parameter-declaration-list ',
|
| 483 |
```
|
| 484 |
|
| 485 |
``` bnf
|
| 486 |
parameter-declaration-list:
|
| 487 |
parameter-declaration
|
|
@@ -498,23 +563,30 @@ parameter-declaration:
|
|
| 498 |
|
| 499 |
The optional *attribute-specifier-seq* in a *parameter-declaration*
|
| 500 |
appertains to the parameter.
|
| 501 |
|
| 502 |
The *parameter-declaration-clause* determines the arguments that can be
|
| 503 |
-
specified, and their processing, when the function is called.
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
|
|
|
|
|
|
|
|
|
| 507 |
arguments. A parameter list consisting of a single unnamed parameter of
|
| 508 |
non-dependent type `void` is equivalent to an empty parameter list.
|
| 509 |
Except for this special case, a parameter shall not have type *cv*
|
| 510 |
`void`. If the *parameter-declaration-clause* terminates with an
|
| 511 |
ellipsis or a function parameter pack ([[temp.variadic]]), the number
|
| 512 |
of arguments shall be equal to or greater than the number of parameters
|
| 513 |
that do not have a default argument and are not function parameter
|
| 514 |
-
packs. Where syntactically correct and where “” is not part of an
|
| 515 |
-
*abstract-declarator*, “” is synonymous with “”.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 516 |
|
| 517 |
``` cpp
|
| 518 |
int printf(const char*, ...);
|
| 519 |
```
|
| 520 |
|
|
@@ -525,31 +597,35 @@ arguments.
|
|
| 525 |
printf("hello world");
|
| 526 |
printf("a=%d b=%d", a, b);
|
| 527 |
```
|
| 528 |
|
| 529 |
However, the first argument must be of a type that can be converted to a
|
| 530 |
-
`const` `char*`
|
|
|
|
|
|
|
|
|
|
|
|
|
| 531 |
accessing arguments passed using the ellipsis (see [[expr.call]] and
|
| 532 |
-
[[support.runtime]]).
|
| 533 |
|
| 534 |
A single name can be used for several different functions in a single
|
| 535 |
scope; this is function overloading (Clause [[over]]). All declarations
|
| 536 |
for a function shall agree exactly in both the return type and the
|
| 537 |
parameter-type-list. The type of a function is determined using the
|
| 538 |
following rules. The type of each parameter (including function
|
| 539 |
parameter packs) is determined from its own *decl-specifier-seq* and
|
| 540 |
*declarator*. After determining the type of each parameter, any
|
| 541 |
-
parameter of type “array of `T`” or
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
`int(*)(const int p, decltype(p)*)` and
|
| 550 |
-
identical types.
|
| 551 |
|
| 552 |
A function type with a *cv-qualifier-seq* or a *ref-qualifier*
|
| 553 |
(including a type named by *typedef-name* ([[dcl.typedef]],
|
| 554 |
[[temp.param]])) shall appear only as:
|
| 555 |
|
|
@@ -560,82 +636,101 @@ A function type with a *cv-qualifier-seq* or a *ref-qualifier*
|
|
| 560 |
- the *type-id* in the default argument of a *type-parameter* (
|
| 561 |
[[temp.param]]), or
|
| 562 |
- the *type-id* of a *template-argument* for a *type-parameter* (
|
| 563 |
[[temp.arg.type]]).
|
| 564 |
|
|
|
|
|
|
|
| 565 |
``` cpp
|
| 566 |
typedef int FIC(int) const;
|
| 567 |
FIC f; // ill-formed: does not declare a member function
|
| 568 |
struct S {
|
| 569 |
FIC f; // OK
|
| 570 |
};
|
| 571 |
FIC S::*pm = &S::f; // OK
|
| 572 |
```
|
| 573 |
|
|
|
|
|
|
|
| 574 |
The effect of a *cv-qualifier-seq* in a function declarator is not the
|
| 575 |
same as adding cv-qualification on top of the function type. In the
|
| 576 |
-
latter case, the cv-qualifiers are ignored.
|
| 577 |
-
|
| 578 |
-
function
|
|
|
|
|
|
|
|
|
|
|
|
|
| 579 |
|
| 580 |
``` cpp
|
| 581 |
typedef void F();
|
| 582 |
struct S {
|
| 583 |
const F f; // OK: equivalent to: void f();
|
| 584 |
};
|
| 585 |
```
|
| 586 |
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
|
|
|
|
|
|
|
|
|
| 591 |
initializations of pointers to functions, references to functions, and
|
| 592 |
-
pointers to member functions.
|
| 593 |
|
| 594 |
-
|
|
|
|
|
|
|
| 595 |
|
| 596 |
``` cpp
|
| 597 |
int fseek(FILE*, long, int);
|
| 598 |
```
|
| 599 |
|
| 600 |
declares a function taking three arguments of the specified types, and
|
| 601 |
returning `int` ([[dcl.type]]).
|
| 602 |
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
|
| 610 |
Types shall not be defined in return or parameter types. The type of a
|
| 611 |
parameter or the return type for a function definition shall not be an
|
| 612 |
-
incomplete
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
classes defined within the class).
|
| 616 |
|
| 617 |
A typedef of function type may be used to declare a function but shall
|
| 618 |
not be used to define a function ([[dcl.fct.def]]).
|
| 619 |
|
|
|
|
|
|
|
| 620 |
``` cpp
|
| 621 |
typedef void F();
|
| 622 |
F fv; // OK: equivalent to void fv();
|
| 623 |
F fv { } // ill-formed
|
| 624 |
void fv() { } // OK: definition of fv
|
| 625 |
```
|
| 626 |
|
|
|
|
|
|
|
| 627 |
An identifier can optionally be provided as a parameter name; if present
|
| 628 |
-
in a function definition ([[dcl.fct.def]]), it names a parameter.
|
| 629 |
-
particular, parameter names are also optional in function definitions
|
| 630 |
-
and names used for a parameter in different declarations and the
|
| 631 |
-
definition of a function need not be the same. If a parameter name is
|
| 632 |
-
present in a function declaration that is not a definition, it cannot be
|
| 633 |
-
used outside of its function declarator because that is the extent of
|
| 634 |
-
its potential scope ([[basic.scope.proto]]).
|
| 635 |
|
| 636 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 637 |
|
| 638 |
``` cpp
|
| 639 |
int i,
|
| 640 |
*pi,
|
| 641 |
f(),
|
|
@@ -655,13 +750,19 @@ The binding of `*fpi(int)` is `*(fpi(int))`, so the declaration
|
|
| 655 |
suggests, and the same construction in an expression requires, the
|
| 656 |
calling of a function `fpi`, and then using indirection through the
|
| 657 |
(pointer) result to yield an integer. In the declarator
|
| 658 |
`(*pif)(const char*, const char*)`, the extra parentheses are necessary
|
| 659 |
to indicate that indirection through a pointer to a function yields a
|
| 660 |
-
function, which is then called.
|
| 661 |
-
|
| 662 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 663 |
|
| 664 |
``` cpp
|
| 665 |
typedef int IFUNC(int);
|
| 666 |
IFUNC* fpif(int);
|
| 667 |
```
|
|
@@ -683,21 +784,30 @@ rather than
|
|
| 683 |
|
| 684 |
``` cpp
|
| 685 |
template <class T, class U> decltype((*(T*)0) + (*(U*)0)) add(T t, U u);
|
| 686 |
```
|
| 687 |
|
|
|
|
|
|
|
| 688 |
A *non-template function* is a function that is not a function template
|
| 689 |
-
specialization.
|
|
|
|
|
|
|
| 690 |
|
| 691 |
A *declarator-id* or *abstract-declarator* containing an ellipsis shall
|
| 692 |
only be used in a *parameter-declaration*. Such a
|
| 693 |
*parameter-declaration* is a parameter pack ([[temp.variadic]]). When
|
| 694 |
it is part of a *parameter-declaration-clause*, the parameter pack is a
|
| 695 |
-
function parameter pack ([[temp.variadic]]).
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 699 |
|
| 700 |
``` cpp
|
| 701 |
template<typename... T> void f(T (* ...t)(int, int));
|
| 702 |
|
| 703 |
int add(int, int);
|
|
@@ -706,24 +816,28 @@ float subtract(int, int);
|
|
| 706 |
void g() {
|
| 707 |
f(add, subtract);
|
| 708 |
}
|
| 709 |
```
|
| 710 |
|
|
|
|
|
|
|
| 711 |
There is a syntactic ambiguity when an ellipsis occurs at the end of a
|
| 712 |
*parameter-declaration-clause* without a preceding comma. In this case,
|
| 713 |
the ellipsis is parsed as part of the *abstract-declarator* if the type
|
| 714 |
of the parameter either names a template parameter pack that has not
|
| 715 |
been expanded or contains `auto`; otherwise, it is parsed as part of the
|
| 716 |
-
*parameter-declaration-clause*.[^
|
| 717 |
|
| 718 |
### Default arguments <a id="dcl.fct.default">[[dcl.fct.default]]</a>
|
| 719 |
|
| 720 |
If an *initializer-clause* is specified in a *parameter-declaration*
|
| 721 |
this *initializer-clause* is used as a default argument. Default
|
| 722 |
arguments will be used in calls where trailing arguments are missing.
|
| 723 |
|
| 724 |
-
|
|
|
|
|
|
|
| 725 |
|
| 726 |
``` cpp
|
| 727 |
void point(int = 3, int = 4);
|
| 728 |
```
|
| 729 |
|
|
@@ -735,17 +849,20 @@ point(1,2); point(1); point();
|
|
| 735 |
```
|
| 736 |
|
| 737 |
The last two calls are equivalent to `point(1,4)` and `point(3,4)`,
|
| 738 |
respectively.
|
| 739 |
|
|
|
|
|
|
|
| 740 |
A default argument shall be specified only in the
|
| 741 |
-
*parameter-declaration-clause* of a function declaration or
|
| 742 |
-
*template-parameter* ([[temp.param]]); in
|
| 743 |
-
*initializer-clause* shall be an
|
| 744 |
-
argument shall not be specified for a
|
| 745 |
-
in a *parameter-declaration-clause*,
|
| 746 |
-
*declarator* or *abstract-declarator* of a
|
|
|
|
| 747 |
|
| 748 |
For non-template functions, default arguments can be added in later
|
| 749 |
declarations of a function in the same scope. Declarations in different
|
| 750 |
scopes have completely distinct sets of default arguments. That is,
|
| 751 |
declarations in inner scopes do not acquire default arguments from
|
|
@@ -754,33 +871,35 @@ declaration, each parameter subsequent to a parameter with a default
|
|
| 754 |
argument shall have a default argument supplied in this or a previous
|
| 755 |
declaration or shall be a function parameter pack. A default argument
|
| 756 |
shall not be redefined by a later declaration (not even to the same
|
| 757 |
value).
|
| 758 |
|
|
|
|
|
|
|
| 759 |
``` cpp
|
| 760 |
void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow
|
| 761 |
// a parameter with a default argument
|
| 762 |
void f(int, int);
|
| 763 |
void f(int, int = 7);
|
| 764 |
void h() {
|
| 765 |
f(3); // OK, calls f(3, 7)
|
| 766 |
-
void f(int = 1, int); // error: does not use default
|
| 767 |
-
// from surrounding scope
|
| 768 |
}
|
| 769 |
void m() {
|
| 770 |
void f(int, int); // has no defaults
|
| 771 |
f(4); // error: wrong number of arguments
|
| 772 |
void f(int, int = 5); // OK
|
| 773 |
f(4); // OK, calls f(4, 5);
|
| 774 |
-
void f(int, int = 5); // error: cannot redefine, even to
|
| 775 |
-
// same value
|
| 776 |
}
|
| 777 |
void n() {
|
| 778 |
f(6); // OK, calls f(6, 7)
|
| 779 |
}
|
| 780 |
```
|
| 781 |
|
|
|
|
|
|
|
| 782 |
For a given inline function defined in different translation units, the
|
| 783 |
accumulated sets of default arguments at the end of the translation
|
| 784 |
units shall be the same; see [[basic.def.odr]]. If a friend declaration
|
| 785 |
specifies a default argument expression, that declaration shall be a
|
| 786 |
definition and shall be the only declaration of the function or function
|
|
@@ -791,12 +910,15 @@ initializer in a declaration of a variable of the parameter type, using
|
|
| 791 |
the copy-initialization semantics ([[dcl.init]]). The names in the
|
| 792 |
default argument are bound, and the semantic constraints are checked, at
|
| 793 |
the point where the default argument appears. Name lookup and checking
|
| 794 |
of semantic constraints for default arguments in function templates and
|
| 795 |
in member functions of class templates are performed as described in
|
| 796 |
-
[[temp.inst]].
|
| 797 |
-
|
|
|
|
|
|
|
|
|
|
| 798 |
|
| 799 |
``` cpp
|
| 800 |
int a = 1;
|
| 801 |
int f(int);
|
| 802 |
int g(int x = f(a)); // default argument: f(::a)
|
|
@@ -808,13 +930,16 @@ void h() {
|
|
| 808 |
g(); // g(f(::a))
|
| 809 |
}
|
| 810 |
}
|
| 811 |
```
|
| 812 |
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
|
|
|
|
|
|
|
|
|
| 816 |
|
| 817 |
Except for member functions of class templates, the default arguments in
|
| 818 |
a member function definition that appears outside of the class
|
| 819 |
definition are added to the set of default arguments provided by the
|
| 820 |
member function declaration in the class definition; the program is
|
|
@@ -822,80 +947,105 @@ ill-formed if a default constructor ([[class.ctor]]), copy or move
|
|
| 822 |
constructor, or copy or move assignment operator ([[class.copy]]) is so
|
| 823 |
declared. Default arguments for a member function of a class template
|
| 824 |
shall be specified on the initial declaration of the member function
|
| 825 |
within the class template.
|
| 826 |
|
|
|
|
|
|
|
| 827 |
``` cpp
|
| 828 |
class C {
|
| 829 |
void f(int i = 3);
|
| 830 |
void g(int i, int j = 99);
|
| 831 |
};
|
| 832 |
|
| 833 |
-
void C::f(int i = 3) {
|
| 834 |
-
}
|
| 835 |
-
void C::g(int i = 88, int j) { // in this translation unit,
|
| 836 |
-
} // C::g can be called with no argument
|
| 837 |
```
|
| 838 |
|
| 839 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 840 |
|
| 841 |
``` cpp
|
| 842 |
void f() {
|
| 843 |
int i;
|
| 844 |
extern void g(int x = i); // error
|
|
|
|
| 845 |
// ...
|
| 846 |
}
|
| 847 |
```
|
| 848 |
|
| 849 |
-
|
| 850 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 851 |
|
| 852 |
``` cpp
|
| 853 |
class A {
|
| 854 |
void f(A* p = this) { } // error
|
| 855 |
};
|
| 856 |
```
|
| 857 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 858 |
A default argument is evaluated each time the function is called with no
|
| 859 |
-
argument for the corresponding parameter.
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
|
|
|
| 864 |
|
| 865 |
``` cpp
|
| 866 |
int a;
|
| 867 |
-
int f(int a, int b = a); // error: parameter a
|
| 868 |
-
// used as default argument
|
| 869 |
typedef int I;
|
| 870 |
int g(float I, int b = I(2)); // error: parameter I found
|
| 871 |
-
int h(int a, int b = sizeof(a)); //
|
| 872 |
-
// in default argument
|
| 873 |
```
|
| 874 |
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
| 880 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 881 |
|
| 882 |
``` cpp
|
| 883 |
int b;
|
| 884 |
class X {
|
| 885 |
int a;
|
| 886 |
-
int mem1(int i = a);
|
| 887 |
-
// used as default argument
|
| 888 |
int mem2(int i = b); // OK; use X::b
|
| 889 |
static int b;
|
| 890 |
};
|
| 891 |
```
|
| 892 |
|
| 893 |
The declaration of `X::mem2()` is meaningful, however, since no object
|
| 894 |
is needed to access the static member `X::b`. Classes, objects, and
|
| 895 |
-
members are described in Clause [[class]].
|
| 896 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 897 |
|
| 898 |
``` cpp
|
| 899 |
int f(int = 0);
|
| 900 |
|
| 901 |
void h() {
|
|
@@ -905,10 +1055,12 @@ void h() {
|
|
| 905 |
|
| 906 |
int (*p1)(int) = &f;
|
| 907 |
int (*p2)() = &f; // error: type mismatch
|
| 908 |
```
|
| 909 |
|
|
|
|
|
|
|
| 910 |
When a declaration of a function is introduced by way of a
|
| 911 |
*using-declaration* ([[namespace.udecl]]), any default argument
|
| 912 |
information associated with the declaration is made known as well. If
|
| 913 |
the function is redeclared thereafter in the namespace with additional
|
| 914 |
default arguments, the additional arguments are also known at any point
|
|
@@ -918,10 +1070,12 @@ A virtual function call ([[class.virtual]]) uses the default arguments
|
|
| 918 |
in the declaration of the virtual function determined by the static type
|
| 919 |
of the pointer or reference denoting the object. An overriding function
|
| 920 |
in a derived class does not acquire default arguments from the function
|
| 921 |
it overrides.
|
| 922 |
|
|
|
|
|
|
|
| 923 |
``` cpp
|
| 924 |
struct A {
|
| 925 |
virtual void f(int a = 7);
|
| 926 |
};
|
| 927 |
struct B : public A {
|
|
@@ -933,5 +1087,7 @@ void m() {
|
|
| 933 |
pa->f(); // OK, calls pa->B::f(7)
|
| 934 |
pb->f(); // error: wrong number of arguments for B::f()
|
| 935 |
}
|
| 936 |
```
|
| 937 |
|
|
|
|
|
|
|
|
|
| 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*.
|
| 18 |
+
|
| 19 |
+
[*Note 1*: If the qualifier is the global `::` scope resolution
|
| 20 |
+
operator, the *declarator-id* refers to a name declared in the global
|
| 21 |
+
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`, `explicit`, or `typedef` specifier applies
|
| 28 |
+
directly to each *declarator-id* in an *init-declarator-list* or
|
| 29 |
+
*member-declarator-list*; the type specified for each *declarator-id*
|
| 30 |
+
depends on both the *decl-specifier-seq* and its *declarator*.
|
| 31 |
|
| 32 |
Thus, a declaration of a particular identifier has the form
|
| 33 |
|
| 34 |
``` cpp
|
| 35 |
T D
|
| 36 |
```
|
| 37 |
|
| 38 |
+
where `T` is of the form *attribute-specifier-seq*ₒₚₜ
|
| 39 |
*decl-specifier-seq* and `D` is a declarator. Following is a recursive
|
| 40 |
procedure for determining the type specified for the contained
|
| 41 |
*declarator-id* by such a declaration.
|
| 42 |
|
| 43 |
First, the *decl-specifier-seq* determines a type. In a declaration
|
| 44 |
|
| 45 |
``` cpp
|
| 46 |
T D
|
| 47 |
```
|
| 48 |
|
| 49 |
+
the *decl-specifier-seq* `T` determines the type `T`.
|
| 50 |
+
|
| 51 |
+
[*Example 1*:
|
| 52 |
+
|
| 53 |
+
In the declaration
|
| 54 |
|
| 55 |
``` cpp
|
| 56 |
int unsigned i;
|
| 57 |
```
|
| 58 |
|
| 59 |
the type specifiers `int` `unsigned` determine the type “`unsigned int`”
|
| 60 |
([[dcl.type.simple]]).
|
| 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`”.
|
| 66 |
|
| 67 |
In a declaration `T` `D` where `D` has the form
|
| 68 |
|
| 69 |
``` bnf
|
| 70 |
+
'(' 'D1' ')'
|
| 71 |
```
|
| 72 |
|
| 73 |
the type of the contained *declarator-id* is the same as that of the
|
| 74 |
contained *declarator-id* in the declaration
|
| 75 |
|
|
|
|
| 86 |
|
| 87 |
``` bnf
|
| 88 |
'*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
| 89 |
```
|
| 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]]) appertains to the pointer and not to the object
|
| 97 |
pointed to.
|
| 98 |
|
| 99 |
+
[*Example 1*:
|
| 100 |
+
|
| 101 |
+
The declarations
|
| 102 |
|
| 103 |
``` cpp
|
| 104 |
const int ci = 10, *pc = &ci, *const cpc = pc, **ppc;
|
| 105 |
int i, *p, *const cp = &i;
|
| 106 |
```
|
|
|
|
| 137 |
Each is unacceptable because it would either change the value of an
|
| 138 |
object declared `const` or allow it to be changed through a
|
| 139 |
cv-unqualified pointer later, for example:
|
| 140 |
|
| 141 |
``` cpp
|
| 142 |
+
*ppc = &ci; // OK, but would make p point to ci because of previous error
|
|
|
|
| 143 |
*p = 5; // clobber ci
|
| 144 |
```
|
| 145 |
|
| 146 |
+
— *end example*]
|
| 147 |
+
|
| 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 ([[class.bit]]) cannot be
|
| 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'
|
| 162 |
'&&' attribute-specifier-seqₒₚₜ 'D1'
|
| 163 |
```
|
| 164 |
|
| 165 |
+
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* ([[dcl.type.simple]]), in which case the
|
| 172 |
+
cv-qualifiers are ignored.
|
| 173 |
+
|
| 174 |
+
[*Example 1*:
|
| 175 |
|
| 176 |
``` cpp
|
| 177 |
typedef int& A;
|
| 178 |
const A aref = 3; // ill-formed; lvalue reference to non-const initialized with rvalue
|
| 179 |
```
|
| 180 |
|
| 181 |
The type of `aref` is “lvalue reference to `int`”, not “lvalue reference
|
| 182 |
+
to `const int`”.
|
| 183 |
+
|
| 184 |
+
— *end example*]
|
| 185 |
+
|
| 186 |
+
[*Note 1*: A reference can be thought of as a name of an
|
| 187 |
+
object. — *end note*]
|
| 188 |
+
|
| 189 |
+
A declarator that specifies the type “reference to cv `void`” is
|
| 190 |
ill-formed.
|
| 191 |
|
| 192 |
A reference type that is declared using `&` is called an *lvalue
|
| 193 |
reference*, and a reference type that is declared using `&&` is called
|
| 194 |
an *rvalue reference*. Lvalue references and rvalue references are
|
| 195 |
distinct types. Except where explicitly noted, they are semantically
|
| 196 |
equivalent and commonly referred to as references.
|
| 197 |
|
| 198 |
+
[*Example 2*:
|
| 199 |
+
|
| 200 |
``` cpp
|
| 201 |
void f(double& a) { a += 3.14; }
|
| 202 |
// ...
|
| 203 |
double d = 0;
|
| 204 |
f(d);
|
|
|
|
| 239 |
```
|
| 240 |
|
| 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* ([[dcl.init.ref]]) except when the declaration
|
| 252 |
contains an explicit `extern` specifier ([[dcl.stc]]), is a class
|
| 253 |
member ([[class.mem]]) declaration within a class definition, or is the
|
| 254 |
declaration of a parameter or a return type ([[dcl.fct]]); see
|
| 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
|
| 260 |
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* ([[dcl.type.simple]]) denotes a type `TR` that is
|
| 267 |
a reference to a type `T`, an attempt to create the type “lvalue
|
| 268 |
reference to cv `TR`” creates the type “lvalue reference to `T`”, while
|
| 269 |
an attempt to create the type “rvalue reference to cv `TR`” creates the
|
| 270 |
type `TR`.
|
| 271 |
|
| 272 |
+
[*Note 3*: This rule is known as reference collapsing. — *end note*]
|
| 273 |
+
|
| 274 |
+
[*Example 3*:
|
| 275 |
+
|
| 276 |
``` cpp
|
| 277 |
int i;
|
| 278 |
typedef int& LRI;
|
| 279 |
typedef int&& RRI;
|
| 280 |
|
|
|
|
| 287 |
|
| 288 |
decltype(r2)& r6 = i; // r6 has the type int&
|
| 289 |
decltype(r2)&& r7 = i; // r7 has the type int&
|
| 290 |
```
|
| 291 |
|
| 292 |
+
— *end example*]
|
| 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'
|
| 304 |
```
|
| 305 |
|
| 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
|
| 317 |
struct X {
|
| 318 |
void f(int);
|
| 319 |
int a;
|
|
|
|
| 335 |
type. `pmi` and `pmf` can be used like this:
|
| 336 |
|
| 337 |
``` cpp
|
| 338 |
X obj;
|
| 339 |
// ...
|
| 340 |
+
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]]), 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 |
|
|
|
|
| 361 |
```
|
| 362 |
|
| 363 |
and the type of the identifier in the declaration `T` `D1` is
|
| 364 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 365 |
`D` is an array type; if the type of the identifier of `D` contains the
|
| 366 |
+
`auto` *type-specifier*, the program is ill-formed. `T` is called the
|
| 367 |
+
array *element type*; this type shall not be a reference type,
|
| 368 |
+
cv `void`, a function type or an abstract class type. If the
|
| 369 |
+
*constant-expression* ([[expr.const]]) is present, it shall be a
|
| 370 |
converted constant expression of type `std::size_t` and its value shall
|
| 371 |
be greater than zero. The constant expression specifies the *bound* of
|
| 372 |
(number of elements in) the array. If the value of the constant
|
| 373 |
expression is `N`, the array has `N` elements numbered `0` to `N-1`, and
|
| 374 |
+
the type of the identifier of `D` is “*derived-declarator-type-list*
|
| 375 |
+
array of `N` `T`”. An object of array type contains a contiguously
|
| 376 |
+
allocated non-empty set of `N` subobjects of type `T`. Except as noted
|
| 377 |
+
below, if the constant expression is omitted, the type of the identifier
|
| 378 |
+
of `D` is “*derived-declarator-type-list* array of unknown bound of
|
| 379 |
+
`T`”, an incomplete object type. The type
|
| 380 |
+
“*derived-declarator-type-list* array of `N` `T`” is a different type
|
| 381 |
+
from the type “*derived-declarator-type-list* array of unknown bound of
|
| 382 |
+
`T`”, see [[basic.types]]. Any type of the form “*cv-qualifier-seq*
|
| 383 |
+
array of `N` `T`” is adjusted to “array of `N` *cv-qualifier-seq* `T`”,
|
| 384 |
+
and similarly for “array of unknown bound of `T`”. The optional
|
| 385 |
+
*attribute-specifier-seq* appertains to the array.
|
| 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 1*: An “array of `N` *cv-qualifier-seq* `T`” has cv-qualified
|
| 398 |
+
type; see [[basic.type.qualifier]]. — *end note*]
|
| 399 |
|
| 400 |
An array can be constructed from one of the fundamental types (except
|
| 401 |
`void`), from a pointer, from a pointer to member, from a class, from an
|
| 402 |
enumeration type, or from another array.
|
| 403 |
|
| 404 |
When several “array of” specifications are adjacent, a multidimensional
|
| 405 |
+
array type is created; only the first of the constant expressions that
|
| 406 |
specify the bounds of the arrays may be omitted. In addition to
|
| 407 |
declarations in which an incomplete object type is allowed, an array
|
| 408 |
bound may be omitted in some cases in the declaration of a function
|
| 409 |
parameter ([[dcl.fct]]). An array bound may also be omitted when the
|
| 410 |
+
declarator is followed by an *initializer* ([[dcl.init]]) or when a
|
| 411 |
+
declarator for a static data member is followed by a
|
| 412 |
+
*brace-or-equal-initializer* ([[class.mem]]). In both cases the bound
|
| 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 |
+
[*Example 2*:
|
| 421 |
|
| 422 |
``` cpp
|
| 423 |
float fa[17], *afp[17];
|
| 424 |
```
|
| 425 |
|
|
|
|
| 449 |
extern int x[];
|
| 450 |
int i = sizeof(x); // error: incomplete object type
|
| 451 |
}
|
| 452 |
```
|
| 453 |
|
| 454 |
+
— *end example*]
|
|
|
|
|
|
|
| 455 |
|
| 456 |
+
[*Note 2*: Conversions affecting expressions of array type are
|
| 457 |
+
described in [[conv.array]]. Objects of array types cannot be modified,
|
| 458 |
+
see [[basic.lval]]. — *end note*]
|
| 459 |
+
|
| 460 |
+
[*Note 3*: Except where it has been declared for a class (
|
| 461 |
+
[[over.sub]]), the subscript operator `[]` is interpreted in such a way
|
| 462 |
+
that `E1[E2]` is identical to `*((E1)+(E2))` ([[expr.sub]]). Because of
|
| 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 |
+
[*Note 4*:
|
| 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 x[3][5];
|
| 485 |
```
|
| 486 |
|
|
|
|
| 493 |
namely five integer objects. The results are added and indirection
|
| 494 |
applied to yield an array (of five integers), which in turn is converted
|
| 495 |
to a pointer to the first of the integers. If there is another subscript
|
| 496 |
the same argument applies again; this time the result is an integer.
|
| 497 |
|
| 498 |
+
— *end example*]
|
| 499 |
+
|
| 500 |
+
— *end note*]
|
| 501 |
+
|
| 502 |
+
[*Note 5*: It follows from all this that arrays in C++are stored
|
| 503 |
+
row-wise (last subscript varies fastest) and that the first subscript in
|
| 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 |
### 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-declaration-clause*) *cv-qualifier-seq*ₒₚₜ
|
| 520 |
+
*ref-qualifier*ₒₚₜ returning `T`”, where the optional `noexcept` is
|
| 521 |
+
present if and only if the exception specification ([[except.spec]]) is
|
| 522 |
+
non-throwing. The optional *attribute-specifier-seq* appertains to the
|
| 523 |
+
function type.
|
| 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` function of
|
| 536 |
+
(*parameter-declaration-clause*) *cv-qualifier-seq**ref-qualifier*
|
| 537 |
+
returning `U`”, where `U` is the type specified by the
|
| 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 |
+
A type of either form is a *function type*.[^8]
|
| 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
|
|
|
|
| 563 |
|
| 564 |
The optional *attribute-specifier-seq* in a *parameter-declaration*
|
| 565 |
appertains to the parameter.
|
| 566 |
|
| 567 |
The *parameter-declaration-clause* determines the arguments that can be
|
| 568 |
+
specified, and their processing, when the function is called.
|
| 569 |
+
|
| 570 |
+
[*Note 1*: The *parameter-declaration-clause* is used to convert the
|
| 571 |
+
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 *cv*
|
| 578 |
`void`. If the *parameter-declaration-clause* terminates with an
|
| 579 |
ellipsis or a function parameter pack ([[temp.variadic]]), the number
|
| 580 |
of arguments shall be equal to or greater than the number of parameters
|
| 581 |
that do not have a default argument and are not function parameter
|
| 582 |
+
packs. Where syntactically correct and where “`...`” is not part of an
|
| 583 |
+
*abstract-declarator*, “`, ...`” is synonymous with “`...`”.
|
| 584 |
+
|
| 585 |
+
[*Example 1*:
|
| 586 |
+
|
| 587 |
+
The declaration
|
| 588 |
|
| 589 |
``` cpp
|
| 590 |
int printf(const char*, ...);
|
| 591 |
```
|
| 592 |
|
|
|
|
| 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 |
A single name can be used for several different functions in a single
|
| 611 |
scope; this is function overloading (Clause [[over]]). All declarations
|
| 612 |
for a function shall agree exactly in both the return type and the
|
| 613 |
parameter-type-list. The type of a function is determined using the
|
| 614 |
following rules. The type of each parameter (including function
|
| 615 |
parameter packs) is determined from its own *decl-specifier-seq* and
|
| 616 |
*declarator*. After determining the type of each parameter, any
|
| 617 |
+
parameter of type “array of `T`” or of function type `T` is adjusted to
|
| 618 |
+
be “pointer to `T`”. After producing the list of parameter types, any
|
| 619 |
+
top-level *cv-qualifier*s modifying a parameter type are deleted when
|
| 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 |
|
| 628 |
A function type with a *cv-qualifier-seq* or a *ref-qualifier*
|
| 629 |
(including a type named by *typedef-name* ([[dcl.typedef]],
|
| 630 |
[[temp.param]])) shall appear only as:
|
| 631 |
|
|
|
|
| 636 |
- the *type-id* in the default argument of a *type-parameter* (
|
| 637 |
[[temp.param]]), or
|
| 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; // ill-formed: does not declare a member function
|
| 646 |
struct S {
|
| 647 |
FIC f; // OK
|
| 648 |
};
|
| 649 |
FIC S::*pm = &S::f; // OK
|
| 650 |
```
|
| 651 |
|
| 652 |
+
— *end example*]
|
| 653 |
+
|
| 654 |
The effect of a *cv-qualifier-seq* in a function declarator is not the
|
| 655 |
same as adding cv-qualification on top of the function type. In the
|
| 656 |
+
latter case, the cv-qualifiers are ignored.
|
| 657 |
+
|
| 658 |
+
[*Note 4*: A function type that has a *cv-qualifier-seq* is not a
|
| 659 |
+
cv-qualified type; there are no cv-qualified function
|
| 660 |
+
types. — *end note*]
|
| 661 |
+
|
| 662 |
+
[*Example 3*:
|
| 663 |
|
| 664 |
``` cpp
|
| 665 |
typedef void F();
|
| 666 |
struct S {
|
| 667 |
const F f; // OK: equivalent to: void f();
|
| 668 |
};
|
| 669 |
```
|
| 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 ([[dcl.fct.default]]), are part of the function type.
|
| 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 |
|
| 681 |
+
[*Example 4*:
|
| 682 |
+
|
| 683 |
+
The declaration
|
| 684 |
|
| 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` ([[dcl.type]]).
|
| 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 |
Types shall not be defined in return or parameter types. The type of a
|
| 700 |
parameter or the return type for a function definition shall not be an
|
| 701 |
+
incomplete (possibly cv-qualified) class type in the context of the
|
| 702 |
+
function definition unless the function is deleted (
|
| 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 ([[dcl.fct.def]]).
|
| 707 |
|
| 708 |
+
[*Example 5*:
|
| 709 |
+
|
| 710 |
``` cpp
|
| 711 |
typedef void F();
|
| 712 |
F fv; // OK: equivalent to void fv();
|
| 713 |
F fv { } // ill-formed
|
| 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 ([[dcl.fct.def]]), it names a parameter.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 ([[basic.scope.proto]]). — *end note*]
|
| 728 |
+
|
| 729 |
+
[*Example 6*:
|
| 730 |
+
|
| 731 |
+
The declaration
|
| 732 |
|
| 733 |
``` cpp
|
| 734 |
int i,
|
| 735 |
*pi,
|
| 736 |
f(),
|
|
|
|
| 750 |
suggests, and the same construction in an expression requires, the
|
| 751 |
calling of a function `fpi`, and then using indirection through the
|
| 752 |
(pointer) result to yield an integer. In the declarator
|
| 753 |
`(*pif)(const char*, const char*)`, the extra parentheses are necessary
|
| 754 |
to indicate that indirection through a pointer to a function yields a
|
| 755 |
+
function, which is then called.
|
| 756 |
+
|
| 757 |
+
— *end example*]
|
| 758 |
+
|
| 759 |
+
[*Note 7*:
|
| 760 |
+
|
| 761 |
+
Typedefs and *trailing-return-type*s are sometimes convenient when the
|
| 762 |
+
return type of a function is complex. For example, the function `fpif`
|
| 763 |
+
above could have been declared
|
| 764 |
|
| 765 |
``` cpp
|
| 766 |
typedef int IFUNC(int);
|
| 767 |
IFUNC* fpif(int);
|
| 768 |
```
|
|
|
|
| 784 |
|
| 785 |
``` cpp
|
| 786 |
template <class T, class U> decltype((*(T*)0) + (*(U*)0)) add(T t, U u);
|
| 787 |
```
|
| 788 |
|
| 789 |
+
— *end note*]
|
| 790 |
+
|
| 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*. Such a
|
| 798 |
*parameter-declaration* is a parameter pack ([[temp.variadic]]). When
|
| 799 |
it is part of a *parameter-declaration-clause*, the parameter pack is a
|
| 800 |
+
function parameter pack ([[temp.variadic]]).
|
| 801 |
+
|
| 802 |
+
[*Note 9*: Otherwise, the *parameter-declaration* is part of a
|
| 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);
|
|
|
|
| 816 |
void g() {
|
| 817 |
f(add, subtract);
|
| 818 |
}
|
| 819 |
```
|
| 820 |
|
| 821 |
+
— *end example*]
|
| 822 |
+
|
| 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*.[^9]
|
| 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. Default
|
| 834 |
arguments will be used in calls where trailing arguments are missing.
|
| 835 |
|
| 836 |
+
[*Example 1*:
|
| 837 |
+
|
| 838 |
+
The declaration
|
| 839 |
|
| 840 |
``` cpp
|
| 841 |
void point(int = 3, int = 4);
|
| 842 |
```
|
| 843 |
|
|
|
|
| 849 |
```
|
| 850 |
|
| 851 |
The last two calls are equivalent to `point(1,4)` and `point(3,4)`,
|
| 852 |
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* ([[temp.param]]); in
|
| 859 |
+
the latter case, the *initializer-clause* shall be an
|
| 860 |
+
*assignment-expression*. A default argument shall not be specified for a
|
| 861 |
+
parameter pack. If it is specified in a *parameter-declaration-clause*,
|
| 862 |
+
it shall not occur within a *declarator* or *abstract-declarator* of a
|
| 863 |
+
*parameter-declaration*.[^10]
|
| 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
|
|
|
|
| 871 |
argument shall have a default argument supplied in this or a previous
|
| 872 |
declaration or shall be a function parameter pack. A default argument
|
| 873 |
shall not be redefined by a later declaration (not even to the same
|
| 874 |
value).
|
| 875 |
|
| 876 |
+
[*Example 2*:
|
| 877 |
+
|
| 878 |
``` cpp
|
| 879 |
void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow
|
| 880 |
// a parameter with a default argument
|
| 881 |
void f(int, int);
|
| 882 |
void f(int, int = 7);
|
| 883 |
void h() {
|
| 884 |
f(3); // OK, calls f(3, 7)
|
| 885 |
+
void f(int = 1, int); // error: does not use default from surrounding scope
|
|
|
|
| 886 |
}
|
| 887 |
void m() {
|
| 888 |
void f(int, int); // has no defaults
|
| 889 |
f(4); // error: wrong number of arguments
|
| 890 |
void f(int, int = 5); // OK
|
| 891 |
f(4); // OK, calls f(4, 5);
|
| 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; see [[basic.def.odr]]. If a friend declaration
|
| 904 |
specifies a default argument expression, that declaration shall be a
|
| 905 |
definition and shall be the only declaration of the function or function
|
|
|
|
| 910 |
the copy-initialization semantics ([[dcl.init]]). The names in the
|
| 911 |
default argument are bound, and the semantic constraints are checked, at
|
| 912 |
the point where the default argument appears. Name lookup and checking
|
| 913 |
of semantic constraints for default arguments in function templates and
|
| 914 |
in member functions of class templates are performed as described in
|
| 915 |
+
[[temp.inst]].
|
| 916 |
+
|
| 917 |
+
[*Example 3*:
|
| 918 |
+
|
| 919 |
+
In the following code, `g` will be called with the value `f(2)`:
|
| 920 |
|
| 921 |
``` cpp
|
| 922 |
int a = 1;
|
| 923 |
int f(int);
|
| 924 |
int g(int x = f(a)); // default argument: f(::a)
|
|
|
|
| 930 |
g(); // g(f(::a))
|
| 931 |
}
|
| 932 |
}
|
| 933 |
```
|
| 934 |
|
| 935 |
+
— *end example*]
|
| 936 |
+
|
| 937 |
+
[*Note 1*: In member function declarations, names in default arguments
|
| 938 |
+
are looked up as described in [[basic.lookup.unqual]]. Access checking
|
| 939 |
+
applies to names in default arguments as described in Clause
|
| 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
|
|
|
|
| 947 |
constructor, or copy or move assignment operator ([[class.copy]]) is so
|
| 948 |
declared. Default arguments for a member function of a class template
|
| 949 |
shall be specified on the initial declaration of the member function
|
| 950 |
within the class template.
|
| 951 |
|
| 952 |
+
[*Example 4*:
|
| 953 |
+
|
| 954 |
``` cpp
|
| 955 |
class C {
|
| 956 |
void f(int i = 3);
|
| 957 |
void g(int i, int j = 99);
|
| 958 |
};
|
| 959 |
|
| 960 |
+
void C::f(int i = 3) {} // error: default argument already specified in class scope
|
| 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 shall not appear as a potentially-evaluated expression
|
| 967 |
+
in a default argument.
|
| 968 |
+
|
| 969 |
+
[*Example 5*:
|
| 970 |
|
| 971 |
``` cpp
|
| 972 |
void f() {
|
| 973 |
int i;
|
| 974 |
extern void g(int x = i); // error
|
| 975 |
+
extern void h(int x = sizeof(i)); // OK
|
| 976 |
// ...
|
| 977 |
}
|
| 978 |
```
|
| 979 |
|
| 980 |
+
— *end example*]
|
| 981 |
+
|
| 982 |
+
[*Note 2*:
|
| 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*:
|
| 988 |
|
| 989 |
``` cpp
|
| 990 |
class A {
|
| 991 |
void f(A* p = this) { } // error
|
| 992 |
};
|
| 993 |
```
|
| 994 |
|
| 995 |
+
— *end example*]
|
| 996 |
+
|
| 997 |
+
— *end note*]
|
| 998 |
+
|
| 999 |
A default argument is evaluated each time the function is called with no
|
| 1000 |
+
argument for the corresponding parameter. A parameter shall not appear
|
| 1001 |
+
as a potentially-evaluated expression in a default argument. Parameters
|
| 1002 |
+
of a function declared before a default argument are in scope and can
|
| 1003 |
+
hide namespace and class member names.
|
| 1004 |
+
|
| 1005 |
+
[*Example 7*:
|
| 1006 |
|
| 1007 |
``` cpp
|
| 1008 |
int a;
|
| 1009 |
+
int f(int a, int b = a); // error: parameter a used as default argument
|
|
|
|
| 1010 |
typedef int I;
|
| 1011 |
int g(float I, int b = I(2)); // error: parameter I found
|
| 1012 |
+
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]]) or unless it is used to form a pointer to member (
|
| 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
|
| 1026 |
+
an initializer.
|
| 1027 |
|
| 1028 |
``` cpp
|
| 1029 |
int b;
|
| 1030 |
class X {
|
| 1031 |
int a;
|
| 1032 |
+
int mem1(int i = a); // error: non-static member a used as default argument
|
|
|
|
| 1033 |
int mem2(int i = b); // OK; use X::b
|
| 1034 |
static int b;
|
| 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 Clause [[class]].
|
| 1041 |
+
|
| 1042 |
+
— *end example*]
|
| 1043 |
+
|
| 1044 |
+
A default argument is not part of the type of a function.
|
| 1045 |
+
|
| 1046 |
+
[*Example 9*:
|
| 1047 |
|
| 1048 |
``` cpp
|
| 1049 |
int f(int = 0);
|
| 1050 |
|
| 1051 |
void h() {
|
|
|
|
| 1055 |
|
| 1056 |
int (*p1)(int) = &f;
|
| 1057 |
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* ([[namespace.udecl]]), any default argument
|
| 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
|
|
|
|
| 1070 |
in the declaration of the virtual function determined by the static type
|
| 1071 |
of the pointer or reference denoting the object. An overriding function
|
| 1072 |
in a derived class does not acquire default arguments from the function
|
| 1073 |
it overrides.
|
| 1074 |
|
| 1075 |
+
[*Example 10*:
|
| 1076 |
+
|
| 1077 |
``` cpp
|
| 1078 |
struct A {
|
| 1079 |
virtual void f(int a = 7);
|
| 1080 |
};
|
| 1081 |
struct B : public A {
|
|
|
|
| 1087 |
pa->f(); // OK, calls pa->B::f(7)
|
| 1088 |
pb->f(); // error: wrong number of arguments for B::f()
|
| 1089 |
}
|
| 1090 |
```
|
| 1091 |
|
| 1092 |
+
— *end example*]
|
| 1093 |
+
|