- tmp/tmpk_da8jod/{from.md → to.md} +5268 -1022
tmp/tmpk_da8jod/{from.md → to.md}
RENAMED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
# Declarations <a id="dcl.dcl">[[dcl.dcl]]</a>
|
| 2 |
|
|
|
|
|
|
|
| 3 |
Declarations generally specify how names are to be interpreted.
|
| 4 |
Declarations have the form
|
| 5 |
|
| 6 |
``` bnf
|
| 7 |
declaration-seq:
|
|
@@ -16,22 +18,25 @@ declaration:
|
|
| 16 |
function-definition
|
| 17 |
template-declaration
|
| 18 |
deduction-guide
|
| 19 |
explicit-instantiation
|
| 20 |
explicit-specialization
|
|
|
|
| 21 |
linkage-specification
|
| 22 |
namespace-definition
|
| 23 |
empty-declaration
|
| 24 |
attribute-declaration
|
|
|
|
| 25 |
```
|
| 26 |
|
| 27 |
``` bnf
|
| 28 |
block-declaration:
|
| 29 |
simple-declaration
|
| 30 |
-
asm-
|
| 31 |
namespace-alias-definition
|
| 32 |
using-declaration
|
|
|
|
| 33 |
using-directive
|
| 34 |
static_assert-declaration
|
| 35 |
alias-declaration
|
| 36 |
opaque-enum-declaration
|
| 37 |
```
|
|
@@ -41,11 +46,11 @@ nodeclspec-function-declaration:
|
|
| 41 |
attribute-specifier-seqₒₚₜ declarator ';'
|
| 42 |
```
|
| 43 |
|
| 44 |
``` bnf
|
| 45 |
alias-declaration:
|
| 46 |
-
|
| 47 |
```
|
| 48 |
|
| 49 |
``` bnf
|
| 50 |
simple-declaration:
|
| 51 |
decl-specifier-seq init-declarator-listₒₚₜ ';'
|
|
@@ -53,12 +58,12 @@ simple-declaration:
|
|
| 53 |
attribute-specifier-seqₒₚₜ decl-specifier-seq ref-qualifierₒₚₜ '[' identifier-list ']' initializer ';'
|
| 54 |
```
|
| 55 |
|
| 56 |
``` bnf
|
| 57 |
static_assert-declaration:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
```
|
| 61 |
|
| 62 |
``` bnf
|
| 63 |
empty-declaration:
|
| 64 |
';'
|
|
@@ -67,28 +72,29 @@ empty-declaration:
|
|
| 67 |
``` bnf
|
| 68 |
attribute-declaration:
|
| 69 |
attribute-specifier-seq ';'
|
| 70 |
```
|
| 71 |
|
| 72 |
-
[*Note 1*: *asm-
|
| 73 |
-
*linkage-specification*s are described in [[dcl.link]]
|
| 74 |
-
*
|
| 75 |
-
*template-declaration*s and *deduction-guide*s are described in
|
| 76 |
-
[[temp]]
|
| 77 |
-
*using-declaration*s are described in
|
| 78 |
-
*using-directive*s are described in
|
|
|
|
| 79 |
|
| 80 |
A *simple-declaration* or *nodeclspec-function-declaration* of the form
|
| 81 |
|
| 82 |
``` bnf
|
| 83 |
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ init-declarator-listₒₚₜ ';'
|
| 84 |
```
|
| 85 |
|
| 86 |
is divided into three parts. Attributes are described in [[dcl.attr]].
|
| 87 |
*decl-specifier*s, the principal components of a *decl-specifier-seq*,
|
| 88 |
are described in [[dcl.spec]]. *declarator*s, the components of an
|
| 89 |
-
*init-declarator-list*, are described in
|
| 90 |
*attribute-specifier-seq* appertains to each of the entities declared by
|
| 91 |
the *declarator*s of the *init-declarator-list*.
|
| 92 |
|
| 93 |
[*Note 2*: In the declaration for an entity, attributes appertaining to
|
| 94 |
that entity may appear at the start of the declaration and after the
|
|
@@ -103,129 +109,133 @@ that entity may appear at the start of the declaration and after the
|
|
| 103 |
— *end example*]
|
| 104 |
|
| 105 |
Except where otherwise specified, the meaning of an
|
| 106 |
*attribute-declaration* is *implementation-defined*.
|
| 107 |
|
| 108 |
-
A declaration occurs in a scope
|
| 109 |
summarized in [[basic.lookup]]. A declaration that declares a function
|
| 110 |
or defines a class, namespace, template, or function also has one or
|
| 111 |
more scopes nested within it. These nested scopes, in turn, can have
|
| 112 |
declarations nested within them. Unless otherwise stated, utterances in
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
declaration.
|
| 117 |
|
| 118 |
In a *simple-declaration*, the optional *init-declarator-list* can be
|
| 119 |
-
omitted only when declaring a class
|
| 120 |
-
[[dcl.enum]]
|
| 121 |
-
*class-specifier*, an *elaborated-type-specifier* with a *class-key*
|
| 122 |
-
[[class.name]]
|
| 123 |
*class-specifier* or *enum-specifier* is present in the
|
| 124 |
*decl-specifier-seq*, the identifiers in these specifiers are among the
|
| 125 |
names being declared by the declaration (as *class-name*s, *enum-name*s,
|
| 126 |
or *enumerator*s, depending on the syntax). In such cases, the
|
| 127 |
*decl-specifier-seq* shall introduce one or more names into the program,
|
| 128 |
or shall redeclare a name introduced by a previous declaration.
|
| 129 |
|
| 130 |
[*Example 2*:
|
| 131 |
|
| 132 |
``` cpp
|
| 133 |
-
enum { }; //
|
| 134 |
-
typedef class { }; //
|
| 135 |
```
|
| 136 |
|
| 137 |
— *end example*]
|
| 138 |
|
| 139 |
In a *static_assert-declaration*, the *constant-expression* shall be a
|
| 140 |
-
contextually converted constant expression of type `bool`
|
| 141 |
-
[[expr.const]]
|
| 142 |
`true`, the declaration has no effect. Otherwise, the program is
|
| 143 |
-
ill-formed, and the resulting diagnostic message
|
| 144 |
shall include the text of the *string-literal*, if one is supplied,
|
| 145 |
-
except that characters not in the basic source character set
|
| 146 |
-
[[lex.charset]]
|
| 147 |
|
| 148 |
[*Example 3*:
|
| 149 |
|
| 150 |
``` cpp
|
| 151 |
-
static_assert(
|
| 152 |
```
|
| 153 |
|
| 154 |
— *end example*]
|
| 155 |
|
| 156 |
An *empty-declaration* has no effect.
|
| 157 |
|
| 158 |
A *simple-declaration* with an *identifier-list* is called a *structured
|
| 159 |
-
binding declaration*
|
| 160 |
-
|
| 161 |
-
*cv-qualifier*s
|
| 162 |
-
*
|
| 163 |
-
|
| 164 |
-
*assignment-expression*
|
|
|
|
| 165 |
|
| 166 |
Each *init-declarator* in the *init-declarator-list* contains exactly
|
| 167 |
one *declarator-id*, which is the name declared by that
|
| 168 |
*init-declarator* and hence one of the names declared by the
|
| 169 |
-
declaration. The *defining-type-specifier*s
|
| 170 |
*decl-specifier-seq* and the recursive *declarator* structure of the
|
| 171 |
-
*init-declarator* describe a type
|
| 172 |
associated with the name being declared by the *init-declarator*.
|
| 173 |
|
| 174 |
If the *decl-specifier-seq* contains the `typedef` specifier, the
|
| 175 |
declaration is called a *typedef declaration* and the name of each
|
| 176 |
*init-declarator* is declared to be a *typedef-name*, synonymous with
|
| 177 |
-
its associated type
|
| 178 |
contains no `typedef` specifier, the declaration is called a *function
|
| 179 |
-
declaration* if the type associated with the name is a function type
|
| 180 |
-
[[dcl.fct]]
|
| 181 |
|
| 182 |
Syntactic components beyond those found in the general form of
|
| 183 |
declaration are added to a function declaration to make a
|
| 184 |
*function-definition*. An object declaration, however, is also a
|
| 185 |
definition unless it contains the `extern` specifier and has no
|
| 186 |
-
initializer
|
| 187 |
-
|
| 188 |
-
[[dcl.init]]
|
| 189 |
|
| 190 |
A *nodeclspec-function-declaration* shall declare a constructor,
|
| 191 |
-
destructor, or conversion function.
|
| 192 |
|
| 193 |
[*Note 3*: A *nodeclspec-function-declaration* can only be used in a
|
| 194 |
-
*template-declaration*
|
| 195 |
-
[[temp.explicit]]
|
| 196 |
-
[[temp.expl.spec]]
|
| 197 |
|
| 198 |
## Specifiers <a id="dcl.spec">[[dcl.spec]]</a>
|
| 199 |
|
| 200 |
The specifiers that can be used in a declaration are
|
| 201 |
|
| 202 |
``` bnf
|
| 203 |
decl-specifier:
|
| 204 |
storage-class-specifier
|
| 205 |
defining-type-specifier
|
| 206 |
function-specifier
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
| 211 |
```
|
| 212 |
|
| 213 |
``` bnf
|
| 214 |
decl-specifier-seq:
|
| 215 |
decl-specifier attribute-specifier-seqₒₚₜ
|
| 216 |
decl-specifier decl-specifier-seq
|
| 217 |
```
|
| 218 |
|
| 219 |
The optional *attribute-specifier-seq* in a *decl-specifier-seq*
|
| 220 |
-
appertains to the type determined by the preceding *decl-specifier*s
|
| 221 |
-
[[dcl.meaning]]
|
| 222 |
-
|
| 223 |
-
|
| 224 |
|
| 225 |
Each *decl-specifier* shall appear at most once in a complete
|
| 226 |
-
*decl-specifier-seq*, except that `long` may appear twice.
|
|
|
|
|
|
|
| 227 |
|
| 228 |
If a *type-name* is encountered while parsing a *decl-specifier-seq*, it
|
| 229 |
is interpreted as part of the *decl-specifier-seq* if and only if there
|
| 230 |
is no previous *defining-type-specifier* other than a *cv-qualifier* in
|
| 231 |
the *decl-specifier-seq*. The sequence shall be self-consistent as
|
|
@@ -273,69 +283,71 @@ void k(unsigned int Pc); // void k(unsigned int)
|
|
| 273 |
|
| 274 |
The storage class specifiers are
|
| 275 |
|
| 276 |
``` bnf
|
| 277 |
storage-class-specifier:
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
```
|
| 283 |
|
| 284 |
At most one *storage-class-specifier* shall appear in a given
|
| 285 |
*decl-specifier-seq*, except that `thread_local` may appear with
|
| 286 |
`static` or `extern`. If `thread_local` appears in any declaration of a
|
| 287 |
variable it shall be present in all declarations of that entity. If a
|
| 288 |
*storage-class-specifier* appears in a *decl-specifier-seq*, there can
|
| 289 |
be no `typedef` specifier in the same *decl-specifier-seq* and the
|
| 290 |
*init-declarator-list* or *member-declarator-list* of the declaration
|
| 291 |
shall not be empty (except for an anonymous union declared in a named
|
| 292 |
-
namespace or in the global namespace, which shall be declared `static`
|
| 293 |
-
[[class.union.anon]])
|
| 294 |
-
|
| 295 |
-
declared by other specifiers.
|
| 296 |
-
`thread_local` shall not be specified in an explicit specialization (
|
| 297 |
-
[[temp.expl.spec]]) or an explicit instantiation ([[temp.explicit]])
|
| 298 |
-
directive.
|
| 299 |
|
| 300 |
-
[*Note 1*:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
block scope or declared as a function parameter has automatic storage
|
| 302 |
-
duration by default
|
| 303 |
|
| 304 |
The `thread_local` specifier indicates that the named entity has thread
|
| 305 |
-
storage duration
|
| 306 |
-
|
| 307 |
-
|
|
|
|
| 308 |
block scope the *storage-class-specifier* `static` is implied if no
|
| 309 |
other *storage-class-specifier* appears in the *decl-specifier-seq*.
|
| 310 |
|
| 311 |
-
The `static` specifier
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
|
|
|
| 322 |
|
| 323 |
-
The `extern` specifier
|
| 324 |
-
|
| 325 |
-
class
|
| 326 |
-
with an `extern` specifier, see [[basic.link]].
|
| 327 |
|
| 328 |
-
[*Note
|
| 329 |
*explicit-instantiation*s and *linkage-specification*s, but it is not a
|
| 330 |
*storage-class-specifier* in such contexts. — *end note*]
|
| 331 |
|
| 332 |
The linkages implied by successive declarations for a given entity shall
|
| 333 |
agree. That is, within a given scope, each declaration declaring the
|
| 334 |
same variable name or the same overloading of a function name shall
|
| 335 |
-
imply the same linkage.
|
| 336 |
-
functions can have a different linkage, however.
|
| 337 |
|
| 338 |
[*Example 1*:
|
| 339 |
|
| 340 |
``` cpp
|
| 341 |
static char* f(); // f() has internal linkage
|
|
@@ -392,71 +404,86 @@ void h() {
|
|
| 392 |
```
|
| 393 |
|
| 394 |
— *end example*]
|
| 395 |
|
| 396 |
The `mutable` specifier shall appear only in the declaration of a
|
| 397 |
-
non-static data member
|
| 398 |
const-qualified nor a reference type.
|
| 399 |
|
| 400 |
[*Example 3*:
|
| 401 |
|
| 402 |
``` cpp
|
| 403 |
class X {
|
| 404 |
mutable const int* p; // OK
|
| 405 |
-
mutable int* const q; //
|
| 406 |
};
|
| 407 |
```
|
| 408 |
|
| 409 |
— *end example*]
|
| 410 |
|
| 411 |
-
The `mutable` specifier on a class data member nullifies a
|
| 412 |
-
specifier applied to the containing class object and permits
|
| 413 |
modification of the mutable class member even though the rest of the
|
| 414 |
-
object is
|
|
|
|
| 415 |
|
| 416 |
### Function specifiers <a id="dcl.fct.spec">[[dcl.fct.spec]]</a>
|
| 417 |
|
| 418 |
-
can be used only in function
|
| 419 |
|
| 420 |
``` bnf
|
| 421 |
function-specifier:
|
| 422 |
-
|
| 423 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 424 |
```
|
| 425 |
|
| 426 |
The `virtual` specifier shall be used only in the initial declaration of
|
| 427 |
a non-static class member function; see [[class.virtual]].
|
| 428 |
|
| 429 |
-
|
| 430 |
constructor or conversion function within its class definition; see
|
| 431 |
[[class.conv.ctor]] and [[class.conv.fct]].
|
| 432 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
### The `typedef` specifier <a id="dcl.typedef">[[dcl.typedef]]</a>
|
| 434 |
|
| 435 |
Declarations containing the *decl-specifier* `typedef` declare
|
| 436 |
-
identifiers that can be used later for naming fundamental
|
| 437 |
-
[[basic.fundamental]]
|
| 438 |
`typedef` specifier shall not be combined in a *decl-specifier-seq* with
|
| 439 |
any other kind of specifier except a *defining-type-specifier*, and it
|
| 440 |
shall not be used in the *decl-specifier-seq* of a
|
| 441 |
-
*parameter-declaration*
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
ill-formed.
|
| 445 |
|
| 446 |
``` bnf
|
| 447 |
typedef-name:
|
| 448 |
identifier
|
|
|
|
| 449 |
```
|
| 450 |
|
| 451 |
-
A name declared with the `typedef` specifier becomes a *typedef-name*.
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
or enum declaration does.
|
| 458 |
|
| 459 |
[*Example 1*:
|
| 460 |
|
| 461 |
After
|
| 462 |
|
|
@@ -487,21 +514,21 @@ particular, it does not define a new type.
|
|
| 487 |
|
| 488 |
``` cpp
|
| 489 |
using handler_t = void (*)(int);
|
| 490 |
extern handler_t ignore;
|
| 491 |
extern void (*ignore)(int); // redeclare ignore
|
| 492 |
-
using cell = pair<void*, cell*>; //
|
| 493 |
```
|
| 494 |
|
| 495 |
— *end example*]
|
| 496 |
|
| 497 |
The *defining-type-specifier-seq* of the *defining-type-id* shall not
|
| 498 |
define a class or enumeration if the *alias-declaration* is the
|
| 499 |
*declaration* of a *template-declaration*.
|
| 500 |
|
| 501 |
In a given non-class scope, a `typedef` specifier can be used to
|
| 502 |
-
|
| 503 |
type to which it already refers.
|
| 504 |
|
| 505 |
[*Example 3*:
|
| 506 |
|
| 507 |
``` cpp
|
|
@@ -511,11 +538,11 @@ typedef int I;
|
|
| 511 |
typedef I I;
|
| 512 |
```
|
| 513 |
|
| 514 |
— *end example*]
|
| 515 |
|
| 516 |
-
In a given class scope, a `typedef` specifier can be used to
|
| 517 |
any *class-name* declared in that scope that is not also a
|
| 518 |
*typedef-name* to refer to the type to which it already refers.
|
| 519 |
|
| 520 |
[*Example 4*:
|
| 521 |
|
|
@@ -527,11 +554,11 @@ struct S {
|
|
| 527 |
};
|
| 528 |
```
|
| 529 |
|
| 530 |
— *end example*]
|
| 531 |
|
| 532 |
-
If a `typedef` specifier is used to
|
| 533 |
that can be referenced using an *elaborated-type-specifier*, the entity
|
| 534 |
can continue to be referenced by an *elaborated-type-specifier* or as an
|
| 535 |
enumeration or class name in an enumeration or class definition
|
| 536 |
respectively.
|
| 537 |
|
|
@@ -546,11 +573,11 @@ int main() {
|
|
| 546 |
struct S { }; // OK
|
| 547 |
```
|
| 548 |
|
| 549 |
— *end example*]
|
| 550 |
|
| 551 |
-
In a given scope, a `typedef` specifier shall not be used to
|
| 552 |
the name of any type declared in that scope to refer to a different
|
| 553 |
type.
|
| 554 |
|
| 555 |
[*Example 6*:
|
| 556 |
|
|
@@ -572,17 +599,19 @@ typedef int complex;
|
|
| 572 |
class complex { ... }; // error: redefinition
|
| 573 |
```
|
| 574 |
|
| 575 |
— *end example*]
|
| 576 |
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
*
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
|
|
|
|
|
|
| 584 |
|
| 585 |
[*Example 8*:
|
| 586 |
|
| 587 |
``` cpp
|
| 588 |
struct S {
|
|
@@ -596,40 +625,67 @@ S a = T(); // OK
|
|
| 596 |
struct T * p; // error
|
| 597 |
```
|
| 598 |
|
| 599 |
— *end example*]
|
| 600 |
|
| 601 |
-
If the typedef declaration defines an unnamed class
|
| 602 |
-
*typedef-name* declared by the declaration to be that
|
| 603 |
-
|
| 604 |
-
|
|
|
|
|
|
|
|
|
|
| 605 |
|
| 606 |
[*Example 9*:
|
| 607 |
|
| 608 |
``` cpp
|
| 609 |
typedef struct { } *ps, S; // S is the class name for linkage purposes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 610 |
```
|
| 611 |
|
| 612 |
— *end example*]
|
| 613 |
|
| 614 |
### The `friend` specifier <a id="dcl.friend">[[dcl.friend]]</a>
|
| 615 |
|
| 616 |
The `friend` specifier is used to specify access to class members; see
|
| 617 |
[[class.friend]].
|
| 618 |
|
| 619 |
-
### The `constexpr`
|
| 620 |
|
| 621 |
The `constexpr` specifier shall be applied only to the definition of a
|
| 622 |
variable or variable template or the declaration of a function or
|
| 623 |
-
function template.
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
`constexpr`
|
|
|
|
|
|
|
| 628 |
|
| 629 |
[*Note 1*: An explicit specialization can differ from the template
|
| 630 |
-
declaration with respect to the `constexpr`
|
|
|
|
| 631 |
|
| 632 |
[*Note 2*: Function parameters cannot be declared
|
| 633 |
`constexpr`. — *end note*]
|
| 634 |
|
| 635 |
[*Example 1*:
|
|
@@ -644,11 +700,11 @@ constexpr struct pixel { // error: pixel is a type
|
|
| 644 |
};
|
| 645 |
constexpr pixel::pixel(int a)
|
| 646 |
: x(a), y(x) // OK: definition
|
| 647 |
{ square(x); }
|
| 648 |
constexpr pixel small(2); // error: square not defined, so small(2)
|
| 649 |
-
// not constant
|
| 650 |
|
| 651 |
constexpr void square(int &x) { // OK: definition
|
| 652 |
x *= x;
|
| 653 |
}
|
| 654 |
constexpr pixel large(4); // OK: square defined
|
|
@@ -658,29 +714,33 @@ int next(constexpr int x) { // error: not for parameters
|
|
| 658 |
extern constexpr int memsz; // error: not a definition
|
| 659 |
```
|
| 660 |
|
| 661 |
— *end example*]
|
| 662 |
|
| 663 |
-
A `constexpr` specifier used in the declaration of a
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
|
|
|
|
|
|
| 667 |
|
| 668 |
The definition of a constexpr function shall satisfy the following
|
| 669 |
requirements:
|
| 670 |
|
| 671 |
-
-
|
| 672 |
-
- its return type shall be a literal type;
|
| 673 |
- each of its parameter types shall be a literal type;
|
| 674 |
-
-
|
| 675 |
-
|
| 676 |
-
|
|
|
|
| 677 |
- a `goto` statement,
|
| 678 |
-
- an identifier label
|
| 679 |
-
- a *try-block*, or
|
| 680 |
- a definition of a variable of non-literal type or of static or
|
| 681 |
-
thread storage duration
|
|
|
|
|
|
|
|
|
|
| 682 |
|
| 683 |
[*Example 2*:
|
| 684 |
|
| 685 |
``` cpp
|
| 686 |
constexpr int square(int x)
|
|
@@ -695,12 +755,12 @@ constexpr int abs(int x) {
|
|
| 695 |
constexpr int first(int n) {
|
| 696 |
static int value = n; // error: variable has static storage duration
|
| 697 |
return value;
|
| 698 |
}
|
| 699 |
constexpr int uninit() {
|
| 700 |
-
int a;
|
| 701 |
-
return a;
|
| 702 |
}
|
| 703 |
constexpr int prev(int x)
|
| 704 |
{ return --x; } // OK
|
| 705 |
constexpr int g(int x, int n) { // OK
|
| 706 |
int r = 1;
|
|
@@ -709,30 +769,13 @@ constexpr int g(int x, int n) { // OK
|
|
| 709 |
}
|
| 710 |
```
|
| 711 |
|
| 712 |
— *end example*]
|
| 713 |
|
| 714 |
-
The definition of a constexpr constructor
|
| 715 |
-
requirements:
|
| 716 |
|
| 717 |
-
- the class shall not have any virtual base classes;
|
| 718 |
-
- each of the parameter types shall be a literal type;
|
| 719 |
-
- its *function-body* shall not be a *function-try-block*.
|
| 720 |
-
|
| 721 |
-
In addition, either its *function-body* shall be `= delete`, or it shall
|
| 722 |
-
satisfy the following requirements:
|
| 723 |
-
|
| 724 |
-
- either its *function-body* shall be `= default`, or the
|
| 725 |
-
*compound-statement* of its *function-body* shall satisfy the
|
| 726 |
-
requirements for a *function-body* of a constexpr function;
|
| 727 |
-
- every non-variant non-static data member and base class subobject
|
| 728 |
-
shall be initialized ([[class.base.init]]);
|
| 729 |
-
- if the class is a union having variant members ([[class.union]]),
|
| 730 |
-
exactly one of them shall be initialized;
|
| 731 |
-
- if the class is a union-like class, but is not a union, for each of
|
| 732 |
-
its anonymous union members having variant members, exactly one of
|
| 733 |
-
them shall be initialized;
|
| 734 |
- for a non-delegating constructor, every constructor selected to
|
| 735 |
initialize non-static data members and base class subobjects shall be
|
| 736 |
a constexpr constructor;
|
| 737 |
- for a delegating constructor, the target constructor shall be a
|
| 738 |
constexpr constructor.
|
|
@@ -747,16 +790,23 @@ private:
|
|
| 747 |
};
|
| 748 |
```
|
| 749 |
|
| 750 |
— *end example*]
|
| 751 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 752 |
For a constexpr function or constexpr constructor that is neither
|
| 753 |
defaulted nor a template, if no argument values exist such that an
|
| 754 |
invocation of the function or constructor could be an evaluated
|
| 755 |
-
subexpression of a core constant expression
|
| 756 |
-
constructor,
|
| 757 |
-
|
|
|
|
| 758 |
required.
|
| 759 |
|
| 760 |
[*Example 4*:
|
| 761 |
|
| 762 |
``` cpp
|
|
@@ -779,27 +829,33 @@ struct D : B {
|
|
| 779 |
|
| 780 |
— *end example*]
|
| 781 |
|
| 782 |
If the instantiated template specialization of a constexpr function
|
| 783 |
template or member function of a class template would fail to satisfy
|
| 784 |
-
the requirements for a constexpr function
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
ill-formed, no diagnostic required.
|
| 791 |
|
| 792 |
-
|
| 793 |
-
equivalent non-constexpr function in
|
|
|
|
| 794 |
|
| 795 |
-
-
|
| 796 |
-
[[expr.const]]
|
| 797 |
-
- copy elision is
|
|
|
|
| 798 |
|
| 799 |
-
|
| 800 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 801 |
|
| 802 |
[*Example 5*:
|
| 803 |
|
| 804 |
``` cpp
|
| 805 |
constexpr int bar(int x, int y) // OK
|
|
@@ -810,14 +866,14 @@ int bar(int x, int y) // error: redefinition of bar
|
|
| 810 |
```
|
| 811 |
|
| 812 |
— *end example*]
|
| 813 |
|
| 814 |
A `constexpr` specifier used in an object declaration declares the
|
| 815 |
-
object as
|
| 816 |
initialized. In any `constexpr` variable declaration, the
|
| 817 |
-
full-expression of the initialization shall be a constant expression
|
| 818 |
-
[[expr.const]]
|
| 819 |
|
| 820 |
[*Example 6*:
|
| 821 |
|
| 822 |
``` cpp
|
| 823 |
struct pixel {
|
|
@@ -827,54 +883,86 @@ constexpr pixel ur = { 1294, 1024 }; // OK
|
|
| 827 |
constexpr pixel origin; // error: initializer missing
|
| 828 |
```
|
| 829 |
|
| 830 |
— *end example*]
|
| 831 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 832 |
### The `inline` specifier <a id="dcl.inline">[[dcl.inline]]</a>
|
| 833 |
|
| 834 |
-
The `inline` specifier
|
| 835 |
-
|
| 836 |
|
| 837 |
-
A function declaration ([[dcl.fct]],
|
| 838 |
with an `inline` specifier declares an *inline function*. The inline
|
| 839 |
specifier indicates to the implementation that inline substitution of
|
| 840 |
the function body at the point of call is to be preferred to the usual
|
| 841 |
function call mechanism. An implementation is not required to perform
|
| 842 |
this inline substitution at the point of call; however, even if this
|
| 843 |
inline substitution is omitted, the other rules for inline functions
|
| 844 |
-
specified in this
|
| 845 |
|
| 846 |
-
|
| 847 |
-
|
|
|
|
| 848 |
|
| 849 |
-
A
|
|
|
|
| 850 |
|
| 851 |
-
The `inline` specifier shall not appear on a block scope
|
| 852 |
-
declaration.
|
| 853 |
-
declaration, that declaration shall be a
|
| 854 |
-
shall have previously been declared inline.
|
| 855 |
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
|
|
|
|
|
|
|
|
|
| 859 |
|
| 860 |
-
[*Note
|
| 861 |
-
|
| 862 |
translation unit. — *end note*]
|
| 863 |
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
|
|
|
|
| 876 |
|
| 877 |
### Type specifiers <a id="dcl.type">[[dcl.type]]</a>
|
| 878 |
|
| 879 |
The type-specifiers are
|
| 880 |
|
|
@@ -905,14 +993,14 @@ defining-type-specifier-seq:
|
|
| 905 |
defining-type-specifier defining-type-specifier-seq
|
| 906 |
```
|
| 907 |
|
| 908 |
The optional *attribute-specifier-seq* in a *type-specifier-seq* or a
|
| 909 |
*defining-type-specifier-seq* appertains to the type denoted by the
|
| 910 |
-
preceding *type-specifier*s or *defining-type-specifier*s
|
| 911 |
-
[[dcl.meaning]]
|
| 912 |
-
|
| 913 |
-
|
| 914 |
|
| 915 |
As a general rule, at most one *defining-type-specifier* is allowed in
|
| 916 |
the complete *decl-specifier-seq* of a *declaration* or in a
|
| 917 |
*defining-type-specifier-seq*, and at most one *type-specifier* is
|
| 918 |
allowed in a *type-specifier-seq*. The only exceptions to this rule are
|
|
@@ -927,16 +1015,16 @@ the following:
|
|
| 927 |
- `long` can be combined with `long`.
|
| 928 |
|
| 929 |
Except in a declaration of a constructor, destructor, or conversion
|
| 930 |
function, at least one *defining-type-specifier* that is not a
|
| 931 |
*cv-qualifier* shall appear in a complete *type-specifier-seq* or a
|
| 932 |
-
complete *decl-specifier-seq*.[^
|
| 933 |
|
| 934 |
[*Note 1*: *enum-specifier*s, *class-specifier*s, and
|
| 935 |
-
*typename-specifier*s are discussed in [[dcl.enum]],
|
| 936 |
-
|
| 937 |
-
discussed in the rest of this
|
| 938 |
|
| 939 |
#### The *cv-qualifier*s <a id="dcl.type.cv">[[dcl.type.cv]]</a>
|
| 940 |
|
| 941 |
There are two *cv-qualifier*s, `const` and `volatile`. Each
|
| 942 |
*cv-qualifier* shall appear at most once in a *cv-qualifier-seq*. If a
|
|
@@ -950,48 +1038,47 @@ cv-qualifiers affect object and function types. — *end note*]
|
|
| 950 |
Redundant cv-qualifications are ignored.
|
| 951 |
|
| 952 |
[*Note 2*: For example, these could be introduced by
|
| 953 |
typedefs. — *end note*]
|
| 954 |
|
| 955 |
-
[*Note 3*: Declaring a variable `const` can affect its linkage
|
| 956 |
-
[[dcl.stc]]
|
| 957 |
-
|
| 958 |
-
|
| 959 |
-
|
| 960 |
|
| 961 |
A pointer or reference to a cv-qualified type need not actually point or
|
| 962 |
refer to a cv-qualified object, but it is treated as if it does; a
|
| 963 |
const-qualified access path cannot be used to modify an object even if
|
| 964 |
the object referenced is a non-const object and can be modified through
|
| 965 |
some other access path.
|
| 966 |
|
| 967 |
[*Note 4*: Cv-qualifiers are supported by the type system so that they
|
| 968 |
-
cannot be subverted without casting
|
| 969 |
-
[[expr.const.cast]]). — *end note*]
|
| 970 |
|
| 971 |
-
|
| 972 |
-
|
| 973 |
-
[[basic.life]]
|
| 974 |
|
| 975 |
[*Example 1*:
|
| 976 |
|
| 977 |
``` cpp
|
| 978 |
const int ci = 3; // cv-qualified (initialized as required)
|
| 979 |
-
ci = 4; //
|
| 980 |
|
| 981 |
int i = 2; // not cv-qualified
|
| 982 |
const int* cip; // pointer to const int
|
| 983 |
cip = &i; // OK: cv-qualified access path to unqualified
|
| 984 |
-
*cip = 4; //
|
| 985 |
|
| 986 |
int* ip;
|
| 987 |
ip = const_cast<int*>(cip); // cast needed to convert const int* to int*
|
| 988 |
*ip = 4; // defined: *ip points to i, a non-const object
|
| 989 |
|
| 990 |
const int* ciq = new const int (3); // initialized as required
|
| 991 |
int* iq = const_cast<int*>(ciq); // cast required
|
| 992 |
-
*iq = 4; // undefined: modifies a const object
|
| 993 |
```
|
| 994 |
|
| 995 |
For another example,
|
| 996 |
|
| 997 |
``` cpp
|
|
@@ -1004,14 +1091,14 @@ struct Y {
|
|
| 1004 |
Y();
|
| 1005 |
};
|
| 1006 |
|
| 1007 |
const Y y;
|
| 1008 |
y.x.i++; // well-formed: mutable member can be modified
|
| 1009 |
-
y.x.j++; //
|
| 1010 |
Y* p = const_cast<Y*>(&y); // cast away const-ness of y
|
| 1011 |
p->x.i = 99; // well-formed: mutable member can be modified
|
| 1012 |
-
p->x.j = 99; // undefined: modifies a const
|
| 1013 |
```
|
| 1014 |
|
| 1015 |
— *end example*]
|
| 1016 |
|
| 1017 |
The semantics of an access through a volatile glvalue are
|
|
@@ -1033,126 +1120,226 @@ C. — *end note*]
|
|
| 1033 |
The simple type specifiers are
|
| 1034 |
|
| 1035 |
``` bnf
|
| 1036 |
simple-type-specifier:
|
| 1037 |
nested-name-specifierₒₚₜ type-name
|
| 1038 |
-
nested-name-specifier
|
| 1039 |
-
nested-name-specifierₒₚₜ template-name
|
| 1040 |
-
'char'
|
| 1041 |
-
'char16_t'
|
| 1042 |
-
'char32_t'
|
| 1043 |
-
'wchar_t'
|
| 1044 |
-
'bool'
|
| 1045 |
-
'short'
|
| 1046 |
-
'int'
|
| 1047 |
-
'long'
|
| 1048 |
-
'signed'
|
| 1049 |
-
'unsigned'
|
| 1050 |
-
'float'
|
| 1051 |
-
'double'
|
| 1052 |
-
'void'
|
| 1053 |
-
'auto'
|
| 1054 |
decltype-specifier
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1055 |
```
|
| 1056 |
|
| 1057 |
``` bnf
|
| 1058 |
type-name:
|
| 1059 |
class-name
|
| 1060 |
enum-name
|
| 1061 |
typedef-name
|
| 1062 |
-
simple-template-id
|
| 1063 |
```
|
| 1064 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1065 |
``` bnf
|
| 1066 |
-
|
| 1067 |
-
'decltype' '(' expression ')'
|
| 1068 |
-
'decltype' '(' 'auto' ')'
|
| 1069 |
```
|
| 1070 |
|
| 1071 |
-
|
| 1072 |
-
|
| 1073 |
-
`typename`ₒₚₜ *nested-name-specifier*ₒₚₜ *template-name* is a
|
| 1074 |
-
placeholder for a deduced class type ([[dcl.type.class.deduct]]). The
|
| 1075 |
-
*template-name* shall name a class template that is not an
|
| 1076 |
-
injected-class-name. The other *simple-type-specifier*s specify either a
|
| 1077 |
-
previously-declared type, a type determined from an expression, or one
|
| 1078 |
-
of the fundamental types ([[basic.fundamental]]). Table
|
| 1079 |
-
[[tab:simple.type.specifiers]] summarizes the valid combinations of
|
| 1080 |
-
*simple-type-specifier*s and the types they specify.
|
| 1081 |
|
| 1082 |
-
**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1083 |
|
| 1084 |
| Specifier(s) | Type |
|
| 1085 |
-
| ---------------------- | -------------------------------------- |
|
| 1086 |
| *type-name* | the type named |
|
| 1087 |
| *simple-template-id* | the type as defined in~ [[temp.names]] |
|
| 1088 |
-
| *
|
| 1089 |
-
|
|
| 1090 |
-
|
|
| 1091 |
-
|
|
| 1092 |
-
|
|
| 1093 |
-
|
|
| 1094 |
-
|
|
| 1095 |
-
|
|
| 1096 |
-
|
|
| 1097 |
-
|
|
| 1098 |
-
|
|
| 1099 |
-
| int
|
| 1100 |
-
|
|
| 1101 |
-
|
|
| 1102 |
-
|
|
| 1103 |
-
| unsigned
|
| 1104 |
-
| unsigned
|
| 1105 |
-
| unsigned long
|
| 1106 |
-
|
|
| 1107 |
-
|
|
| 1108 |
-
|
|
| 1109 |
-
| signed long
|
| 1110 |
-
|
|
| 1111 |
-
| long long
|
| 1112 |
-
| long
|
| 1113 |
-
| long
|
| 1114 |
-
|
|
| 1115 |
-
|
|
| 1116 |
-
|
|
| 1117 |
-
| short
|
| 1118 |
-
|
|
| 1119 |
-
|
|
| 1120 |
-
|
|
| 1121 |
-
|
|
| 1122 |
-
|
|
| 1123 |
-
|
|
| 1124 |
-
|
|
| 1125 |
-
|
|
| 1126 |
|
| 1127 |
|
| 1128 |
When multiple *simple-type-specifier*s are allowed, they can be freely
|
| 1129 |
intermixed with other *decl-specifier*s in any order.
|
| 1130 |
|
| 1131 |
-
[*Note
|
| 1132 |
type are represented as signed or unsigned quantities. The `signed`
|
| 1133 |
specifier forces `char` objects to be signed; it is redundant in other
|
| 1134 |
contexts. — *end note*]
|
| 1135 |
|
| 1136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1137 |
follows:
|
| 1138 |
|
| 1139 |
-
- if
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
- otherwise, if
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
- otherwise, if
|
| 1147 |
-
|
| 1148 |
-
|
| 1149 |
-
|
| 1150 |
-
- otherwise, `decltype(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1151 |
|
| 1152 |
The operand of the `decltype` specifier is an unevaluated operand
|
| 1153 |
-
|
| 1154 |
|
| 1155 |
[*Example 1*:
|
| 1156 |
|
| 1157 |
``` cpp
|
| 1158 |
const int&& foo();
|
|
@@ -1165,29 +1352,30 @@ decltype(a->x) x3; // type is double
|
|
| 1165 |
decltype((a->x)) x4 = x3; // type is const double&
|
| 1166 |
```
|
| 1167 |
|
| 1168 |
— *end example*]
|
| 1169 |
|
| 1170 |
-
[*Note
|
| 1171 |
are specified in [[dcl.spec.auto]]. — *end note*]
|
| 1172 |
|
| 1173 |
-
If the operand of a *decltype-specifier* is a prvalue
|
| 1174 |
-
|
| 1175 |
-
|
| 1176 |
-
|
|
|
|
| 1177 |
|
| 1178 |
-
[*Note
|
| 1179 |
is not destroyed. Thus, a class type is not instantiated as a result of
|
| 1180 |
being the type of a function call in this context. In this context, the
|
| 1181 |
common purpose of writing the expression is merely to refer to its type.
|
| 1182 |
In that sense, a *decltype-specifier* is analogous to a use of a
|
| 1183 |
*typedef-name*, so the usual reasons for requiring a complete type do
|
| 1184 |
not apply. In particular, it is not necessary to allocate storage for a
|
| 1185 |
temporary object or to enforce the semantic constraints associated with
|
| 1186 |
invoking the type’s destructor. — *end note*]
|
| 1187 |
|
| 1188 |
-
[*Note
|
| 1189 |
meaning in this context. — *end note*]
|
| 1190 |
|
| 1191 |
[*Example 2*:
|
| 1192 |
|
| 1193 |
``` cpp
|
|
@@ -1202,149 +1390,74 @@ template<class T> auto f(T) // #1
|
|
| 1202 |
// (A temporary is not introduced as a result of the use of i().)
|
| 1203 |
template<class T> auto f(T) // #2
|
| 1204 |
-> void;
|
| 1205 |
auto g() -> void {
|
| 1206 |
f(42); // OK: calls #2. (#1 is not a viable candidate: type deduction
|
| 1207 |
-
// fails
|
| 1208 |
// decltype-specifier)
|
| 1209 |
}
|
| 1210 |
template<class T> auto q(T)
|
| 1211 |
-> decltype((h<T>())); // does not force completion of A<T>; A<T>::~A() is not implicitly
|
| 1212 |
// used within the context of this decltype-specifier
|
| 1213 |
void r() {
|
| 1214 |
-
q(42); //
|
| 1215 |
-
// the specialization ``q(T) -> decltype((h<T>()))
|
| 1216 |
-
//
|
| 1217 |
-
// destructor is used, so the program is ill-formed
|
| 1218 |
}
|
| 1219 |
```
|
| 1220 |
|
| 1221 |
— *end example*]
|
| 1222 |
|
| 1223 |
-
####
|
| 1224 |
|
| 1225 |
``` bnf
|
| 1226 |
-
|
| 1227 |
-
|
| 1228 |
-
|
| 1229 |
-
class-key nested-name-specifier 'template'ₒₚₜ simple-template-id
|
| 1230 |
-
'enum' nested-name-specifierₒₚₜ identifier
|
| 1231 |
```
|
| 1232 |
|
| 1233 |
-
|
| 1234 |
-
|
| 1235 |
-
a declaration. If an *elaborated-type-specifier* is the sole constituent
|
| 1236 |
-
of a declaration, the declaration is ill-formed unless it is an explicit
|
| 1237 |
-
specialization ([[temp.expl.spec]]), an explicit instantiation (
|
| 1238 |
-
[[temp.explicit]]) or it has one of the following forms:
|
| 1239 |
|
| 1240 |
-
``
|
| 1241 |
-
|
| 1242 |
-
|
| 1243 |
-
|
| 1244 |
-
|
| 1245 |
-
|
| 1246 |
-
```
|
| 1247 |
-
|
| 1248 |
-
In the first case, the *attribute-specifier-seq*, if any, appertains to
|
| 1249 |
-
the class being declared; the attributes in the
|
| 1250 |
-
*attribute-specifier-seq* are thereafter considered attributes of the
|
| 1251 |
-
class whenever it is named.
|
| 1252 |
-
|
| 1253 |
-
[[basic.lookup.elab]] describes how name lookup proceeds for the
|
| 1254 |
-
*identifier* in an *elaborated-type-specifier*. If the *identifier*
|
| 1255 |
-
resolves to a *class-name* or *enum-name*, the
|
| 1256 |
-
*elaborated-type-specifier* introduces it into the declaration the same
|
| 1257 |
-
way a *simple-type-specifier* introduces its *type-name*. If the
|
| 1258 |
-
*identifier* resolves to a *typedef-name* or the *simple-template-id*
|
| 1259 |
-
resolves to an alias template specialization, the
|
| 1260 |
-
*elaborated-type-specifier* is ill-formed.
|
| 1261 |
-
|
| 1262 |
-
[*Note 1*:
|
| 1263 |
-
|
| 1264 |
-
This implies that, within a class template with a template
|
| 1265 |
-
*type-parameter* `T`, the declaration
|
| 1266 |
-
|
| 1267 |
-
``` cpp
|
| 1268 |
-
friend class T;
|
| 1269 |
-
```
|
| 1270 |
-
|
| 1271 |
-
is ill-formed. However, the similar declaration `friend T;` is allowed (
|
| 1272 |
-
[[class.friend]]).
|
| 1273 |
-
|
| 1274 |
-
— *end note*]
|
| 1275 |
-
|
| 1276 |
-
The *class-key* or `enum` keyword present in the
|
| 1277 |
-
*elaborated-type-specifier* shall agree in kind with the declaration to
|
| 1278 |
-
which the name in the *elaborated-type-specifier* refers. This rule also
|
| 1279 |
-
applies to the form of *elaborated-type-specifier* that declares a
|
| 1280 |
-
*class-name* or `friend` class since it can be construed as referring to
|
| 1281 |
-
the definition of the class. Thus, in any *elaborated-type-specifier*,
|
| 1282 |
-
the `enum` keyword shall be used to refer to an enumeration (
|
| 1283 |
-
[[dcl.enum]]), the `union` *class-key* shall be used to refer to a union
|
| 1284 |
-
(Clause [[class]]), and either the `class` or `struct` *class-key*
|
| 1285 |
-
shall be used to refer to a class (Clause [[class]]) declared using the
|
| 1286 |
-
`class` or `struct` *class-key*.
|
| 1287 |
-
|
| 1288 |
-
[*Example 1*:
|
| 1289 |
-
|
| 1290 |
-
``` cpp
|
| 1291 |
-
enum class E { a, b };
|
| 1292 |
-
enum E x = E::a; // OK
|
| 1293 |
-
```
|
| 1294 |
-
|
| 1295 |
-
— *end example*]
|
| 1296 |
-
|
| 1297 |
-
#### The `auto` specifier <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
|
| 1298 |
|
| 1299 |
-
|
| 1300 |
-
|
| 1301 |
-
|
| 1302 |
-
function type having a *trailing-return-type* or to signify that a
|
| 1303 |
-
lambda is a generic lambda ([[expr.prim.lambda.closure]]). The `auto`
|
| 1304 |
-
*type-specifier* is also used to introduce a structured binding
|
| 1305 |
-
declaration ([[dcl.struct.bind]]).
|
| 1306 |
|
| 1307 |
The placeholder type can appear with a function declarator in the
|
| 1308 |
*decl-specifier-seq*, *type-specifier-seq*, *conversion-function-id*, or
|
| 1309 |
*trailing-return-type*, in any context where such a declarator is valid.
|
| 1310 |
-
If the function declarator includes a *trailing-return-type*
|
| 1311 |
-
[[dcl.fct]]
|
| 1312 |
type of the function. Otherwise, the function declarator shall declare a
|
| 1313 |
function. If the declared return type of the function contains a
|
| 1314 |
placeholder type, the return type of the function is deduced from
|
| 1315 |
-
non-discarded `return` statements, if any, in the body of the function
|
| 1316 |
-
[[stmt.if]]
|
| 1317 |
-
|
| 1318 |
-
|
| 1319 |
-
|
| 1320 |
-
|
| 1321 |
-
|
| 1322 |
-
|
| 1323 |
-
|
| 1324 |
-
|
| 1325 |
-
``` cpp
|
| 1326 |
-
auto glambda = [](int i, auto a) { return i; }; // OK: a generic lambda
|
| 1327 |
-
```
|
| 1328 |
-
|
| 1329 |
-
— *end example*]
|
| 1330 |
-
|
| 1331 |
-
The type of a variable declared using `auto` or `decltype(auto)` is
|
| 1332 |
-
deduced from its initializer. This use is allowed in an initializing
|
| 1333 |
-
declaration ([[dcl.init]]) of a variable. `auto` or `decltype(auto)`
|
| 1334 |
-
shall appear as one of the *decl-specifier*s in the *decl-specifier-seq*
|
| 1335 |
-
and the *decl-specifier-seq* shall be followed by one or more
|
| 1336 |
-
*declarator*s, each of which shall be followed by a non-empty
|
| 1337 |
-
*initializer*. In an *initializer* of the form
|
| 1338 |
|
| 1339 |
``` cpp
|
| 1340 |
( expression-list )
|
| 1341 |
```
|
| 1342 |
|
| 1343 |
the *expression-list* shall be a single *assignment-expression*.
|
| 1344 |
|
| 1345 |
-
[*Example
|
| 1346 |
|
| 1347 |
``` cpp
|
| 1348 |
auto x = 5; // OK: x has type int
|
| 1349 |
const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
|
| 1350 |
static auto y = 0.0; // OK: y has type double
|
|
@@ -1354,25 +1467,28 @@ auto g() { return 0.0; } // OK: g returns double
|
|
| 1354 |
auto h(); // OK: h's return type will be deduced when it is defined
|
| 1355 |
```
|
| 1356 |
|
| 1357 |
— *end example*]
|
| 1358 |
|
|
|
|
|
|
|
|
|
|
| 1359 |
A placeholder type can also be used in the *type-specifier-seq* in the
|
| 1360 |
-
*new-type-id* or *type-id* of a *new-expression*
|
| 1361 |
-
|
| 1362 |
-
in a *template-parameter*
|
| 1363 |
|
| 1364 |
-
A program that uses
|
| 1365 |
-
|
| 1366 |
|
| 1367 |
If the *init-declarator-list* contains more than one *init-declarator*,
|
| 1368 |
they shall all form declarations of variables. The type of each declared
|
| 1369 |
-
variable is determined by placeholder type deduction
|
| 1370 |
-
[[dcl.type.auto.deduct]]
|
| 1371 |
type is not the same in each deduction, the program is ill-formed.
|
| 1372 |
|
| 1373 |
-
[*Example
|
| 1374 |
|
| 1375 |
``` cpp
|
| 1376 |
auto x = 5, *y = &x; // OK: auto is int
|
| 1377 |
auto a = 5, b = { 1, 2 }; // error: different types for auto
|
| 1378 |
```
|
|
@@ -1387,53 +1503,60 @@ same in each deduction, the program is ill-formed.
|
|
| 1387 |
If a function with a declared return type that uses a placeholder type
|
| 1388 |
has no non-discarded `return` statements, the return type is deduced as
|
| 1389 |
though from a `return` statement with no operand at the closing brace of
|
| 1390 |
the function body.
|
| 1391 |
|
| 1392 |
-
[*Example
|
| 1393 |
|
| 1394 |
``` cpp
|
| 1395 |
auto f() { } // OK, return type is void
|
| 1396 |
-
auto* g() { } // error
|
| 1397 |
```
|
| 1398 |
|
| 1399 |
— *end example*]
|
| 1400 |
|
| 1401 |
-
|
| 1402 |
-
|
| 1403 |
-
|
| 1404 |
-
the return type deduced from that statement can be used in the rest of
|
| 1405 |
-
the function, including in other `return` statements.
|
| 1406 |
|
| 1407 |
-
[*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1408 |
|
| 1409 |
``` cpp
|
| 1410 |
-
auto n = n; // error
|
| 1411 |
auto f();
|
| 1412 |
-
void g() { &f; } // error
|
| 1413 |
auto sum(int i) {
|
| 1414 |
if (i == 1)
|
| 1415 |
return i; // sum's return type is int
|
| 1416 |
else
|
| 1417 |
return sum(i-1)+i; // OK, sum's return type has been deduced
|
| 1418 |
}
|
| 1419 |
```
|
| 1420 |
|
| 1421 |
— *end example*]
|
| 1422 |
|
| 1423 |
-
Return type deduction for a
|
| 1424 |
-
|
| 1425 |
-
|
| 1426 |
-
operand.
|
| 1427 |
|
| 1428 |
-
[*Note
|
| 1429 |
template will cause an implicit instantiation. Any errors that arise
|
| 1430 |
from this instantiation are not in the immediate context of the function
|
| 1431 |
-
type and can result in the program being ill-formed
|
| 1432 |
-
[[temp.deduct]]
|
| 1433 |
|
| 1434 |
-
[*Example
|
| 1435 |
|
| 1436 |
``` cpp
|
| 1437 |
template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
|
| 1438 |
typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type
|
| 1439 |
template<class T> auto f(T* t) { return *t; }
|
|
@@ -1443,49 +1566,61 @@ void g() { int (*p)(int*) = &f; } // instantiates both fs to deter
|
|
| 1443 |
|
| 1444 |
— *end example*]
|
| 1445 |
|
| 1446 |
Redeclarations or specializations of a function or function template
|
| 1447 |
with a declared return type that uses a placeholder type shall also use
|
| 1448 |
-
that placeholder, not a deduced type.
|
|
|
|
|
|
|
|
|
|
| 1449 |
|
| 1450 |
-
[*Example
|
| 1451 |
|
| 1452 |
``` cpp
|
| 1453 |
auto f();
|
| 1454 |
auto f() { return 42; } // return type is int
|
| 1455 |
auto f(); // OK
|
| 1456 |
-
int f(); // error
|
| 1457 |
-
decltype(auto) f(); // error
|
| 1458 |
|
| 1459 |
template <typename T> auto g(T t) { return t; } // #1
|
| 1460 |
template auto g(int); // OK, return type is int
|
| 1461 |
-
template char g(char); // error
|
| 1462 |
template<> auto g(double); // OK, forward declaration with unknown return type
|
| 1463 |
|
| 1464 |
template <class T> T g(T t) { return t; } // OK, not functionally equivalent to #1
|
| 1465 |
template char g(char); // OK, now there is a matching template
|
| 1466 |
template auto g(float); // still matches #1
|
| 1467 |
|
| 1468 |
-
void h() { return g(42); } // error
|
| 1469 |
|
| 1470 |
template <typename T> struct A {
|
| 1471 |
friend T frf(T);
|
| 1472 |
};
|
| 1473 |
auto frf(int i) { return i; } // not a friend of A<int>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1474 |
```
|
| 1475 |
|
| 1476 |
— *end example*]
|
| 1477 |
|
| 1478 |
A function declared with a return type that uses a placeholder type
|
| 1479 |
-
shall not be `virtual`
|
| 1480 |
|
| 1481 |
-
|
| 1482 |
-
|
| 1483 |
-
but it also does not prevent that entity from being instantiated as
|
| 1484 |
-
needed to determine its type.
|
| 1485 |
|
| 1486 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1487 |
|
| 1488 |
``` cpp
|
| 1489 |
template <typename T> auto f(T t) { return t; }
|
| 1490 |
extern template auto f(int); // does not instantiate f<int>
|
| 1491 |
int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit
|
|
@@ -1498,45 +1633,46 @@ int (*p)(int) = f; // instantiates f<int> to determine its return t
|
|
| 1498 |
|
| 1499 |
*Placeholder type deduction* is the process by which a type containing a
|
| 1500 |
placeholder type is replaced by a deduced type.
|
| 1501 |
|
| 1502 |
A type `T` containing a placeholder type, and a corresponding
|
| 1503 |
-
initializer
|
| 1504 |
|
| 1505 |
- for a non-discarded `return` statement that occurs in a function
|
| 1506 |
declared with a return type that contains a placeholder type, `T` is
|
| 1507 |
-
the declared return type and
|
| 1508 |
-
statement. If the `return` statement has no operand, then
|
| 1509 |
`void()`;
|
| 1510 |
- for a variable declared with a type that contains a placeholder type,
|
| 1511 |
-
`T` is the declared type of the variable and
|
| 1512 |
-
|
| 1513 |
shall be a *braced-init-list* containing only a single
|
| 1514 |
-
*assignment-expression* and
|
| 1515 |
- for a non-type template parameter declared with a type that contains a
|
| 1516 |
placeholder type, `T` is the declared type of the non-type template
|
| 1517 |
-
parameter and
|
| 1518 |
|
| 1519 |
In the case of a `return` statement with no operand or with an operand
|
| 1520 |
-
of type `void`, `T` shall be either
|
|
|
|
| 1521 |
|
| 1522 |
-
If the deduction is for a `return` statement and
|
| 1523 |
-
*braced-init-list*
|
| 1524 |
|
| 1525 |
-
If the placeholder is the
|
| 1526 |
-
replacing `T` is determined using the rules
|
| 1527 |
-
deduction. Obtain `P` from `T` by replacing the
|
| 1528 |
-
|
| 1529 |
-
|
| 1530 |
-
`std::initializer_list<U>`. Deduce a
|
| 1531 |
-
template argument deduction from a
|
| 1532 |
-
[[temp.deduct.call]]
|
| 1533 |
-
and the corresponding argument is
|
| 1534 |
-
declaration is ill-formed. Otherwise, T' is obtained by
|
| 1535 |
-
deduced `U` into `P`.
|
| 1536 |
|
| 1537 |
-
[*Example
|
| 1538 |
|
| 1539 |
``` cpp
|
| 1540 |
auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
|
| 1541 |
auto x2 = { 1, 2.0 }; // error: cannot deduce element type
|
| 1542 |
auto x3{ 1, 2 }; // error: not a single element
|
|
@@ -1544,11 +1680,11 @@ auto x4 = { 3 }; // decltype(x4) is std::initializer_list<int>
|
|
| 1544 |
auto x5{ 3 }; // decltype(x5) is int
|
| 1545 |
```
|
| 1546 |
|
| 1547 |
— *end example*]
|
| 1548 |
|
| 1549 |
-
[*Example
|
| 1550 |
|
| 1551 |
``` cpp
|
| 1552 |
const auto &i = expr;
|
| 1553 |
```
|
| 1554 |
|
|
@@ -1559,16 +1695,16 @@ The type of `i` is the deduced type of the parameter `u` in the call
|
|
| 1559 |
template <class U> void f(const U& u);
|
| 1560 |
```
|
| 1561 |
|
| 1562 |
— *end example*]
|
| 1563 |
|
| 1564 |
-
If the placeholder is the
|
| 1565 |
-
be the placeholder alone. The type deduced
|
| 1566 |
-
described in [[dcl.type.simple]], as though
|
| 1567 |
-
the `decltype`.
|
| 1568 |
|
| 1569 |
-
[*Example
|
| 1570 |
|
| 1571 |
``` cpp
|
| 1572 |
int i;
|
| 1573 |
int&& f();
|
| 1574 |
auto x2a(i); // decltype(x2a) is int
|
|
@@ -1578,36 +1714,56 @@ decltype(auto) x3d = i; // decltype(x3d) is int
|
|
| 1578 |
auto x4a = (i); // decltype(x4a) is int
|
| 1579 |
decltype(auto) x4d = (i); // decltype(x4d) is int&
|
| 1580 |
auto x5a = f(); // decltype(x5a) is int
|
| 1581 |
decltype(auto) x5d = f(); // decltype(x5d) is int&&
|
| 1582 |
auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
|
| 1583 |
-
decltype(auto) x6d = { 1, 2 }; // error
|
| 1584 |
auto *x7a = &i; // decltype(x7a) is int*
|
| 1585 |
-
decltype(auto)*x7d = &i; // error
|
| 1586 |
```
|
| 1587 |
|
| 1588 |
— *end example*]
|
| 1589 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1590 |
#### Deduced class template specialization types <a id="dcl.type.class.deduct">[[dcl.type.class.deduct]]</a>
|
| 1591 |
|
| 1592 |
If a placeholder for a deduced class type appears as a *decl-specifier*
|
| 1593 |
-
in the *decl-specifier-seq* of an initializing declaration
|
| 1594 |
-
|
| 1595 |
-
|
| 1596 |
-
|
| 1597 |
-
|
| 1598 |
-
|
| 1599 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1600 |
|
| 1601 |
A placeholder for a deduced class type can also be used in the
|
| 1602 |
*type-specifier-seq* in the *new-type-id* or *type-id* of a
|
| 1603 |
-
*new-expression*
|
| 1604 |
-
|
| 1605 |
-
|
| 1606 |
-
|
|
|
|
| 1607 |
|
| 1608 |
-
[*Example
|
| 1609 |
|
| 1610 |
``` cpp
|
| 1611 |
template<class T> struct container {
|
| 1612 |
container(T t) {}
|
| 1613 |
template<class Iter> container(Iter beg, Iter end);
|
|
@@ -1616,29 +1772,3607 @@ template<class Iter>
|
|
| 1616 |
container(Iter b, Iter e) -> container<typename std::iterator_traits<Iter>::value_type>;
|
| 1617 |
std::vector<double> v = { ... };
|
| 1618 |
|
| 1619 |
container c(7); // OK, deduces int for T
|
| 1620 |
auto d = container(v.begin(), v.end()); // OK, deduces double for T
|
| 1621 |
-
container e{5, 6}; // error
|
| 1622 |
```
|
| 1623 |
|
| 1624 |
— *end example*]
|
| 1625 |
|
| 1626 |
-
##
|
| 1627 |
|
| 1628 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1629 |
constants. Its name becomes an *enum-name* within its scope.
|
| 1630 |
|
| 1631 |
``` bnf
|
| 1632 |
enum-name:
|
| 1633 |
identifier
|
| 1634 |
```
|
| 1635 |
|
| 1636 |
``` bnf
|
| 1637 |
enum-specifier:
|
| 1638 |
enum-head '{' enumerator-listₒₚₜ '}'
|
| 1639 |
-
enum-head '{' enumerator-list ', }'
|
| 1640 |
```
|
| 1641 |
|
| 1642 |
``` bnf
|
| 1643 |
enum-head:
|
| 1644 |
enum-key attribute-specifier-seqₒₚₜ enum-head-nameₒₚₜ enum-baseₒₚₜ
|
|
@@ -1649,18 +5383,18 @@ enum-head-name:
|
|
| 1649 |
nested-name-specifierₒₚₜ identifier
|
| 1650 |
```
|
| 1651 |
|
| 1652 |
``` bnf
|
| 1653 |
opaque-enum-declaration:
|
| 1654 |
-
enum-key attribute-specifier-seqₒₚₜ
|
| 1655 |
```
|
| 1656 |
|
| 1657 |
``` bnf
|
| 1658 |
enum-key:
|
| 1659 |
-
|
| 1660 |
-
|
| 1661 |
-
|
| 1662 |
```
|
| 1663 |
|
| 1664 |
``` bnf
|
| 1665 |
enum-base:
|
| 1666 |
':' type-specifier-seq
|
|
@@ -1708,19 +5442,20 @@ bit-field of enumeration type.
|
|
| 1708 |
|
| 1709 |
— *end example*]
|
| 1710 |
|
| 1711 |
— *end note*]
|
| 1712 |
|
| 1713 |
-
If
|
| 1714 |
-
declaration shall be an explicit
|
|
|
|
| 1715 |
|
| 1716 |
The enumeration type declared with an *enum-key* of only `enum` is an
|
| 1717 |
*unscoped enumeration*, and its *enumerator*s are *unscoped
|
| 1718 |
enumerators*. The *enum-key*s `enum class` and `enum struct` are
|
| 1719 |
semantically equivalent; an enumeration type declared with one of these
|
| 1720 |
is a *scoped enumeration*, and its *enumerator*s are *scoped
|
| 1721 |
-
enumerators*. The optional *
|
| 1722 |
declaration of a scoped enumeration. The *type-specifier-seq* of an
|
| 1723 |
*enum-base* shall name an integral type; any cv-qualification is
|
| 1724 |
ignored. An *opaque-enum-declaration* declaring an unscoped enumeration
|
| 1725 |
shall not omit the *enum-base*. The identifiers in an *enumerator-list*
|
| 1726 |
are declared as constants, and can appear wherever constants are
|
|
@@ -1748,42 +5483,45 @@ that enumerator.
|
|
| 1748 |
|
| 1749 |
An *opaque-enum-declaration* is either a redeclaration of an enumeration
|
| 1750 |
in the current scope or a declaration of a new enumeration.
|
| 1751 |
|
| 1752 |
[*Note 2*: An enumeration declared by an *opaque-enum-declaration* has
|
| 1753 |
-
fixed underlying type and is a complete type. The list of enumerators
|
| 1754 |
can be provided in a later redeclaration with an
|
| 1755 |
*enum-specifier*. — *end note*]
|
| 1756 |
|
| 1757 |
A scoped enumeration shall not be later redeclared as unscoped or with a
|
| 1758 |
different underlying type. An unscoped enumeration shall not be later
|
| 1759 |
redeclared as scoped and each redeclaration shall include an *enum-base*
|
| 1760 |
specifying the same underlying type as in the original declaration.
|
| 1761 |
|
| 1762 |
-
If
|
| 1763 |
-
*
|
| 1764 |
-
|
| 1765 |
-
|
| 1766 |
-
|
| 1767 |
-
namespace
|
|
|
|
|
|
|
|
|
|
| 1768 |
|
| 1769 |
Each enumeration defines a type that is different from all other types.
|
| 1770 |
Each enumeration also has an *underlying type*. The underlying type can
|
| 1771 |
be explicitly specified using an *enum-base*. For a scoped enumeration
|
| 1772 |
type, the underlying type is `int` if it is not explicitly specified. In
|
| 1773 |
both of these cases, the underlying type is said to be *fixed*.
|
| 1774 |
Following the closing brace of an *enum-specifier*, each enumerator has
|
| 1775 |
the type of its enumeration. If the underlying type is fixed, the type
|
| 1776 |
of each enumerator prior to the closing brace is the underlying type and
|
| 1777 |
the *constant-expression* in the *enumerator-definition* shall be a
|
| 1778 |
-
converted constant expression of the underlying type
|
| 1779 |
-
|
| 1780 |
-
|
| 1781 |
|
| 1782 |
- If an initializer is specified for an enumerator, the
|
| 1783 |
-
*constant-expression* shall be an integral constant expression
|
| 1784 |
-
[[expr.const]]
|
| 1785 |
enumerator has the underlying type of that enumeration type, otherwise
|
| 1786 |
it has the same type as the expression.
|
| 1787 |
- If no initializer is specified for the first enumerator, its type is
|
| 1788 |
an unspecified signed integral type.
|
| 1789 |
- Otherwise the type of the enumerator is the same as that of the
|
|
@@ -1791,12 +5529,12 @@ to the closing brace is determined as follows:
|
|
| 1791 |
in that type, in which case the type is an unspecified integral type
|
| 1792 |
sufficient to contain the incremented value. If no such type exists,
|
| 1793 |
the program is ill-formed.
|
| 1794 |
|
| 1795 |
An enumeration whose underlying type is fixed is an incomplete type from
|
| 1796 |
-
its point of declaration
|
| 1797 |
-
|
| 1798 |
enumeration whose underlying type is not fixed is an incomplete type
|
| 1799 |
from its point of declaration to immediately after the closing `}` of
|
| 1800 |
its *enum-specifier*, at which point it becomes a complete type.
|
| 1801 |
|
| 1802 |
For an enumeration whose underlying type is not fixed, the underlying
|
|
@@ -1808,30 +5546,24 @@ type except that the underlying type shall not be larger than `int`
|
|
| 1808 |
unless the value of an enumerator cannot fit in an `int` or
|
| 1809 |
`unsigned int`. If the *enumerator-list* is empty, the underlying type
|
| 1810 |
is as if the enumeration had a single enumerator with value 0.
|
| 1811 |
|
| 1812 |
For an enumeration whose underlying type is fixed, the values of the
|
| 1813 |
-
enumeration are the values of the underlying type. Otherwise,
|
| 1814 |
-
|
| 1815 |
-
|
| 1816 |
-
|
| 1817 |
-
|
| 1818 |
-
|
| 1819 |
-
|
| 1820 |
-
|
| 1821 |
-
The size of the smallest bit-field large enough to hold all the values
|
| 1822 |
-
of the enumeration type is max(M,1) if bₘin is zero and M+1 otherwise.
|
| 1823 |
-
It is possible to define an enumeration that has values not defined by
|
| 1824 |
-
any of its enumerators. If the *enumerator-list* is empty, the values of
|
| 1825 |
-
the enumeration are as if the enumeration had a single enumerator with
|
| 1826 |
-
value 0.[^4]
|
| 1827 |
|
| 1828 |
Two enumeration types are *layout-compatible enumerations* if they have
|
| 1829 |
the same underlying type.
|
| 1830 |
|
| 1831 |
The value of an enumerator or an object of an unscoped enumeration type
|
| 1832 |
-
is converted to an integer by integral promotion
|
| 1833 |
|
| 1834 |
[*Example 3*:
|
| 1835 |
|
| 1836 |
``` cpp
|
| 1837 |
enum color { red, yellow, green=20, blue };
|
|
@@ -1864,12 +5596,20 @@ if (y) { } // error: no Col to bool conversion
|
|
| 1864 |
|
| 1865 |
— *end example*]
|
| 1866 |
|
| 1867 |
Each *enum-name* and each unscoped *enumerator* is declared in the scope
|
| 1868 |
that immediately contains the *enum-specifier*. Each scoped *enumerator*
|
| 1869 |
-
is declared in the scope of the enumeration.
|
| 1870 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1871 |
|
| 1872 |
[*Example 4*:
|
| 1873 |
|
| 1874 |
``` cpp
|
| 1875 |
enum direction { left='l', right='r' };
|
|
@@ -1913,28 +5653,92 @@ void g(X* p) {
|
|
| 1913 |
}
|
| 1914 |
```
|
| 1915 |
|
| 1916 |
— *end example*]
|
| 1917 |
|
| 1918 |
-
|
| 1919 |
-
|
| 1920 |
-
|
| 1921 |
-
|
| 1922 |
-
|
| 1923 |
-
|
| 1924 |
-
|
| 1925 |
-
|
| 1926 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1927 |
|
| 1928 |
## Namespaces <a id="basic.namespace">[[basic.namespace]]</a>
|
| 1929 |
|
| 1930 |
A namespace is an optionally-named declarative region. The name of a
|
| 1931 |
namespace can be used to access entities declared in that namespace;
|
| 1932 |
that is, the members of the namespace. Unlike other declarative regions,
|
| 1933 |
the definition of a namespace can be split over several parts of one or
|
| 1934 |
more translation units.
|
| 1935 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1936 |
The outermost declarative region of a translation unit is a namespace;
|
| 1937 |
see [[basic.scope.namespace]].
|
| 1938 |
|
| 1939 |
### Namespace definition <a id="namespace.def">[[namespace.def]]</a>
|
| 1940 |
|
|
@@ -1951,46 +5755,46 @@ namespace-definition:
|
|
| 1951 |
nested-namespace-definition
|
| 1952 |
```
|
| 1953 |
|
| 1954 |
``` bnf
|
| 1955 |
named-namespace-definition:
|
| 1956 |
-
|
| 1957 |
```
|
| 1958 |
|
| 1959 |
``` bnf
|
| 1960 |
unnamed-namespace-definition:
|
| 1961 |
-
|
| 1962 |
```
|
| 1963 |
|
| 1964 |
``` bnf
|
| 1965 |
nested-namespace-definition:
|
| 1966 |
-
|
| 1967 |
```
|
| 1968 |
|
| 1969 |
``` bnf
|
| 1970 |
enclosing-namespace-specifier:
|
| 1971 |
identifier
|
| 1972 |
-
enclosing-namespace-specifier '::' identifier
|
| 1973 |
```
|
| 1974 |
|
| 1975 |
``` bnf
|
| 1976 |
namespace-body:
|
| 1977 |
declaration-seqₒₚₜ
|
| 1978 |
```
|
| 1979 |
|
| 1980 |
-
Every *namespace-definition* shall appear
|
| 1981 |
-
|
| 1982 |
|
| 1983 |
In a *named-namespace-definition*, the *identifier* is the name of the
|
| 1984 |
-
namespace. If the *identifier*, when looked up
|
| 1985 |
-
|
| 1986 |
-
|
| 1987 |
-
|
| 1988 |
-
of
|
| 1989 |
-
|
| 1990 |
-
|
| 1991 |
-
|
| 1992 |
|
| 1993 |
Because a *namespace-definition* contains *declaration*s in its
|
| 1994 |
*namespace-body* and a *namespace-definition* is itself a *declaration*,
|
| 1995 |
it follows that *namespace-definition*s can be nested.
|
| 1996 |
|
|
@@ -2044,22 +5848,22 @@ The optional *attribute-specifier-seq* in a *named-namespace-definition*
|
|
| 2044 |
appertains to the namespace being defined or extended.
|
| 2045 |
|
| 2046 |
Members of an inline namespace can be used in most respects as though
|
| 2047 |
they were members of the enclosing namespace. Specifically, the inline
|
| 2048 |
namespace and its enclosing namespace are both added to the set of
|
| 2049 |
-
associated namespaces used in argument-dependent lookup
|
| 2050 |
-
[[basic.lookup.argdep]]
|
| 2051 |
-
|
| 2052 |
-
|
| 2053 |
-
|
| 2054 |
-
|
| 2055 |
-
|
| 2056 |
-
|
| 2057 |
-
|
| 2058 |
-
|
| 2059 |
-
|
| 2060 |
-
|
| 2061 |
|
| 2062 |
These properties are transitive: if a namespace `N` contains an inline
|
| 2063 |
namespace `M`, which in turn contains an inline namespace `O`, then the
|
| 2064 |
members of `O` can be used as though they were members of `M` or `N`.
|
| 2065 |
The *inline namespace set* of `N` is the transitive closure of all
|
|
@@ -2070,26 +5874,29 @@ namespaces.
|
|
| 2070 |
|
| 2071 |
A *nested-namespace-definition* with an *enclosing-namespace-specifier*
|
| 2072 |
`E`, *identifier* `I` and *namespace-body* `B` is equivalent to
|
| 2073 |
|
| 2074 |
``` cpp
|
| 2075 |
-
namespace E { namespace I { B } }
|
| 2076 |
```
|
| 2077 |
|
|
|
|
|
|
|
|
|
|
| 2078 |
[*Example 3*:
|
| 2079 |
|
| 2080 |
``` cpp
|
| 2081 |
-
namespace A::B::C {
|
| 2082 |
int i;
|
| 2083 |
}
|
| 2084 |
```
|
| 2085 |
|
| 2086 |
The above has the same effect as:
|
| 2087 |
|
| 2088 |
``` cpp
|
| 2089 |
namespace A {
|
| 2090 |
-
namespace B {
|
| 2091 |
namespace C {
|
| 2092 |
int i;
|
| 2093 |
}
|
| 2094 |
}
|
| 2095 |
}
|
|
@@ -2100,21 +5907,21 @@ namespace A {
|
|
| 2100 |
#### Unnamed namespaces <a id="namespace.unnamed">[[namespace.unnamed]]</a>
|
| 2101 |
|
| 2102 |
An *unnamed-namespace-definition* behaves as if it were replaced by
|
| 2103 |
|
| 2104 |
``` bnf
|
| 2105 |
-
|
| 2106 |
-
|
| 2107 |
-
|
| 2108 |
```
|
| 2109 |
|
| 2110 |
where `inline` appears if and only if it appears in the
|
| 2111 |
-
*unnamed-namespace-definition* and all occurrences of `unique
|
| 2112 |
translation unit are replaced by the same identifier, and this
|
| 2113 |
identifier differs from all other identifiers in the translation unit.
|
| 2114 |
The optional *attribute-specifier-seq* in the
|
| 2115 |
-
*unnamed-namespace-definition* appertains to `unique
|
| 2116 |
|
| 2117 |
[*Example 1*:
|
| 2118 |
|
| 2119 |
``` cpp
|
| 2120 |
namespace { int i; } // unique::i
|
|
@@ -2139,19 +5946,19 @@ void h() {
|
|
| 2139 |
— *end example*]
|
| 2140 |
|
| 2141 |
#### Namespace member definitions <a id="namespace.memdef">[[namespace.memdef]]</a>
|
| 2142 |
|
| 2143 |
A declaration in a namespace `N` (excluding declarations in nested
|
| 2144 |
-
scopes) whose *declarator-id* is an *unqualified-id*
|
| 2145 |
-
whose *class-head-name*
|
| 2146 |
-
|
| 2147 |
-
|
| 2148 |
-
[[dcl.type.elab]]
|
| 2149 |
-
|
| 2150 |
|
| 2151 |
-
[*Note 1*: An explicit instantiation
|
| 2152 |
-
specialization
|
| 2153 |
name and thus may be declared using an *unqualified-id* in a member of
|
| 2154 |
the enclosing namespace set, if the primary template is declared in an
|
| 2155 |
inline namespace. — *end note*]
|
| 2156 |
|
| 2157 |
[*Example 1*:
|
|
@@ -2169,14 +5976,14 @@ namespace X {
|
|
| 2169 |
```
|
| 2170 |
|
| 2171 |
— *end example*]
|
| 2172 |
|
| 2173 |
Members of a named namespace can also be defined outside that namespace
|
| 2174 |
-
by explicit qualification
|
| 2175 |
-
|
| 2176 |
-
|
| 2177 |
-
|
| 2178 |
|
| 2179 |
[*Example 2*:
|
| 2180 |
|
| 2181 |
``` cpp
|
| 2182 |
namespace Q {
|
|
@@ -2195,32 +6002,32 @@ namespace R {
|
|
| 2195 |
}
|
| 2196 |
```
|
| 2197 |
|
| 2198 |
— *end example*]
|
| 2199 |
|
| 2200 |
-
If a
|
| 2201 |
-
function, class template or function template[^
|
| 2202 |
-
of the innermost enclosing namespace. The
|
| 2203 |
-
by itself make the name visible to unqualified lookup
|
| 2204 |
-
[[basic.lookup.unqual]]
|
| 2205 |
|
| 2206 |
[*Note 2*: The name of the friend will be visible in its namespace if a
|
| 2207 |
matching declaration is provided at namespace scope (either before or
|
| 2208 |
after the class definition granting friendship). — *end note*]
|
| 2209 |
|
| 2210 |
If a friend function or function template is called, its name may be
|
| 2211 |
found by the name lookup that considers functions from namespaces and
|
| 2212 |
-
classes associated with the types of the function arguments
|
| 2213 |
-
[[basic.lookup.argdep]]
|
| 2214 |
-
|
| 2215 |
-
|
| 2216 |
-
|
| 2217 |
-
|
| 2218 |
|
| 2219 |
-
[*Note 3*: The other forms of
|
| 2220 |
-
|
| 2221 |
-
|
| 2222 |
|
| 2223 |
[*Example 3*:
|
| 2224 |
|
| 2225 |
``` cpp
|
| 2226 |
// Assume f and g have not yet been declared.
|
|
@@ -2266,11 +6073,11 @@ namespace-alias:
|
|
| 2266 |
identifier
|
| 2267 |
```
|
| 2268 |
|
| 2269 |
``` bnf
|
| 2270 |
namespace-alias-definition:
|
| 2271 |
-
|
| 2272 |
```
|
| 2273 |
|
| 2274 |
``` bnf
|
| 2275 |
qualified-namespace-specifier:
|
| 2276 |
nested-name-specifierₒₚₜ namespace-name
|
|
@@ -2299,29 +6106,231 @@ namespace CWVLN = Company_with_very_long_name; // OK: duplicate
|
|
| 2299 |
namespace CWVLN = CWVLN;
|
| 2300 |
```
|
| 2301 |
|
| 2302 |
— *end example*]
|
| 2303 |
|
| 2304 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2305 |
|
| 2306 |
``` bnf
|
| 2307 |
using-declaration:
|
| 2308 |
-
|
| 2309 |
```
|
| 2310 |
|
| 2311 |
``` bnf
|
| 2312 |
using-declarator-list:
|
| 2313 |
using-declarator '...'ₒₚₜ
|
| 2314 |
using-declarator-list ',' using-declarator '...'ₒₚₜ
|
| 2315 |
```
|
| 2316 |
|
| 2317 |
``` bnf
|
| 2318 |
using-declarator:
|
| 2319 |
-
|
| 2320 |
```
|
| 2321 |
|
| 2322 |
-
Each *using-declarator* in a *using-declaration* [^
|
| 2323 |
of declarations into the declarative region in which the
|
| 2324 |
*using-declaration* appears. The set of declarations introduced by the
|
| 2325 |
*using-declarator* is found by performing qualified name lookup (
|
| 2326 |
[[basic.lookup.qual]], [[class.member.lookup]]) for the name in the
|
| 2327 |
*using-declarator*, excluding functions that are hidden as described
|
|
@@ -2360,17 +6369,30 @@ struct D : B {
|
|
| 2360 |
```
|
| 2361 |
|
| 2362 |
— *end example*]
|
| 2363 |
|
| 2364 |
In a *using-declaration* used as a *member-declaration*, each
|
| 2365 |
-
*using-declarator*
|
| 2366 |
-
|
| 2367 |
-
its *nested-name-specifier* shall name a direct base class of the class
|
| 2368 |
-
being defined.
|
| 2369 |
|
| 2370 |
[*Example 2*:
|
| 2371 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2372 |
``` cpp
|
| 2373 |
template <typename... bases>
|
| 2374 |
struct X : bases... {
|
| 2375 |
using bases::g...;
|
| 2376 |
};
|
|
@@ -2378,11 +6400,11 @@ struct X : bases... {
|
|
| 2378 |
X<B, D> x; // OK: B::g and D::g introduced
|
| 2379 |
```
|
| 2380 |
|
| 2381 |
— *end example*]
|
| 2382 |
|
| 2383 |
-
[*Example
|
| 2384 |
|
| 2385 |
``` cpp
|
| 2386 |
class C {
|
| 2387 |
int g();
|
| 2388 |
};
|
|
@@ -2399,45 +6421,44 @@ class D2 : public B {
|
|
| 2399 |
|
| 2400 |
[*Note 2*: Since destructors do not have names, a *using-declaration*
|
| 2401 |
cannot refer to a destructor for a base class. Since specializations of
|
| 2402 |
member templates for conversion functions are not found by name lookup,
|
| 2403 |
they are not considered when a *using-declaration* specifies a
|
| 2404 |
-
conversion function
|
| 2405 |
|
| 2406 |
If a constructor or assignment operator brought from a base class into a
|
| 2407 |
derived class has the signature of a copy/move constructor or assignment
|
| 2408 |
-
operator for the derived class ([[class.copy]]
|
| 2409 |
-
*using-declaration* does not by itself
|
| 2410 |
-
|
| 2411 |
-
|
| 2412 |
-
|
|
|
|
| 2413 |
|
| 2414 |
A *using-declaration* shall not name a *template-id*.
|
| 2415 |
|
| 2416 |
-
[*Example
|
| 2417 |
|
| 2418 |
``` cpp
|
| 2419 |
struct A {
|
| 2420 |
template <class T> void f(T);
|
| 2421 |
template <class T> struct X { };
|
| 2422 |
};
|
| 2423 |
struct B : A {
|
| 2424 |
-
using A::f<double>; //
|
| 2425 |
-
using A::X<int>; //
|
| 2426 |
};
|
| 2427 |
```
|
| 2428 |
|
| 2429 |
— *end example*]
|
| 2430 |
|
| 2431 |
A *using-declaration* shall not name a namespace.
|
| 2432 |
|
| 2433 |
-
A *using-declaration*
|
|
|
|
| 2434 |
|
| 2435 |
-
|
| 2436 |
-
*member-declaration*.
|
| 2437 |
-
|
| 2438 |
-
[*Example 5*:
|
| 2439 |
|
| 2440 |
``` cpp
|
| 2441 |
struct X {
|
| 2442 |
int i;
|
| 2443 |
static int s;
|
|
@@ -2450,13 +6471,13 @@ void f() {
|
|
| 2450 |
```
|
| 2451 |
|
| 2452 |
— *end example*]
|
| 2453 |
|
| 2454 |
Members declared by a *using-declaration* can be referred to by explicit
|
| 2455 |
-
qualification just like other member names
|
| 2456 |
|
| 2457 |
-
[*Example
|
| 2458 |
|
| 2459 |
``` cpp
|
| 2460 |
void f();
|
| 2461 |
|
| 2462 |
namespace A {
|
|
@@ -2478,11 +6499,11 @@ void h()
|
|
| 2478 |
— *end example*]
|
| 2479 |
|
| 2480 |
A *using-declaration* is a *declaration* and can therefore be used
|
| 2481 |
repeatedly where (and only where) multiple declarations are allowed.
|
| 2482 |
|
| 2483 |
-
[*Example
|
| 2484 |
|
| 2485 |
``` cpp
|
| 2486 |
namespace A {
|
| 2487 |
int i;
|
| 2488 |
}
|
|
@@ -2505,15 +6526,15 @@ struct X : B {
|
|
| 2505 |
[*Note 3*: For a *using-declaration* whose *nested-name-specifier*
|
| 2506 |
names a namespace, members added to the namespace after the
|
| 2507 |
*using-declaration* are not in the set of introduced declarations, so
|
| 2508 |
they are not considered when a use of the name is made. Thus, additional
|
| 2509 |
overloads added after the *using-declaration* are ignored, but default
|
| 2510 |
-
function arguments
|
| 2511 |
-
[[temp.param]]
|
| 2512 |
[[temp.expl.spec]]) are considered. — *end note*]
|
| 2513 |
|
| 2514 |
-
[*Example
|
| 2515 |
|
| 2516 |
``` cpp
|
| 2517 |
namespace A {
|
| 2518 |
void f(int);
|
| 2519 |
}
|
|
@@ -2538,17 +6559,17 @@ void bar() {
|
|
| 2538 |
[*Note 4*: Partial specializations of class templates are found by
|
| 2539 |
looking up the primary class template and then considering all partial
|
| 2540 |
specializations of that template. If a *using-declaration* names a class
|
| 2541 |
template, partial specializations introduced after the
|
| 2542 |
*using-declaration* are effectively visible because the primary template
|
| 2543 |
-
is visible
|
| 2544 |
|
| 2545 |
Since a *using-declaration* is a declaration, the restrictions on
|
| 2546 |
-
declarations of the same name in the same declarative region
|
| 2547 |
-
[[basic.scope]]
|
| 2548 |
|
| 2549 |
-
[*Example
|
| 2550 |
|
| 2551 |
``` cpp
|
| 2552 |
namespace A {
|
| 2553 |
int x;
|
| 2554 |
}
|
|
@@ -2579,25 +6600,26 @@ void func() {
|
|
| 2579 |
```
|
| 2580 |
|
| 2581 |
— *end example*]
|
| 2582 |
|
| 2583 |
If a function declaration in namespace scope or block scope has the same
|
| 2584 |
-
name and the same parameter-type-list
|
| 2585 |
introduced by a *using-declaration*, and the declarations do not declare
|
| 2586 |
the same function, the program is ill-formed. If a function template
|
| 2587 |
declaration in namespace scope has the same name, parameter-type-list,
|
| 2588 |
-
return type, and template
|
| 2589 |
-
introduced by a *using-declaration*, the program
|
|
|
|
| 2590 |
|
| 2591 |
[*Note 5*:
|
| 2592 |
|
| 2593 |
Two *using-declaration*s may introduce functions with the same name and
|
| 2594 |
the same parameter-type-list. If, for a call to an unqualified function
|
| 2595 |
name, function overload resolution selects the functions introduced by
|
| 2596 |
such *using-declaration*s, the function call is ill-formed.
|
| 2597 |
|
| 2598 |
-
[*Example
|
| 2599 |
|
| 2600 |
``` cpp
|
| 2601 |
namespace B {
|
| 2602 |
void f(int);
|
| 2603 |
void f(double);
|
|
@@ -2622,16 +6644,17 @@ void h() {
|
|
| 2622 |
— *end note*]
|
| 2623 |
|
| 2624 |
When a *using-declarator* brings declarations from a base class into a
|
| 2625 |
derived class, member functions and member function templates in the
|
| 2626 |
derived class override and/or hide member functions and member function
|
| 2627 |
-
templates with the same name, parameter-type-list
|
| 2628 |
-
cv-qualification, and *ref-qualifier* (if
|
| 2629 |
-
than conflicting). Such hidden or
|
| 2630 |
-
from the set of declarations
|
|
|
|
| 2631 |
|
| 2632 |
-
[*Example
|
| 2633 |
|
| 2634 |
``` cpp
|
| 2635 |
struct B {
|
| 2636 |
virtual void f(int);
|
| 2637 |
virtual void f(char);
|
|
@@ -2668,11 +6691,11 @@ struct B2 {
|
|
| 2668 |
|
| 2669 |
struct D1 : B1, B2 {
|
| 2670 |
using B1::B1;
|
| 2671 |
using B2::B2;
|
| 2672 |
};
|
| 2673 |
-
D1 d1(0); //
|
| 2674 |
|
| 2675 |
struct D2 : B1, B2 {
|
| 2676 |
using B1::B1;
|
| 2677 |
using B2::B2;
|
| 2678 |
D2(int); // OK: D2::D2(int) hides B1::B1(int) and B2::B2(int)
|
|
@@ -2680,25 +6703,31 @@ struct D2 : B1, B2 {
|
|
| 2680 |
D2 d2(0); // calls D2::D2(int)
|
| 2681 |
```
|
| 2682 |
|
| 2683 |
— *end example*]
|
| 2684 |
|
| 2685 |
-
For the purpose of
|
| 2686 |
-
|
| 2687 |
-
|
| 2688 |
-
|
| 2689 |
-
|
| 2690 |
-
|
| 2691 |
-
|
| 2692 |
-
|
| 2693 |
-
|
| 2694 |
-
|
| 2695 |
-
|
| 2696 |
-
|
| 2697 |
-
|
| 2698 |
-
|
| 2699 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2700 |
|
| 2701 |
In a *using-declarator* that does not name a constructor, all members of
|
| 2702 |
the set of introduced declarations shall be accessible. In a
|
| 2703 |
*using-declarator* that names a constructor, no access check is
|
| 2704 |
performed. In particular, if a derived class uses a *using-declarator*
|
|
@@ -2707,18 +6736,18 @@ If the name is that of an overloaded member function, then all functions
|
|
| 2707 |
named shall be accessible. The base class members mentioned by a
|
| 2708 |
*using-declarator* shall be visible in the scope of at least one of the
|
| 2709 |
direct base classes of the class where the *using-declarator* is
|
| 2710 |
specified.
|
| 2711 |
|
| 2712 |
-
[*Note
|
| 2713 |
|
| 2714 |
Because a *using-declarator* designates a base class member (and not a
|
| 2715 |
member subobject or a member function of a base class subobject), a
|
| 2716 |
*using-declarator* cannot be used to resolve inherited member
|
| 2717 |
ambiguities.
|
| 2718 |
|
| 2719 |
-
[*Example
|
| 2720 |
|
| 2721 |
``` cpp
|
| 2722 |
struct A { int x(); };
|
| 2723 |
struct B : A { };
|
| 2724 |
struct C : A {
|
|
@@ -2744,11 +6773,11 @@ for a *member-declaration*. A *using-declarator* that names a
|
|
| 2744 |
constructor does not create a synonym; instead, the additional
|
| 2745 |
constructors are accessible if they would be accessible when used to
|
| 2746 |
construct an object of the corresponding base class, and the
|
| 2747 |
accessibility of the *using-declaration* is ignored.
|
| 2748 |
|
| 2749 |
-
[*Example
|
| 2750 |
|
| 2751 |
``` cpp
|
| 2752 |
class A {
|
| 2753 |
private:
|
| 2754 |
void f(char);
|
|
@@ -2766,228 +6795,25 @@ public:
|
|
| 2766 |
```
|
| 2767 |
|
| 2768 |
— *end example*]
|
| 2769 |
|
| 2770 |
If a *using-declarator* uses the keyword `typename` and specifies a
|
| 2771 |
-
dependent name
|
| 2772 |
-
*using-declaration* is treated as a *typedef-name*
|
| 2773 |
-
|
| 2774 |
-
### Using directive <a id="namespace.udir">[[namespace.udir]]</a>
|
| 2775 |
-
|
| 2776 |
-
``` bnf
|
| 2777 |
-
using-directive:
|
| 2778 |
-
attribute-specifier-seqₒₚₜ 'using namespace' nested-name-specifierₒₚₜ namespace-name ';'
|
| 2779 |
-
```
|
| 2780 |
-
|
| 2781 |
-
A *using-directive* shall not appear in class scope, but may appear in
|
| 2782 |
-
namespace scope or in block scope.
|
| 2783 |
-
|
| 2784 |
-
[*Note 1*: When looking up a *namespace-name* in a *using-directive*,
|
| 2785 |
-
only namespace names are considered, see
|
| 2786 |
-
[[basic.lookup.udir]]. — *end note*]
|
| 2787 |
-
|
| 2788 |
-
The optional *attribute-specifier-seq* appertains to the
|
| 2789 |
-
*using-directive*.
|
| 2790 |
-
|
| 2791 |
-
A *using-directive* specifies that the names in the nominated namespace
|
| 2792 |
-
can be used in the scope in which the *using-directive* appears after
|
| 2793 |
-
the *using-directive*. During unqualified name lookup (
|
| 2794 |
-
[[basic.lookup.unqual]]), the names appear as if they were declared in
|
| 2795 |
-
the nearest enclosing namespace which contains both the
|
| 2796 |
-
*using-directive* and the nominated namespace.
|
| 2797 |
-
|
| 2798 |
-
[*Note 2*: In this context, “contains” means “contains directly or
|
| 2799 |
-
indirectly”. — *end note*]
|
| 2800 |
-
|
| 2801 |
-
A *using-directive* does not add any members to the declarative region
|
| 2802 |
-
in which it appears.
|
| 2803 |
-
|
| 2804 |
-
[*Example 1*:
|
| 2805 |
-
|
| 2806 |
-
``` cpp
|
| 2807 |
-
namespace A {
|
| 2808 |
-
int i;
|
| 2809 |
-
namespace B {
|
| 2810 |
-
namespace C {
|
| 2811 |
-
int i;
|
| 2812 |
-
}
|
| 2813 |
-
using namespace A::B::C;
|
| 2814 |
-
void f1() {
|
| 2815 |
-
i = 5; // OK, C::i visible in B and hides A::i
|
| 2816 |
-
}
|
| 2817 |
-
}
|
| 2818 |
-
namespace D {
|
| 2819 |
-
using namespace B;
|
| 2820 |
-
using namespace C;
|
| 2821 |
-
void f2() {
|
| 2822 |
-
i = 5; // ambiguous, B::C::i or A::i?
|
| 2823 |
-
}
|
| 2824 |
-
}
|
| 2825 |
-
void f3() {
|
| 2826 |
-
i = 5; // uses A::i
|
| 2827 |
-
}
|
| 2828 |
-
}
|
| 2829 |
-
void f4() {
|
| 2830 |
-
i = 5; // ill-formed; neither i is visible
|
| 2831 |
-
}
|
| 2832 |
-
```
|
| 2833 |
-
|
| 2834 |
-
— *end example*]
|
| 2835 |
-
|
| 2836 |
-
For unqualified lookup ([[basic.lookup.unqual]]), the *using-directive*
|
| 2837 |
-
is transitive: if a scope contains a *using-directive* that nominates a
|
| 2838 |
-
second namespace that itself contains *using-directive*s, the effect is
|
| 2839 |
-
as if the *using-directive*s from the second namespace also appeared in
|
| 2840 |
-
the first.
|
| 2841 |
-
|
| 2842 |
-
[*Note 3*: For qualified lookup, see
|
| 2843 |
-
[[namespace.qual]]. — *end note*]
|
| 2844 |
-
|
| 2845 |
-
[*Example 2*:
|
| 2846 |
-
|
| 2847 |
-
``` cpp
|
| 2848 |
-
namespace M {
|
| 2849 |
-
int i;
|
| 2850 |
-
}
|
| 2851 |
-
|
| 2852 |
-
namespace N {
|
| 2853 |
-
int i;
|
| 2854 |
-
using namespace M;
|
| 2855 |
-
}
|
| 2856 |
-
|
| 2857 |
-
void f() {
|
| 2858 |
-
using namespace N;
|
| 2859 |
-
i = 7; // error: both M::i and N::i are visible
|
| 2860 |
-
}
|
| 2861 |
-
```
|
| 2862 |
-
|
| 2863 |
-
For another example,
|
| 2864 |
-
|
| 2865 |
-
``` cpp
|
| 2866 |
-
namespace A {
|
| 2867 |
-
int i;
|
| 2868 |
-
}
|
| 2869 |
-
namespace B {
|
| 2870 |
-
int i;
|
| 2871 |
-
int j;
|
| 2872 |
-
namespace C {
|
| 2873 |
-
namespace D {
|
| 2874 |
-
using namespace A;
|
| 2875 |
-
int j;
|
| 2876 |
-
int k;
|
| 2877 |
-
int a = i; // B::i hides A::i
|
| 2878 |
-
}
|
| 2879 |
-
using namespace D;
|
| 2880 |
-
int k = 89; // no problem yet
|
| 2881 |
-
int l = k; // ambiguous: C::k or D::k
|
| 2882 |
-
int m = i; // B::i hides A::i
|
| 2883 |
-
int n = j; // D::j hides B::j
|
| 2884 |
-
}
|
| 2885 |
-
}
|
| 2886 |
-
```
|
| 2887 |
-
|
| 2888 |
-
— *end example*]
|
| 2889 |
-
|
| 2890 |
-
If a namespace is extended ([[namespace.def]]) after a
|
| 2891 |
-
*using-directive* for that namespace is given, the additional members of
|
| 2892 |
-
the extended namespace and the members of namespaces nominated by
|
| 2893 |
-
*using-directive*s in the extending *namespace-definition* can be used
|
| 2894 |
-
after the extending *namespace-definition*.
|
| 2895 |
-
|
| 2896 |
-
If name lookup finds a declaration for a name in two different
|
| 2897 |
-
namespaces, and the declarations do not declare the same entity and do
|
| 2898 |
-
not declare functions, the use of the name is ill-formed.
|
| 2899 |
-
|
| 2900 |
-
[*Note 4*:
|
| 2901 |
-
|
| 2902 |
-
In particular, the name of a variable, function or enumerator does not
|
| 2903 |
-
hide the name of a class or enumeration declared in a different
|
| 2904 |
-
namespace. For example,
|
| 2905 |
-
|
| 2906 |
-
``` cpp
|
| 2907 |
-
namespace A {
|
| 2908 |
-
class X { };
|
| 2909 |
-
extern "C" int g();
|
| 2910 |
-
extern "C++" int h();
|
| 2911 |
-
}
|
| 2912 |
-
namespace B {
|
| 2913 |
-
void X(int);
|
| 2914 |
-
extern "C" int g();
|
| 2915 |
-
extern "C++" int h(int);
|
| 2916 |
-
}
|
| 2917 |
-
using namespace A;
|
| 2918 |
-
using namespace B;
|
| 2919 |
-
|
| 2920 |
-
void f() {
|
| 2921 |
-
X(1); // error: name X found in two namespaces
|
| 2922 |
-
g(); // OK: name g refers to the same entity
|
| 2923 |
-
h(); // OK: overload resolution selects A::h
|
| 2924 |
-
}
|
| 2925 |
-
```
|
| 2926 |
-
|
| 2927 |
-
— *end note*]
|
| 2928 |
-
|
| 2929 |
-
During overload resolution, all functions from the transitive search are
|
| 2930 |
-
considered for argument matching. The set of declarations found by the
|
| 2931 |
-
transitive search is unordered.
|
| 2932 |
-
|
| 2933 |
-
[*Note 5*: In particular, the order in which namespaces were considered
|
| 2934 |
-
and the relationships among the namespaces implied by the
|
| 2935 |
-
*using-directive*s do not cause preference to be given to any of the
|
| 2936 |
-
declarations found by the search. — *end note*]
|
| 2937 |
-
|
| 2938 |
-
An ambiguity exists if the best match finds two functions with the same
|
| 2939 |
-
signature, even if one is in a namespace reachable through
|
| 2940 |
-
*using-directive*s in the namespace of the other.[^7]
|
| 2941 |
-
|
| 2942 |
-
[*Example 3*:
|
| 2943 |
-
|
| 2944 |
-
``` cpp
|
| 2945 |
-
namespace D {
|
| 2946 |
-
int d1;
|
| 2947 |
-
void f(char);
|
| 2948 |
-
}
|
| 2949 |
-
using namespace D;
|
| 2950 |
-
|
| 2951 |
-
int d1; // OK: no conflict with D::d1
|
| 2952 |
-
|
| 2953 |
-
namespace E {
|
| 2954 |
-
int e;
|
| 2955 |
-
void f(int);
|
| 2956 |
-
}
|
| 2957 |
-
|
| 2958 |
-
namespace D { // namespace extension
|
| 2959 |
-
int d2;
|
| 2960 |
-
using namespace E;
|
| 2961 |
-
void f(int);
|
| 2962 |
-
}
|
| 2963 |
-
|
| 2964 |
-
void f() {
|
| 2965 |
-
d1++; // error: ambiguous ::d1 or D::d1?
|
| 2966 |
-
::d1++; // OK
|
| 2967 |
-
D::d1++; // OK
|
| 2968 |
-
d2++; // OK: D::d2
|
| 2969 |
-
e++; // OK: E::e
|
| 2970 |
-
f(1); // error: ambiguous: D::f(int) or E::f(int)?
|
| 2971 |
-
f('a'); // OK: D::f(char)
|
| 2972 |
-
}
|
| 2973 |
-
```
|
| 2974 |
-
|
| 2975 |
-
— *end example*]
|
| 2976 |
|
| 2977 |
## The `asm` declaration <a id="dcl.asm">[[dcl.asm]]</a>
|
| 2978 |
|
| 2979 |
An `asm` declaration has the form
|
| 2980 |
|
| 2981 |
``` bnf
|
| 2982 |
-
asm-
|
| 2983 |
-
attribute-specifier-seqₒₚₜ
|
| 2984 |
```
|
| 2985 |
|
| 2986 |
The `asm` declaration is conditionally-supported; its meaning is
|
| 2987 |
*implementation-defined*. The optional *attribute-specifier-seq* in an
|
| 2988 |
-
*asm-
|
| 2989 |
|
| 2990 |
[*Note 1*: Typically it is used to pass information through the
|
| 2991 |
implementation to an assembler. — *end note*]
|
| 2992 |
|
| 2993 |
## Linkage specifications <a id="dcl.link">[[dcl.link]]</a>
|
|
@@ -3001,27 +6827,27 @@ described here. For example, a particular language linkage may be
|
|
| 3001 |
associated with a particular form of representing names of objects and
|
| 3002 |
functions with external linkage, or with a particular calling
|
| 3003 |
convention, etc. — *end note*]
|
| 3004 |
|
| 3005 |
The default language linkage of all function types, function names, and
|
| 3006 |
-
variable names is C++language linkage. Two function types with
|
| 3007 |
-
language linkages are distinct types even if they are
|
| 3008 |
-
identical.
|
| 3009 |
|
| 3010 |
-
Linkage
|
| 3011 |
achieved using a *linkage-specification*:
|
| 3012 |
|
| 3013 |
``` bnf
|
| 3014 |
linkage-specification:
|
| 3015 |
-
|
| 3016 |
-
|
| 3017 |
```
|
| 3018 |
|
| 3019 |
The *string-literal* indicates the required language linkage. This
|
| 3020 |
-
|
| 3021 |
-
`"C
|
| 3022 |
-
|
| 3023 |
|
| 3024 |
[*Note 2*: Therefore, a linkage-specification with a *string-literal*
|
| 3025 |
that is unknown to the implementation requires a
|
| 3026 |
diagnostic. — *end note*]
|
| 3027 |
|
|
@@ -3029,27 +6855,33 @@ diagnostic. — *end note*]
|
|
| 3029 |
be taken from the document defining that language. For example, `Ada`
|
| 3030 |
(not `ADA`) and `Fortran` or `FORTRAN`, depending on the
|
| 3031 |
vintage. — *end note*]
|
| 3032 |
|
| 3033 |
Every implementation shall provide for linkage to functions written in
|
| 3034 |
-
the C programming language, `"C"`, and linkage to C++functions,
|
|
|
|
| 3035 |
|
| 3036 |
[*Example 1*:
|
| 3037 |
|
| 3038 |
``` cpp
|
| 3039 |
-
complex sqrt(complex); // C++linkage by default
|
| 3040 |
extern "C" {
|
| 3041 |
double sqrt(double); // C linkage
|
| 3042 |
}
|
| 3043 |
```
|
| 3044 |
|
| 3045 |
— *end example*]
|
| 3046 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3047 |
Linkage specifications nest. When linkage specifications nest, the
|
| 3048 |
innermost one determines the language linkage. A linkage specification
|
| 3049 |
does not establish a scope. A *linkage-specification* shall occur only
|
| 3050 |
-
in namespace scope
|
| 3051 |
specified language linkage applies to the function types of all function
|
| 3052 |
declarators, function names with external linkage, and variable names
|
| 3053 |
with external linkage declared within the *linkage-specification*.
|
| 3054 |
|
| 3055 |
[*Example 2*:
|
|
@@ -3057,17 +6889,17 @@ with external linkage declared within the *linkage-specification*.
|
|
| 3057 |
``` cpp
|
| 3058 |
extern "C" // the name f1 and its function type have C language linkage;
|
| 3059 |
void f1(void(*pf)(int)); // pf is a pointer to a C function
|
| 3060 |
|
| 3061 |
extern "C" typedef void FUNC();
|
| 3062 |
-
FUNC f2; // the name f2 has C++language linkage and the
|
| 3063 |
// function's type has C language linkage
|
| 3064 |
|
| 3065 |
extern "C" FUNC f3; // the name of function f3 and the function's type have C language linkage
|
| 3066 |
|
| 3067 |
-
void (*pf2)(FUNC*); // the name of the variable pf2 has C++linkage and the type
|
| 3068 |
-
// of pf2 is ``pointer to C++function that takes one parameter of type
|
| 3069 |
// pointer to C function''
|
| 3070 |
extern "C" {
|
| 3071 |
static void f4(); // the name of the function f4 has internal linkage (not C language linkage)
|
| 3072 |
// and the function's type has C language linkage.
|
| 3073 |
}
|
|
@@ -3097,39 +6929,39 @@ functions.
|
|
| 3097 |
``` cpp
|
| 3098 |
extern "C" typedef void FUNC_c();
|
| 3099 |
|
| 3100 |
class C {
|
| 3101 |
void mf1(FUNC_c*); // the name of the function mf1 and the member function's type have
|
| 3102 |
-
// C++language linkage; the parameter has type ``pointer to C function''
|
| 3103 |
|
| 3104 |
FUNC_c mf2; // the name of the function mf2 and the member function's type have
|
| 3105 |
-
// C++language linkage
|
| 3106 |
|
| 3107 |
-
static FUNC_c* q; // the name of the data member q has C++language linkage and
|
| 3108 |
// the data member's type is ``pointer to C function''
|
| 3109 |
};
|
| 3110 |
|
| 3111 |
extern "C" {
|
| 3112 |
class X {
|
| 3113 |
void mf(); // the name of the function mf and the member function's type have
|
| 3114 |
-
// C++language linkage
|
| 3115 |
-
void mf2(void(*)()); // the name of the function mf2 has C++language linkage;
|
| 3116 |
// the parameter has type ``pointer to C function''
|
| 3117 |
};
|
| 3118 |
}
|
| 3119 |
```
|
| 3120 |
|
| 3121 |
— *end example*]
|
| 3122 |
|
| 3123 |
If two declarations declare functions with the same name and
|
| 3124 |
-
parameter-type-list
|
| 3125 |
-
|
| 3126 |
-
|
| 3127 |
-
|
| 3128 |
-
|
| 3129 |
-
|
| 3130 |
-
|
| 3131 |
function can be declared without a linkage specification after an
|
| 3132 |
explicit linkage specification has been seen; the linkage explicitly
|
| 3133 |
specified in the earlier declaration is not affected by such a function
|
| 3134 |
declaration.
|
| 3135 |
|
|
@@ -3159,28 +6991,28 @@ namespace scope. — *end note*]
|
|
| 3159 |
int x;
|
| 3160 |
namespace A {
|
| 3161 |
extern "C" int f();
|
| 3162 |
extern "C" int g() { return 1; }
|
| 3163 |
extern "C" int h();
|
| 3164 |
-
extern "C" int x(); //
|
| 3165 |
}
|
| 3166 |
|
| 3167 |
namespace B {
|
| 3168 |
extern "C" int f(); // A::f and B::f refer to the same function
|
| 3169 |
-
extern "C" int g() { return 1; } //
|
| 3170 |
}
|
| 3171 |
|
| 3172 |
int A::f() { return 98; } // definition for the function f with C language linkage
|
| 3173 |
extern "C" int h() { return 97; } // definition for the function h with C language linkage
|
| 3174 |
// A::h and ::h refer to the same function
|
| 3175 |
```
|
| 3176 |
|
| 3177 |
— *end example*]
|
| 3178 |
|
| 3179 |
A declaration directly contained in a *linkage-specification* is treated
|
| 3180 |
-
as if it contains the `extern` specifier
|
| 3181 |
-
|
| 3182 |
definition. Such a declaration shall not specify a storage class.
|
| 3183 |
|
| 3184 |
[*Example 5*:
|
| 3185 |
|
| 3186 |
``` cpp
|
|
@@ -3223,17 +7055,17 @@ attribute-specifier:
|
|
| 3223 |
alignment-specifier
|
| 3224 |
```
|
| 3225 |
|
| 3226 |
``` bnf
|
| 3227 |
alignment-specifier:
|
| 3228 |
-
|
| 3229 |
-
|
| 3230 |
```
|
| 3231 |
|
| 3232 |
``` bnf
|
| 3233 |
attribute-using-prefix:
|
| 3234 |
-
|
| 3235 |
```
|
| 3236 |
|
| 3237 |
``` bnf
|
| 3238 |
attribute-list:
|
| 3239 |
attributeₒₚₜ
|
|
@@ -3309,45 +7141,50 @@ contain an *attribute-scoped-token* and every *attribute-token* in that
|
|
| 3309 |
[*Note 2*: For each individual attribute, the form of the
|
| 3310 |
*balanced-token-seq* will be specified. — *end note*]
|
| 3311 |
|
| 3312 |
In an *attribute-list*, an ellipsis may appear only if that
|
| 3313 |
*attribute*’s specification permits it. An *attribute* followed by an
|
| 3314 |
-
ellipsis is a pack expansion
|
| 3315 |
-
|
| 3316 |
-
|
| 3317 |
-
|
| 3318 |
-
|
| 3319 |
-
|
| 3320 |
-
|
| 3321 |
-
|
| 3322 |
-
|
| 3323 |
-
*attribute-argument-clause* (if any).
|
| 3324 |
|
| 3325 |
Each *attribute-specifier-seq* is said to *appertain* to some entity or
|
| 3326 |
-
statement, identified by the syntactic context where it appears (
|
| 3327 |
-
[[stmt.stmt]],
|
| 3328 |
*attribute-specifier-seq* that appertains to some entity or statement
|
| 3329 |
contains an *attribute* or *alignment-specifier* that is not allowed to
|
| 3330 |
apply to that entity or statement, the program is ill-formed. If an
|
| 3331 |
-
*attribute-specifier-seq* appertains to a friend declaration
|
| 3332 |
-
[[class.friend]]
|
| 3333 |
-
|
| 3334 |
-
[
|
|
|
|
| 3335 |
|
| 3336 |
For an *attribute-token* (including an *attribute-scoped-token*) not
|
| 3337 |
-
specified in this
|
| 3338 |
-
|
| 3339 |
-
|
| 3340 |
|
| 3341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3342 |
*attribute-namespace* in an *attribute-scoped-token*. — *end note*]
|
| 3343 |
|
| 3344 |
Two consecutive left square bracket tokens shall appear only when
|
| 3345 |
introducing an *attribute-specifier* or within the *balanced-token-seq*
|
| 3346 |
of an *attribute-argument-clause*.
|
| 3347 |
|
| 3348 |
-
[*Note
|
| 3349 |
*attribute-specifier* is not allowed, the program is ill-formed even if
|
| 3350 |
the brackets match an alternative grammar production. — *end note*]
|
| 3351 |
|
| 3352 |
[*Example 2*:
|
| 3353 |
|
|
@@ -3366,30 +7203,27 @@ void f() {
|
|
| 3366 |
|
| 3367 |
### Alignment specifier <a id="dcl.align">[[dcl.align]]</a>
|
| 3368 |
|
| 3369 |
An *alignment-specifier* may be applied to a variable or to a class data
|
| 3370 |
member, but it shall not be applied to a bit-field, a function
|
| 3371 |
-
parameter, or an *exception-declaration*
|
| 3372 |
-
*alignment-specifier* may also be applied to the declaration
|
| 3373 |
-
|
| 3374 |
-
[[
|
| 3375 |
-
|
| 3376 |
-
*opaque-enum-declaration* or *enum-head*, respectively ([[dcl.enum]])).
|
| 3377 |
-
An *alignment-specifier* with an ellipsis is a pack expansion (
|
| 3378 |
-
[[temp.variadic]]).
|
| 3379 |
|
| 3380 |
When the *alignment-specifier* is of the form `alignas(`
|
| 3381 |
*constant-expression* `)`:
|
| 3382 |
|
| 3383 |
- the *constant-expression* shall be an integral constant expression
|
| 3384 |
-
- if the constant expression does not evaluate to an alignment value
|
| 3385 |
-
[[basic.align]]
|
| 3386 |
implementation does not support that alignment in the context of the
|
| 3387 |
declaration, the program is ill-formed.
|
| 3388 |
|
| 3389 |
An *alignment-specifier* of the form `alignas(` *type-id* `)` has the
|
| 3390 |
-
same effect as `alignas({}alignof(` *type-id* `))`
|
| 3391 |
|
| 3392 |
The alignment requirement of an entity is the strictest nonzero
|
| 3393 |
alignment specified by its *alignment-specifier*s, if any; otherwise,
|
| 3394 |
the *alignment-specifier*s have no effect.
|
| 3395 |
|
|
@@ -3422,11 +7256,11 @@ different *alignment-specifier*s in different translation units.
|
|
| 3422 |
``` cpp
|
| 3423 |
// Translation unit #1:
|
| 3424 |
struct S { int x; } s, *p = &s;
|
| 3425 |
|
| 3426 |
// Translation unit #2:
|
| 3427 |
-
struct alignas(16) S; //
|
| 3428 |
extern S* p;
|
| 3429 |
```
|
| 3430 |
|
| 3431 |
— *end example*]
|
| 3432 |
|
|
@@ -3463,15 +7297,15 @@ The *attribute-token* `carries_dependency` specifies dependency
|
|
| 3463 |
propagation into and out of functions. It shall appear at most once in
|
| 3464 |
each *attribute-list* and no *attribute-argument-clause* shall be
|
| 3465 |
present. The attribute may be applied to the *declarator-id* of a
|
| 3466 |
*parameter-declaration* in a function declaration or lambda, in which
|
| 3467 |
case it specifies that the initialization of the parameter carries a
|
| 3468 |
-
dependency to
|
| 3469 |
-
|
| 3470 |
-
|
| 3471 |
-
|
| 3472 |
-
|
| 3473 |
|
| 3474 |
The first declaration of a function shall specify the
|
| 3475 |
`carries_dependency` attribute for its *declarator-id* if any
|
| 3476 |
declaration of the function specifies the `carries_dependency`
|
| 3477 |
attribute. Furthermore, the first declaration of a function shall
|
|
@@ -3496,11 +7330,11 @@ code. — *end note*]
|
|
| 3496 |
struct foo { int* a; int* b; };
|
| 3497 |
std::atomic<struct foo *> foo_head[10];
|
| 3498 |
int foo_array[10][10];
|
| 3499 |
|
| 3500 |
[[carries_dependency]] struct foo* f(int i) {
|
| 3501 |
-
return foo_head[i].load(
|
| 3502 |
}
|
| 3503 |
|
| 3504 |
int g(int* x, int* y [[carries_dependency]]) {
|
| 3505 |
return kill_dependency(foo_array[*x][*y]);
|
| 3506 |
}
|
|
@@ -3523,17 +7357,15 @@ void h(int i) {
|
|
| 3523 |
|
| 3524 |
The `carries_dependency` attribute on function `f` means that the return
|
| 3525 |
value carries a dependency out of `f`, so that the implementation need
|
| 3526 |
not constrain ordering upon return from `f`. Implementations of `f` and
|
| 3527 |
its caller may choose to preserve dependencies instead of emitting
|
| 3528 |
-
hardware memory ordering instructions (a.k.a.
|
| 3529 |
-
|
| 3530 |
-
|
| 3531 |
-
but its
|
| 3532 |
-
|
| 3533 |
-
implementation might need to insert a fence prior to the second call to
|
| 3534 |
-
`g`.
|
| 3535 |
|
| 3536 |
— *end example*]
|
| 3537 |
|
| 3538 |
### Deprecated attribute <a id="dcl.attr.deprecated">[[dcl.attr.deprecated]]</a>
|
| 3539 |
|
|
@@ -3545,12 +7377,12 @@ entities that are deemed obsolescent or unsafe. — *end note*]
|
|
| 3545 |
|
| 3546 |
It shall appear at most once in each *attribute-list*. An
|
| 3547 |
*attribute-argument-clause* may be present and, if present, it shall
|
| 3548 |
have the form:
|
| 3549 |
|
| 3550 |
-
```
|
| 3551 |
-
( string-literal )
|
| 3552 |
```
|
| 3553 |
|
| 3554 |
[*Note 2*: The *string-literal* in the *attribute-argument-clause*
|
| 3555 |
could be used to explain the rationale for deprecation and/or to suggest
|
| 3556 |
a replacing entity. — *end note*]
|
|
@@ -3569,35 +7401,37 @@ the entity. — *end note*]
|
|
| 3569 |
|
| 3570 |
Redeclarations using different forms of the attribute (with or without
|
| 3571 |
the *attribute-argument-clause* or with different
|
| 3572 |
*attribute-argument-clause*s) are allowed.
|
| 3573 |
|
| 3574 |
-
|
| 3575 |
-
produce a diagnostic message in case the program refers to
|
| 3576 |
-
entity other than to declare it, after a declaration that
|
| 3577 |
-
attribute. The diagnostic message
|
| 3578 |
-
the *attribute-argument-clause* of any `deprecated`
|
| 3579 |
-
the name or entity.
|
| 3580 |
|
| 3581 |
### Fallthrough attribute <a id="dcl.attr.fallthrough">[[dcl.attr.fallthrough]]</a>
|
| 3582 |
|
| 3583 |
-
The *attribute-token* `fallthrough` may be applied to a null statement
|
| 3584 |
-
[[stmt.expr]]
|
| 3585 |
*attribute-token* `fallthrough` shall appear at most once in each
|
| 3586 |
*attribute-list* and no *attribute-argument-clause* shall be present. A
|
| 3587 |
fallthrough statement may only appear within an enclosing `switch`
|
| 3588 |
-
statement
|
| 3589 |
after a fallthrough statement shall be a labeled statement whose label
|
| 3590 |
-
is a case label or default label for the same `switch` statement
|
| 3591 |
-
|
|
|
|
|
|
|
|
|
|
| 3592 |
|
| 3593 |
-
|
| 3594 |
-
warning that an implementation might otherwise issue for a
|
| 3595 |
-
default label that is reachable from another case or default
|
| 3596 |
-
some path of execution. Implementations
|
| 3597 |
-
warning if a fallthrough statement is not dynamically
|
| 3598 |
-
reachable. — *end note*]
|
| 3599 |
|
| 3600 |
[*Example 1*:
|
| 3601 |
|
| 3602 |
``` cpp
|
| 3603 |
void f(int n) {
|
|
@@ -3606,76 +7440,183 @@ void f(int n) {
|
|
| 3606 |
case 1:
|
| 3607 |
case 2:
|
| 3608 |
g();
|
| 3609 |
[[fallthrough]];
|
| 3610 |
case 3: // warning on fallthrough discouraged
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3611 |
h();
|
| 3612 |
case 4: // implementation may warn on fallthrough
|
| 3613 |
i();
|
| 3614 |
-
[[fallthrough]]; //
|
| 3615 |
}
|
| 3616 |
}
|
| 3617 |
```
|
| 3618 |
|
| 3619 |
— *end example*]
|
| 3620 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3621 |
### Maybe unused attribute <a id="dcl.attr.unused">[[dcl.attr.unused]]</a>
|
| 3622 |
|
| 3623 |
The *attribute-token* `maybe_unused` indicates that a name or entity is
|
| 3624 |
possibly intentionally unused. It shall appear at most once in each
|
| 3625 |
*attribute-list* and no *attribute-argument-clause* shall be present.
|
| 3626 |
|
| 3627 |
The attribute may be applied to the declaration of a class, a
|
| 3628 |
-
*typedef-name*, a variable
|
| 3629 |
-
enumeration, or an enumerator.
|
| 3630 |
-
|
| 3631 |
-
[*Note 1*: For an entity marked `maybe_unused`, implementations are
|
| 3632 |
-
encouraged not to emit a warning that the entity is unused, or that the
|
| 3633 |
-
entity is used despite the presence of the attribute. — *end note*]
|
| 3634 |
|
| 3635 |
A name or entity declared without the `maybe_unused` attribute can later
|
| 3636 |
be redeclared with the attribute and vice versa. An entity is considered
|
| 3637 |
marked after the first declaration that marks it.
|
| 3638 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3639 |
[*Example 1*:
|
| 3640 |
|
| 3641 |
``` cpp
|
| 3642 |
[[maybe_unused]] void f([[maybe_unused]] bool thing1,
|
| 3643 |
[[maybe_unused]] bool thing2) {
|
| 3644 |
[[maybe_unused]] bool b = thing1 && thing2;
|
| 3645 |
assert(b);
|
| 3646 |
}
|
| 3647 |
```
|
| 3648 |
|
| 3649 |
-
Implementations
|
| 3650 |
-
|
| 3651 |
|
| 3652 |
— *end example*]
|
| 3653 |
|
| 3654 |
### Nodiscard attribute <a id="dcl.attr.nodiscard">[[dcl.attr.nodiscard]]</a>
|
| 3655 |
|
| 3656 |
The *attribute-token* `nodiscard` may be applied to the *declarator-id*
|
| 3657 |
in a function declaration or to the declaration of a class or
|
| 3658 |
-
enumeration. It shall appear at most once in each *attribute-list*
|
| 3659 |
-
|
| 3660 |
-
|
| 3661 |
-
|
| 3662 |
-
|
| 3663 |
-
|
| 3664 |
-
|
| 3665 |
-
|
| 3666 |
-
|
| 3667 |
-
|
| 3668 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3669 |
|
| 3670 |
[*Example 1*:
|
| 3671 |
|
| 3672 |
``` cpp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3673 |
struct [[nodiscard]] error_info { ... };
|
| 3674 |
error_info enable_missile_safety_mode();
|
| 3675 |
void launch_missiles();
|
| 3676 |
void test_missiles() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3677 |
enable_missile_safety_mode(); // warning encouraged
|
| 3678 |
launch_missiles();
|
| 3679 |
}
|
| 3680 |
error_info &foo();
|
| 3681 |
void f() { foo(); } // warning not encouraged: not a nodiscard call, because neither
|
|
@@ -3702,12 +7643,12 @@ If a function `f` is called where `f` was previously declared with the
|
|
| 3702 |
undefined.
|
| 3703 |
|
| 3704 |
[*Note 1*: The function may terminate by throwing an
|
| 3705 |
exception. — *end note*]
|
| 3706 |
|
| 3707 |
-
|
| 3708 |
-
function marked `[[noreturn]]` might return.
|
| 3709 |
|
| 3710 |
[*Example 1*:
|
| 3711 |
|
| 3712 |
``` cpp
|
| 3713 |
[[ noreturn ]] void f() {
|
|
@@ -3720,5 +7661,310 @@ function marked `[[noreturn]]` might return. — *end note*]
|
|
| 3720 |
}
|
| 3721 |
```
|
| 3722 |
|
| 3723 |
— *end example*]
|
| 3724 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Declarations <a id="dcl.dcl">[[dcl.dcl]]</a>
|
| 2 |
|
| 3 |
+
## Preamble <a id="dcl.pre">[[dcl.pre]]</a>
|
| 4 |
+
|
| 5 |
Declarations generally specify how names are to be interpreted.
|
| 6 |
Declarations have the form
|
| 7 |
|
| 8 |
``` bnf
|
| 9 |
declaration-seq:
|
|
|
|
| 18 |
function-definition
|
| 19 |
template-declaration
|
| 20 |
deduction-guide
|
| 21 |
explicit-instantiation
|
| 22 |
explicit-specialization
|
| 23 |
+
export-declaration
|
| 24 |
linkage-specification
|
| 25 |
namespace-definition
|
| 26 |
empty-declaration
|
| 27 |
attribute-declaration
|
| 28 |
+
module-import-declaration
|
| 29 |
```
|
| 30 |
|
| 31 |
``` bnf
|
| 32 |
block-declaration:
|
| 33 |
simple-declaration
|
| 34 |
+
asm-declaration
|
| 35 |
namespace-alias-definition
|
| 36 |
using-declaration
|
| 37 |
+
using-enum-declaration
|
| 38 |
using-directive
|
| 39 |
static_assert-declaration
|
| 40 |
alias-declaration
|
| 41 |
opaque-enum-declaration
|
| 42 |
```
|
|
|
|
| 46 |
attribute-specifier-seqₒₚₜ declarator ';'
|
| 47 |
```
|
| 48 |
|
| 49 |
``` bnf
|
| 50 |
alias-declaration:
|
| 51 |
+
using identifier attribute-specifier-seqₒₚₜ '=' defining-type-id ';'
|
| 52 |
```
|
| 53 |
|
| 54 |
``` bnf
|
| 55 |
simple-declaration:
|
| 56 |
decl-specifier-seq init-declarator-listₒₚₜ ';'
|
|
|
|
| 58 |
attribute-specifier-seqₒₚₜ decl-specifier-seq ref-qualifierₒₚₜ '[' identifier-list ']' initializer ';'
|
| 59 |
```
|
| 60 |
|
| 61 |
``` bnf
|
| 62 |
static_assert-declaration:
|
| 63 |
+
static_assert '(' constant-expression ')' ';'
|
| 64 |
+
static_assert '(' constant-expression ',' string-literal ')' ';'
|
| 65 |
```
|
| 66 |
|
| 67 |
``` bnf
|
| 68 |
empty-declaration:
|
| 69 |
';'
|
|
|
|
| 72 |
``` bnf
|
| 73 |
attribute-declaration:
|
| 74 |
attribute-specifier-seq ';'
|
| 75 |
```
|
| 76 |
|
| 77 |
+
[*Note 1*: *asm-declaration*s are described in [[dcl.asm]], and
|
| 78 |
+
*linkage-specification*s are described in [[dcl.link]];
|
| 79 |
+
*function-definition*s are described in [[dcl.fct.def]] and
|
| 80 |
+
*template-declaration*s and *deduction-guide*s are described in
|
| 81 |
+
[[temp.deduct.guide]]; *namespace-definition*s are described in
|
| 82 |
+
[[namespace.def]], *using-declaration*s are described in
|
| 83 |
+
[[namespace.udecl]] and *using-directive*s are described in
|
| 84 |
+
[[namespace.udir]]. — *end note*]
|
| 85 |
|
| 86 |
A *simple-declaration* or *nodeclspec-function-declaration* of the form
|
| 87 |
|
| 88 |
``` bnf
|
| 89 |
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ init-declarator-listₒₚₜ ';'
|
| 90 |
```
|
| 91 |
|
| 92 |
is divided into three parts. Attributes are described in [[dcl.attr]].
|
| 93 |
*decl-specifier*s, the principal components of a *decl-specifier-seq*,
|
| 94 |
are described in [[dcl.spec]]. *declarator*s, the components of an
|
| 95 |
+
*init-declarator-list*, are described in [[dcl.decl]]. The
|
| 96 |
*attribute-specifier-seq* appertains to each of the entities declared by
|
| 97 |
the *declarator*s of the *init-declarator-list*.
|
| 98 |
|
| 99 |
[*Note 2*: In the declaration for an entity, attributes appertaining to
|
| 100 |
that entity may appear at the start of the declaration and after the
|
|
|
|
| 109 |
— *end example*]
|
| 110 |
|
| 111 |
Except where otherwise specified, the meaning of an
|
| 112 |
*attribute-declaration* is *implementation-defined*.
|
| 113 |
|
| 114 |
+
A declaration occurs in a scope [[basic.scope]]; the scope rules are
|
| 115 |
summarized in [[basic.lookup]]. A declaration that declares a function
|
| 116 |
or defines a class, namespace, template, or function also has one or
|
| 117 |
more scopes nested within it. These nested scopes, in turn, can have
|
| 118 |
declarations nested within them. Unless otherwise stated, utterances in
|
| 119 |
+
[[dcl.dcl]] about components in, of, or contained by a declaration or
|
| 120 |
+
subcomponent thereof refer only to those components of the declaration
|
| 121 |
+
that are *not* nested within scopes nested within the declaration.
|
|
|
|
| 122 |
|
| 123 |
In a *simple-declaration*, the optional *init-declarator-list* can be
|
| 124 |
+
omitted only when declaring a class [[class]] or enumeration
|
| 125 |
+
[[dcl.enum]], that is, when the *decl-specifier-seq* contains either a
|
| 126 |
+
*class-specifier*, an *elaborated-type-specifier* with a *class-key*
|
| 127 |
+
[[class.name]], or an *enum-specifier*. In these cases and whenever a
|
| 128 |
*class-specifier* or *enum-specifier* is present in the
|
| 129 |
*decl-specifier-seq*, the identifiers in these specifiers are among the
|
| 130 |
names being declared by the declaration (as *class-name*s, *enum-name*s,
|
| 131 |
or *enumerator*s, depending on the syntax). In such cases, the
|
| 132 |
*decl-specifier-seq* shall introduce one or more names into the program,
|
| 133 |
or shall redeclare a name introduced by a previous declaration.
|
| 134 |
|
| 135 |
[*Example 2*:
|
| 136 |
|
| 137 |
``` cpp
|
| 138 |
+
enum { }; // error
|
| 139 |
+
typedef class { }; // error
|
| 140 |
```
|
| 141 |
|
| 142 |
— *end example*]
|
| 143 |
|
| 144 |
In a *static_assert-declaration*, the *constant-expression* shall be a
|
| 145 |
+
contextually converted constant expression of type `bool`
|
| 146 |
+
[[expr.const]]. If the value of the expression when so converted is
|
| 147 |
`true`, the declaration has no effect. Otherwise, the program is
|
| 148 |
+
ill-formed, and the resulting diagnostic message [[intro.compliance]]
|
| 149 |
shall include the text of the *string-literal*, if one is supplied,
|
| 150 |
+
except that characters not in the basic source character set
|
| 151 |
+
[[lex.charset]] are not required to appear in the diagnostic message.
|
| 152 |
|
| 153 |
[*Example 3*:
|
| 154 |
|
| 155 |
``` cpp
|
| 156 |
+
static_assert(sizeof(int) == sizeof(void*), "wrong pointer size");
|
| 157 |
```
|
| 158 |
|
| 159 |
— *end example*]
|
| 160 |
|
| 161 |
An *empty-declaration* has no effect.
|
| 162 |
|
| 163 |
A *simple-declaration* with an *identifier-list* is called a *structured
|
| 164 |
+
binding declaration* [[dcl.struct.bind]]. If the *decl-specifier-seq*
|
| 165 |
+
contains any *decl-specifier* other than `static`, `thread_local`,
|
| 166 |
+
`auto` [[dcl.spec.auto]], or *cv-qualifier*s, the program is ill-formed.
|
| 167 |
+
The *initializer* shall be of the form “`=` *assignment-expression*”, of
|
| 168 |
+
the form “`{` *assignment-expression* `}`”, or of the form “`(`
|
| 169 |
+
*assignment-expression* `)`”, where the *assignment-expression* is of
|
| 170 |
+
array or non-union class type.
|
| 171 |
|
| 172 |
Each *init-declarator* in the *init-declarator-list* contains exactly
|
| 173 |
one *declarator-id*, which is the name declared by that
|
| 174 |
*init-declarator* and hence one of the names declared by the
|
| 175 |
+
declaration. The *defining-type-specifier*s [[dcl.type]] in the
|
| 176 |
*decl-specifier-seq* and the recursive *declarator* structure of the
|
| 177 |
+
*init-declarator* describe a type [[dcl.meaning]], which is then
|
| 178 |
associated with the name being declared by the *init-declarator*.
|
| 179 |
|
| 180 |
If the *decl-specifier-seq* contains the `typedef` specifier, the
|
| 181 |
declaration is called a *typedef declaration* and the name of each
|
| 182 |
*init-declarator* is declared to be a *typedef-name*, synonymous with
|
| 183 |
+
its associated type [[dcl.typedef]]. If the *decl-specifier-seq*
|
| 184 |
contains no `typedef` specifier, the declaration is called a *function
|
| 185 |
+
declaration* if the type associated with the name is a function type
|
| 186 |
+
[[dcl.fct]] and an *object declaration* otherwise.
|
| 187 |
|
| 188 |
Syntactic components beyond those found in the general form of
|
| 189 |
declaration are added to a function declaration to make a
|
| 190 |
*function-definition*. An object declaration, however, is also a
|
| 191 |
definition unless it contains the `extern` specifier and has no
|
| 192 |
+
initializer [[basic.def]]. An object definition causes storage of
|
| 193 |
+
appropriate size and alignment to be reserved and any appropriate
|
| 194 |
+
initialization [[dcl.init]] to be done.
|
| 195 |
|
| 196 |
A *nodeclspec-function-declaration* shall declare a constructor,
|
| 197 |
+
destructor, or conversion function.
|
| 198 |
|
| 199 |
[*Note 3*: A *nodeclspec-function-declaration* can only be used in a
|
| 200 |
+
*template-declaration* [[temp.pre]], *explicit-instantiation*
|
| 201 |
+
[[temp.explicit]], or *explicit-specialization*
|
| 202 |
+
[[temp.expl.spec]]. — *end note*]
|
| 203 |
|
| 204 |
## Specifiers <a id="dcl.spec">[[dcl.spec]]</a>
|
| 205 |
|
| 206 |
The specifiers that can be used in a declaration are
|
| 207 |
|
| 208 |
``` bnf
|
| 209 |
decl-specifier:
|
| 210 |
storage-class-specifier
|
| 211 |
defining-type-specifier
|
| 212 |
function-specifier
|
| 213 |
+
friend
|
| 214 |
+
typedef
|
| 215 |
+
constexpr
|
| 216 |
+
consteval
|
| 217 |
+
constinit
|
| 218 |
+
inline
|
| 219 |
```
|
| 220 |
|
| 221 |
``` bnf
|
| 222 |
decl-specifier-seq:
|
| 223 |
decl-specifier attribute-specifier-seqₒₚₜ
|
| 224 |
decl-specifier decl-specifier-seq
|
| 225 |
```
|
| 226 |
|
| 227 |
The optional *attribute-specifier-seq* in a *decl-specifier-seq*
|
| 228 |
+
appertains to the type determined by the preceding *decl-specifier*s
|
| 229 |
+
[[dcl.meaning]]. The *attribute-specifier-seq* affects the type only for
|
| 230 |
+
the declaration it appears in, not other declarations involving the same
|
| 231 |
+
type.
|
| 232 |
|
| 233 |
Each *decl-specifier* shall appear at most once in a complete
|
| 234 |
+
*decl-specifier-seq*, except that `long` may appear twice. At most one
|
| 235 |
+
of the `constexpr`, `consteval`, and `constinit` keywords shall appear
|
| 236 |
+
in a *decl-specifier-seq*.
|
| 237 |
|
| 238 |
If a *type-name* is encountered while parsing a *decl-specifier-seq*, it
|
| 239 |
is interpreted as part of the *decl-specifier-seq* if and only if there
|
| 240 |
is no previous *defining-type-specifier* other than a *cv-qualifier* in
|
| 241 |
the *decl-specifier-seq*. The sequence shall be self-consistent as
|
|
|
|
| 283 |
|
| 284 |
The storage class specifiers are
|
| 285 |
|
| 286 |
``` bnf
|
| 287 |
storage-class-specifier:
|
| 288 |
+
static
|
| 289 |
+
thread_local
|
| 290 |
+
extern
|
| 291 |
+
mutable
|
| 292 |
```
|
| 293 |
|
| 294 |
At most one *storage-class-specifier* shall appear in a given
|
| 295 |
*decl-specifier-seq*, except that `thread_local` may appear with
|
| 296 |
`static` or `extern`. If `thread_local` appears in any declaration of a
|
| 297 |
variable it shall be present in all declarations of that entity. If a
|
| 298 |
*storage-class-specifier* appears in a *decl-specifier-seq*, there can
|
| 299 |
be no `typedef` specifier in the same *decl-specifier-seq* and the
|
| 300 |
*init-declarator-list* or *member-declarator-list* of the declaration
|
| 301 |
shall not be empty (except for an anonymous union declared in a named
|
| 302 |
+
namespace or in the global namespace, which shall be declared `static`
|
| 303 |
+
[[class.union.anon]]). The *storage-class-specifier* applies to the name
|
| 304 |
+
declared by each *init-declarator* in the list and not to any names
|
| 305 |
+
declared by other specifiers.
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
+
[*Note 1*: See [[temp.expl.spec]] and [[temp.explicit]] for
|
| 308 |
+
restrictions in explicit specializations and explicit instantiations,
|
| 309 |
+
respectively. — *end note*]
|
| 310 |
+
|
| 311 |
+
[*Note 2*: A variable declared without a *storage-class-specifier* at
|
| 312 |
block scope or declared as a function parameter has automatic storage
|
| 313 |
+
duration by default [[basic.stc.auto]]. — *end note*]
|
| 314 |
|
| 315 |
The `thread_local` specifier indicates that the named entity has thread
|
| 316 |
+
storage duration [[basic.stc.thread]]. It shall be applied only to the
|
| 317 |
+
declaration of a variable of namespace or block scope, to a structured
|
| 318 |
+
binding declaration [[dcl.struct.bind]], or to the declaration of a
|
| 319 |
+
static data member. When `thread_local` is applied to a variable of
|
| 320 |
block scope the *storage-class-specifier* `static` is implied if no
|
| 321 |
other *storage-class-specifier* appears in the *decl-specifier-seq*.
|
| 322 |
|
| 323 |
+
The `static` specifier shall be applied only to the declaration of a
|
| 324 |
+
variable or function, to a structured binding declaration
|
| 325 |
+
[[dcl.struct.bind]], or to the declaration of an anonymous union
|
| 326 |
+
[[class.union.anon]]. There can be no `static` function declarations
|
| 327 |
+
within a block, nor any `static` function parameters. A `static`
|
| 328 |
+
specifier used in the declaration of a variable declares the variable to
|
| 329 |
+
have static storage duration [[basic.stc.static]], unless accompanied by
|
| 330 |
+
the `thread_local` specifier, which declares the variable to have thread
|
| 331 |
+
storage duration [[basic.stc.thread]]. A `static` specifier can be used
|
| 332 |
+
in declarations of class members; [[class.static]] describes its
|
| 333 |
+
effect. For the linkage of a name declared with a `static` specifier,
|
| 334 |
+
see [[basic.link]].
|
| 335 |
|
| 336 |
+
The `extern` specifier shall be applied only to the declaration of a
|
| 337 |
+
variable or function. The `extern` specifier shall not be used in the
|
| 338 |
+
declaration of a class member or function parameter. For the linkage of
|
| 339 |
+
a name declared with an `extern` specifier, see [[basic.link]].
|
| 340 |
|
| 341 |
+
[*Note 3*: The `extern` keyword can also be used in
|
| 342 |
*explicit-instantiation*s and *linkage-specification*s, but it is not a
|
| 343 |
*storage-class-specifier* in such contexts. — *end note*]
|
| 344 |
|
| 345 |
The linkages implied by successive declarations for a given entity shall
|
| 346 |
agree. That is, within a given scope, each declaration declaring the
|
| 347 |
same variable name or the same overloading of a function name shall
|
| 348 |
+
imply the same linkage.
|
|
|
|
| 349 |
|
| 350 |
[*Example 1*:
|
| 351 |
|
| 352 |
``` cpp
|
| 353 |
static char* f(); // f() has internal linkage
|
|
|
|
| 404 |
```
|
| 405 |
|
| 406 |
— *end example*]
|
| 407 |
|
| 408 |
The `mutable` specifier shall appear only in the declaration of a
|
| 409 |
+
non-static data member [[class.mem]] whose type is neither
|
| 410 |
const-qualified nor a reference type.
|
| 411 |
|
| 412 |
[*Example 3*:
|
| 413 |
|
| 414 |
``` cpp
|
| 415 |
class X {
|
| 416 |
mutable const int* p; // OK
|
| 417 |
+
mutable int* const q; // error
|
| 418 |
};
|
| 419 |
```
|
| 420 |
|
| 421 |
— *end example*]
|
| 422 |
|
| 423 |
+
[*Note 4*: The `mutable` specifier on a class data member nullifies a
|
| 424 |
+
`const` specifier applied to the containing class object and permits
|
| 425 |
modification of the mutable class member even though the rest of the
|
| 426 |
+
object is const ([[basic.type.qualifier]],
|
| 427 |
+
[[dcl.type.cv]]). — *end note*]
|
| 428 |
|
| 429 |
### Function specifiers <a id="dcl.fct.spec">[[dcl.fct.spec]]</a>
|
| 430 |
|
| 431 |
+
A *function-specifier* can be used only in a function declaration.
|
| 432 |
|
| 433 |
``` bnf
|
| 434 |
function-specifier:
|
| 435 |
+
virtual
|
| 436 |
+
explicit-specifier
|
| 437 |
+
```
|
| 438 |
+
|
| 439 |
+
``` bnf
|
| 440 |
+
explicit-specifier:
|
| 441 |
+
explicit '(' constant-expression ')'
|
| 442 |
+
explicit
|
| 443 |
```
|
| 444 |
|
| 445 |
The `virtual` specifier shall be used only in the initial declaration of
|
| 446 |
a non-static class member function; see [[class.virtual]].
|
| 447 |
|
| 448 |
+
An *explicit-specifier* shall be used only in the declaration of a
|
| 449 |
constructor or conversion function within its class definition; see
|
| 450 |
[[class.conv.ctor]] and [[class.conv.fct]].
|
| 451 |
|
| 452 |
+
In an *explicit-specifier*, the *constant-expression*, if supplied,
|
| 453 |
+
shall be a contextually converted constant expression of type `bool`
|
| 454 |
+
[[expr.const]]. The *explicit-specifier* `explicit` without a
|
| 455 |
+
*constant-expression* is equivalent to the *explicit-specifier*
|
| 456 |
+
`explicit(true)`. If the constant expression evaluates to `true`, the
|
| 457 |
+
function is explicit. Otherwise, the function is not explicit. A `(`
|
| 458 |
+
token that follows `explicit` is parsed as part of the
|
| 459 |
+
*explicit-specifier*.
|
| 460 |
+
|
| 461 |
### The `typedef` specifier <a id="dcl.typedef">[[dcl.typedef]]</a>
|
| 462 |
|
| 463 |
Declarations containing the *decl-specifier* `typedef` declare
|
| 464 |
+
identifiers that can be used later for naming fundamental
|
| 465 |
+
[[basic.fundamental]] or compound [[basic.compound]] types. The
|
| 466 |
`typedef` specifier shall not be combined in a *decl-specifier-seq* with
|
| 467 |
any other kind of specifier except a *defining-type-specifier*, and it
|
| 468 |
shall not be used in the *decl-specifier-seq* of a
|
| 469 |
+
*parameter-declaration* [[dcl.fct]] nor in the *decl-specifier-seq* of a
|
| 470 |
+
*function-definition* [[dcl.fct.def]]. If a `typedef` specifier appears
|
| 471 |
+
in a declaration without a *declarator*, the program is ill-formed.
|
|
|
|
| 472 |
|
| 473 |
``` bnf
|
| 474 |
typedef-name:
|
| 475 |
identifier
|
| 476 |
+
simple-template-id
|
| 477 |
```
|
| 478 |
|
| 479 |
+
A name declared with the `typedef` specifier becomes a *typedef-name*. A
|
| 480 |
+
*typedef-name* names the type associated with the *identifier*
|
| 481 |
+
[[dcl.decl]] or *simple-template-id* [[temp.pre]]; a *typedef-name* is
|
| 482 |
+
thus a synonym for another type. A *typedef-name* does not introduce a
|
| 483 |
+
new type the way a class declaration [[class.name]] or enum declaration
|
| 484 |
+
[[dcl.enum]] does.
|
|
|
|
| 485 |
|
| 486 |
[*Example 1*:
|
| 487 |
|
| 488 |
After
|
| 489 |
|
|
|
|
| 514 |
|
| 515 |
``` cpp
|
| 516 |
using handler_t = void (*)(int);
|
| 517 |
extern handler_t ignore;
|
| 518 |
extern void (*ignore)(int); // redeclare ignore
|
| 519 |
+
using cell = pair<void*, cell*>; // error
|
| 520 |
```
|
| 521 |
|
| 522 |
— *end example*]
|
| 523 |
|
| 524 |
The *defining-type-specifier-seq* of the *defining-type-id* shall not
|
| 525 |
define a class or enumeration if the *alias-declaration* is the
|
| 526 |
*declaration* of a *template-declaration*.
|
| 527 |
|
| 528 |
In a given non-class scope, a `typedef` specifier can be used to
|
| 529 |
+
redeclare the name of any type declared in that scope to refer to the
|
| 530 |
type to which it already refers.
|
| 531 |
|
| 532 |
[*Example 3*:
|
| 533 |
|
| 534 |
``` cpp
|
|
|
|
| 538 |
typedef I I;
|
| 539 |
```
|
| 540 |
|
| 541 |
— *end example*]
|
| 542 |
|
| 543 |
+
In a given class scope, a `typedef` specifier can be used to redeclare
|
| 544 |
any *class-name* declared in that scope that is not also a
|
| 545 |
*typedef-name* to refer to the type to which it already refers.
|
| 546 |
|
| 547 |
[*Example 4*:
|
| 548 |
|
|
|
|
| 554 |
};
|
| 555 |
```
|
| 556 |
|
| 557 |
— *end example*]
|
| 558 |
|
| 559 |
+
If a `typedef` specifier is used to redeclare in a given scope an entity
|
| 560 |
that can be referenced using an *elaborated-type-specifier*, the entity
|
| 561 |
can continue to be referenced by an *elaborated-type-specifier* or as an
|
| 562 |
enumeration or class name in an enumeration or class definition
|
| 563 |
respectively.
|
| 564 |
|
|
|
|
| 573 |
struct S { }; // OK
|
| 574 |
```
|
| 575 |
|
| 576 |
— *end example*]
|
| 577 |
|
| 578 |
+
In a given scope, a `typedef` specifier shall not be used to redeclare
|
| 579 |
the name of any type declared in that scope to refer to a different
|
| 580 |
type.
|
| 581 |
|
| 582 |
[*Example 6*:
|
| 583 |
|
|
|
|
| 599 |
class complex { ... }; // error: redefinition
|
| 600 |
```
|
| 601 |
|
| 602 |
— *end example*]
|
| 603 |
|
| 604 |
+
A *simple-template-id* is only a *typedef-name* if its *template-name*
|
| 605 |
+
names an alias template or a template *template-parameter*.
|
| 606 |
+
|
| 607 |
+
[*Note 1*: A *simple-template-id* that names a class template
|
| 608 |
+
specialization is a *class-name* [[class.name]]. If a *typedef-name* is
|
| 609 |
+
used to identify the subject of an *elaborated-type-specifier*
|
| 610 |
+
[[dcl.type.elab]], a class definition [[class]], a constructor
|
| 611 |
+
declaration [[class.ctor]], or a destructor declaration [[class.dtor]],
|
| 612 |
+
the program is ill-formed. — *end note*]
|
| 613 |
|
| 614 |
[*Example 8*:
|
| 615 |
|
| 616 |
``` cpp
|
| 617 |
struct S {
|
|
|
|
| 625 |
struct T * p; // error
|
| 626 |
```
|
| 627 |
|
| 628 |
— *end example*]
|
| 629 |
|
| 630 |
+
If the typedef declaration defines an unnamed class or enumeration, the
|
| 631 |
+
first *typedef-name* declared by the declaration to be that type is used
|
| 632 |
+
to denote the type for linkage purposes only [[basic.link]].
|
| 633 |
+
|
| 634 |
+
[*Note 2*: A typedef declaration involving a *lambda-expression* does
|
| 635 |
+
not itself define the associated closure type, and so the closure type
|
| 636 |
+
is not given a name for linkage purposes. — *end note*]
|
| 637 |
|
| 638 |
[*Example 9*:
|
| 639 |
|
| 640 |
``` cpp
|
| 641 |
typedef struct { } *ps, S; // S is the class name for linkage purposes
|
| 642 |
+
typedef decltype([]{}) C; // the closure type has no name for linkage purposes
|
| 643 |
+
```
|
| 644 |
+
|
| 645 |
+
— *end example*]
|
| 646 |
+
|
| 647 |
+
An unnamed class with a typedef name for linkage purposes shall not
|
| 648 |
+
|
| 649 |
+
- declare any members other than non-static data members, member
|
| 650 |
+
enumerations, or member classes,
|
| 651 |
+
- have any base classes or default member initializers, or
|
| 652 |
+
- contain a *lambda-expression*,
|
| 653 |
+
|
| 654 |
+
and all member classes shall also satisfy these requirements
|
| 655 |
+
(recursively).
|
| 656 |
+
|
| 657 |
+
[*Example 10*:
|
| 658 |
+
|
| 659 |
+
``` cpp
|
| 660 |
+
typedef struct {
|
| 661 |
+
int f() {}
|
| 662 |
+
} X; // error: struct with typedef name for linkage has member functions
|
| 663 |
```
|
| 664 |
|
| 665 |
— *end example*]
|
| 666 |
|
| 667 |
### The `friend` specifier <a id="dcl.friend">[[dcl.friend]]</a>
|
| 668 |
|
| 669 |
The `friend` specifier is used to specify access to class members; see
|
| 670 |
[[class.friend]].
|
| 671 |
|
| 672 |
+
### The `constexpr` and `consteval` specifiers <a id="dcl.constexpr">[[dcl.constexpr]]</a>
|
| 673 |
|
| 674 |
The `constexpr` specifier shall be applied only to the definition of a
|
| 675 |
variable or variable template or the declaration of a function or
|
| 676 |
+
function template. The `consteval` specifier shall be applied only to
|
| 677 |
+
the declaration of a function or function template. A function or static
|
| 678 |
+
data member declared with the `constexpr` or `consteval` specifier is
|
| 679 |
+
implicitly an inline function or variable [[dcl.inline]]. If any
|
| 680 |
+
declaration of a function or function template has a `constexpr` or
|
| 681 |
+
`consteval` specifier, then all its declarations shall contain the same
|
| 682 |
+
specifier.
|
| 683 |
|
| 684 |
[*Note 1*: An explicit specialization can differ from the template
|
| 685 |
+
declaration with respect to the `constexpr` or `consteval`
|
| 686 |
+
specifier. — *end note*]
|
| 687 |
|
| 688 |
[*Note 2*: Function parameters cannot be declared
|
| 689 |
`constexpr`. — *end note*]
|
| 690 |
|
| 691 |
[*Example 1*:
|
|
|
|
| 700 |
};
|
| 701 |
constexpr pixel::pixel(int a)
|
| 702 |
: x(a), y(x) // OK: definition
|
| 703 |
{ square(x); }
|
| 704 |
constexpr pixel small(2); // error: square not defined, so small(2)
|
| 705 |
+
// not constant[expr.const] so constexpr not satisfied
|
| 706 |
|
| 707 |
constexpr void square(int &x) { // OK: definition
|
| 708 |
x *= x;
|
| 709 |
}
|
| 710 |
constexpr pixel large(4); // OK: square defined
|
|
|
|
| 714 |
extern constexpr int memsz; // error: not a definition
|
| 715 |
```
|
| 716 |
|
| 717 |
— *end example*]
|
| 718 |
|
| 719 |
+
A `constexpr` or `consteval` specifier used in the declaration of a
|
| 720 |
+
function declares that function to be a *constexpr function*. A function
|
| 721 |
+
or constructor declared with the `consteval` specifier is called an
|
| 722 |
+
*immediate function*. A destructor, an allocation function, or a
|
| 723 |
+
deallocation function shall not be declared with the `consteval`
|
| 724 |
+
specifier.
|
| 725 |
|
| 726 |
The definition of a constexpr function shall satisfy the following
|
| 727 |
requirements:
|
| 728 |
|
| 729 |
+
- its return type (if any) shall be a literal type;
|
|
|
|
| 730 |
- each of its parameter types shall be a literal type;
|
| 731 |
+
- it shall not be a coroutine [[dcl.fct.def.coroutine]];
|
| 732 |
+
- if the function is a constructor or destructor, its class shall not
|
| 733 |
+
have any virtual base classes;
|
| 734 |
+
- its *function-body* shall not enclose [[stmt.pre]]
|
| 735 |
- a `goto` statement,
|
| 736 |
+
- an identifier label [[stmt.label]],
|
|
|
|
| 737 |
- a definition of a variable of non-literal type or of static or
|
| 738 |
+
thread storage duration.
|
| 739 |
+
|
| 740 |
+
\[*Note 3*: A *function-body* that is `= delete` or `= default`
|
| 741 |
+
encloses none of the above. — *end note*]
|
| 742 |
|
| 743 |
[*Example 2*:
|
| 744 |
|
| 745 |
``` cpp
|
| 746 |
constexpr int square(int x)
|
|
|
|
| 755 |
constexpr int first(int n) {
|
| 756 |
static int value = n; // error: variable has static storage duration
|
| 757 |
return value;
|
| 758 |
}
|
| 759 |
constexpr int uninit() {
|
| 760 |
+
struct { int a; } s;
|
| 761 |
+
return s.a; // error: uninitialized read of s.a
|
| 762 |
}
|
| 763 |
constexpr int prev(int x)
|
| 764 |
{ return --x; } // OK
|
| 765 |
constexpr int g(int x, int n) { // OK
|
| 766 |
int r = 1;
|
|
|
|
| 769 |
}
|
| 770 |
```
|
| 771 |
|
| 772 |
— *end example*]
|
| 773 |
|
| 774 |
+
The definition of a constexpr constructor whose *function-body* is not
|
| 775 |
+
`= delete` shall additionally satisfy the following requirements:
|
| 776 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 777 |
- for a non-delegating constructor, every constructor selected to
|
| 778 |
initialize non-static data members and base class subobjects shall be
|
| 779 |
a constexpr constructor;
|
| 780 |
- for a delegating constructor, the target constructor shall be a
|
| 781 |
constexpr constructor.
|
|
|
|
| 790 |
};
|
| 791 |
```
|
| 792 |
|
| 793 |
— *end example*]
|
| 794 |
|
| 795 |
+
The definition of a constexpr destructor whose *function-body* is not
|
| 796 |
+
`= delete` shall additionally satisfy the following requirement:
|
| 797 |
+
|
| 798 |
+
- for every subobject of class type or (possibly multi-dimensional)
|
| 799 |
+
array thereof, that class type shall have a constexpr destructor.
|
| 800 |
+
|
| 801 |
For a constexpr function or constexpr constructor that is neither
|
| 802 |
defaulted nor a template, if no argument values exist such that an
|
| 803 |
invocation of the function or constructor could be an evaluated
|
| 804 |
+
subexpression of a core constant expression [[expr.const]], or, for a
|
| 805 |
+
constructor, an evaluated subexpression of the initialization
|
| 806 |
+
full-expression of some constant-initialized object
|
| 807 |
+
[[basic.start.static]], the program is ill-formed, no diagnostic
|
| 808 |
required.
|
| 809 |
|
| 810 |
[*Example 4*:
|
| 811 |
|
| 812 |
``` cpp
|
|
|
|
| 829 |
|
| 830 |
— *end example*]
|
| 831 |
|
| 832 |
If the instantiated template specialization of a constexpr function
|
| 833 |
template or member function of a class template would fail to satisfy
|
| 834 |
+
the requirements for a constexpr function, that specialization is still
|
| 835 |
+
a constexpr function, even though a call to such a function cannot
|
| 836 |
+
appear in a constant expression. If no specialization of the template
|
| 837 |
+
would satisfy the requirements for a constexpr function when considered
|
| 838 |
+
as a non-template function, the template is ill-formed, no diagnostic
|
| 839 |
+
required.
|
|
|
|
| 840 |
|
| 841 |
+
An invocation of a constexpr function in a given context produces the
|
| 842 |
+
same result as an invocation of an equivalent non-constexpr function in
|
| 843 |
+
the same context in all respects except that
|
| 844 |
|
| 845 |
+
- an invocation of a constexpr function can appear in a constant
|
| 846 |
+
expression [[expr.const]] and
|
| 847 |
+
- copy elision is not performed in a constant expression
|
| 848 |
+
[[class.copy.elision]].
|
| 849 |
|
| 850 |
+
[*Note 4*: Declaring a function constexpr can change whether an
|
| 851 |
+
expression is a constant expression. This can indirectly cause calls to
|
| 852 |
+
`std::is_constant_evaluated` within an invocation of the function to
|
| 853 |
+
produce a different value. — *end note*]
|
| 854 |
+
|
| 855 |
+
The `constexpr` and `consteval` specifiers have no effect on the type of
|
| 856 |
+
a constexpr function.
|
| 857 |
|
| 858 |
[*Example 5*:
|
| 859 |
|
| 860 |
``` cpp
|
| 861 |
constexpr int bar(int x, int y) // OK
|
|
|
|
| 866 |
```
|
| 867 |
|
| 868 |
— *end example*]
|
| 869 |
|
| 870 |
A `constexpr` specifier used in an object declaration declares the
|
| 871 |
+
object as const. Such an object shall have literal type and shall be
|
| 872 |
initialized. In any `constexpr` variable declaration, the
|
| 873 |
+
full-expression of the initialization shall be a constant expression
|
| 874 |
+
[[expr.const]]. A `constexpr` variable shall have constant destruction.
|
| 875 |
|
| 876 |
[*Example 6*:
|
| 877 |
|
| 878 |
``` cpp
|
| 879 |
struct pixel {
|
|
|
|
| 883 |
constexpr pixel origin; // error: initializer missing
|
| 884 |
```
|
| 885 |
|
| 886 |
— *end example*]
|
| 887 |
|
| 888 |
+
### The `constinit` specifier <a id="dcl.constinit">[[dcl.constinit]]</a>
|
| 889 |
+
|
| 890 |
+
The `constinit` specifier shall be applied only to a declaration of a
|
| 891 |
+
variable with static or thread storage duration. If the specifier is
|
| 892 |
+
applied to any declaration of a variable, it shall be applied to the
|
| 893 |
+
initializing declaration. No diagnostic is required if no `constinit`
|
| 894 |
+
declaration is reachable at the point of the initializing declaration.
|
| 895 |
+
|
| 896 |
+
If a variable declared with the `constinit` specifier has dynamic
|
| 897 |
+
initialization [[basic.start.dynamic]], the program is ill-formed.
|
| 898 |
+
|
| 899 |
+
[*Note 1*: The `constinit` specifier ensures that the variable is
|
| 900 |
+
initialized during static initialization
|
| 901 |
+
[[basic.start.static]]. — *end note*]
|
| 902 |
+
|
| 903 |
+
[*Example 1*:
|
| 904 |
+
|
| 905 |
+
``` cpp
|
| 906 |
+
const char * g() { return "dynamic initialization"; }
|
| 907 |
+
constexpr const char * f(bool p) { return p ? "constant initializer" : g(); }
|
| 908 |
+
constinit const char * c = f(true); // OK
|
| 909 |
+
constinit const char * d = f(false); // error
|
| 910 |
+
```
|
| 911 |
+
|
| 912 |
+
— *end example*]
|
| 913 |
+
|
| 914 |
### The `inline` specifier <a id="dcl.inline">[[dcl.inline]]</a>
|
| 915 |
|
| 916 |
+
The `inline` specifier shall be applied only to the declaration of a
|
| 917 |
+
variable or function.
|
| 918 |
|
| 919 |
+
A function declaration ([[dcl.fct]], [[class.mfct]], [[class.friend]])
|
| 920 |
with an `inline` specifier declares an *inline function*. The inline
|
| 921 |
specifier indicates to the implementation that inline substitution of
|
| 922 |
the function body at the point of call is to be preferred to the usual
|
| 923 |
function call mechanism. An implementation is not required to perform
|
| 924 |
this inline substitution at the point of call; however, even if this
|
| 925 |
inline substitution is omitted, the other rules for inline functions
|
| 926 |
+
specified in this subclause shall still be respected.
|
| 927 |
|
| 928 |
+
[*Note 1*: The `inline` keyword has no effect on the linkage of a
|
| 929 |
+
function. In certain cases, an inline function cannot use names with
|
| 930 |
+
internal linkage; see [[basic.link]]. — *end note*]
|
| 931 |
|
| 932 |
+
A variable declaration with an `inline` specifier declares an
|
| 933 |
+
*inline variable*.
|
| 934 |
|
| 935 |
+
The `inline` specifier shall not appear on a block scope declaration or
|
| 936 |
+
on the declaration of a function parameter. If the `inline` specifier is
|
| 937 |
+
used in a friend function declaration, that declaration shall be a
|
| 938 |
+
definition or the function shall have previously been declared inline.
|
| 939 |
|
| 940 |
+
If a definition of a function or variable is reachable at the point of
|
| 941 |
+
its first declaration as inline, the program is ill-formed. If a
|
| 942 |
+
function or variable with external or module linkage is declared inline
|
| 943 |
+
in one definition domain, an inline declaration of it shall be reachable
|
| 944 |
+
from the end of every definition domain in which it is declared; no
|
| 945 |
+
diagnostic is required.
|
| 946 |
|
| 947 |
+
[*Note 2*: A call to an inline function or a use of an inline variable
|
| 948 |
+
may be encountered before its definition becomes reachable in a
|
| 949 |
translation unit. — *end note*]
|
| 950 |
|
| 951 |
+
[*Note 3*: An inline function or variable with external or module
|
| 952 |
+
linkage has the same address in all translation units. A `static` local
|
| 953 |
+
variable in an inline function with external or module linkage always
|
| 954 |
+
refers to the same object. A type defined within the body of an inline
|
| 955 |
+
function with external or module linkage is the same type in every
|
| 956 |
+
translation unit. — *end note*]
|
| 957 |
+
|
| 958 |
+
If an inline function or variable that is attached to a named module is
|
| 959 |
+
declared in a definition domain, it shall be defined in that domain.
|
| 960 |
+
|
| 961 |
+
[*Note 4*: A constexpr function [[dcl.constexpr]] is implicitly inline.
|
| 962 |
+
In the global module, a function defined within a class definition is
|
| 963 |
+
implicitly inline ([[class.mfct]], [[class.friend]]). — *end note*]
|
| 964 |
|
| 965 |
### Type specifiers <a id="dcl.type">[[dcl.type]]</a>
|
| 966 |
|
| 967 |
The type-specifiers are
|
| 968 |
|
|
|
|
| 993 |
defining-type-specifier defining-type-specifier-seq
|
| 994 |
```
|
| 995 |
|
| 996 |
The optional *attribute-specifier-seq* in a *type-specifier-seq* or a
|
| 997 |
*defining-type-specifier-seq* appertains to the type denoted by the
|
| 998 |
+
preceding *type-specifier*s or *defining-type-specifier*s
|
| 999 |
+
[[dcl.meaning]]. The *attribute-specifier-seq* affects the type only for
|
| 1000 |
+
the declaration it appears in, not other declarations involving the same
|
| 1001 |
+
type.
|
| 1002 |
|
| 1003 |
As a general rule, at most one *defining-type-specifier* is allowed in
|
| 1004 |
the complete *decl-specifier-seq* of a *declaration* or in a
|
| 1005 |
*defining-type-specifier-seq*, and at most one *type-specifier* is
|
| 1006 |
allowed in a *type-specifier-seq*. The only exceptions to this rule are
|
|
|
|
| 1015 |
- `long` can be combined with `long`.
|
| 1016 |
|
| 1017 |
Except in a declaration of a constructor, destructor, or conversion
|
| 1018 |
function, at least one *defining-type-specifier* that is not a
|
| 1019 |
*cv-qualifier* shall appear in a complete *type-specifier-seq* or a
|
| 1020 |
+
complete *decl-specifier-seq*.[^1]
|
| 1021 |
|
| 1022 |
[*Note 1*: *enum-specifier*s, *class-specifier*s, and
|
| 1023 |
+
*typename-specifier*s are discussed in [[dcl.enum]], [[class]], and
|
| 1024 |
+
[[temp.res]], respectively. The remaining *type-specifier*s are
|
| 1025 |
+
discussed in the rest of this subclause. — *end note*]
|
| 1026 |
|
| 1027 |
#### The *cv-qualifier*s <a id="dcl.type.cv">[[dcl.type.cv]]</a>
|
| 1028 |
|
| 1029 |
There are two *cv-qualifier*s, `const` and `volatile`. Each
|
| 1030 |
*cv-qualifier* shall appear at most once in a *cv-qualifier-seq*. If a
|
|
|
|
| 1038 |
Redundant cv-qualifications are ignored.
|
| 1039 |
|
| 1040 |
[*Note 2*: For example, these could be introduced by
|
| 1041 |
typedefs. — *end note*]
|
| 1042 |
|
| 1043 |
+
[*Note 3*: Declaring a variable `const` can affect its linkage
|
| 1044 |
+
[[dcl.stc]] and its usability in constant expressions [[expr.const]]. As
|
| 1045 |
+
described in [[dcl.init]], the definition of an object or subobject of
|
| 1046 |
+
const-qualified type must specify an initializer or be subject to
|
| 1047 |
+
default-initialization. — *end note*]
|
| 1048 |
|
| 1049 |
A pointer or reference to a cv-qualified type need not actually point or
|
| 1050 |
refer to a cv-qualified object, but it is treated as if it does; a
|
| 1051 |
const-qualified access path cannot be used to modify an object even if
|
| 1052 |
the object referenced is a non-const object and can be modified through
|
| 1053 |
some other access path.
|
| 1054 |
|
| 1055 |
[*Note 4*: Cv-qualifiers are supported by the type system so that they
|
| 1056 |
+
cannot be subverted without casting [[expr.const.cast]]. — *end note*]
|
|
|
|
| 1057 |
|
| 1058 |
+
Any attempt to modify ([[expr.ass]], [[expr.post.incr]],
|
| 1059 |
+
[[expr.pre.incr]]) a const object [[basic.type.qualifier]] during its
|
| 1060 |
+
lifetime [[basic.life]] results in undefined behavior.
|
| 1061 |
|
| 1062 |
[*Example 1*:
|
| 1063 |
|
| 1064 |
``` cpp
|
| 1065 |
const int ci = 3; // cv-qualified (initialized as required)
|
| 1066 |
+
ci = 4; // error: attempt to modify const
|
| 1067 |
|
| 1068 |
int i = 2; // not cv-qualified
|
| 1069 |
const int* cip; // pointer to const int
|
| 1070 |
cip = &i; // OK: cv-qualified access path to unqualified
|
| 1071 |
+
*cip = 4; // error: attempt to modify through ptr to const
|
| 1072 |
|
| 1073 |
int* ip;
|
| 1074 |
ip = const_cast<int*>(cip); // cast needed to convert const int* to int*
|
| 1075 |
*ip = 4; // defined: *ip points to i, a non-const object
|
| 1076 |
|
| 1077 |
const int* ciq = new const int (3); // initialized as required
|
| 1078 |
int* iq = const_cast<int*>(ciq); // cast required
|
| 1079 |
+
*iq = 4; // undefined behavior: modifies a const object
|
| 1080 |
```
|
| 1081 |
|
| 1082 |
For another example,
|
| 1083 |
|
| 1084 |
``` cpp
|
|
|
|
| 1091 |
Y();
|
| 1092 |
};
|
| 1093 |
|
| 1094 |
const Y y;
|
| 1095 |
y.x.i++; // well-formed: mutable member can be modified
|
| 1096 |
+
y.x.j++; // error: const-qualified member modified
|
| 1097 |
Y* p = const_cast<Y*>(&y); // cast away const-ness of y
|
| 1098 |
p->x.i = 99; // well-formed: mutable member can be modified
|
| 1099 |
+
p->x.j = 99; // undefined behavior: modifies a const subobject
|
| 1100 |
```
|
| 1101 |
|
| 1102 |
— *end example*]
|
| 1103 |
|
| 1104 |
The semantics of an access through a volatile glvalue are
|
|
|
|
| 1120 |
The simple type specifiers are
|
| 1121 |
|
| 1122 |
``` bnf
|
| 1123 |
simple-type-specifier:
|
| 1124 |
nested-name-specifierₒₚₜ type-name
|
| 1125 |
+
nested-name-specifier template simple-template-id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1126 |
decltype-specifier
|
| 1127 |
+
placeholder-type-specifier
|
| 1128 |
+
nested-name-specifierₒₚₜ template-name
|
| 1129 |
+
char
|
| 1130 |
+
char8_t
|
| 1131 |
+
char16_t
|
| 1132 |
+
char32_t
|
| 1133 |
+
wchar_t
|
| 1134 |
+
bool
|
| 1135 |
+
short
|
| 1136 |
+
int
|
| 1137 |
+
long
|
| 1138 |
+
signed
|
| 1139 |
+
unsigned
|
| 1140 |
+
float
|
| 1141 |
+
double
|
| 1142 |
+
void
|
| 1143 |
```
|
| 1144 |
|
| 1145 |
``` bnf
|
| 1146 |
type-name:
|
| 1147 |
class-name
|
| 1148 |
enum-name
|
| 1149 |
typedef-name
|
|
|
|
| 1150 |
```
|
| 1151 |
|
| 1152 |
+
A *placeholder-type-specifier* is a placeholder for a type to be deduced
|
| 1153 |
+
[[dcl.spec.auto]]. A *type-specifier* of the form `typename`ₒₚₜ
|
| 1154 |
+
*nested-name-specifier*ₒₚₜ *template-name* is a placeholder for a
|
| 1155 |
+
deduced class type [[dcl.type.class.deduct]]. The
|
| 1156 |
+
*nested-name-specifier*, if any, shall be non-dependent and the
|
| 1157 |
+
*template-name* shall name a deducible template. A *deducible template*
|
| 1158 |
+
is either a class template or is an alias template whose
|
| 1159 |
+
*defining-type-id* is of the form
|
| 1160 |
+
|
| 1161 |
``` bnf
|
| 1162 |
+
typenameₒₚₜ nested-name-specifierₒₚₜ templateₒₚₜ simple-template-id
|
|
|
|
|
|
|
| 1163 |
```
|
| 1164 |
|
| 1165 |
+
where the *nested-name-specifier* (if any) is non-dependent and the
|
| 1166 |
+
*template-name* of the *simple-template-id* names a deducible template.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1167 |
|
| 1168 |
+
[*Note 1*: An injected-class-name is never interpreted as a
|
| 1169 |
+
*template-name* in contexts where class template argument deduction
|
| 1170 |
+
would be performed [[temp.local]]. — *end note*]
|
| 1171 |
+
|
| 1172 |
+
The other *simple-type-specifier*s specify either a previously-declared
|
| 1173 |
+
type, a type determined from an expression, or one of the fundamental
|
| 1174 |
+
types [[basic.fundamental]]. [[dcl.type.simple]] summarizes the valid
|
| 1175 |
+
combinations of *simple-type-specifier*s and the types they specify.
|
| 1176 |
+
|
| 1177 |
+
**Table: *simple-type-specifier*{s} and the types they specify** <a id="dcl.type.simple">[dcl.type.simple]</a>
|
| 1178 |
|
| 1179 |
| Specifier(s) | Type |
|
| 1180 |
+
| ---------------------------- | ------------------------------------------------- |
|
| 1181 |
| *type-name* | the type named |
|
| 1182 |
| *simple-template-id* | the type as defined in~ [[temp.names]] |
|
| 1183 |
+
| *decltype-specifier* | the type as defined in~ [[dcl.type.decltype]] |
|
| 1184 |
+
| *placeholder-type-specifier* | the type as defined in~ [[dcl.spec.auto]] |
|
| 1185 |
+
| *template-name* | the type as defined in~ [[dcl.type.class.deduct]] |
|
| 1186 |
+
| `char` | ```char`'' |
|
| 1187 |
+
| `unsigned char` | ```unsigned char`'' |
|
| 1188 |
+
| `signed char` | ```signed char`'' |
|
| 1189 |
+
| `char8_t` | ```char8_t`'' |
|
| 1190 |
+
| `char16_t` | ```char16_t`'' |
|
| 1191 |
+
| `char32_t` | ```char32_t`'' |
|
| 1192 |
+
| `bool` | ```bool`'' |
|
| 1193 |
+
| `unsigned` | ```unsigned int`'' |
|
| 1194 |
+
| `unsigned int` | ```unsigned int`'' |
|
| 1195 |
+
| `signed` | ```int`'' |
|
| 1196 |
+
| `signed int` | ```int`'' |
|
| 1197 |
+
| `int` | ```int`'' |
|
| 1198 |
+
| `unsigned short int` | ```unsigned short int`'' |
|
| 1199 |
+
| `unsigned short` | ```unsigned short int`'' |
|
| 1200 |
+
| `unsigned long int` | ```unsigned long int`'' |
|
| 1201 |
+
| `unsigned long` | ```unsigned long int`'' |
|
| 1202 |
+
| `unsigned long long int` | ```unsigned long long int`'' |
|
| 1203 |
+
| `unsigned long long` | ```unsigned long long int`'' |
|
| 1204 |
+
| `signed long int` | ```long int`'' |
|
| 1205 |
+
| `signed long` | ```long int`'' |
|
| 1206 |
+
| `signed long long int` | ```long long int`'' |
|
| 1207 |
+
| `signed long long` | ```long long int`'' |
|
| 1208 |
+
| `long long int` | ```long long int`'' |
|
| 1209 |
+
| `long long` | ```long long int`'' |
|
| 1210 |
+
| `long int` | ```long int`'' |
|
| 1211 |
+
| `long` | ```long int`'' |
|
| 1212 |
+
| `signed short int` | ```short int`'' |
|
| 1213 |
+
| `signed short` | ```short int`'' |
|
| 1214 |
+
| `short int` | ```short int`'' |
|
| 1215 |
+
| `short` | ```short int`'' |
|
| 1216 |
+
| `wchar_t` | ```wchar_t`'' |
|
| 1217 |
+
| `float` | ```float`'' |
|
| 1218 |
+
| `double` | ```double`'' |
|
| 1219 |
+
| `long double` | ```long double`'' |
|
| 1220 |
+
| `void` | ```void`'' |
|
| 1221 |
|
| 1222 |
|
| 1223 |
When multiple *simple-type-specifier*s are allowed, they can be freely
|
| 1224 |
intermixed with other *decl-specifier*s in any order.
|
| 1225 |
|
| 1226 |
+
[*Note 2*: It is *implementation-defined* whether objects of `char`
|
| 1227 |
type are represented as signed or unsigned quantities. The `signed`
|
| 1228 |
specifier forces `char` objects to be signed; it is redundant in other
|
| 1229 |
contexts. — *end note*]
|
| 1230 |
|
| 1231 |
+
#### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
|
| 1232 |
+
|
| 1233 |
+
``` bnf
|
| 1234 |
+
elaborated-type-specifier:
|
| 1235 |
+
class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
|
| 1236 |
+
class-key simple-template-id
|
| 1237 |
+
class-key nested-name-specifier templateₒₚₜ simple-template-id
|
| 1238 |
+
elaborated-enum-specifier
|
| 1239 |
+
```
|
| 1240 |
+
|
| 1241 |
+
``` bnf
|
| 1242 |
+
elaborated-enum-specifier:
|
| 1243 |
+
enum nested-name-specifierₒₚₜ identifier
|
| 1244 |
+
```
|
| 1245 |
+
|
| 1246 |
+
An *attribute-specifier-seq* shall not appear in an
|
| 1247 |
+
*elaborated-type-specifier* unless the latter is the sole constituent of
|
| 1248 |
+
a declaration. If an *elaborated-type-specifier* is the sole constituent
|
| 1249 |
+
of a declaration, the declaration is ill-formed unless it is an explicit
|
| 1250 |
+
specialization [[temp.expl.spec]], an explicit instantiation
|
| 1251 |
+
[[temp.explicit]] or it has one of the following forms:
|
| 1252 |
+
|
| 1253 |
+
``` bnf
|
| 1254 |
+
class-key attribute-specifier-seqₒₚₜ identifier ';'
|
| 1255 |
+
friend class-key '::ₒₚₜ ' identifier ';'
|
| 1256 |
+
friend class-key '::ₒₚₜ ' simple-template-id ';'
|
| 1257 |
+
friend class-key nested-name-specifier identifier ';'
|
| 1258 |
+
friend class-key nested-name-specifier templateₒₚₜ simple-template-id ';'
|
| 1259 |
+
```
|
| 1260 |
+
|
| 1261 |
+
In the first case, the *attribute-specifier-seq*, if any, appertains to
|
| 1262 |
+
the class being declared; the attributes in the
|
| 1263 |
+
*attribute-specifier-seq* are thereafter considered attributes of the
|
| 1264 |
+
class whenever it is named.
|
| 1265 |
+
|
| 1266 |
+
[*Note 1*: [[basic.lookup.elab]] describes how name lookup proceeds
|
| 1267 |
+
for the *identifier* in an *elaborated-type-specifier*. — *end note*]
|
| 1268 |
+
|
| 1269 |
+
If the *identifier* or *simple-template-id* resolves to a *class-name*
|
| 1270 |
+
or *enum-name*, the *elaborated-type-specifier* introduces it into the
|
| 1271 |
+
declaration the same way a *simple-type-specifier* introduces its
|
| 1272 |
+
*type-name* [[dcl.type.simple]]. If the *identifier* or
|
| 1273 |
+
*simple-template-id* resolves to a *typedef-name* ([[dcl.typedef]],
|
| 1274 |
+
[[temp.names]]), the *elaborated-type-specifier* is ill-formed.
|
| 1275 |
+
|
| 1276 |
+
[*Note 2*:
|
| 1277 |
+
|
| 1278 |
+
This implies that, within a class template with a template
|
| 1279 |
+
*type-parameter* `T`, the declaration
|
| 1280 |
+
|
| 1281 |
+
``` cpp
|
| 1282 |
+
friend class T;
|
| 1283 |
+
```
|
| 1284 |
+
|
| 1285 |
+
is ill-formed. However, the similar declaration `friend T;` is allowed
|
| 1286 |
+
[[class.friend]].
|
| 1287 |
+
|
| 1288 |
+
— *end note*]
|
| 1289 |
+
|
| 1290 |
+
The *class-key* or `enum` keyword present in the
|
| 1291 |
+
*elaborated-type-specifier* shall agree in kind with the declaration to
|
| 1292 |
+
which the name in the *elaborated-type-specifier* refers. This rule also
|
| 1293 |
+
applies to the form of *elaborated-type-specifier* that declares a
|
| 1294 |
+
*class-name* or friend class since it can be construed as referring to
|
| 1295 |
+
the definition of the class. Thus, in any *elaborated-type-specifier*,
|
| 1296 |
+
the `enum` keyword shall be used to refer to an enumeration
|
| 1297 |
+
[[dcl.enum]], the `union` *class-key* shall be used to refer to a union
|
| 1298 |
+
[[class.union]], and either the `class` or `struct` *class-key* shall be
|
| 1299 |
+
used to refer to a non-union class [[class.pre]].
|
| 1300 |
+
|
| 1301 |
+
[*Example 1*:
|
| 1302 |
+
|
| 1303 |
+
``` cpp
|
| 1304 |
+
enum class E { a, b };
|
| 1305 |
+
enum E x = E::a; // OK
|
| 1306 |
+
struct S { } s;
|
| 1307 |
+
class S* p = &s; // OK
|
| 1308 |
+
```
|
| 1309 |
+
|
| 1310 |
+
— *end example*]
|
| 1311 |
+
|
| 1312 |
+
#### Decltype specifiers <a id="dcl.type.decltype">[[dcl.type.decltype]]</a>
|
| 1313 |
+
|
| 1314 |
+
``` bnf
|
| 1315 |
+
decltype-specifier:
|
| 1316 |
+
decltype '(' expression ')'
|
| 1317 |
+
```
|
| 1318 |
+
|
| 1319 |
+
For an expression E, the type denoted by `decltype(E)` is defined as
|
| 1320 |
follows:
|
| 1321 |
|
| 1322 |
+
- if E is an unparenthesized *id-expression* naming a structured binding
|
| 1323 |
+
[[dcl.struct.bind]], `decltype(E)` is the referenced type as given in
|
| 1324 |
+
the specification of the structured binding declaration;
|
| 1325 |
+
- otherwise, if E is an unparenthesized *id-expression* naming a
|
| 1326 |
+
non-type *template-parameter* [[temp.param]], `decltype(E)` is the
|
| 1327 |
+
type of the *template-parameter* after performing any necessary type
|
| 1328 |
+
deduction ([[dcl.spec.auto]], [[dcl.type.class.deduct]]);
|
| 1329 |
+
- otherwise, if E is an unparenthesized *id-expression* or an
|
| 1330 |
+
unparenthesized class member access [[expr.ref]], `decltype(E)` is the
|
| 1331 |
+
type of the entity named by E. If there is no such entity, or if E
|
| 1332 |
+
names a set of overloaded functions, the program is ill-formed;
|
| 1333 |
+
- otherwise, if E is an xvalue, `decltype(E)` is `T&&`, where `T` is the
|
| 1334 |
+
type of E;
|
| 1335 |
+
- otherwise, if E is an lvalue, `decltype(E)` is `T&`, where `T` is the
|
| 1336 |
+
type of E;
|
| 1337 |
+
- otherwise, `decltype(E)` is the type of E.
|
| 1338 |
|
| 1339 |
The operand of the `decltype` specifier is an unevaluated operand
|
| 1340 |
+
[[expr.prop]].
|
| 1341 |
|
| 1342 |
[*Example 1*:
|
| 1343 |
|
| 1344 |
``` cpp
|
| 1345 |
const int&& foo();
|
|
|
|
| 1352 |
decltype((a->x)) x4 = x3; // type is const double&
|
| 1353 |
```
|
| 1354 |
|
| 1355 |
— *end example*]
|
| 1356 |
|
| 1357 |
+
[*Note 1*: The rules for determining types involving `decltype(auto)`
|
| 1358 |
are specified in [[dcl.spec.auto]]. — *end note*]
|
| 1359 |
|
| 1360 |
+
If the operand of a *decltype-specifier* is a prvalue and is not a
|
| 1361 |
+
(possibly parenthesized) immediate invocation [[expr.const]], the
|
| 1362 |
+
temporary materialization conversion is not applied [[conv.rval]] and no
|
| 1363 |
+
result object is provided for the prvalue. The type of the prvalue may
|
| 1364 |
+
be incomplete or an abstract class type.
|
| 1365 |
|
| 1366 |
+
[*Note 2*: As a result, storage is not allocated for the prvalue and it
|
| 1367 |
is not destroyed. Thus, a class type is not instantiated as a result of
|
| 1368 |
being the type of a function call in this context. In this context, the
|
| 1369 |
common purpose of writing the expression is merely to refer to its type.
|
| 1370 |
In that sense, a *decltype-specifier* is analogous to a use of a
|
| 1371 |
*typedef-name*, so the usual reasons for requiring a complete type do
|
| 1372 |
not apply. In particular, it is not necessary to allocate storage for a
|
| 1373 |
temporary object or to enforce the semantic constraints associated with
|
| 1374 |
invoking the type’s destructor. — *end note*]
|
| 1375 |
|
| 1376 |
+
[*Note 3*: Unlike the preceding rule, parentheses have no special
|
| 1377 |
meaning in this context. — *end note*]
|
| 1378 |
|
| 1379 |
[*Example 2*:
|
| 1380 |
|
| 1381 |
``` cpp
|
|
|
|
| 1390 |
// (A temporary is not introduced as a result of the use of i().)
|
| 1391 |
template<class T> auto f(T) // #2
|
| 1392 |
-> void;
|
| 1393 |
auto g() -> void {
|
| 1394 |
f(42); // OK: calls #2. (#1 is not a viable candidate: type deduction
|
| 1395 |
+
// fails[temp.deduct] because A<int>::~A() is implicitly used in its
|
| 1396 |
// decltype-specifier)
|
| 1397 |
}
|
| 1398 |
template<class T> auto q(T)
|
| 1399 |
-> decltype((h<T>())); // does not force completion of A<T>; A<T>::~A() is not implicitly
|
| 1400 |
// used within the context of this decltype-specifier
|
| 1401 |
void r() {
|
| 1402 |
+
q(42); // error: deduction against q succeeds, so overload resolution selects
|
| 1403 |
+
// the specialization ``q(T) -> decltype((h<T>()))'' with T=int;
|
| 1404 |
+
// the return type is A<int>, so a temporary is introduced and its
|
| 1405 |
+
// destructor is used, so the program is ill-formed
|
| 1406 |
}
|
| 1407 |
```
|
| 1408 |
|
| 1409 |
— *end example*]
|
| 1410 |
|
| 1411 |
+
#### Placeholder type specifiers <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
|
| 1412 |
|
| 1413 |
``` bnf
|
| 1414 |
+
placeholder-type-specifier:
|
| 1415 |
+
type-constraintₒₚₜ auto
|
| 1416 |
+
type-constraintₒₚₜ decltype '(' auto ')'
|
|
|
|
|
|
|
| 1417 |
```
|
| 1418 |
|
| 1419 |
+
A *placeholder-type-specifier* designates a placeholder type that will
|
| 1420 |
+
be replaced later by deduction from an initializer.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1421 |
|
| 1422 |
+
A *placeholder-type-specifier* of the form *type-constraint*ₒₚₜ `auto`
|
| 1423 |
+
can be used as a *decl-specifier* of the *decl-specifier-seq* of a
|
| 1424 |
+
*parameter-declaration* of a function declaration or *lambda-expression*
|
| 1425 |
+
and, if it is not the `auto` *type-specifier* introducing a
|
| 1426 |
+
*trailing-return-type* (see below), is a *generic parameter type
|
| 1427 |
+
placeholder* of the function declaration or *lambda-expression*.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1428 |
|
| 1429 |
+
[*Note 1*: Having a generic parameter type placeholder signifies that
|
| 1430 |
+
the function is an abbreviated function template [[dcl.fct]] or the
|
| 1431 |
+
lambda is a generic lambda [[expr.prim.lambda]]. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1432 |
|
| 1433 |
The placeholder type can appear with a function declarator in the
|
| 1434 |
*decl-specifier-seq*, *type-specifier-seq*, *conversion-function-id*, or
|
| 1435 |
*trailing-return-type*, in any context where such a declarator is valid.
|
| 1436 |
+
If the function declarator includes a *trailing-return-type*
|
| 1437 |
+
[[dcl.fct]], that *trailing-return-type* specifies the declared return
|
| 1438 |
type of the function. Otherwise, the function declarator shall declare a
|
| 1439 |
function. If the declared return type of the function contains a
|
| 1440 |
placeholder type, the return type of the function is deduced from
|
| 1441 |
+
non-discarded `return` statements, if any, in the body of the function
|
| 1442 |
+
[[stmt.if]].
|
| 1443 |
+
|
| 1444 |
+
The type of a variable declared using a placeholder type is deduced from
|
| 1445 |
+
its initializer. This use is allowed in an initializing declaration
|
| 1446 |
+
[[dcl.init]] of a variable. The placeholder type shall appear as one of
|
| 1447 |
+
the *decl-specifier*s in the *decl-specifier-seq* and the
|
| 1448 |
+
*decl-specifier-seq* shall be followed by one or more *declarator*s,
|
| 1449 |
+
each of which shall be followed by a non-empty *initializer*. In an
|
| 1450 |
+
*initializer* of the form
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1451 |
|
| 1452 |
``` cpp
|
| 1453 |
( expression-list )
|
| 1454 |
```
|
| 1455 |
|
| 1456 |
the *expression-list* shall be a single *assignment-expression*.
|
| 1457 |
|
| 1458 |
+
[*Example 1*:
|
| 1459 |
|
| 1460 |
``` cpp
|
| 1461 |
auto x = 5; // OK: x has type int
|
| 1462 |
const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
|
| 1463 |
static auto y = 0.0; // OK: y has type double
|
|
|
|
| 1467 |
auto h(); // OK: h's return type will be deduced when it is defined
|
| 1468 |
```
|
| 1469 |
|
| 1470 |
— *end example*]
|
| 1471 |
|
| 1472 |
+
The `auto` *type-specifier* can also be used to introduce a structured
|
| 1473 |
+
binding declaration [[dcl.struct.bind]].
|
| 1474 |
+
|
| 1475 |
A placeholder type can also be used in the *type-specifier-seq* in the
|
| 1476 |
+
*new-type-id* or *type-id* of a *new-expression* [[expr.new]] and as a
|
| 1477 |
+
*decl-specifier* of the *parameter-declaration*'s *decl-specifier-seq*
|
| 1478 |
+
in a *template-parameter* [[temp.param]].
|
| 1479 |
|
| 1480 |
+
A program that uses a placeholder type in a context not explicitly
|
| 1481 |
+
allowed in this subclause is ill-formed.
|
| 1482 |
|
| 1483 |
If the *init-declarator-list* contains more than one *init-declarator*,
|
| 1484 |
they shall all form declarations of variables. The type of each declared
|
| 1485 |
+
variable is determined by placeholder type deduction
|
| 1486 |
+
[[dcl.type.auto.deduct]], and if the type that replaces the placeholder
|
| 1487 |
type is not the same in each deduction, the program is ill-formed.
|
| 1488 |
|
| 1489 |
+
[*Example 2*:
|
| 1490 |
|
| 1491 |
``` cpp
|
| 1492 |
auto x = 5, *y = &x; // OK: auto is int
|
| 1493 |
auto a = 5, b = { 1, 2 }; // error: different types for auto
|
| 1494 |
```
|
|
|
|
| 1503 |
If a function with a declared return type that uses a placeholder type
|
| 1504 |
has no non-discarded `return` statements, the return type is deduced as
|
| 1505 |
though from a `return` statement with no operand at the closing brace of
|
| 1506 |
the function body.
|
| 1507 |
|
| 1508 |
+
[*Example 3*:
|
| 1509 |
|
| 1510 |
``` cpp
|
| 1511 |
auto f() { } // OK, return type is void
|
| 1512 |
+
auto* g() { } // error: cannot deduce auto* from void()
|
| 1513 |
```
|
| 1514 |
|
| 1515 |
— *end example*]
|
| 1516 |
|
| 1517 |
+
An exported function with a declared return type that uses a placeholder
|
| 1518 |
+
type shall be defined in the translation unit containing its exported
|
| 1519 |
+
declaration, outside the *private-module-fragment* (if any).
|
|
|
|
|
|
|
| 1520 |
|
| 1521 |
+
[*Note 2*: The deduced return type cannot have a name with internal
|
| 1522 |
+
linkage [[basic.link]]. — *end note*]
|
| 1523 |
+
|
| 1524 |
+
If the name of an entity with an undeduced placeholder type appears in
|
| 1525 |
+
an expression, the program is ill-formed. Once a non-discarded `return`
|
| 1526 |
+
statement has been seen in a function, however, the return type deduced
|
| 1527 |
+
from that statement can be used in the rest of the function, including
|
| 1528 |
+
in other `return` statements.
|
| 1529 |
+
|
| 1530 |
+
[*Example 4*:
|
| 1531 |
|
| 1532 |
``` cpp
|
| 1533 |
+
auto n = n; // error: n's initializer refers to n
|
| 1534 |
auto f();
|
| 1535 |
+
void g() { &f; } // error: f's return type is unknown
|
| 1536 |
auto sum(int i) {
|
| 1537 |
if (i == 1)
|
| 1538 |
return i; // sum's return type is int
|
| 1539 |
else
|
| 1540 |
return sum(i-1)+i; // OK, sum's return type has been deduced
|
| 1541 |
}
|
| 1542 |
```
|
| 1543 |
|
| 1544 |
— *end example*]
|
| 1545 |
|
| 1546 |
+
Return type deduction for a templated entity that is a function or
|
| 1547 |
+
function template with a placeholder in its declared type occurs when
|
| 1548 |
+
the definition is instantiated even if the function body contains a
|
| 1549 |
+
`return` statement with a non-type-dependent operand.
|
| 1550 |
|
| 1551 |
+
[*Note 3*: Therefore, any use of a specialization of the function
|
| 1552 |
template will cause an implicit instantiation. Any errors that arise
|
| 1553 |
from this instantiation are not in the immediate context of the function
|
| 1554 |
+
type and can result in the program being ill-formed
|
| 1555 |
+
[[temp.deduct]]. — *end note*]
|
| 1556 |
|
| 1557 |
+
[*Example 5*:
|
| 1558 |
|
| 1559 |
``` cpp
|
| 1560 |
template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
|
| 1561 |
typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type
|
| 1562 |
template<class T> auto f(T* t) { return *t; }
|
|
|
|
| 1566 |
|
| 1567 |
— *end example*]
|
| 1568 |
|
| 1569 |
Redeclarations or specializations of a function or function template
|
| 1570 |
with a declared return type that uses a placeholder type shall also use
|
| 1571 |
+
that placeholder, not a deduced type. Similarly, redeclarations or
|
| 1572 |
+
specializations of a function or function template with a declared
|
| 1573 |
+
return type that does not use a placeholder type shall not use a
|
| 1574 |
+
placeholder.
|
| 1575 |
|
| 1576 |
+
[*Example 6*:
|
| 1577 |
|
| 1578 |
``` cpp
|
| 1579 |
auto f();
|
| 1580 |
auto f() { return 42; } // return type is int
|
| 1581 |
auto f(); // OK
|
| 1582 |
+
int f(); // error: cannot be overloaded with auto f()
|
| 1583 |
+
decltype(auto) f(); // error: auto and decltype(auto) don't match
|
| 1584 |
|
| 1585 |
template <typename T> auto g(T t) { return t; } // #1
|
| 1586 |
template auto g(int); // OK, return type is int
|
| 1587 |
+
template char g(char); // error: no matching template
|
| 1588 |
template<> auto g(double); // OK, forward declaration with unknown return type
|
| 1589 |
|
| 1590 |
template <class T> T g(T t) { return t; } // OK, not functionally equivalent to #1
|
| 1591 |
template char g(char); // OK, now there is a matching template
|
| 1592 |
template auto g(float); // still matches #1
|
| 1593 |
|
| 1594 |
+
void h() { return g(42); } // error: ambiguous
|
| 1595 |
|
| 1596 |
template <typename T> struct A {
|
| 1597 |
friend T frf(T);
|
| 1598 |
};
|
| 1599 |
auto frf(int i) { return i; } // not a friend of A<int>
|
| 1600 |
+
extern int v;
|
| 1601 |
+
auto v = 17; // OK, redeclares v
|
| 1602 |
+
struct S {
|
| 1603 |
+
static int i;
|
| 1604 |
+
};
|
| 1605 |
+
auto S::i = 23; // OK
|
| 1606 |
```
|
| 1607 |
|
| 1608 |
— *end example*]
|
| 1609 |
|
| 1610 |
A function declared with a return type that uses a placeholder type
|
| 1611 |
+
shall not be `virtual` [[class.virtual]].
|
| 1612 |
|
| 1613 |
+
A function declared with a return type that uses a placeholder type
|
| 1614 |
+
shall not be a coroutine [[dcl.fct.def.coroutine]].
|
|
|
|
|
|
|
| 1615 |
|
| 1616 |
+
An explicit instantiation declaration [[temp.explicit]] does not cause
|
| 1617 |
+
the instantiation of an entity declared using a placeholder type, but it
|
| 1618 |
+
also does not prevent that entity from being instantiated as needed to
|
| 1619 |
+
determine its type.
|
| 1620 |
+
|
| 1621 |
+
[*Example 7*:
|
| 1622 |
|
| 1623 |
``` cpp
|
| 1624 |
template <typename T> auto f(T t) { return t; }
|
| 1625 |
extern template auto f(int); // does not instantiate f<int>
|
| 1626 |
int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit
|
|
|
|
| 1633 |
|
| 1634 |
*Placeholder type deduction* is the process by which a type containing a
|
| 1635 |
placeholder type is replaced by a deduced type.
|
| 1636 |
|
| 1637 |
A type `T` containing a placeholder type, and a corresponding
|
| 1638 |
+
initializer E, are determined as follows:
|
| 1639 |
|
| 1640 |
- for a non-discarded `return` statement that occurs in a function
|
| 1641 |
declared with a return type that contains a placeholder type, `T` is
|
| 1642 |
+
the declared return type and E is the operand of the `return`
|
| 1643 |
+
statement. If the `return` statement has no operand, then E is
|
| 1644 |
`void()`;
|
| 1645 |
- for a variable declared with a type that contains a placeholder type,
|
| 1646 |
+
`T` is the declared type of the variable and E is the initializer. If
|
| 1647 |
+
the initialization is direct-list-initialization, the initializer
|
| 1648 |
shall be a *braced-init-list* containing only a single
|
| 1649 |
+
*assignment-expression* and E is the *assignment-expression*;
|
| 1650 |
- for a non-type template parameter declared with a type that contains a
|
| 1651 |
placeholder type, `T` is the declared type of the non-type template
|
| 1652 |
+
parameter and E is the corresponding template argument.
|
| 1653 |
|
| 1654 |
In the case of a `return` statement with no operand or with an operand
|
| 1655 |
+
of type `void`, `T` shall be either *type-constraint*ₒₚₜ
|
| 1656 |
+
`decltype(auto)` or cv *type-constraint*ₒₚₜ `auto`.
|
| 1657 |
|
| 1658 |
+
If the deduction is for a `return` statement and E is a
|
| 1659 |
+
*braced-init-list* [[dcl.init.list]], the program is ill-formed.
|
| 1660 |
|
| 1661 |
+
If the *placeholder-type-specifier* is of the form *type-constraint*ₒₚₜ
|
| 1662 |
+
`auto`, the deduced type T' replacing `T` is determined using the rules
|
| 1663 |
+
for template argument deduction. Obtain `P` from `T` by replacing the
|
| 1664 |
+
occurrences of *type-constraint*ₒₚₜ `auto` either with a new invented
|
| 1665 |
+
type template parameter `U` or, if the initialization is
|
| 1666 |
+
copy-list-initialization, with `std::initializer_list<U>`. Deduce a
|
| 1667 |
+
value for `U` using the rules of template argument deduction from a
|
| 1668 |
+
function call [[temp.deduct.call]], where `P` is a function template
|
| 1669 |
+
parameter type and the corresponding argument is E. If the deduction
|
| 1670 |
+
fails, the declaration is ill-formed. Otherwise, T' is obtained by
|
| 1671 |
+
substituting the deduced `U` into `P`.
|
| 1672 |
|
| 1673 |
+
[*Example 8*:
|
| 1674 |
|
| 1675 |
``` cpp
|
| 1676 |
auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
|
| 1677 |
auto x2 = { 1, 2.0 }; // error: cannot deduce element type
|
| 1678 |
auto x3{ 1, 2 }; // error: not a single element
|
|
|
|
| 1680 |
auto x5{ 3 }; // decltype(x5) is int
|
| 1681 |
```
|
| 1682 |
|
| 1683 |
— *end example*]
|
| 1684 |
|
| 1685 |
+
[*Example 9*:
|
| 1686 |
|
| 1687 |
``` cpp
|
| 1688 |
const auto &i = expr;
|
| 1689 |
```
|
| 1690 |
|
|
|
|
| 1695 |
template <class U> void f(const U& u);
|
| 1696 |
```
|
| 1697 |
|
| 1698 |
— *end example*]
|
| 1699 |
|
| 1700 |
+
If the *placeholder-type-specifier* is of the form *type-constraint*ₒₚₜ
|
| 1701 |
+
`decltype(auto)`, `T` shall be the placeholder alone. The type deduced
|
| 1702 |
+
for `T` is determined as described in [[dcl.type.simple]], as though E
|
| 1703 |
+
had been the operand of the `decltype`.
|
| 1704 |
|
| 1705 |
+
[*Example 10*:
|
| 1706 |
|
| 1707 |
``` cpp
|
| 1708 |
int i;
|
| 1709 |
int&& f();
|
| 1710 |
auto x2a(i); // decltype(x2a) is int
|
|
|
|
| 1714 |
auto x4a = (i); // decltype(x4a) is int
|
| 1715 |
decltype(auto) x4d = (i); // decltype(x4d) is int&
|
| 1716 |
auto x5a = f(); // decltype(x5a) is int
|
| 1717 |
decltype(auto) x5d = f(); // decltype(x5d) is int&&
|
| 1718 |
auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
|
| 1719 |
+
decltype(auto) x6d = { 1, 2 }; // error: { 1, 2 } is not an expression
|
| 1720 |
auto *x7a = &i; // decltype(x7a) is int*
|
| 1721 |
+
decltype(auto)*x7d = &i; // error: declared type is not plain decltype(auto)
|
| 1722 |
```
|
| 1723 |
|
| 1724 |
— *end example*]
|
| 1725 |
|
| 1726 |
+
For a *placeholder-type-specifier* with a *type-constraint*, the
|
| 1727 |
+
immediately-declared constraint [[temp.param]] of the *type-constraint*
|
| 1728 |
+
for the type deduced for the placeholder shall be satisfied.
|
| 1729 |
+
|
| 1730 |
#### Deduced class template specialization types <a id="dcl.type.class.deduct">[[dcl.type.class.deduct]]</a>
|
| 1731 |
|
| 1732 |
If a placeholder for a deduced class type appears as a *decl-specifier*
|
| 1733 |
+
in the *decl-specifier-seq* of an initializing declaration [[dcl.init]]
|
| 1734 |
+
of a variable, the declared type of the variable shall be cv `T`, where
|
| 1735 |
+
`T` is the placeholder.
|
| 1736 |
+
|
| 1737 |
+
[*Example 1*:
|
| 1738 |
+
|
| 1739 |
+
``` cpp
|
| 1740 |
+
template <class ...T> struct A {
|
| 1741 |
+
A(T...) {}
|
| 1742 |
+
};
|
| 1743 |
+
A x[29]{}; // error: no declarator operators allowed
|
| 1744 |
+
const A& y{}; // error: no declarator operators allowed
|
| 1745 |
+
```
|
| 1746 |
+
|
| 1747 |
+
— *end example*]
|
| 1748 |
+
|
| 1749 |
+
The placeholder is replaced by the return type of the function selected
|
| 1750 |
+
by overload resolution for class template deduction
|
| 1751 |
+
[[over.match.class.deduct]]. If the *decl-specifier-seq* is followed by
|
| 1752 |
+
an *init-declarator-list* or *member-declarator-list* containing more
|
| 1753 |
+
than one *declarator*, the type that replaces the placeholder shall be
|
| 1754 |
+
the same in each deduction.
|
| 1755 |
|
| 1756 |
A placeholder for a deduced class type can also be used in the
|
| 1757 |
*type-specifier-seq* in the *new-type-id* or *type-id* of a
|
| 1758 |
+
*new-expression* [[expr.new]], as the *simple-type-specifier* in an
|
| 1759 |
+
explicit type conversion (functional notation) [[expr.type.conv]], or as
|
| 1760 |
+
the *type-specifier* in the *parameter-declaration* of a
|
| 1761 |
+
*template-parameter* [[temp.param]]. A placeholder for a deduced class
|
| 1762 |
+
type shall not appear in any other context.
|
| 1763 |
|
| 1764 |
+
[*Example 2*:
|
| 1765 |
|
| 1766 |
``` cpp
|
| 1767 |
template<class T> struct container {
|
| 1768 |
container(T t) {}
|
| 1769 |
template<class Iter> container(Iter beg, Iter end);
|
|
|
|
| 1772 |
container(Iter b, Iter e) -> container<typename std::iterator_traits<Iter>::value_type>;
|
| 1773 |
std::vector<double> v = { ... };
|
| 1774 |
|
| 1775 |
container c(7); // OK, deduces int for T
|
| 1776 |
auto d = container(v.begin(), v.end()); // OK, deduces double for T
|
| 1777 |
+
container e{5, 6}; // error: int is not an iterator
|
| 1778 |
```
|
| 1779 |
|
| 1780 |
— *end example*]
|
| 1781 |
|
| 1782 |
+
## Declarators <a id="dcl.decl">[[dcl.decl]]</a>
|
| 1783 |
|
| 1784 |
+
A declarator declares a single variable, function, or type, within a
|
| 1785 |
+
declaration. The *init-declarator-list* appearing in a declaration is a
|
| 1786 |
+
comma-separated sequence of declarators, each of which can have an
|
| 1787 |
+
initializer.
|
| 1788 |
+
|
| 1789 |
+
``` bnf
|
| 1790 |
+
init-declarator-list:
|
| 1791 |
+
init-declarator
|
| 1792 |
+
init-declarator-list ',' init-declarator
|
| 1793 |
+
```
|
| 1794 |
+
|
| 1795 |
+
``` bnf
|
| 1796 |
+
init-declarator:
|
| 1797 |
+
declarator initializerₒₚₜ
|
| 1798 |
+
declarator requires-clause
|
| 1799 |
+
```
|
| 1800 |
+
|
| 1801 |
+
The three components of a *simple-declaration* are the attributes
|
| 1802 |
+
[[dcl.attr]], the specifiers (*decl-specifier-seq*; [[dcl.spec]]) and
|
| 1803 |
+
the declarators (*init-declarator-list*). The specifiers indicate the
|
| 1804 |
+
type, storage class or other properties of the entities being declared.
|
| 1805 |
+
The declarators specify the names of these entities and (optionally)
|
| 1806 |
+
modify the type of the specifiers with operators such as `*` (pointer
|
| 1807 |
+
to) and `()` (function returning). Initial values can also be specified
|
| 1808 |
+
in a declarator; initializers are discussed in [[dcl.init]] and
|
| 1809 |
+
[[class.init]].
|
| 1810 |
+
|
| 1811 |
+
Each *init-declarator* in a declaration is analyzed separately as if it
|
| 1812 |
+
was in a declaration by itself.
|
| 1813 |
+
|
| 1814 |
+
[*Note 1*:
|
| 1815 |
+
|
| 1816 |
+
A declaration with several declarators is usually equivalent to the
|
| 1817 |
+
corresponding sequence of declarations each with a single declarator.
|
| 1818 |
+
That is
|
| 1819 |
+
|
| 1820 |
+
``` cpp
|
| 1821 |
+
T D1, D2, ... Dn;
|
| 1822 |
+
```
|
| 1823 |
+
|
| 1824 |
+
is usually equivalent to
|
| 1825 |
+
|
| 1826 |
+
``` cpp
|
| 1827 |
+
T D1; T D2; ... T Dn;
|
| 1828 |
+
```
|
| 1829 |
+
|
| 1830 |
+
where `T` is a *decl-specifier-seq* and each `Di` is an
|
| 1831 |
+
*init-declarator*. One exception is when a name introduced by one of the
|
| 1832 |
+
*declarator*s hides a type name used by the *decl-specifier*s, so that
|
| 1833 |
+
when the same *decl-specifier*s are used in a subsequent declaration,
|
| 1834 |
+
they do not have the same meaning, as in
|
| 1835 |
+
|
| 1836 |
+
``` cpp
|
| 1837 |
+
struct S { ... };
|
| 1838 |
+
S S, T; // declare two instances of struct S
|
| 1839 |
+
```
|
| 1840 |
+
|
| 1841 |
+
which is not equivalent to
|
| 1842 |
+
|
| 1843 |
+
``` cpp
|
| 1844 |
+
struct S { ... };
|
| 1845 |
+
S S;
|
| 1846 |
+
S T; // error
|
| 1847 |
+
```
|
| 1848 |
+
|
| 1849 |
+
Another exception is when `T` is `auto` [[dcl.spec.auto]], for example:
|
| 1850 |
+
|
| 1851 |
+
``` cpp
|
| 1852 |
+
auto i = 1, j = 2.0; // error: deduced types for i and j do not match
|
| 1853 |
+
```
|
| 1854 |
+
|
| 1855 |
+
as opposed to
|
| 1856 |
+
|
| 1857 |
+
``` cpp
|
| 1858 |
+
auto i = 1; // OK: i deduced to have type int
|
| 1859 |
+
auto j = 2.0; // OK: j deduced to have type double
|
| 1860 |
+
```
|
| 1861 |
+
|
| 1862 |
+
— *end note*]
|
| 1863 |
+
|
| 1864 |
+
The optional *requires-clause* [[temp.pre]] in an *init-declarator* or
|
| 1865 |
+
*member-declarator* shall be present only if the declarator declares a
|
| 1866 |
+
templated function [[dcl.fct]]. When present after a declarator, the
|
| 1867 |
+
*requires-clause* is called the *trailing *requires-clause**. The
|
| 1868 |
+
trailing *requires-clause* introduces the *constraint-expression* that
|
| 1869 |
+
results from interpreting its *constraint-logical-or-expression* as a
|
| 1870 |
+
*constraint-expression*.
|
| 1871 |
+
|
| 1872 |
+
[*Example 1*:
|
| 1873 |
+
|
| 1874 |
+
``` cpp
|
| 1875 |
+
void f1(int a) requires true; // error: non-templated function
|
| 1876 |
+
template<typename T>
|
| 1877 |
+
auto f2(T a) -> bool requires true; // OK
|
| 1878 |
+
template<typename T>
|
| 1879 |
+
auto f3(T a) requires true -> bool; // error: requires-clause precedes trailing-return-type
|
| 1880 |
+
void (*pf)() requires true; // error: constraint on a variable
|
| 1881 |
+
void g(int (*)() requires true); // error: constraint on a parameter-declaration
|
| 1882 |
+
|
| 1883 |
+
auto* p = new void(*)(char) requires true; // error: not a function declaration
|
| 1884 |
+
```
|
| 1885 |
+
|
| 1886 |
+
— *end example*]
|
| 1887 |
+
|
| 1888 |
+
Declarators have the syntax
|
| 1889 |
+
|
| 1890 |
+
``` bnf
|
| 1891 |
+
declarator:
|
| 1892 |
+
ptr-declarator
|
| 1893 |
+
noptr-declarator parameters-and-qualifiers trailing-return-type
|
| 1894 |
+
```
|
| 1895 |
+
|
| 1896 |
+
``` bnf
|
| 1897 |
+
ptr-declarator:
|
| 1898 |
+
noptr-declarator
|
| 1899 |
+
ptr-operator ptr-declarator
|
| 1900 |
+
```
|
| 1901 |
+
|
| 1902 |
+
``` bnf
|
| 1903 |
+
noptr-declarator:
|
| 1904 |
+
declarator-id attribute-specifier-seqₒₚₜ
|
| 1905 |
+
noptr-declarator parameters-and-qualifiers
|
| 1906 |
+
noptr-declarator '[' constant-expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
|
| 1907 |
+
'(' ptr-declarator ')'
|
| 1908 |
+
```
|
| 1909 |
+
|
| 1910 |
+
``` bnf
|
| 1911 |
+
parameters-and-qualifiers:
|
| 1912 |
+
'(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 1913 |
+
ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 1914 |
+
```
|
| 1915 |
+
|
| 1916 |
+
``` bnf
|
| 1917 |
+
trailing-return-type:
|
| 1918 |
+
'->' type-id
|
| 1919 |
+
```
|
| 1920 |
+
|
| 1921 |
+
``` bnf
|
| 1922 |
+
ptr-operator:
|
| 1923 |
+
'*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ
|
| 1924 |
+
'&' attribute-specifier-seqₒₚₜ
|
| 1925 |
+
'&&' attribute-specifier-seqₒₚₜ
|
| 1926 |
+
nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ
|
| 1927 |
+
```
|
| 1928 |
+
|
| 1929 |
+
``` bnf
|
| 1930 |
+
cv-qualifier-seq:
|
| 1931 |
+
cv-qualifier cv-qualifier-seqₒₚₜ
|
| 1932 |
+
```
|
| 1933 |
+
|
| 1934 |
+
``` bnf
|
| 1935 |
+
cv-qualifier:
|
| 1936 |
+
const
|
| 1937 |
+
volatile
|
| 1938 |
+
```
|
| 1939 |
+
|
| 1940 |
+
``` bnf
|
| 1941 |
+
ref-qualifier:
|
| 1942 |
+
'&'
|
| 1943 |
+
'&&'
|
| 1944 |
+
```
|
| 1945 |
+
|
| 1946 |
+
``` bnf
|
| 1947 |
+
declarator-id:
|
| 1948 |
+
'...'ₒₚₜ id-expression
|
| 1949 |
+
```
|
| 1950 |
+
|
| 1951 |
+
### Type names <a id="dcl.name">[[dcl.name]]</a>
|
| 1952 |
+
|
| 1953 |
+
To specify type conversions explicitly, and as an argument of `sizeof`,
|
| 1954 |
+
`alignof`, `new`, or `typeid`, the name of a type shall be specified.
|
| 1955 |
+
This can be done with a *type-id*, which is syntactically a declaration
|
| 1956 |
+
for a variable or function of that type that omits the name of the
|
| 1957 |
+
entity.
|
| 1958 |
+
|
| 1959 |
+
``` bnf
|
| 1960 |
+
type-id:
|
| 1961 |
+
type-specifier-seq abstract-declaratorₒₚₜ
|
| 1962 |
+
```
|
| 1963 |
+
|
| 1964 |
+
``` bnf
|
| 1965 |
+
defining-type-id:
|
| 1966 |
+
defining-type-specifier-seq abstract-declaratorₒₚₜ
|
| 1967 |
+
```
|
| 1968 |
+
|
| 1969 |
+
``` bnf
|
| 1970 |
+
abstract-declarator:
|
| 1971 |
+
ptr-abstract-declarator
|
| 1972 |
+
noptr-abstract-declaratorₒₚₜ parameters-and-qualifiers trailing-return-type
|
| 1973 |
+
abstract-pack-declarator
|
| 1974 |
+
```
|
| 1975 |
+
|
| 1976 |
+
``` bnf
|
| 1977 |
+
ptr-abstract-declarator:
|
| 1978 |
+
noptr-abstract-declarator
|
| 1979 |
+
ptr-operator ptr-abstract-declaratorₒₚₜ
|
| 1980 |
+
```
|
| 1981 |
+
|
| 1982 |
+
``` bnf
|
| 1983 |
+
noptr-abstract-declarator:
|
| 1984 |
+
noptr-abstract-declaratorₒₚₜ parameters-and-qualifiers
|
| 1985 |
+
noptr-abstract-declaratorₒₚₜ '[' constant-expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
|
| 1986 |
+
'(' ptr-abstract-declarator ')'
|
| 1987 |
+
```
|
| 1988 |
+
|
| 1989 |
+
``` bnf
|
| 1990 |
+
abstract-pack-declarator:
|
| 1991 |
+
noptr-abstract-pack-declarator
|
| 1992 |
+
ptr-operator abstract-pack-declarator
|
| 1993 |
+
```
|
| 1994 |
+
|
| 1995 |
+
``` bnf
|
| 1996 |
+
noptr-abstract-pack-declarator:
|
| 1997 |
+
noptr-abstract-pack-declarator parameters-and-qualifiers
|
| 1998 |
+
noptr-abstract-pack-declarator '[' constant-expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
|
| 1999 |
+
'...'
|
| 2000 |
+
```
|
| 2001 |
+
|
| 2002 |
+
It is possible to identify uniquely the location in the
|
| 2003 |
+
*abstract-declarator* where the identifier would appear if the
|
| 2004 |
+
construction were a declarator in a declaration. The named type is then
|
| 2005 |
+
the same as the type of the hypothetical identifier.
|
| 2006 |
+
|
| 2007 |
+
[*Example 1*:
|
| 2008 |
+
|
| 2009 |
+
``` cpp
|
| 2010 |
+
int // int i
|
| 2011 |
+
int * // int *pi
|
| 2012 |
+
int *[3] // int *p[3]
|
| 2013 |
+
int (*)[3] // int (*p3i)[3]
|
| 2014 |
+
int *() // int *f()
|
| 2015 |
+
int (*)(double) // int (*pf)(double)
|
| 2016 |
+
```
|
| 2017 |
+
|
| 2018 |
+
name respectively the types “`int`”, “pointer to `int`”, “array of 3
|
| 2019 |
+
pointers to `int`”, “pointer to array of 3 `int`”, “function of (no
|
| 2020 |
+
parameters) returning pointer to `int`”, and “pointer to a function of
|
| 2021 |
+
(`double`) returning `int`”.
|
| 2022 |
+
|
| 2023 |
+
— *end example*]
|
| 2024 |
+
|
| 2025 |
+
A type can also be named (often more easily) by using a `typedef`
|
| 2026 |
+
[[dcl.typedef]].
|
| 2027 |
+
|
| 2028 |
+
### Ambiguity resolution <a id="dcl.ambig.res">[[dcl.ambig.res]]</a>
|
| 2029 |
+
|
| 2030 |
+
The ambiguity arising from the similarity between a function-style cast
|
| 2031 |
+
and a declaration mentioned in [[stmt.ambig]] can also occur in the
|
| 2032 |
+
context of a declaration. In that context, the choice is between a
|
| 2033 |
+
function declaration with a redundant set of parentheses around a
|
| 2034 |
+
parameter name and an object declaration with a function-style cast as
|
| 2035 |
+
the initializer. Just as for the ambiguities mentioned in
|
| 2036 |
+
[[stmt.ambig]], the resolution is to consider any construct that could
|
| 2037 |
+
possibly be a declaration a declaration.
|
| 2038 |
+
|
| 2039 |
+
[*Note 1*: A declaration can be explicitly disambiguated by adding
|
| 2040 |
+
parentheses around the argument. The ambiguity can be avoided by use of
|
| 2041 |
+
copy-initialization or list-initialization syntax, or by use of a
|
| 2042 |
+
non-function-style cast. — *end note*]
|
| 2043 |
+
|
| 2044 |
+
[*Example 1*:
|
| 2045 |
+
|
| 2046 |
+
``` cpp
|
| 2047 |
+
struct S {
|
| 2048 |
+
S(int);
|
| 2049 |
+
};
|
| 2050 |
+
|
| 2051 |
+
void foo(double a) {
|
| 2052 |
+
S w(int(a)); // function declaration
|
| 2053 |
+
S x(int()); // function declaration
|
| 2054 |
+
S y((int(a))); // object declaration
|
| 2055 |
+
S y((int)a); // object declaration
|
| 2056 |
+
S z = int(a); // object declaration
|
| 2057 |
+
}
|
| 2058 |
+
```
|
| 2059 |
+
|
| 2060 |
+
— *end example*]
|
| 2061 |
+
|
| 2062 |
+
An ambiguity can arise from the similarity between a function-style cast
|
| 2063 |
+
and a *type-id*. The resolution is that any construct that could
|
| 2064 |
+
possibly be a *type-id* in its syntactic context shall be considered a
|
| 2065 |
+
*type-id*.
|
| 2066 |
+
|
| 2067 |
+
[*Example 2*:
|
| 2068 |
+
|
| 2069 |
+
``` cpp
|
| 2070 |
+
template <class T> struct X {};
|
| 2071 |
+
template <int N> struct Y {};
|
| 2072 |
+
X<int()> a; // type-id
|
| 2073 |
+
X<int(1)> b; // expression (ill-formed)
|
| 2074 |
+
Y<int()> c; // type-id (ill-formed)
|
| 2075 |
+
Y<int(1)> d; // expression
|
| 2076 |
+
|
| 2077 |
+
void foo(signed char a) {
|
| 2078 |
+
sizeof(int()); // type-id (ill-formed)
|
| 2079 |
+
sizeof(int(a)); // expression
|
| 2080 |
+
sizeof(int(unsigned(a))); // type-id (ill-formed)
|
| 2081 |
+
|
| 2082 |
+
(int())+1; // type-id (ill-formed)
|
| 2083 |
+
(int(a))+1; // expression
|
| 2084 |
+
(int(unsigned(a)))+1; // type-id (ill-formed)
|
| 2085 |
+
}
|
| 2086 |
+
```
|
| 2087 |
+
|
| 2088 |
+
— *end example*]
|
| 2089 |
+
|
| 2090 |
+
Another ambiguity arises in a *parameter-declaration-clause* when a
|
| 2091 |
+
*type-name* is nested in parentheses. In this case, the choice is
|
| 2092 |
+
between the declaration of a parameter of type pointer to function and
|
| 2093 |
+
the declaration of a parameter with redundant parentheses around the
|
| 2094 |
+
*declarator-id*. The resolution is to consider the *type-name* as a
|
| 2095 |
+
*simple-type-specifier* rather than a *declarator-id*.
|
| 2096 |
+
|
| 2097 |
+
[*Example 3*:
|
| 2098 |
+
|
| 2099 |
+
``` cpp
|
| 2100 |
+
class C { };
|
| 2101 |
+
void f(int(C)) { } // void f(int(*fp)(C c)) { }
|
| 2102 |
+
// not: void f(int C) { }
|
| 2103 |
+
|
| 2104 |
+
int g(C);
|
| 2105 |
+
|
| 2106 |
+
void foo() {
|
| 2107 |
+
f(1); // error: cannot convert 1 to function pointer
|
| 2108 |
+
f(g); // OK
|
| 2109 |
+
}
|
| 2110 |
+
```
|
| 2111 |
+
|
| 2112 |
+
For another example,
|
| 2113 |
+
|
| 2114 |
+
``` cpp
|
| 2115 |
+
class C { };
|
| 2116 |
+
void h(int *(C[10])); // void h(int *(*_fp)(C _parm[10]));
|
| 2117 |
+
// not: void h(int *C[10]);
|
| 2118 |
+
```
|
| 2119 |
+
|
| 2120 |
+
— *end example*]
|
| 2121 |
+
|
| 2122 |
+
### Meaning of declarators <a id="dcl.meaning">[[dcl.meaning]]</a>
|
| 2123 |
+
|
| 2124 |
+
A declarator contains exactly one *declarator-id*; it names the
|
| 2125 |
+
identifier that is declared. An *unqualified-id* occurring in a
|
| 2126 |
+
*declarator-id* shall be a simple *identifier* except for the
|
| 2127 |
+
declaration of some special functions ([[class.ctor]], [[class.conv]],
|
| 2128 |
+
[[class.dtor]], [[over.oper]]) and for the declaration of template
|
| 2129 |
+
specializations or partial specializations [[temp.spec]]. When the
|
| 2130 |
+
*declarator-id* is qualified, the declaration shall refer to a
|
| 2131 |
+
previously declared member of the class or namespace to which the
|
| 2132 |
+
qualifier refers (or, in the case of a namespace, of an element of the
|
| 2133 |
+
inline namespace set of that namespace [[namespace.def]]) or to a
|
| 2134 |
+
specialization thereof; the member shall not merely have been introduced
|
| 2135 |
+
by a *using-declaration* in the scope of the class or namespace
|
| 2136 |
+
nominated by the *nested-name-specifier* of the *declarator-id*. The
|
| 2137 |
+
*nested-name-specifier* of a qualified *declarator-id* shall not begin
|
| 2138 |
+
with a *decltype-specifier*.
|
| 2139 |
+
|
| 2140 |
+
[*Note 1*: If the qualifier is the global `::` scope resolution
|
| 2141 |
+
operator, the *declarator-id* refers to a name declared in the global
|
| 2142 |
+
namespace scope. — *end note*]
|
| 2143 |
+
|
| 2144 |
+
The optional *attribute-specifier-seq* following a *declarator-id*
|
| 2145 |
+
appertains to the entity that is declared.
|
| 2146 |
+
|
| 2147 |
+
A `static`, `thread_local`, `extern`, `mutable`, `friend`, `inline`,
|
| 2148 |
+
`virtual`, `constexpr`, or `typedef` specifier or an
|
| 2149 |
+
*explicit-specifier* applies directly to each *declarator-id* in an
|
| 2150 |
+
*init-declarator-list* or *member-declarator-list*; the type specified
|
| 2151 |
+
for each *declarator-id* depends on both the *decl-specifier-seq* and
|
| 2152 |
+
its *declarator*.
|
| 2153 |
+
|
| 2154 |
+
Thus, a declaration of a particular identifier has the form
|
| 2155 |
+
|
| 2156 |
+
``` cpp
|
| 2157 |
+
T D
|
| 2158 |
+
```
|
| 2159 |
+
|
| 2160 |
+
where `T` is of the form *attribute-specifier-seq*ₒₚₜ
|
| 2161 |
+
*decl-specifier-seq* and `D` is a declarator. Following is a recursive
|
| 2162 |
+
procedure for determining the type specified for the contained
|
| 2163 |
+
*declarator-id* by such a declaration.
|
| 2164 |
+
|
| 2165 |
+
First, the *decl-specifier-seq* determines a type. In a declaration
|
| 2166 |
+
|
| 2167 |
+
``` cpp
|
| 2168 |
+
T D
|
| 2169 |
+
```
|
| 2170 |
+
|
| 2171 |
+
the *decl-specifier-seq* `T` determines the type `T`.
|
| 2172 |
+
|
| 2173 |
+
[*Example 1*:
|
| 2174 |
+
|
| 2175 |
+
In the declaration
|
| 2176 |
+
|
| 2177 |
+
``` cpp
|
| 2178 |
+
int unsigned i;
|
| 2179 |
+
```
|
| 2180 |
+
|
| 2181 |
+
the type specifiers `int` `unsigned` determine the type “`unsigned int`”
|
| 2182 |
+
[[dcl.type.simple]].
|
| 2183 |
+
|
| 2184 |
+
— *end example*]
|
| 2185 |
+
|
| 2186 |
+
In a declaration *attribute-specifier-seq*ₒₚₜ `T` `D` where `D` is an
|
| 2187 |
+
unadorned identifier the type of this identifier is “`T`”.
|
| 2188 |
+
|
| 2189 |
+
In a declaration `T` `D` where `D` has the form
|
| 2190 |
+
|
| 2191 |
+
``` bnf
|
| 2192 |
+
'(' 'D1' ')'
|
| 2193 |
+
```
|
| 2194 |
+
|
| 2195 |
+
the type of the contained *declarator-id* is the same as that of the
|
| 2196 |
+
contained *declarator-id* in the declaration
|
| 2197 |
+
|
| 2198 |
+
``` cpp
|
| 2199 |
+
T D1
|
| 2200 |
+
```
|
| 2201 |
+
|
| 2202 |
+
Parentheses do not alter the type of the embedded *declarator-id*, but
|
| 2203 |
+
they can alter the binding of complex declarators.
|
| 2204 |
+
|
| 2205 |
+
#### Pointers <a id="dcl.ptr">[[dcl.ptr]]</a>
|
| 2206 |
+
|
| 2207 |
+
In a declaration `T` `D` where `D` has the form
|
| 2208 |
+
|
| 2209 |
+
``` bnf
|
| 2210 |
+
'*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
| 2211 |
+
```
|
| 2212 |
+
|
| 2213 |
+
and the type of the identifier in the declaration `T` `D1` is
|
| 2214 |
+
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 2215 |
+
`D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
|
| 2216 |
+
`T`”. The *cv-qualifier*s apply to the pointer and not to the object
|
| 2217 |
+
pointed to. Similarly, the optional *attribute-specifier-seq*
|
| 2218 |
+
[[dcl.attr.grammar]] appertains to the pointer and not to the object
|
| 2219 |
+
pointed to.
|
| 2220 |
+
|
| 2221 |
+
[*Example 1*:
|
| 2222 |
+
|
| 2223 |
+
The declarations
|
| 2224 |
+
|
| 2225 |
+
``` cpp
|
| 2226 |
+
const int ci = 10, *pc = &ci, *const cpc = pc, **ppc;
|
| 2227 |
+
int i, *p, *const cp = &i;
|
| 2228 |
+
```
|
| 2229 |
+
|
| 2230 |
+
declare `ci`, a constant integer; `pc`, a pointer to a constant integer;
|
| 2231 |
+
`cpc`, a constant pointer to a constant integer; `ppc`, a pointer to a
|
| 2232 |
+
pointer to a constant integer; `i`, an integer; `p`, a pointer to
|
| 2233 |
+
integer; and `cp`, a constant pointer to integer. The value of `ci`,
|
| 2234 |
+
`cpc`, and `cp` cannot be changed after initialization. The value of
|
| 2235 |
+
`pc` can be changed, and so can the object pointed to by `cp`. Examples
|
| 2236 |
+
of some correct operations are
|
| 2237 |
+
|
| 2238 |
+
``` cpp
|
| 2239 |
+
i = ci;
|
| 2240 |
+
*cp = ci;
|
| 2241 |
+
pc++;
|
| 2242 |
+
pc = cpc;
|
| 2243 |
+
pc = p;
|
| 2244 |
+
ppc = &pc;
|
| 2245 |
+
```
|
| 2246 |
+
|
| 2247 |
+
Examples of ill-formed operations are
|
| 2248 |
+
|
| 2249 |
+
``` cpp
|
| 2250 |
+
ci = 1; // error
|
| 2251 |
+
ci++; // error
|
| 2252 |
+
*pc = 2; // error
|
| 2253 |
+
cp = &ci; // error
|
| 2254 |
+
cpc++; // error
|
| 2255 |
+
p = pc; // error
|
| 2256 |
+
ppc = &p; // error
|
| 2257 |
+
```
|
| 2258 |
+
|
| 2259 |
+
Each is unacceptable because it would either change the value of an
|
| 2260 |
+
object declared `const` or allow it to be changed through a
|
| 2261 |
+
cv-unqualified pointer later, for example:
|
| 2262 |
+
|
| 2263 |
+
``` cpp
|
| 2264 |
+
*ppc = &ci; // OK, but would make p point to ci because of previous error
|
| 2265 |
+
*p = 5; // clobber ci
|
| 2266 |
+
```
|
| 2267 |
+
|
| 2268 |
+
— *end example*]
|
| 2269 |
+
|
| 2270 |
+
See also [[expr.ass]] and [[dcl.init]].
|
| 2271 |
+
|
| 2272 |
+
[*Note 1*: Forming a pointer to reference type is ill-formed; see
|
| 2273 |
+
[[dcl.ref]]. Forming a function pointer type is ill-formed if the
|
| 2274 |
+
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 2275 |
+
[[dcl.fct]]. Since the address of a bit-field [[class.bit]] cannot be
|
| 2276 |
+
taken, a pointer can never point to a bit-field. — *end note*]
|
| 2277 |
+
|
| 2278 |
+
#### References <a id="dcl.ref">[[dcl.ref]]</a>
|
| 2279 |
+
|
| 2280 |
+
In a declaration `T` `D` where `D` has either of the forms
|
| 2281 |
+
|
| 2282 |
+
``` bnf
|
| 2283 |
+
'&' attribute-specifier-seqₒₚₜ 'D1'
|
| 2284 |
+
'&&' attribute-specifier-seqₒₚₜ 'D1'
|
| 2285 |
+
```
|
| 2286 |
+
|
| 2287 |
+
and the type of the identifier in the declaration `T` `D1` is
|
| 2288 |
+
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 2289 |
+
`D` is “*derived-declarator-type-list* reference to `T`”. The optional
|
| 2290 |
+
*attribute-specifier-seq* appertains to the reference type. Cv-qualified
|
| 2291 |
+
references are ill-formed except when the cv-qualifiers are introduced
|
| 2292 |
+
through the use of a *typedef-name* ([[dcl.typedef]], [[temp.param]])
|
| 2293 |
+
or *decltype-specifier* [[dcl.type.simple]], in which case the
|
| 2294 |
+
cv-qualifiers are ignored.
|
| 2295 |
+
|
| 2296 |
+
[*Example 1*:
|
| 2297 |
+
|
| 2298 |
+
``` cpp
|
| 2299 |
+
typedef int& A;
|
| 2300 |
+
const A aref = 3; // error: lvalue reference to non-const initialized with rvalue
|
| 2301 |
+
```
|
| 2302 |
+
|
| 2303 |
+
The type of `aref` is “lvalue reference to `int`”, not “lvalue reference
|
| 2304 |
+
to `const int`”.
|
| 2305 |
+
|
| 2306 |
+
— *end example*]
|
| 2307 |
+
|
| 2308 |
+
[*Note 1*: A reference can be thought of as a name of an
|
| 2309 |
+
object. — *end note*]
|
| 2310 |
+
|
| 2311 |
+
A declarator that specifies the type “reference to cv `void`” is
|
| 2312 |
+
ill-formed.
|
| 2313 |
+
|
| 2314 |
+
A reference type that is declared using `&` is called an *lvalue
|
| 2315 |
+
reference*, and a reference type that is declared using `&&` is called
|
| 2316 |
+
an *rvalue reference*. Lvalue references and rvalue references are
|
| 2317 |
+
distinct types. Except where explicitly noted, they are semantically
|
| 2318 |
+
equivalent and commonly referred to as references.
|
| 2319 |
+
|
| 2320 |
+
[*Example 2*:
|
| 2321 |
+
|
| 2322 |
+
``` cpp
|
| 2323 |
+
void f(double& a) { a += 3.14; }
|
| 2324 |
+
// ...
|
| 2325 |
+
double d = 0;
|
| 2326 |
+
f(d);
|
| 2327 |
+
```
|
| 2328 |
+
|
| 2329 |
+
declares `a` to be a reference parameter of `f` so the call `f(d)` will
|
| 2330 |
+
add `3.14` to `d`.
|
| 2331 |
+
|
| 2332 |
+
``` cpp
|
| 2333 |
+
int v[20];
|
| 2334 |
+
// ...
|
| 2335 |
+
int& g(int i) { return v[i]; }
|
| 2336 |
+
// ...
|
| 2337 |
+
g(3) = 7;
|
| 2338 |
+
```
|
| 2339 |
+
|
| 2340 |
+
declares the function `g()` to return a reference to an integer so
|
| 2341 |
+
`g(3)=7` will assign `7` to the fourth element of the array `v`. For
|
| 2342 |
+
another example,
|
| 2343 |
+
|
| 2344 |
+
``` cpp
|
| 2345 |
+
struct link {
|
| 2346 |
+
link* next;
|
| 2347 |
+
};
|
| 2348 |
+
|
| 2349 |
+
link* first;
|
| 2350 |
+
|
| 2351 |
+
void h(link*& p) { // p is a reference to pointer
|
| 2352 |
+
p->next = first;
|
| 2353 |
+
first = p;
|
| 2354 |
+
p = 0;
|
| 2355 |
+
}
|
| 2356 |
+
|
| 2357 |
+
void k() {
|
| 2358 |
+
link* q = new link;
|
| 2359 |
+
h(q);
|
| 2360 |
+
}
|
| 2361 |
+
```
|
| 2362 |
+
|
| 2363 |
+
declares `p` to be a reference to a pointer to `link` so `h(q)` will
|
| 2364 |
+
leave `q` with the value zero. See also [[dcl.init.ref]].
|
| 2365 |
+
|
| 2366 |
+
— *end example*]
|
| 2367 |
+
|
| 2368 |
+
It is unspecified whether or not a reference requires storage
|
| 2369 |
+
[[basic.stc]].
|
| 2370 |
+
|
| 2371 |
+
There shall be no references to references, no arrays of references, and
|
| 2372 |
+
no pointers to references. The declaration of a reference shall contain
|
| 2373 |
+
an *initializer* [[dcl.init.ref]] except when the declaration contains
|
| 2374 |
+
an explicit `extern` specifier [[dcl.stc]], is a class member
|
| 2375 |
+
[[class.mem]] declaration within a class definition, or is the
|
| 2376 |
+
declaration of a parameter or a return type [[dcl.fct]]; see
|
| 2377 |
+
[[basic.def]]. A reference shall be initialized to refer to a valid
|
| 2378 |
+
object or function.
|
| 2379 |
+
|
| 2380 |
+
[*Note 2*: In particular, a null reference cannot exist in a
|
| 2381 |
+
well-defined program, because the only way to create such a reference
|
| 2382 |
+
would be to bind it to the “object” obtained by indirection through a
|
| 2383 |
+
null pointer, which causes undefined behavior. As described in
|
| 2384 |
+
[[class.bit]], a reference cannot be bound directly to a
|
| 2385 |
+
bit-field. — *end note*]
|
| 2386 |
+
|
| 2387 |
+
If a *typedef-name* ([[dcl.typedef]], [[temp.param]]) or a
|
| 2388 |
+
*decltype-specifier* [[dcl.type.simple]] denotes a type `TR` that is a
|
| 2389 |
+
reference to a type `T`, an attempt to create the type “lvalue reference
|
| 2390 |
+
to cv `TR`” creates the type “lvalue reference to `T`”, while an attempt
|
| 2391 |
+
to create the type “rvalue reference to cv `TR`” creates the type `TR`.
|
| 2392 |
+
|
| 2393 |
+
[*Note 3*: This rule is known as reference collapsing. — *end note*]
|
| 2394 |
+
|
| 2395 |
+
[*Example 3*:
|
| 2396 |
+
|
| 2397 |
+
``` cpp
|
| 2398 |
+
int i;
|
| 2399 |
+
typedef int& LRI;
|
| 2400 |
+
typedef int&& RRI;
|
| 2401 |
+
|
| 2402 |
+
LRI& r1 = i; // r1 has the type int&
|
| 2403 |
+
const LRI& r2 = i; // r2 has the type int&
|
| 2404 |
+
const LRI&& r3 = i; // r3 has the type int&
|
| 2405 |
+
|
| 2406 |
+
RRI& r4 = i; // r4 has the type int&
|
| 2407 |
+
RRI&& r5 = 5; // r5 has the type int&&
|
| 2408 |
+
|
| 2409 |
+
decltype(r2)& r6 = i; // r6 has the type int&
|
| 2410 |
+
decltype(r2)&& r7 = i; // r7 has the type int&
|
| 2411 |
+
```
|
| 2412 |
+
|
| 2413 |
+
— *end example*]
|
| 2414 |
+
|
| 2415 |
+
[*Note 4*: Forming a reference to function type is ill-formed if the
|
| 2416 |
+
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 2417 |
+
[[dcl.fct]]. — *end note*]
|
| 2418 |
+
|
| 2419 |
+
#### Pointers to members <a id="dcl.mptr">[[dcl.mptr]]</a>
|
| 2420 |
+
|
| 2421 |
+
In a declaration `T` `D` where `D` has the form
|
| 2422 |
+
|
| 2423 |
+
``` bnf
|
| 2424 |
+
nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
| 2425 |
+
```
|
| 2426 |
+
|
| 2427 |
+
and the *nested-name-specifier* denotes a class, and the type of the
|
| 2428 |
+
identifier in the declaration `T` `D1` is
|
| 2429 |
+
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 2430 |
+
`D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
|
| 2431 |
+
member of class *nested-name-specifier* of type `T`”. The optional
|
| 2432 |
+
*attribute-specifier-seq* [[dcl.attr.grammar]] appertains to the
|
| 2433 |
+
pointer-to-member.
|
| 2434 |
+
|
| 2435 |
+
[*Example 1*:
|
| 2436 |
+
|
| 2437 |
+
``` cpp
|
| 2438 |
+
struct X {
|
| 2439 |
+
void f(int);
|
| 2440 |
+
int a;
|
| 2441 |
+
};
|
| 2442 |
+
struct Y;
|
| 2443 |
+
|
| 2444 |
+
int X::* pmi = &X::a;
|
| 2445 |
+
void (X::* pmf)(int) = &X::f;
|
| 2446 |
+
double X::* pmd;
|
| 2447 |
+
char Y::* pmc;
|
| 2448 |
+
```
|
| 2449 |
+
|
| 2450 |
+
declares `pmi`, `pmf`, `pmd` and `pmc` to be a pointer to a member of
|
| 2451 |
+
`X` of type `int`, a pointer to a member of `X` of type `void(int)`, a
|
| 2452 |
+
pointer to a member of `X` of type `double` and a pointer to a member of
|
| 2453 |
+
`Y` of type `char` respectively. The declaration of `pmd` is well-formed
|
| 2454 |
+
even though `X` has no members of type `double`. Similarly, the
|
| 2455 |
+
declaration of `pmc` is well-formed even though `Y` is an incomplete
|
| 2456 |
+
type. `pmi` and `pmf` can be used like this:
|
| 2457 |
+
|
| 2458 |
+
``` cpp
|
| 2459 |
+
X obj;
|
| 2460 |
+
// ...
|
| 2461 |
+
obj.*pmi = 7; // assign 7 to an integer member of obj
|
| 2462 |
+
(obj.*pmf)(7); // call a function member of obj with the argument 7
|
| 2463 |
+
```
|
| 2464 |
+
|
| 2465 |
+
— *end example*]
|
| 2466 |
+
|
| 2467 |
+
A pointer to member shall not point to a static member of a class
|
| 2468 |
+
[[class.static]], a member with reference type, or “cv `void`”.
|
| 2469 |
+
|
| 2470 |
+
[*Note 1*: See also [[expr.unary]] and [[expr.mptr.oper]]. The type
|
| 2471 |
+
“pointer to member” is distinct from the type “pointer”, that is, a
|
| 2472 |
+
pointer to member is declared only by the pointer-to-member declarator
|
| 2473 |
+
syntax, and never by the pointer declarator syntax. There is no
|
| 2474 |
+
“reference-to-member” type in C++. — *end note*]
|
| 2475 |
+
|
| 2476 |
+
#### Arrays <a id="dcl.array">[[dcl.array]]</a>
|
| 2477 |
+
|
| 2478 |
+
In a declaration `T` `D` where `D` has the form
|
| 2479 |
+
|
| 2480 |
+
``` bnf
|
| 2481 |
+
'D1' '[' constant-expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
|
| 2482 |
+
```
|
| 2483 |
+
|
| 2484 |
+
and the type of the contained *declarator-id* in the declaration `T`
|
| 2485 |
+
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 2486 |
+
*declarator-id* in `D` is “*derived-declarator-type-list* array of `N`
|
| 2487 |
+
`T`”. The *constant-expression* shall be a converted constant expression
|
| 2488 |
+
of type `std::size_t` [[expr.const]]. Its value `N` specifies the *array
|
| 2489 |
+
bound*, i.e., the number of elements in the array; `N` shall be greater
|
| 2490 |
+
than zero.
|
| 2491 |
+
|
| 2492 |
+
In a declaration `T` `D` where `D` has the form
|
| 2493 |
+
|
| 2494 |
+
``` bnf
|
| 2495 |
+
'D1 [ ]' attribute-specifier-seqₒₚₜ
|
| 2496 |
+
```
|
| 2497 |
+
|
| 2498 |
+
and the type of the contained *declarator-id* in the declaration `T`
|
| 2499 |
+
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 2500 |
+
*declarator-id* in `D` is “*derived-declarator-type-list* array of
|
| 2501 |
+
unknown bound of `T`”, except as specified below.
|
| 2502 |
+
|
| 2503 |
+
A type of the form “array of `N` `U`” or “array of unknown bound of `U`”
|
| 2504 |
+
is an *array type*. The optional *attribute-specifier-seq* appertains to
|
| 2505 |
+
the array type.
|
| 2506 |
+
|
| 2507 |
+
`U` is called the array *element type*; this type shall not be a
|
| 2508 |
+
placeholder type [[dcl.spec.auto]], a reference type, a function type,
|
| 2509 |
+
an array of unknown bound, or cv `void`.
|
| 2510 |
+
|
| 2511 |
+
[*Note 1*: An array can be constructed from one of the fundamental
|
| 2512 |
+
types (except `void`), from a pointer, from a pointer to member, from a
|
| 2513 |
+
class, from an enumeration type, or from an array of known
|
| 2514 |
+
bound. — *end note*]
|
| 2515 |
+
|
| 2516 |
+
[*Example 1*:
|
| 2517 |
+
|
| 2518 |
+
``` cpp
|
| 2519 |
+
float fa[17], *afp[17];
|
| 2520 |
+
```
|
| 2521 |
+
|
| 2522 |
+
declares an array of `float` numbers and an array of pointers to `float`
|
| 2523 |
+
numbers.
|
| 2524 |
+
|
| 2525 |
+
— *end example*]
|
| 2526 |
+
|
| 2527 |
+
Any type of the form “*cv-qualifier-seq* array of `N` `U`” is adjusted
|
| 2528 |
+
to “array of `N` *cv-qualifier-seq* `U`”, and similarly for “array of
|
| 2529 |
+
unknown bound of `U`”.
|
| 2530 |
+
|
| 2531 |
+
[*Example 2*:
|
| 2532 |
+
|
| 2533 |
+
``` cpp
|
| 2534 |
+
typedef int A[5], AA[2][3];
|
| 2535 |
+
typedef const A CA; // type is ``array of 5 const int''
|
| 2536 |
+
typedef const AA CAA; // type is ``array of 2 array of 3 const int''
|
| 2537 |
+
```
|
| 2538 |
+
|
| 2539 |
+
— *end example*]
|
| 2540 |
+
|
| 2541 |
+
[*Note 2*: An “array of `N` *cv-qualifier-seq* `U`” has cv-qualified
|
| 2542 |
+
type; see [[basic.type.qualifier]]. — *end note*]
|
| 2543 |
+
|
| 2544 |
+
An object of type “array of `N` `U`” contains a contiguously allocated
|
| 2545 |
+
non-empty set of `N` subobjects of type `U`, known as the *elements* of
|
| 2546 |
+
the array, and numbered `0` to `N-1`.
|
| 2547 |
+
|
| 2548 |
+
In addition to declarations in which an incomplete object type is
|
| 2549 |
+
allowed, an array bound may be omitted in some cases in the declaration
|
| 2550 |
+
of a function parameter [[dcl.fct]]. An array bound may also be omitted
|
| 2551 |
+
when an object (but not a non-static data member) of array type is
|
| 2552 |
+
initialized and the declarator is followed by an initializer (
|
| 2553 |
+
[[dcl.init]], [[class.mem]], [[expr.type.conv]], [[expr.new]]). In these
|
| 2554 |
+
cases, the array bound is calculated from the number of initial elements
|
| 2555 |
+
(say, `N`) supplied [[dcl.init.aggr]], and the type of the array is
|
| 2556 |
+
“array of `N` `U`”.
|
| 2557 |
+
|
| 2558 |
+
Furthermore, if there is a preceding declaration of the entity in the
|
| 2559 |
+
same scope in which the bound was specified, an omitted array bound is
|
| 2560 |
+
taken to be the same as in that earlier declaration, and similarly for
|
| 2561 |
+
the definition of a static data member of a class.
|
| 2562 |
+
|
| 2563 |
+
[*Example 3*:
|
| 2564 |
+
|
| 2565 |
+
``` cpp
|
| 2566 |
+
extern int x[10];
|
| 2567 |
+
struct S {
|
| 2568 |
+
static int y[10];
|
| 2569 |
+
};
|
| 2570 |
+
|
| 2571 |
+
int x[]; // OK: bound is 10
|
| 2572 |
+
int S::y[]; // OK: bound is 10
|
| 2573 |
+
|
| 2574 |
+
void f() {
|
| 2575 |
+
extern int x[];
|
| 2576 |
+
int i = sizeof(x); // error: incomplete object type
|
| 2577 |
+
}
|
| 2578 |
+
```
|
| 2579 |
+
|
| 2580 |
+
— *end example*]
|
| 2581 |
+
|
| 2582 |
+
[*Note 3*:
|
| 2583 |
+
|
| 2584 |
+
When several “array of” specifications are adjacent, a multidimensional
|
| 2585 |
+
array type is created; only the first of the constant expressions that
|
| 2586 |
+
specify the bounds of the arrays may be omitted.
|
| 2587 |
+
|
| 2588 |
+
[*Example 4*:
|
| 2589 |
+
|
| 2590 |
+
``` cpp
|
| 2591 |
+
int x3d[3][5][7];
|
| 2592 |
+
```
|
| 2593 |
+
|
| 2594 |
+
declares an array of three elements, each of which is an array of five
|
| 2595 |
+
elements, each of which is an array of seven integers. The overall array
|
| 2596 |
+
can be viewed as a three-dimensional array of integers, with rank
|
| 2597 |
+
3 × 5 × 7. Any of the expressions `x3d`, `x3d[i]`, `x3d[i][j]`,
|
| 2598 |
+
`x3d[i][j][k]` can reasonably appear in an expression. The expression
|
| 2599 |
+
`x3d[i]` is equivalent to `*(x3d + i)`; in that expression, `x3d` is
|
| 2600 |
+
subject to the array-to-pointer conversion [[conv.array]] and is first
|
| 2601 |
+
converted to a pointer to a 2-dimensional array with rank 5 × 7 that
|
| 2602 |
+
points to the first element of `x3d`. Then `i` is added, which on
|
| 2603 |
+
typical implementations involves multiplying `i` by the length of the
|
| 2604 |
+
object to which the pointer points, which is `sizeof(int)`× 5 × 7. The
|
| 2605 |
+
result of the addition and indirection is an lvalue denoting the `i`ᵗʰ
|
| 2606 |
+
array element of `x3d` (an array of five arrays of seven integers). If
|
| 2607 |
+
there is another subscript, the same argument applies again, so
|
| 2608 |
+
`x3d[i][j]` is an lvalue denoting the `j`ᵗʰ array element of the `i`ᵗʰ
|
| 2609 |
+
array element of `x3d` (an array of seven integers), and `x3d[i][j][k]`
|
| 2610 |
+
is an lvalue denoting the `k`ᵗʰ array element of the `j`ᵗʰ array element
|
| 2611 |
+
of the `i`ᵗʰ array element of `x3d` (an integer).
|
| 2612 |
+
|
| 2613 |
+
— *end example*]
|
| 2614 |
+
|
| 2615 |
+
The first subscript in the declaration helps determine the amount of
|
| 2616 |
+
storage consumed by an array but plays no other part in subscript
|
| 2617 |
+
calculations.
|
| 2618 |
+
|
| 2619 |
+
— *end note*]
|
| 2620 |
+
|
| 2621 |
+
[*Note 4*: Conversions affecting expressions of array type are
|
| 2622 |
+
described in [[conv.array]]. — *end note*]
|
| 2623 |
+
|
| 2624 |
+
[*Note 5*: The subscript operator can be overloaded for a class
|
| 2625 |
+
[[over.sub]]. For the operator’s built-in meaning, see
|
| 2626 |
+
[[expr.sub]]. — *end note*]
|
| 2627 |
+
|
| 2628 |
+
#### Functions <a id="dcl.fct">[[dcl.fct]]</a>
|
| 2629 |
+
|
| 2630 |
+
In a declaration `T` `D` where `D` has the form
|
| 2631 |
+
|
| 2632 |
+
``` bnf
|
| 2633 |
+
'D1' '(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 2634 |
+
ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 2635 |
+
```
|
| 2636 |
+
|
| 2637 |
+
and the type of the contained *declarator-id* in the declaration `T`
|
| 2638 |
+
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 2639 |
+
*declarator-id* in `D` is “*derived-declarator-type-list* `noexcept`ₒₚₜ
|
| 2640 |
+
function of parameter-type-list *cv-qualifier-seq*ₒₚₜ
|
| 2641 |
+
*ref-qualifier*ₒₚₜ returning `T`”, where
|
| 2642 |
+
|
| 2643 |
+
- the parameter-type-list is derived from the
|
| 2644 |
+
*parameter-declaration-clause* as described below and
|
| 2645 |
+
- the optional `noexcept` is present if and only if the exception
|
| 2646 |
+
specification [[except.spec]] is non-throwing.
|
| 2647 |
+
|
| 2648 |
+
The optional *attribute-specifier-seq* appertains to the function type.
|
| 2649 |
+
|
| 2650 |
+
In a declaration `T` `D` where `D` has the form
|
| 2651 |
+
|
| 2652 |
+
``` bnf
|
| 2653 |
+
'D1' '(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 2654 |
+
ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-type
|
| 2655 |
+
```
|
| 2656 |
+
|
| 2657 |
+
and the type of the contained *declarator-id* in the declaration `T`
|
| 2658 |
+
`D1` is “*derived-declarator-type-list* `T`”, `T` shall be the single
|
| 2659 |
+
*type-specifier* `auto`. The type of the *declarator-id* in `D` is
|
| 2660 |
+
“*derived-declarator-type-list* `noexcept`ₒₚₜ function of
|
| 2661 |
+
parameter-type-list *cv-qualifier-seq*ₒₚₜ *ref-qualifier*ₒₚₜ returning
|
| 2662 |
+
`U`”, where
|
| 2663 |
+
|
| 2664 |
+
- the parameter-type-list is derived from the
|
| 2665 |
+
*parameter-declaration-clause* as described below,
|
| 2666 |
+
- `U` is the type specified by the *trailing-return-type*, and
|
| 2667 |
+
- the optional `noexcept` is present if and only if the exception
|
| 2668 |
+
specification is non-throwing.
|
| 2669 |
+
|
| 2670 |
+
The optional *attribute-specifier-seq* appertains to the function type.
|
| 2671 |
+
|
| 2672 |
+
A type of either form is a *function type*.[^2]
|
| 2673 |
+
|
| 2674 |
+
``` bnf
|
| 2675 |
+
parameter-declaration-clause:
|
| 2676 |
+
parameter-declaration-listₒₚₜ '...'ₒₚₜ
|
| 2677 |
+
parameter-declaration-list ',' '...'
|
| 2678 |
+
```
|
| 2679 |
+
|
| 2680 |
+
``` bnf
|
| 2681 |
+
parameter-declaration-list:
|
| 2682 |
+
parameter-declaration
|
| 2683 |
+
parameter-declaration-list ',' parameter-declaration
|
| 2684 |
+
```
|
| 2685 |
+
|
| 2686 |
+
``` bnf
|
| 2687 |
+
parameter-declaration:
|
| 2688 |
+
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator
|
| 2689 |
+
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator '=' initializer-clause
|
| 2690 |
+
attribute-specifier-seqₒₚₜ decl-specifier-seq abstract-declaratorₒₚₜ
|
| 2691 |
+
attribute-specifier-seqₒₚₜ decl-specifier-seq abstract-declaratorₒₚₜ '=' initializer-clause
|
| 2692 |
+
```
|
| 2693 |
+
|
| 2694 |
+
The optional *attribute-specifier-seq* in a *parameter-declaration*
|
| 2695 |
+
appertains to the parameter.
|
| 2696 |
+
|
| 2697 |
+
The *parameter-declaration-clause* determines the arguments that can be
|
| 2698 |
+
specified, and their processing, when the function is called.
|
| 2699 |
+
|
| 2700 |
+
[*Note 1*: The *parameter-declaration-clause* is used to convert the
|
| 2701 |
+
arguments specified on the function call; see
|
| 2702 |
+
[[expr.call]]. — *end note*]
|
| 2703 |
+
|
| 2704 |
+
If the *parameter-declaration-clause* is empty, the function takes no
|
| 2705 |
+
arguments. A parameter list consisting of a single unnamed parameter of
|
| 2706 |
+
non-dependent type `void` is equivalent to an empty parameter list.
|
| 2707 |
+
Except for this special case, a parameter shall not have type cv `void`.
|
| 2708 |
+
A parameter with volatile-qualified type is deprecated; see
|
| 2709 |
+
[[depr.volatile.type]]. If the *parameter-declaration-clause* terminates
|
| 2710 |
+
with an ellipsis or a function parameter pack [[temp.variadic]], the
|
| 2711 |
+
number of arguments shall be equal to or greater than the number of
|
| 2712 |
+
parameters that do not have a default argument and are not function
|
| 2713 |
+
parameter packs. Where syntactically correct and where “`...`” is not
|
| 2714 |
+
part of an *abstract-declarator*, “`, ...`” is synonymous with “`...`”.
|
| 2715 |
+
|
| 2716 |
+
[*Example 1*:
|
| 2717 |
+
|
| 2718 |
+
The declaration
|
| 2719 |
+
|
| 2720 |
+
``` cpp
|
| 2721 |
+
int printf(const char*, ...);
|
| 2722 |
+
```
|
| 2723 |
+
|
| 2724 |
+
declares a function that can be called with varying numbers and types of
|
| 2725 |
+
arguments.
|
| 2726 |
+
|
| 2727 |
+
``` cpp
|
| 2728 |
+
printf("hello world");
|
| 2729 |
+
printf("a=%d b=%d", a, b);
|
| 2730 |
+
```
|
| 2731 |
+
|
| 2732 |
+
However, the first argument must be of a type that can be converted to a
|
| 2733 |
+
`const` `char*`.
|
| 2734 |
+
|
| 2735 |
+
— *end example*]
|
| 2736 |
+
|
| 2737 |
+
[*Note 2*: The standard header `<cstdarg>` contains a mechanism for
|
| 2738 |
+
accessing arguments passed using the ellipsis (see [[expr.call]] and
|
| 2739 |
+
[[support.runtime]]). — *end note*]
|
| 2740 |
+
|
| 2741 |
+
The type of a function is determined using the following rules. The type
|
| 2742 |
+
of each parameter (including function parameter packs) is determined
|
| 2743 |
+
from its own *decl-specifier-seq* and *declarator*. After determining
|
| 2744 |
+
the type of each parameter, any parameter of type “array of `T`” or of
|
| 2745 |
+
function type `T` is adjusted to be “pointer to `T`”. After producing
|
| 2746 |
+
the list of parameter types, any top-level *cv-qualifier*s modifying a
|
| 2747 |
+
parameter type are deleted when forming the function type. The resulting
|
| 2748 |
+
list of transformed parameter types and the presence or absence of the
|
| 2749 |
+
ellipsis or a function parameter pack is the function’s
|
| 2750 |
+
*parameter-type-list*.
|
| 2751 |
+
|
| 2752 |
+
[*Note 3*: This transformation does not affect the types of the
|
| 2753 |
+
parameters. For example, `int(*)(const int p, decltype(p)*)` and
|
| 2754 |
+
`int(*)(int, const int*)` are identical types. — *end note*]
|
| 2755 |
+
|
| 2756 |
+
A function type with a *cv-qualifier-seq* or a *ref-qualifier*
|
| 2757 |
+
(including a type named by *typedef-name* ([[dcl.typedef]],
|
| 2758 |
+
[[temp.param]])) shall appear only as:
|
| 2759 |
+
|
| 2760 |
+
- the function type for a non-static member function,
|
| 2761 |
+
- the function type to which a pointer to member refers,
|
| 2762 |
+
- the top-level function type of a function typedef declaration or
|
| 2763 |
+
*alias-declaration*,
|
| 2764 |
+
- the *type-id* in the default argument of a *type-parameter*
|
| 2765 |
+
[[temp.param]], or
|
| 2766 |
+
- the *type-id* of a *template-argument* for a *type-parameter*
|
| 2767 |
+
[[temp.arg.type]].
|
| 2768 |
+
|
| 2769 |
+
[*Example 2*:
|
| 2770 |
+
|
| 2771 |
+
``` cpp
|
| 2772 |
+
typedef int FIC(int) const;
|
| 2773 |
+
FIC f; // error: does not declare a member function
|
| 2774 |
+
struct S {
|
| 2775 |
+
FIC f; // OK
|
| 2776 |
+
};
|
| 2777 |
+
FIC S::*pm = &S::f; // OK
|
| 2778 |
+
```
|
| 2779 |
+
|
| 2780 |
+
— *end example*]
|
| 2781 |
+
|
| 2782 |
+
The effect of a *cv-qualifier-seq* in a function declarator is not the
|
| 2783 |
+
same as adding cv-qualification on top of the function type. In the
|
| 2784 |
+
latter case, the cv-qualifiers are ignored.
|
| 2785 |
+
|
| 2786 |
+
[*Note 4*: A function type that has a *cv-qualifier-seq* is not a
|
| 2787 |
+
cv-qualified type; there are no cv-qualified function
|
| 2788 |
+
types. — *end note*]
|
| 2789 |
+
|
| 2790 |
+
[*Example 3*:
|
| 2791 |
+
|
| 2792 |
+
``` cpp
|
| 2793 |
+
typedef void F();
|
| 2794 |
+
struct S {
|
| 2795 |
+
const F f; // OK: equivalent to: void f();
|
| 2796 |
+
};
|
| 2797 |
+
```
|
| 2798 |
+
|
| 2799 |
+
— *end example*]
|
| 2800 |
+
|
| 2801 |
+
The return type, the parameter-type-list, the *ref-qualifier*, the
|
| 2802 |
+
*cv-qualifier-seq*, and the exception specification, but not the default
|
| 2803 |
+
arguments [[dcl.fct.default]] or the trailing *requires-clause*
|
| 2804 |
+
[[dcl.decl]], are part of the function type.
|
| 2805 |
+
|
| 2806 |
+
[*Note 5*: Function types are checked during the assignments and
|
| 2807 |
+
initializations of pointers to functions, references to functions, and
|
| 2808 |
+
pointers to member functions. — *end note*]
|
| 2809 |
+
|
| 2810 |
+
[*Example 4*:
|
| 2811 |
+
|
| 2812 |
+
The declaration
|
| 2813 |
+
|
| 2814 |
+
``` cpp
|
| 2815 |
+
int fseek(FILE*, long, int);
|
| 2816 |
+
```
|
| 2817 |
+
|
| 2818 |
+
declares a function taking three arguments of the specified types, and
|
| 2819 |
+
returning `int` [[dcl.type]].
|
| 2820 |
+
|
| 2821 |
+
— *end example*]
|
| 2822 |
+
|
| 2823 |
+
A single name can be used for several different functions in a single
|
| 2824 |
+
scope; this is function overloading [[over]]. All declarations for a
|
| 2825 |
+
function shall have equivalent return types, parameter-type-lists, and
|
| 2826 |
+
*requires-clause*s [[temp.over.link]].
|
| 2827 |
+
|
| 2828 |
+
Functions shall not have a return type of type array or function,
|
| 2829 |
+
although they may have a return type of type pointer or reference to
|
| 2830 |
+
such things. There shall be no arrays of functions, although there can
|
| 2831 |
+
be arrays of pointers to functions.
|
| 2832 |
+
|
| 2833 |
+
A volatile-qualified return type is deprecated; see
|
| 2834 |
+
[[depr.volatile.type]].
|
| 2835 |
+
|
| 2836 |
+
Types shall not be defined in return or parameter types.
|
| 2837 |
+
|
| 2838 |
+
A typedef of function type may be used to declare a function but shall
|
| 2839 |
+
not be used to define a function [[dcl.fct.def]].
|
| 2840 |
+
|
| 2841 |
+
[*Example 5*:
|
| 2842 |
+
|
| 2843 |
+
``` cpp
|
| 2844 |
+
typedef void F();
|
| 2845 |
+
F fv; // OK: equivalent to void fv();
|
| 2846 |
+
F fv { } // error
|
| 2847 |
+
void fv() { } // OK: definition of fv
|
| 2848 |
+
```
|
| 2849 |
+
|
| 2850 |
+
— *end example*]
|
| 2851 |
+
|
| 2852 |
+
An identifier can optionally be provided as a parameter name; if present
|
| 2853 |
+
in a function definition [[dcl.fct.def]], it names a parameter.
|
| 2854 |
+
|
| 2855 |
+
[*Note 6*: In particular, parameter names are also optional in function
|
| 2856 |
+
definitions and names used for a parameter in different declarations and
|
| 2857 |
+
the definition of a function need not be the same. If a parameter name
|
| 2858 |
+
is present in a function declaration that is not a definition, it cannot
|
| 2859 |
+
be used outside of its function declarator because that is the extent of
|
| 2860 |
+
its potential scope [[basic.scope.param]]. — *end note*]
|
| 2861 |
+
|
| 2862 |
+
[*Example 6*:
|
| 2863 |
+
|
| 2864 |
+
The declaration
|
| 2865 |
+
|
| 2866 |
+
``` cpp
|
| 2867 |
+
int i,
|
| 2868 |
+
*pi,
|
| 2869 |
+
f(),
|
| 2870 |
+
*fpi(int),
|
| 2871 |
+
(*pif)(const char*, const char*),
|
| 2872 |
+
(*fpif(int))(int);
|
| 2873 |
+
```
|
| 2874 |
+
|
| 2875 |
+
declares an integer `i`, a pointer `pi` to an integer, a function `f`
|
| 2876 |
+
taking no arguments and returning an integer, a function `fpi` taking an
|
| 2877 |
+
integer argument and returning a pointer to an integer, a pointer `pif`
|
| 2878 |
+
to a function which takes two pointers to constant characters and
|
| 2879 |
+
returns an integer, a function `fpif` taking an integer argument and
|
| 2880 |
+
returning a pointer to a function that takes an integer argument and
|
| 2881 |
+
returns an integer. It is especially useful to compare `fpi` and `pif`.
|
| 2882 |
+
The binding of `*fpi(int)` is `*(fpi(int))`, so the declaration
|
| 2883 |
+
suggests, and the same construction in an expression requires, the
|
| 2884 |
+
calling of a function `fpi`, and then using indirection through the
|
| 2885 |
+
(pointer) result to yield an integer. In the declarator
|
| 2886 |
+
`(*pif)(const char*, const char*)`, the extra parentheses are necessary
|
| 2887 |
+
to indicate that indirection through a pointer to a function yields a
|
| 2888 |
+
function, which is then called.
|
| 2889 |
+
|
| 2890 |
+
— *end example*]
|
| 2891 |
+
|
| 2892 |
+
[*Note 7*:
|
| 2893 |
+
|
| 2894 |
+
Typedefs and *trailing-return-type*s are sometimes convenient when the
|
| 2895 |
+
return type of a function is complex. For example, the function `fpif`
|
| 2896 |
+
above could have been declared
|
| 2897 |
+
|
| 2898 |
+
``` cpp
|
| 2899 |
+
typedef int IFUNC(int);
|
| 2900 |
+
IFUNC* fpif(int);
|
| 2901 |
+
```
|
| 2902 |
+
|
| 2903 |
+
or
|
| 2904 |
+
|
| 2905 |
+
``` cpp
|
| 2906 |
+
auto fpif(int)->int(*)(int);
|
| 2907 |
+
```
|
| 2908 |
+
|
| 2909 |
+
A *trailing-return-type* is most useful for a type that would be more
|
| 2910 |
+
complicated to specify before the *declarator-id*:
|
| 2911 |
+
|
| 2912 |
+
``` cpp
|
| 2913 |
+
template <class T, class U> auto add(T t, U u) -> decltype(t + u);
|
| 2914 |
+
```
|
| 2915 |
+
|
| 2916 |
+
rather than
|
| 2917 |
+
|
| 2918 |
+
``` cpp
|
| 2919 |
+
template <class T, class U> decltype((*(T*)0) + (*(U*)0)) add(T t, U u);
|
| 2920 |
+
```
|
| 2921 |
+
|
| 2922 |
+
— *end note*]
|
| 2923 |
+
|
| 2924 |
+
A *non-template function* is a function that is not a function template
|
| 2925 |
+
specialization.
|
| 2926 |
+
|
| 2927 |
+
[*Note 8*: A function template is not a function. — *end note*]
|
| 2928 |
+
|
| 2929 |
+
An *abbreviated function template* is a function declaration that has
|
| 2930 |
+
one or more generic parameter type placeholders [[dcl.spec.auto]]. An
|
| 2931 |
+
abbreviated function template is equivalent to a function template
|
| 2932 |
+
[[temp.fct]] whose *template-parameter-list* includes one invented type
|
| 2933 |
+
*template-parameter* for each generic parameter type placeholder of the
|
| 2934 |
+
function declaration, in order of appearance. For a
|
| 2935 |
+
*placeholder-type-specifier* of the form `auto`, the invented parameter
|
| 2936 |
+
is an unconstrained *type-parameter*. For a *placeholder-type-specifier*
|
| 2937 |
+
of the form *type-constraint* `auto`, the invented parameter is a
|
| 2938 |
+
*type-parameter* with that *type-constraint*. The invented type
|
| 2939 |
+
*template-parameter* is a template parameter pack if the corresponding
|
| 2940 |
+
*parameter-declaration* declares a function parameter pack [[dcl.fct]].
|
| 2941 |
+
If the placeholder contains `decltype(auto)`, the program is ill-formed.
|
| 2942 |
+
The adjusted function parameters of an abbreviated function template are
|
| 2943 |
+
derived from the *parameter-declaration-clause* by replacing each
|
| 2944 |
+
occurrence of a placeholder with the name of the corresponding invented
|
| 2945 |
+
*template-parameter*.
|
| 2946 |
+
|
| 2947 |
+
[*Example 7*:
|
| 2948 |
+
|
| 2949 |
+
``` cpp
|
| 2950 |
+
template<typename T> concept C1 = /* ... */;
|
| 2951 |
+
template<typename T> concept C2 = /* ... */;
|
| 2952 |
+
template<typename... Ts> concept C3 = /* ... */;
|
| 2953 |
+
|
| 2954 |
+
void g1(const C1 auto*, C2 auto&);
|
| 2955 |
+
void g2(C1 auto&...);
|
| 2956 |
+
void g3(C3 auto...);
|
| 2957 |
+
void g4(C3 auto);
|
| 2958 |
+
```
|
| 2959 |
+
|
| 2960 |
+
These declarations are functionally equivalent (but not equivalent) to
|
| 2961 |
+
the following declarations.
|
| 2962 |
+
|
| 2963 |
+
``` cpp
|
| 2964 |
+
template<C1 T, C2 U> void g1(const T*, U&);
|
| 2965 |
+
template<C1... Ts> void g2(Ts&...);
|
| 2966 |
+
template<C3... Ts> void g3(Ts...);
|
| 2967 |
+
template<C3 T> void g4(T);
|
| 2968 |
+
```
|
| 2969 |
+
|
| 2970 |
+
Abbreviated function templates can be specialized like all function
|
| 2971 |
+
templates.
|
| 2972 |
+
|
| 2973 |
+
``` cpp
|
| 2974 |
+
template<> void g1<int>(const int*, const double&); // OK, specialization of g1<int, const double>
|
| 2975 |
+
```
|
| 2976 |
+
|
| 2977 |
+
— *end example*]
|
| 2978 |
+
|
| 2979 |
+
An abbreviated function template can have a *template-head*. The
|
| 2980 |
+
invented *template-parameters* are appended to the
|
| 2981 |
+
*template-parameter-list* after the explicitly declared
|
| 2982 |
+
*template-parameters*.
|
| 2983 |
+
|
| 2984 |
+
[*Example 8*:
|
| 2985 |
+
|
| 2986 |
+
``` cpp
|
| 2987 |
+
template<typename> concept C = /* ... */;
|
| 2988 |
+
|
| 2989 |
+
template <typename T, C U>
|
| 2990 |
+
void g(T x, U y, C auto z);
|
| 2991 |
+
```
|
| 2992 |
+
|
| 2993 |
+
This is functionally equivalent to each of the following two
|
| 2994 |
+
declarations.
|
| 2995 |
+
|
| 2996 |
+
``` cpp
|
| 2997 |
+
template<typename T, C U, C W>
|
| 2998 |
+
void g(T x, U y, W z);
|
| 2999 |
+
|
| 3000 |
+
template<typename T, typename U, typename W>
|
| 3001 |
+
requires C<U> && C<W>
|
| 3002 |
+
void g(T x, U y, W z);
|
| 3003 |
+
```
|
| 3004 |
+
|
| 3005 |
+
— *end example*]
|
| 3006 |
+
|
| 3007 |
+
A function declaration at block scope shall not declare an abbreviated
|
| 3008 |
+
function template.
|
| 3009 |
+
|
| 3010 |
+
A *declarator-id* or *abstract-declarator* containing an ellipsis shall
|
| 3011 |
+
only be used in a *parameter-declaration*. When it is part of a
|
| 3012 |
+
*parameter-declaration-clause*, the *parameter-declaration* declares a
|
| 3013 |
+
function parameter pack [[temp.variadic]]. Otherwise, the
|
| 3014 |
+
*parameter-declaration* is part of a *template-parameter-list* and
|
| 3015 |
+
declares a template parameter pack; see [[temp.param]]. A function
|
| 3016 |
+
parameter pack is a pack expansion [[temp.variadic]].
|
| 3017 |
+
|
| 3018 |
+
[*Example 9*:
|
| 3019 |
+
|
| 3020 |
+
``` cpp
|
| 3021 |
+
template<typename... T> void f(T (* ...t)(int, int));
|
| 3022 |
+
|
| 3023 |
+
int add(int, int);
|
| 3024 |
+
float subtract(int, int);
|
| 3025 |
+
|
| 3026 |
+
void g() {
|
| 3027 |
+
f(add, subtract);
|
| 3028 |
+
}
|
| 3029 |
+
```
|
| 3030 |
+
|
| 3031 |
+
— *end example*]
|
| 3032 |
+
|
| 3033 |
+
There is a syntactic ambiguity when an ellipsis occurs at the end of a
|
| 3034 |
+
*parameter-declaration-clause* without a preceding comma. In this case,
|
| 3035 |
+
the ellipsis is parsed as part of the *abstract-declarator* if the type
|
| 3036 |
+
of the parameter either names a template parameter pack that has not
|
| 3037 |
+
been expanded or contains `auto`; otherwise, it is parsed as part of the
|
| 3038 |
+
*parameter-declaration-clause*.[^3]
|
| 3039 |
+
|
| 3040 |
+
#### Default arguments <a id="dcl.fct.default">[[dcl.fct.default]]</a>
|
| 3041 |
+
|
| 3042 |
+
If an *initializer-clause* is specified in a *parameter-declaration*
|
| 3043 |
+
this *initializer-clause* is used as a default argument.
|
| 3044 |
+
|
| 3045 |
+
[*Note 1*: Default arguments will be used in calls where trailing
|
| 3046 |
+
arguments are missing [[expr.call]]. — *end note*]
|
| 3047 |
+
|
| 3048 |
+
[*Example 1*:
|
| 3049 |
+
|
| 3050 |
+
The declaration
|
| 3051 |
+
|
| 3052 |
+
``` cpp
|
| 3053 |
+
void point(int = 3, int = 4);
|
| 3054 |
+
```
|
| 3055 |
+
|
| 3056 |
+
declares a function that can be called with zero, one, or two arguments
|
| 3057 |
+
of type `int`. It can be called in any of these ways:
|
| 3058 |
+
|
| 3059 |
+
``` cpp
|
| 3060 |
+
point(1,2); point(1); point();
|
| 3061 |
+
```
|
| 3062 |
+
|
| 3063 |
+
The last two calls are equivalent to `point(1,4)` and `point(3,4)`,
|
| 3064 |
+
respectively.
|
| 3065 |
+
|
| 3066 |
+
— *end example*]
|
| 3067 |
+
|
| 3068 |
+
A default argument shall be specified only in the
|
| 3069 |
+
*parameter-declaration-clause* of a function declaration or
|
| 3070 |
+
*lambda-declarator* or in a *template-parameter* [[temp.param]]; in the
|
| 3071 |
+
latter case, the *initializer-clause* shall be an
|
| 3072 |
+
*assignment-expression*. A default argument shall not be specified for a
|
| 3073 |
+
template parameter pack or a function parameter pack. If it is specified
|
| 3074 |
+
in a *parameter-declaration-clause*, it shall not occur within a
|
| 3075 |
+
*declarator* or *abstract-declarator* of a *parameter-declaration*.[^4]
|
| 3076 |
+
|
| 3077 |
+
For non-template functions, default arguments can be added in later
|
| 3078 |
+
declarations of a function in the same scope. Declarations in different
|
| 3079 |
+
scopes have completely distinct sets of default arguments. That is,
|
| 3080 |
+
declarations in inner scopes do not acquire default arguments from
|
| 3081 |
+
declarations in outer scopes, and vice versa. In a given function
|
| 3082 |
+
declaration, each parameter subsequent to a parameter with a default
|
| 3083 |
+
argument shall have a default argument supplied in this or a previous
|
| 3084 |
+
declaration, unless the parameter was expanded from a parameter pack, or
|
| 3085 |
+
shall be a function parameter pack.
|
| 3086 |
+
|
| 3087 |
+
[*Note 2*: A default argument cannot be redefined by a later
|
| 3088 |
+
declaration (not even to the same value)
|
| 3089 |
+
[[basic.def.odr]]. — *end note*]
|
| 3090 |
+
|
| 3091 |
+
[*Example 2*:
|
| 3092 |
+
|
| 3093 |
+
``` cpp
|
| 3094 |
+
void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow
|
| 3095 |
+
// a parameter with a default argument
|
| 3096 |
+
void f(int, int);
|
| 3097 |
+
void f(int, int = 7);
|
| 3098 |
+
void h() {
|
| 3099 |
+
f(3); // OK, calls f(3, 7)
|
| 3100 |
+
void f(int = 1, int); // error: does not use default from surrounding scope
|
| 3101 |
+
}
|
| 3102 |
+
void m() {
|
| 3103 |
+
void f(int, int); // has no defaults
|
| 3104 |
+
f(4); // error: wrong number of arguments
|
| 3105 |
+
void f(int, int = 5); // OK
|
| 3106 |
+
f(4); // OK, calls f(4, 5);
|
| 3107 |
+
void f(int, int = 5); // error: cannot redefine, even to same value
|
| 3108 |
+
}
|
| 3109 |
+
void n() {
|
| 3110 |
+
f(6); // OK, calls f(6, 7)
|
| 3111 |
+
}
|
| 3112 |
+
template<class ... T> struct C {
|
| 3113 |
+
void f(int n = 0, T...);
|
| 3114 |
+
};
|
| 3115 |
+
C<int> c; // OK, instantiates declaration void C::f(int n = 0, int)
|
| 3116 |
+
```
|
| 3117 |
+
|
| 3118 |
+
— *end example*]
|
| 3119 |
+
|
| 3120 |
+
For a given inline function defined in different translation units, the
|
| 3121 |
+
accumulated sets of default arguments at the end of the translation
|
| 3122 |
+
units shall be the same; no diagnostic is required. If a friend
|
| 3123 |
+
declaration specifies a default argument expression, that declaration
|
| 3124 |
+
shall be a definition and shall be the only declaration of the function
|
| 3125 |
+
or function template in the translation unit.
|
| 3126 |
+
|
| 3127 |
+
The default argument has the same semantic constraints as the
|
| 3128 |
+
initializer in a declaration of a variable of the parameter type, using
|
| 3129 |
+
the copy-initialization semantics [[dcl.init]]. The names in the default
|
| 3130 |
+
argument are bound, and the semantic constraints are checked, at the
|
| 3131 |
+
point where the default argument appears. Name lookup and checking of
|
| 3132 |
+
semantic constraints for default arguments in function templates and in
|
| 3133 |
+
member functions of class templates are performed as described in
|
| 3134 |
+
[[temp.inst]].
|
| 3135 |
+
|
| 3136 |
+
[*Example 3*:
|
| 3137 |
+
|
| 3138 |
+
In the following code, `g` will be called with the value `f(2)`:
|
| 3139 |
+
|
| 3140 |
+
``` cpp
|
| 3141 |
+
int a = 1;
|
| 3142 |
+
int f(int);
|
| 3143 |
+
int g(int x = f(a)); // default argument: f(::a)
|
| 3144 |
+
|
| 3145 |
+
void h() {
|
| 3146 |
+
a = 2;
|
| 3147 |
+
{
|
| 3148 |
+
int a = 3;
|
| 3149 |
+
g(); // g(f(::a))
|
| 3150 |
+
}
|
| 3151 |
+
}
|
| 3152 |
+
```
|
| 3153 |
+
|
| 3154 |
+
— *end example*]
|
| 3155 |
+
|
| 3156 |
+
[*Note 3*: In member function declarations, names in default arguments
|
| 3157 |
+
are looked up as described in [[basic.lookup.unqual]]. Access checking
|
| 3158 |
+
applies to names in default arguments as described in
|
| 3159 |
+
[[class.access]]. — *end note*]
|
| 3160 |
+
|
| 3161 |
+
Except for member functions of class templates, the default arguments in
|
| 3162 |
+
a member function definition that appears outside of the class
|
| 3163 |
+
definition are added to the set of default arguments provided by the
|
| 3164 |
+
member function declaration in the class definition; the program is
|
| 3165 |
+
ill-formed if a default constructor [[class.default.ctor]], copy or move
|
| 3166 |
+
constructor [[class.copy.ctor]], or copy or move assignment operator
|
| 3167 |
+
[[class.copy.assign]] is so declared. Default arguments for a member
|
| 3168 |
+
function of a class template shall be specified on the initial
|
| 3169 |
+
declaration of the member function within the class template.
|
| 3170 |
+
|
| 3171 |
+
[*Example 4*:
|
| 3172 |
+
|
| 3173 |
+
``` cpp
|
| 3174 |
+
class C {
|
| 3175 |
+
void f(int i = 3);
|
| 3176 |
+
void g(int i, int j = 99);
|
| 3177 |
+
};
|
| 3178 |
+
|
| 3179 |
+
void C::f(int i = 3) {} // error: default argument already specified in class scope
|
| 3180 |
+
void C::g(int i = 88, int j) {} // in this translation unit, C::g can be called with no argument
|
| 3181 |
+
```
|
| 3182 |
+
|
| 3183 |
+
— *end example*]
|
| 3184 |
+
|
| 3185 |
+
[*Note 4*: A local variable cannot be odr-used [[basic.def.odr]] in a
|
| 3186 |
+
default argument. — *end note*]
|
| 3187 |
+
|
| 3188 |
+
[*Example 5*:
|
| 3189 |
+
|
| 3190 |
+
``` cpp
|
| 3191 |
+
void f() {
|
| 3192 |
+
int i;
|
| 3193 |
+
extern void g(int x = i); // error
|
| 3194 |
+
extern void h(int x = sizeof(i)); // OK
|
| 3195 |
+
// ...
|
| 3196 |
+
}
|
| 3197 |
+
```
|
| 3198 |
+
|
| 3199 |
+
— *end example*]
|
| 3200 |
+
|
| 3201 |
+
[*Note 5*:
|
| 3202 |
+
|
| 3203 |
+
The keyword `this` may not appear in a default argument of a member
|
| 3204 |
+
function; see [[expr.prim.this]].
|
| 3205 |
+
|
| 3206 |
+
[*Example 6*:
|
| 3207 |
+
|
| 3208 |
+
``` cpp
|
| 3209 |
+
class A {
|
| 3210 |
+
void f(A* p = this) { } // error
|
| 3211 |
+
};
|
| 3212 |
+
```
|
| 3213 |
+
|
| 3214 |
+
— *end example*]
|
| 3215 |
+
|
| 3216 |
+
— *end note*]
|
| 3217 |
+
|
| 3218 |
+
A default argument is evaluated each time the function is called with no
|
| 3219 |
+
argument for the corresponding parameter. A parameter shall not appear
|
| 3220 |
+
as a potentially-evaluated expression in a default argument. Parameters
|
| 3221 |
+
of a function declared before a default argument are in scope and can
|
| 3222 |
+
hide namespace and class member names.
|
| 3223 |
+
|
| 3224 |
+
[*Example 7*:
|
| 3225 |
+
|
| 3226 |
+
``` cpp
|
| 3227 |
+
int a;
|
| 3228 |
+
int f(int a, int b = a); // error: parameter a used as default argument
|
| 3229 |
+
typedef int I;
|
| 3230 |
+
int g(float I, int b = I(2)); // error: parameter I found
|
| 3231 |
+
int h(int a, int b = sizeof(a)); // OK, unevaluated operand
|
| 3232 |
+
```
|
| 3233 |
+
|
| 3234 |
+
— *end example*]
|
| 3235 |
+
|
| 3236 |
+
A non-static member shall not appear in a default argument unless it
|
| 3237 |
+
appears as the *id-expression* of a class member access expression
|
| 3238 |
+
[[expr.ref]] or unless it is used to form a pointer to member
|
| 3239 |
+
[[expr.unary.op]].
|
| 3240 |
+
|
| 3241 |
+
[*Example 8*:
|
| 3242 |
+
|
| 3243 |
+
The declaration of `X::mem1()` in the following example is ill-formed
|
| 3244 |
+
because no object is supplied for the non-static member `X::a` used as
|
| 3245 |
+
an initializer.
|
| 3246 |
+
|
| 3247 |
+
``` cpp
|
| 3248 |
+
int b;
|
| 3249 |
+
class X {
|
| 3250 |
+
int a;
|
| 3251 |
+
int mem1(int i = a); // error: non-static member a used as default argument
|
| 3252 |
+
int mem2(int i = b); // OK; use X::b
|
| 3253 |
+
static int b;
|
| 3254 |
+
};
|
| 3255 |
+
```
|
| 3256 |
+
|
| 3257 |
+
The declaration of `X::mem2()` is meaningful, however, since no object
|
| 3258 |
+
is needed to access the static member `X::b`. Classes, objects, and
|
| 3259 |
+
members are described in [[class]].
|
| 3260 |
+
|
| 3261 |
+
— *end example*]
|
| 3262 |
+
|
| 3263 |
+
A default argument is not part of the type of a function.
|
| 3264 |
+
|
| 3265 |
+
[*Example 9*:
|
| 3266 |
+
|
| 3267 |
+
``` cpp
|
| 3268 |
+
int f(int = 0);
|
| 3269 |
+
|
| 3270 |
+
void h() {
|
| 3271 |
+
int j = f(1);
|
| 3272 |
+
int k = f(); // OK, means f(0)
|
| 3273 |
+
}
|
| 3274 |
+
|
| 3275 |
+
int (*p1)(int) = &f;
|
| 3276 |
+
int (*p2)() = &f; // error: type mismatch
|
| 3277 |
+
```
|
| 3278 |
+
|
| 3279 |
+
— *end example*]
|
| 3280 |
+
|
| 3281 |
+
When a declaration of a function is introduced by way of a
|
| 3282 |
+
*using-declaration* [[namespace.udecl]], any default argument
|
| 3283 |
+
information associated with the declaration is made known as well. If
|
| 3284 |
+
the function is redeclared thereafter in the namespace with additional
|
| 3285 |
+
default arguments, the additional arguments are also known at any point
|
| 3286 |
+
following the redeclaration where the *using-declaration* is in scope.
|
| 3287 |
+
|
| 3288 |
+
A virtual function call [[class.virtual]] uses the default arguments in
|
| 3289 |
+
the declaration of the virtual function determined by the static type of
|
| 3290 |
+
the pointer or reference denoting the object. An overriding function in
|
| 3291 |
+
a derived class does not acquire default arguments from the function it
|
| 3292 |
+
overrides.
|
| 3293 |
+
|
| 3294 |
+
[*Example 10*:
|
| 3295 |
+
|
| 3296 |
+
``` cpp
|
| 3297 |
+
struct A {
|
| 3298 |
+
virtual void f(int a = 7);
|
| 3299 |
+
};
|
| 3300 |
+
struct B : public A {
|
| 3301 |
+
void f(int a);
|
| 3302 |
+
};
|
| 3303 |
+
void m() {
|
| 3304 |
+
B* pb = new B;
|
| 3305 |
+
A* pa = pb;
|
| 3306 |
+
pa->f(); // OK, calls pa->B::f(7)
|
| 3307 |
+
pb->f(); // error: wrong number of arguments for B::f()
|
| 3308 |
+
}
|
| 3309 |
+
```
|
| 3310 |
+
|
| 3311 |
+
— *end example*]
|
| 3312 |
+
|
| 3313 |
+
## Initializers <a id="dcl.init">[[dcl.init]]</a>
|
| 3314 |
+
|
| 3315 |
+
The process of initialization described in this subclause applies to all
|
| 3316 |
+
initializations regardless of syntactic context, including the
|
| 3317 |
+
initialization of a function parameter [[expr.call]], the initialization
|
| 3318 |
+
of a return value [[stmt.return]], or when an initializer follows a
|
| 3319 |
+
declarator.
|
| 3320 |
+
|
| 3321 |
+
``` bnf
|
| 3322 |
+
initializer:
|
| 3323 |
+
brace-or-equal-initializer
|
| 3324 |
+
'(' expression-list ')'
|
| 3325 |
+
```
|
| 3326 |
+
|
| 3327 |
+
``` bnf
|
| 3328 |
+
brace-or-equal-initializer:
|
| 3329 |
+
'=' initializer-clause
|
| 3330 |
+
braced-init-list
|
| 3331 |
+
```
|
| 3332 |
+
|
| 3333 |
+
``` bnf
|
| 3334 |
+
initializer-clause:
|
| 3335 |
+
assignment-expression
|
| 3336 |
+
braced-init-list
|
| 3337 |
+
```
|
| 3338 |
+
|
| 3339 |
+
``` bnf
|
| 3340 |
+
braced-init-list:
|
| 3341 |
+
'{' initializer-list ','ₒₚₜ '}'
|
| 3342 |
+
'{' designated-initializer-list ','ₒₚₜ '}'
|
| 3343 |
+
'{' '}'
|
| 3344 |
+
```
|
| 3345 |
+
|
| 3346 |
+
``` bnf
|
| 3347 |
+
initializer-list:
|
| 3348 |
+
initializer-clause '...'ₒₚₜ
|
| 3349 |
+
initializer-list ',' initializer-clause '...'ₒₚₜ
|
| 3350 |
+
```
|
| 3351 |
+
|
| 3352 |
+
``` bnf
|
| 3353 |
+
designated-initializer-list:
|
| 3354 |
+
designated-initializer-clause
|
| 3355 |
+
designated-initializer-list ',' designated-initializer-clause
|
| 3356 |
+
```
|
| 3357 |
+
|
| 3358 |
+
``` bnf
|
| 3359 |
+
designated-initializer-clause:
|
| 3360 |
+
designator brace-or-equal-initializer
|
| 3361 |
+
```
|
| 3362 |
+
|
| 3363 |
+
``` bnf
|
| 3364 |
+
designator:
|
| 3365 |
+
'.' identifier
|
| 3366 |
+
```
|
| 3367 |
+
|
| 3368 |
+
``` bnf
|
| 3369 |
+
expr-or-braced-init-list:
|
| 3370 |
+
expression
|
| 3371 |
+
braced-init-list
|
| 3372 |
+
```
|
| 3373 |
+
|
| 3374 |
+
[*Note 1*: The rules in this subclause apply even if the grammar
|
| 3375 |
+
permits only the *brace-or-equal-initializer* form of *initializer* in a
|
| 3376 |
+
given context. — *end note*]
|
| 3377 |
+
|
| 3378 |
+
Except for objects declared with the `constexpr` specifier, for which
|
| 3379 |
+
see [[dcl.constexpr]], an *initializer* in the definition of a variable
|
| 3380 |
+
can consist of arbitrary expressions involving literals and previously
|
| 3381 |
+
declared variables and functions, regardless of the variable’s storage
|
| 3382 |
+
duration.
|
| 3383 |
+
|
| 3384 |
+
[*Example 1*:
|
| 3385 |
+
|
| 3386 |
+
``` cpp
|
| 3387 |
+
int f(int);
|
| 3388 |
+
int a = 2;
|
| 3389 |
+
int b = f(a);
|
| 3390 |
+
int c(b);
|
| 3391 |
+
```
|
| 3392 |
+
|
| 3393 |
+
— *end example*]
|
| 3394 |
+
|
| 3395 |
+
[*Note 2*: Default arguments are more restricted; see
|
| 3396 |
+
[[dcl.fct.default]]. — *end note*]
|
| 3397 |
+
|
| 3398 |
+
[*Note 3*: The order of initialization of variables with static storage
|
| 3399 |
+
duration is described in [[basic.start]] and
|
| 3400 |
+
[[stmt.dcl]]. — *end note*]
|
| 3401 |
+
|
| 3402 |
+
A declaration of a block-scope variable with external or internal
|
| 3403 |
+
linkage that has an *initializer* is ill-formed.
|
| 3404 |
+
|
| 3405 |
+
To *zero-initialize* an object or reference of type `T` means:
|
| 3406 |
+
|
| 3407 |
+
- if `T` is a scalar type [[basic.types]], the object is initialized to
|
| 3408 |
+
the value obtained by converting the integer literal `0` (zero) to
|
| 3409 |
+
`T`;[^5]
|
| 3410 |
+
- if `T` is a (possibly cv-qualified) non-union class type, its padding
|
| 3411 |
+
bits [[basic.types]] are initialized to zero bits and each non-static
|
| 3412 |
+
data member, each non-virtual base class subobject, and, if the object
|
| 3413 |
+
is not a base class subobject, each virtual base class subobject is
|
| 3414 |
+
zero-initialized;
|
| 3415 |
+
- if `T` is a (possibly cv-qualified) union type, its padding bits
|
| 3416 |
+
[[basic.types]] are initialized to zero bits and the object’s first
|
| 3417 |
+
non-static named data member is zero-initialized;
|
| 3418 |
+
- if `T` is an array type, each element is zero-initialized;
|
| 3419 |
+
- if `T` is a reference type, no initialization is performed.
|
| 3420 |
+
|
| 3421 |
+
To *default-initialize* an object of type `T` means:
|
| 3422 |
+
|
| 3423 |
+
- If `T` is a (possibly cv-qualified) class type [[class]], constructors
|
| 3424 |
+
are considered. The applicable constructors are enumerated
|
| 3425 |
+
[[over.match.ctor]], and the best one for the *initializer* `()` is
|
| 3426 |
+
chosen through overload resolution [[over.match]]. The constructor
|
| 3427 |
+
thus selected is called, with an empty argument list, to initialize
|
| 3428 |
+
the object.
|
| 3429 |
+
- If `T` is an array type, each element is default-initialized.
|
| 3430 |
+
- Otherwise, no initialization is performed.
|
| 3431 |
+
|
| 3432 |
+
A class type `T` is *const-default-constructible* if
|
| 3433 |
+
default-initialization of `T` would invoke a user-provided constructor
|
| 3434 |
+
of `T` (not inherited from a base class) or if
|
| 3435 |
+
|
| 3436 |
+
- each direct non-variant non-static data member `M` of `T` has a
|
| 3437 |
+
default member initializer or, if `M` is of class type `X` (or array
|
| 3438 |
+
thereof), `X` is const-default-constructible,
|
| 3439 |
+
- if `T` is a union with at least one non-static data member, exactly
|
| 3440 |
+
one variant member has a default member initializer,
|
| 3441 |
+
- if `T` is not a union, for each anonymous union member with at least
|
| 3442 |
+
one non-static data member (if any), exactly one non-static data
|
| 3443 |
+
member has a default member initializer, and
|
| 3444 |
+
- each potentially constructed base class of `T` is
|
| 3445 |
+
const-default-constructible.
|
| 3446 |
+
|
| 3447 |
+
If a program calls for the default-initialization of an object of a
|
| 3448 |
+
const-qualified type `T`, `T` shall be a const-default-constructible
|
| 3449 |
+
class type or array thereof.
|
| 3450 |
+
|
| 3451 |
+
To *value-initialize* an object of type `T` means:
|
| 3452 |
+
|
| 3453 |
+
- if `T` is a (possibly cv-qualified) class type [[class]], then
|
| 3454 |
+
- if `T` has either no default constructor [[class.default.ctor]] or a
|
| 3455 |
+
default constructor that is user-provided or deleted, then the
|
| 3456 |
+
object is default-initialized;
|
| 3457 |
+
- otherwise, the object is zero-initialized and the semantic
|
| 3458 |
+
constraints for default-initialization are checked, and if `T` has a
|
| 3459 |
+
non-trivial default constructor, the object is default-initialized;
|
| 3460 |
+
- if `T` is an array type, then each element is value-initialized;
|
| 3461 |
+
- otherwise, the object is zero-initialized.
|
| 3462 |
+
|
| 3463 |
+
A program that calls for default-initialization or value-initialization
|
| 3464 |
+
of an entity of reference type is ill-formed.
|
| 3465 |
+
|
| 3466 |
+
[*Note 4*: For every object of static storage duration, static
|
| 3467 |
+
initialization [[basic.start.static]] is performed at program startup
|
| 3468 |
+
before any other initialization takes place. In some cases, additional
|
| 3469 |
+
initialization is done later. — *end note*]
|
| 3470 |
+
|
| 3471 |
+
An object whose initializer is an empty set of parentheses, i.e., `()`,
|
| 3472 |
+
shall be value-initialized.
|
| 3473 |
+
|
| 3474 |
+
[*Note 5*:
|
| 3475 |
+
|
| 3476 |
+
Since `()` is not permitted by the syntax for *initializer*,
|
| 3477 |
+
|
| 3478 |
+
``` cpp
|
| 3479 |
+
X a();
|
| 3480 |
+
```
|
| 3481 |
+
|
| 3482 |
+
is not the declaration of an object of class `X`, but the declaration of
|
| 3483 |
+
a function taking no argument and returning an `X`. The form `()` is
|
| 3484 |
+
permitted in certain other initialization contexts ([[expr.new]],
|
| 3485 |
+
[[expr.type.conv]], [[class.base.init]]).
|
| 3486 |
+
|
| 3487 |
+
— *end note*]
|
| 3488 |
+
|
| 3489 |
+
If no initializer is specified for an object, the object is
|
| 3490 |
+
default-initialized.
|
| 3491 |
+
|
| 3492 |
+
An initializer for a static member is in the scope of the member’s
|
| 3493 |
+
class.
|
| 3494 |
+
|
| 3495 |
+
[*Example 2*:
|
| 3496 |
+
|
| 3497 |
+
``` cpp
|
| 3498 |
+
int a;
|
| 3499 |
+
|
| 3500 |
+
struct X {
|
| 3501 |
+
static int a;
|
| 3502 |
+
static int b;
|
| 3503 |
+
};
|
| 3504 |
+
|
| 3505 |
+
int X::a = 1;
|
| 3506 |
+
int X::b = a; // X::b = X::a
|
| 3507 |
+
```
|
| 3508 |
+
|
| 3509 |
+
— *end example*]
|
| 3510 |
+
|
| 3511 |
+
If the entity being initialized does not have class type, the
|
| 3512 |
+
*expression-list* in a parenthesized initializer shall be a single
|
| 3513 |
+
expression.
|
| 3514 |
+
|
| 3515 |
+
The initialization that occurs in the `=` form of a
|
| 3516 |
+
*brace-or-equal-initializer* or *condition* [[stmt.select]], as well as
|
| 3517 |
+
in argument passing, function return, throwing an exception
|
| 3518 |
+
[[except.throw]], handling an exception [[except.handle]], and aggregate
|
| 3519 |
+
member initialization [[dcl.init.aggr]], is called
|
| 3520 |
+
*copy-initialization*.
|
| 3521 |
+
|
| 3522 |
+
[*Note 6*: Copy-initialization may invoke a move
|
| 3523 |
+
[[class.copy.ctor]]. — *end note*]
|
| 3524 |
+
|
| 3525 |
+
The initialization that occurs
|
| 3526 |
+
|
| 3527 |
+
- for an *initializer* that is a parenthesized *expression-list* or a
|
| 3528 |
+
*braced-init-list*,
|
| 3529 |
+
- for a *new-initializer* [[expr.new]],
|
| 3530 |
+
- in a `static_cast` expression [[expr.static.cast]],
|
| 3531 |
+
- in a functional notation type conversion [[expr.type.conv]], and
|
| 3532 |
+
- in the *braced-init-list* form of a *condition*
|
| 3533 |
+
|
| 3534 |
+
is called *direct-initialization*.
|
| 3535 |
+
|
| 3536 |
+
The semantics of initializers are as follows. The *destination type* is
|
| 3537 |
+
the type of the object or reference being initialized and the *source
|
| 3538 |
+
type* is the type of the initializer expression. If the initializer is
|
| 3539 |
+
not a single (possibly parenthesized) expression, the source type is not
|
| 3540 |
+
defined.
|
| 3541 |
+
|
| 3542 |
+
- If the initializer is a (non-parenthesized) *braced-init-list* or is
|
| 3543 |
+
`=` *braced-init-list*, the object or reference is list-initialized
|
| 3544 |
+
[[dcl.init.list]].
|
| 3545 |
+
- If the destination type is a reference type, see [[dcl.init.ref]].
|
| 3546 |
+
- If the destination type is an array of characters, an array of
|
| 3547 |
+
`char8_t`, an array of `char16_t`, an array of `char32_t`, or an array
|
| 3548 |
+
of `wchar_t`, and the initializer is a *string-literal*, see
|
| 3549 |
+
[[dcl.init.string]].
|
| 3550 |
+
- If the initializer is `()`, the object is value-initialized.
|
| 3551 |
+
- Otherwise, if the destination type is an array, the object is
|
| 3552 |
+
initialized as follows. Let x₁, …, xₖ be the elements of the
|
| 3553 |
+
*expression-list*. If the destination type is an array of unknown
|
| 3554 |
+
bound, it is defined as having k elements. Let n denote the array size
|
| 3555 |
+
after this potential adjustment. If k is greater than n, the program
|
| 3556 |
+
is ill-formed. Otherwise, the iᵗʰ array element is copy-initialized
|
| 3557 |
+
with xᵢ for each 1 ≤ i ≤ k, and value-initialized for each k < i ≤ n.
|
| 3558 |
+
For each 1 ≤ i < j ≤ n, every value computation and side effect
|
| 3559 |
+
associated with the initialization of the iᵗʰ element of the array is
|
| 3560 |
+
sequenced before those associated with the initialization of the jᵗʰ
|
| 3561 |
+
element.
|
| 3562 |
+
- Otherwise, if the destination type is a (possibly cv-qualified) class
|
| 3563 |
+
type:
|
| 3564 |
+
- If the initializer expression is a prvalue and the cv-unqualified
|
| 3565 |
+
version of the source type is the same class as the class of the
|
| 3566 |
+
destination, the initializer expression is used to initialize the
|
| 3567 |
+
destination object. \[*Example 3*: `T x = T(T(T()));` calls the `T`
|
| 3568 |
+
default constructor to initialize `x`. — *end example*]
|
| 3569 |
+
- Otherwise, if the initialization is direct-initialization, or if it
|
| 3570 |
+
is copy-initialization where the cv-unqualified version of the
|
| 3571 |
+
source type is the same class as, or a derived class of, the class
|
| 3572 |
+
of the destination, constructors are considered. The applicable
|
| 3573 |
+
constructors are enumerated [[over.match.ctor]], and the best one is
|
| 3574 |
+
chosen through overload resolution [[over.match]]. Then:
|
| 3575 |
+
- If overload resolution is successful, the selected constructor is
|
| 3576 |
+
called to initialize the object, with the initializer expression
|
| 3577 |
+
or *expression-list* as its argument(s).
|
| 3578 |
+
- Otherwise, if no constructor is viable, the destination type is an
|
| 3579 |
+
aggregate class, and the initializer is a parenthesized
|
| 3580 |
+
*expression-list*, the object is initialized as follows. Let e₁,
|
| 3581 |
+
…, eₙ be the elements of the aggregate [[dcl.init.aggr]]. Let x₁,
|
| 3582 |
+
…, xₖ be the elements of the *expression-list*. If k is greater
|
| 3583 |
+
than n, the program is ill-formed. The element eᵢ is
|
| 3584 |
+
copy-initialized with xᵢ for 1 ≤ i ≤ k. The remaining elements are
|
| 3585 |
+
initialized with their default member initializers, if any, and
|
| 3586 |
+
otherwise are value-initialized. For each 1 ≤ i < j ≤ n, every
|
| 3587 |
+
value computation and side effect associated with the
|
| 3588 |
+
initialization of eᵢ is sequenced before those associated with the
|
| 3589 |
+
initialization of eⱼ.
|
| 3590 |
+
\[*Note 7*:
|
| 3591 |
+
By contrast with direct-list-initialization, narrowing conversions
|
| 3592 |
+
[[dcl.init.list]] are permitted, designators are not permitted, a
|
| 3593 |
+
temporary object bound to a reference does not have its lifetime
|
| 3594 |
+
extended [[class.temporary]], and there is no brace elision.
|
| 3595 |
+
\[*Example 4*:
|
| 3596 |
+
``` cpp
|
| 3597 |
+
struct A {
|
| 3598 |
+
int a;
|
| 3599 |
+
int&& r;
|
| 3600 |
+
};
|
| 3601 |
+
|
| 3602 |
+
int f();
|
| 3603 |
+
int n = 10;
|
| 3604 |
+
|
| 3605 |
+
A a1{1, f()}; // OK, lifetime is extended
|
| 3606 |
+
A a2(1, f()); // well-formed, but dangling reference
|
| 3607 |
+
A a3{1.0, 1}; // error: narrowing conversion
|
| 3608 |
+
A a4(1.0, 1); // well-formed, but dangling reference
|
| 3609 |
+
A a5(1.0, std::move(n)); // OK
|
| 3610 |
+
```
|
| 3611 |
+
|
| 3612 |
+
— *end example*]
|
| 3613 |
+
— *end note*]
|
| 3614 |
+
- Otherwise, the initialization is ill-formed.
|
| 3615 |
+
- Otherwise (i.e., for the remaining copy-initialization cases),
|
| 3616 |
+
user-defined conversions that can convert from the source type to
|
| 3617 |
+
the destination type or (when a conversion function is used) to a
|
| 3618 |
+
derived class thereof are enumerated as described in
|
| 3619 |
+
[[over.match.copy]], and the best one is chosen through overload
|
| 3620 |
+
resolution [[over.match]]. If the conversion cannot be done or is
|
| 3621 |
+
ambiguous, the initialization is ill-formed. The function selected
|
| 3622 |
+
is called with the initializer expression as its argument; if the
|
| 3623 |
+
function is a constructor, the call is a prvalue of the
|
| 3624 |
+
cv-unqualified version of the destination type whose result object
|
| 3625 |
+
is initialized by the constructor. The call is used to
|
| 3626 |
+
direct-initialize, according to the rules above, the object that is
|
| 3627 |
+
the destination of the copy-initialization.
|
| 3628 |
+
- Otherwise, if the source type is a (possibly cv-qualified) class type,
|
| 3629 |
+
conversion functions are considered. The applicable conversion
|
| 3630 |
+
functions are enumerated [[over.match.conv]], and the best one is
|
| 3631 |
+
chosen through overload resolution [[over.match]]. The user-defined
|
| 3632 |
+
conversion so selected is called to convert the initializer expression
|
| 3633 |
+
into the object being initialized. If the conversion cannot be done or
|
| 3634 |
+
is ambiguous, the initialization is ill-formed.
|
| 3635 |
+
- Otherwise, if the initialization is direct-initialization, the source
|
| 3636 |
+
type is `std::nullptr_t`, and the destination type is `bool`, the
|
| 3637 |
+
initial value of the object being initialized is `false`.
|
| 3638 |
+
- Otherwise, the initial value of the object being initialized is the
|
| 3639 |
+
(possibly converted) value of the initializer expression. A standard
|
| 3640 |
+
conversion sequence [[conv]] will be used, if necessary, to convert
|
| 3641 |
+
the initializer expression to the cv-unqualified version of the
|
| 3642 |
+
destination type; no user-defined conversions are considered. If the
|
| 3643 |
+
conversion cannot be done, the initialization is ill-formed. When
|
| 3644 |
+
initializing a bit-field with a value that it cannot represent, the
|
| 3645 |
+
resulting value of the bit-field is *implementation-defined*.
|
| 3646 |
+
\[*Note 8*:
|
| 3647 |
+
An expression of type “*cv1* `T`” can initialize an object of type
|
| 3648 |
+
“*cv2* `T`” independently of the cv-qualifiers *cv1* and *cv2*.
|
| 3649 |
+
``` cpp
|
| 3650 |
+
int a;
|
| 3651 |
+
const int b = a;
|
| 3652 |
+
int c = b;
|
| 3653 |
+
```
|
| 3654 |
+
|
| 3655 |
+
— *end note*]
|
| 3656 |
+
|
| 3657 |
+
An *initializer-clause* followed by an ellipsis is a pack expansion
|
| 3658 |
+
[[temp.variadic]].
|
| 3659 |
+
|
| 3660 |
+
If the initializer is a parenthesized *expression-list*, the expressions
|
| 3661 |
+
are evaluated in the order specified for function calls [[expr.call]].
|
| 3662 |
+
|
| 3663 |
+
The same *identifier* shall not appear in multiple *designator*s of a
|
| 3664 |
+
*designated-initializer-list*.
|
| 3665 |
+
|
| 3666 |
+
An object whose initialization has completed is deemed to be
|
| 3667 |
+
constructed, even if the object is of non-class type or no constructor
|
| 3668 |
+
of the object’s class is invoked for the initialization.
|
| 3669 |
+
|
| 3670 |
+
[*Note 9*: Such an object might have been value-initialized or
|
| 3671 |
+
initialized by aggregate initialization [[dcl.init.aggr]] or by an
|
| 3672 |
+
inherited constructor [[class.inhctor.init]]. — *end note*]
|
| 3673 |
+
|
| 3674 |
+
Destroying an object of class type invokes the destructor of the class.
|
| 3675 |
+
Destroying a scalar type has no effect other than ending the lifetime of
|
| 3676 |
+
the object [[basic.life]]. Destroying an array destroys each element in
|
| 3677 |
+
reverse subscript order.
|
| 3678 |
+
|
| 3679 |
+
A declaration that specifies the initialization of a variable, whether
|
| 3680 |
+
from an explicit initializer or by default-initialization, is called the
|
| 3681 |
+
*initializing declaration* of that variable.
|
| 3682 |
+
|
| 3683 |
+
[*Note 10*: In most cases this is the defining declaration
|
| 3684 |
+
[[basic.def]] of the variable, but the initializing declaration of a
|
| 3685 |
+
non-inline static data member [[class.static.data]] might be the
|
| 3686 |
+
declaration within the class definition and not the definition at
|
| 3687 |
+
namespace scope. — *end note*]
|
| 3688 |
+
|
| 3689 |
+
### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
|
| 3690 |
+
|
| 3691 |
+
An *aggregate* is an array or a class [[class]] with
|
| 3692 |
+
|
| 3693 |
+
- no user-declared or inherited constructors [[class.ctor]],
|
| 3694 |
+
- no private or protected direct non-static data members
|
| 3695 |
+
[[class.access]],
|
| 3696 |
+
- no virtual functions [[class.virtual]], and
|
| 3697 |
+
- no virtual, private, or protected base classes [[class.mi]].
|
| 3698 |
+
|
| 3699 |
+
[*Note 1*: Aggregate initialization does not allow accessing protected
|
| 3700 |
+
and private base class’ members or constructors. — *end note*]
|
| 3701 |
+
|
| 3702 |
+
The *elements* of an aggregate are:
|
| 3703 |
+
|
| 3704 |
+
- for an array, the array elements in increasing subscript order, or
|
| 3705 |
+
- for a class, the direct base classes in declaration order, followed by
|
| 3706 |
+
the direct non-static data members [[class.mem]] that are not members
|
| 3707 |
+
of an anonymous union, in declaration order.
|
| 3708 |
+
|
| 3709 |
+
When an aggregate is initialized by an initializer list as specified in
|
| 3710 |
+
[[dcl.init.list]], the elements of the initializer list are taken as
|
| 3711 |
+
initializers for the elements of the aggregate. The *explicitly
|
| 3712 |
+
initialized elements* of the aggregate are determined as follows:
|
| 3713 |
+
|
| 3714 |
+
- If the initializer list is a *designated-initializer-list*, the
|
| 3715 |
+
aggregate shall be of class type, the *identifier* in each
|
| 3716 |
+
*designator* shall name a direct non-static data member of the class,
|
| 3717 |
+
and the explicitly initialized elements of the aggregate are the
|
| 3718 |
+
elements that are, or contain, those members.
|
| 3719 |
+
- If the initializer list is an *initializer-list*, the explicitly
|
| 3720 |
+
initialized elements of the aggregate are the first n elements of the
|
| 3721 |
+
aggregate, where n is the number of elements in the initializer list.
|
| 3722 |
+
- Otherwise, the initializer list must be `{}`, and there are no
|
| 3723 |
+
explicitly initialized elements.
|
| 3724 |
+
|
| 3725 |
+
For each explicitly initialized element:
|
| 3726 |
+
|
| 3727 |
+
- If the element is an anonymous union object and the initializer list
|
| 3728 |
+
is a *designated-initializer-list*, the anonymous union object is
|
| 3729 |
+
initialized by the *designated-initializer-list* `{ `*D*` }`, where
|
| 3730 |
+
*D* is the *designated-initializer-clause* naming a member of the
|
| 3731 |
+
anonymous union object. There shall be only one such
|
| 3732 |
+
*designated-initializer-clause*.
|
| 3733 |
+
\[*Example 1*:
|
| 3734 |
+
``` cpp
|
| 3735 |
+
struct C {
|
| 3736 |
+
union {
|
| 3737 |
+
int a;
|
| 3738 |
+
const char* p;
|
| 3739 |
+
};
|
| 3740 |
+
int x;
|
| 3741 |
+
} c = { .a = 1, .x = 3 };
|
| 3742 |
+
```
|
| 3743 |
+
|
| 3744 |
+
initializes `c.a` with 1 and `c.x` with 3.
|
| 3745 |
+
— *end example*]
|
| 3746 |
+
- Otherwise, the element is copy-initialized from the corresponding
|
| 3747 |
+
*initializer-clause* or is initialized with the
|
| 3748 |
+
*brace-or-equal-initializer* of the corresponding
|
| 3749 |
+
*designated-initializer-clause*. If that initializer is of the form
|
| 3750 |
+
*assignment-expression* or `= `*assignment-expression* and a narrowing
|
| 3751 |
+
conversion [[dcl.init.list]] is required to convert the expression,
|
| 3752 |
+
the program is ill-formed.
|
| 3753 |
+
\[*Note 2*: If an initializer is itself an initializer list, the
|
| 3754 |
+
element is list-initialized, which will result in a recursive
|
| 3755 |
+
application of the rules in this subclause if the element is an
|
| 3756 |
+
aggregate. — *end note*]
|
| 3757 |
+
\[*Example 2*:
|
| 3758 |
+
``` cpp
|
| 3759 |
+
struct A {
|
| 3760 |
+
int x;
|
| 3761 |
+
struct B {
|
| 3762 |
+
int i;
|
| 3763 |
+
int j;
|
| 3764 |
+
} b;
|
| 3765 |
+
} a = { 1, { 2, 3 } };
|
| 3766 |
+
```
|
| 3767 |
+
|
| 3768 |
+
initializes `a.x` with 1, `a.b.i` with 2, `a.b.j` with 3.
|
| 3769 |
+
``` cpp
|
| 3770 |
+
struct base1 { int b1, b2 = 42; };
|
| 3771 |
+
struct base2 {
|
| 3772 |
+
base2() {
|
| 3773 |
+
b3 = 42;
|
| 3774 |
+
}
|
| 3775 |
+
int b3;
|
| 3776 |
+
};
|
| 3777 |
+
struct derived : base1, base2 {
|
| 3778 |
+
int d;
|
| 3779 |
+
};
|
| 3780 |
+
|
| 3781 |
+
derived d1{{1, 2}, {}, 4};
|
| 3782 |
+
derived d2{{}, {}, 4};
|
| 3783 |
+
```
|
| 3784 |
+
|
| 3785 |
+
initializes `d1.b1` with 1, `d1.b2` with 2, `d1.b3` with 42, `d1.d`
|
| 3786 |
+
with 4, and `d2.b1` with 0, `d2.b2` with 42, `d2.b3` with 42, `d2.d`
|
| 3787 |
+
with 4.
|
| 3788 |
+
— *end example*]
|
| 3789 |
+
|
| 3790 |
+
For a non-union aggregate, each element that is not an explicitly
|
| 3791 |
+
initialized element is initialized as follows:
|
| 3792 |
+
|
| 3793 |
+
- If the element has a default member initializer [[class.mem]], the
|
| 3794 |
+
element is initialized from that initializer.
|
| 3795 |
+
- Otherwise, if the element is not a reference, the element is
|
| 3796 |
+
copy-initialized from an empty initializer list [[dcl.init.list]].
|
| 3797 |
+
- Otherwise, the program is ill-formed.
|
| 3798 |
+
|
| 3799 |
+
If the aggregate is a union and the initializer list is empty, then
|
| 3800 |
+
|
| 3801 |
+
- if any variant member has a default member initializer, that member is
|
| 3802 |
+
initialized from its default member initializer;
|
| 3803 |
+
- otherwise, the first member of the union (if any) is copy-initialized
|
| 3804 |
+
from an empty initializer list.
|
| 3805 |
+
|
| 3806 |
+
[*Example 3*:
|
| 3807 |
+
|
| 3808 |
+
``` cpp
|
| 3809 |
+
struct S { int a; const char* b; int c; int d = b[a]; };
|
| 3810 |
+
S ss = { 1, "asdf" };
|
| 3811 |
+
```
|
| 3812 |
+
|
| 3813 |
+
initializes `ss.a` with 1, `ss.b` with `"asdf"`, `ss.c` with the value
|
| 3814 |
+
of an expression of the form `int{}` (that is, `0`), and `ss.d` with the
|
| 3815 |
+
value of `ss.b[ss.a]` (that is, `'s'`), and in
|
| 3816 |
+
|
| 3817 |
+
``` cpp
|
| 3818 |
+
struct X { int i, j, k = 42; };
|
| 3819 |
+
X a[] = { 1, 2, 3, 4, 5, 6 };
|
| 3820 |
+
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
|
| 3821 |
+
```
|
| 3822 |
+
|
| 3823 |
+
`a` and `b` have the same value
|
| 3824 |
+
|
| 3825 |
+
``` cpp
|
| 3826 |
+
struct A {
|
| 3827 |
+
string a;
|
| 3828 |
+
int b = 42;
|
| 3829 |
+
int c = -1;
|
| 3830 |
+
};
|
| 3831 |
+
```
|
| 3832 |
+
|
| 3833 |
+
`A{.c=21}` has the following steps:
|
| 3834 |
+
|
| 3835 |
+
- Initialize `a` with `{}`
|
| 3836 |
+
- Initialize `b` with `= 42`
|
| 3837 |
+
- Initialize `c` with `= 21`
|
| 3838 |
+
|
| 3839 |
+
— *end example*]
|
| 3840 |
+
|
| 3841 |
+
The initializations of the elements of the aggregate are evaluated in
|
| 3842 |
+
the element order. That is, all value computations and side effects
|
| 3843 |
+
associated with a given element are sequenced before those of any
|
| 3844 |
+
element that follows it in order.
|
| 3845 |
+
|
| 3846 |
+
An aggregate that is a class can also be initialized with a single
|
| 3847 |
+
expression not enclosed in braces, as described in [[dcl.init]].
|
| 3848 |
+
|
| 3849 |
+
The destructor for each element of class type is potentially invoked
|
| 3850 |
+
[[class.dtor]] from the context where the aggregate initialization
|
| 3851 |
+
occurs.
|
| 3852 |
+
|
| 3853 |
+
[*Note 3*: This provision ensures that destructors can be called for
|
| 3854 |
+
fully-constructed subobjects in case an exception is thrown
|
| 3855 |
+
[[except.ctor]]. — *end note*]
|
| 3856 |
+
|
| 3857 |
+
An array of unknown bound initialized with a brace-enclosed
|
| 3858 |
+
*initializer-list* containing `n` *initializer-clause*s is defined as
|
| 3859 |
+
having `n` elements [[dcl.array]].
|
| 3860 |
+
|
| 3861 |
+
[*Example 4*:
|
| 3862 |
+
|
| 3863 |
+
``` cpp
|
| 3864 |
+
int x[] = { 1, 3, 5 };
|
| 3865 |
+
```
|
| 3866 |
+
|
| 3867 |
+
declares and initializes `x` as a one-dimensional array that has three
|
| 3868 |
+
elements since no size was specified and there are three initializers.
|
| 3869 |
+
|
| 3870 |
+
— *end example*]
|
| 3871 |
+
|
| 3872 |
+
An array of unknown bound shall not be initialized with an empty
|
| 3873 |
+
*braced-init-list* `{}`. [^6]
|
| 3874 |
+
|
| 3875 |
+
[*Note 4*:
|
| 3876 |
+
|
| 3877 |
+
A default member initializer does not determine the bound for a member
|
| 3878 |
+
array of unknown bound. Since the default member initializer is ignored
|
| 3879 |
+
if a suitable *mem-initializer* is present [[class.base.init]], the
|
| 3880 |
+
default member initializer is not considered to initialize the array of
|
| 3881 |
+
unknown bound.
|
| 3882 |
+
|
| 3883 |
+
[*Example 5*:
|
| 3884 |
+
|
| 3885 |
+
``` cpp
|
| 3886 |
+
struct S {
|
| 3887 |
+
int y[] = { 0 }; // error: non-static data member of incomplete type
|
| 3888 |
+
};
|
| 3889 |
+
```
|
| 3890 |
+
|
| 3891 |
+
— *end example*]
|
| 3892 |
+
|
| 3893 |
+
— *end note*]
|
| 3894 |
+
|
| 3895 |
+
[*Note 5*:
|
| 3896 |
+
|
| 3897 |
+
Static data members, non-static data members of anonymous union members,
|
| 3898 |
+
and unnamed bit-fields are not considered elements of the aggregate.
|
| 3899 |
+
|
| 3900 |
+
[*Example 6*:
|
| 3901 |
+
|
| 3902 |
+
``` cpp
|
| 3903 |
+
struct A {
|
| 3904 |
+
int i;
|
| 3905 |
+
static int s;
|
| 3906 |
+
int j;
|
| 3907 |
+
int :17;
|
| 3908 |
+
int k;
|
| 3909 |
+
} a = { 1, 2, 3 };
|
| 3910 |
+
```
|
| 3911 |
+
|
| 3912 |
+
Here, the second initializer 2 initializes `a.j` and not the static data
|
| 3913 |
+
member `A::s`, and the third initializer 3 initializes `a.k` and not the
|
| 3914 |
+
unnamed bit-field before it.
|
| 3915 |
+
|
| 3916 |
+
— *end example*]
|
| 3917 |
+
|
| 3918 |
+
— *end note*]
|
| 3919 |
+
|
| 3920 |
+
An *initializer-list* is ill-formed if the number of
|
| 3921 |
+
*initializer-clause*s exceeds the number of elements of the aggregate.
|
| 3922 |
+
|
| 3923 |
+
[*Example 7*:
|
| 3924 |
+
|
| 3925 |
+
``` cpp
|
| 3926 |
+
char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
|
| 3927 |
+
```
|
| 3928 |
+
|
| 3929 |
+
is ill-formed.
|
| 3930 |
+
|
| 3931 |
+
— *end example*]
|
| 3932 |
+
|
| 3933 |
+
If a member has a default member initializer and a potentially-evaluated
|
| 3934 |
+
subexpression thereof is an aggregate initialization that would use that
|
| 3935 |
+
default member initializer, the program is ill-formed.
|
| 3936 |
+
|
| 3937 |
+
[*Example 8*:
|
| 3938 |
+
|
| 3939 |
+
``` cpp
|
| 3940 |
+
struct A;
|
| 3941 |
+
extern A a;
|
| 3942 |
+
struct A {
|
| 3943 |
+
const A& a1 { A{a,a} }; // OK
|
| 3944 |
+
const A& a2 { A{} }; // error
|
| 3945 |
+
};
|
| 3946 |
+
A a{a,a}; // OK
|
| 3947 |
+
|
| 3948 |
+
struct B {
|
| 3949 |
+
int n = B{}.n; // error
|
| 3950 |
+
};
|
| 3951 |
+
```
|
| 3952 |
+
|
| 3953 |
+
— *end example*]
|
| 3954 |
+
|
| 3955 |
+
If an aggregate class `C` contains a subaggregate element `e` with no
|
| 3956 |
+
elements, the *initializer-clause* for `e` shall not be omitted from an
|
| 3957 |
+
*initializer-list* for an object of type `C` unless the
|
| 3958 |
+
*initializer-clause*s for all elements of `C` following `e` are also
|
| 3959 |
+
omitted.
|
| 3960 |
+
|
| 3961 |
+
[*Example 9*:
|
| 3962 |
+
|
| 3963 |
+
``` cpp
|
| 3964 |
+
struct S { } s;
|
| 3965 |
+
struct A {
|
| 3966 |
+
S s1;
|
| 3967 |
+
int i1;
|
| 3968 |
+
S s2;
|
| 3969 |
+
int i2;
|
| 3970 |
+
S s3;
|
| 3971 |
+
int i3;
|
| 3972 |
+
} a = {
|
| 3973 |
+
{ }, // Required initialization
|
| 3974 |
+
0,
|
| 3975 |
+
s, // Required initialization
|
| 3976 |
+
0
|
| 3977 |
+
}; // Initialization not required for A::s3 because A::i3 is also not initialized
|
| 3978 |
+
```
|
| 3979 |
+
|
| 3980 |
+
— *end example*]
|
| 3981 |
+
|
| 3982 |
+
When initializing a multi-dimensional array, the *initializer-clause*s
|
| 3983 |
+
initialize the elements with the last (rightmost) index of the array
|
| 3984 |
+
varying the fastest [[dcl.array]].
|
| 3985 |
+
|
| 3986 |
+
[*Example 10*:
|
| 3987 |
+
|
| 3988 |
+
``` cpp
|
| 3989 |
+
int x[2][2] = { 3, 1, 4, 2 };
|
| 3990 |
+
```
|
| 3991 |
+
|
| 3992 |
+
initializes `x[0][0]` to `3`, `x[0][1]` to `1`, `x[1][0]` to `4`, and
|
| 3993 |
+
`x[1][1]` to `2`. On the other hand,
|
| 3994 |
+
|
| 3995 |
+
``` cpp
|
| 3996 |
+
float y[4][3] = {
|
| 3997 |
+
{ 1 }, { 2 }, { 3 }, { 4 }
|
| 3998 |
+
};
|
| 3999 |
+
```
|
| 4000 |
+
|
| 4001 |
+
initializes the first column of `y` (regarded as a two-dimensional
|
| 4002 |
+
array) and leaves the rest zero.
|
| 4003 |
+
|
| 4004 |
+
— *end example*]
|
| 4005 |
+
|
| 4006 |
+
Braces can be elided in an *initializer-list* as follows. If the
|
| 4007 |
+
*initializer-list* begins with a left brace, then the succeeding
|
| 4008 |
+
comma-separated list of *initializer-clause*s initializes the elements
|
| 4009 |
+
of a subaggregate; it is erroneous for there to be more
|
| 4010 |
+
*initializer-clause*s than elements. If, however, the *initializer-list*
|
| 4011 |
+
for a subaggregate does not begin with a left brace, then only enough
|
| 4012 |
+
*initializer-clause*s from the list are taken to initialize the elements
|
| 4013 |
+
of the subaggregate; any remaining *initializer-clause*s are left to
|
| 4014 |
+
initialize the next element of the aggregate of which the current
|
| 4015 |
+
subaggregate is an element.
|
| 4016 |
+
|
| 4017 |
+
[*Example 11*:
|
| 4018 |
+
|
| 4019 |
+
``` cpp
|
| 4020 |
+
float y[4][3] = {
|
| 4021 |
+
{ 1, 3, 5 },
|
| 4022 |
+
{ 2, 4, 6 },
|
| 4023 |
+
{ 3, 5, 7 },
|
| 4024 |
+
};
|
| 4025 |
+
```
|
| 4026 |
+
|
| 4027 |
+
is a completely-braced initialization: 1, 3, and 5 initialize the first
|
| 4028 |
+
row of the array `y[0]`, namely `y[0][0]`, `y[0][1]`, and `y[0][2]`.
|
| 4029 |
+
Likewise the next two lines initialize `y[1]` and `y[2]`. The
|
| 4030 |
+
initializer ends early and therefore `y[3]`s elements are initialized as
|
| 4031 |
+
if explicitly initialized with an expression of the form `float()`, that
|
| 4032 |
+
is, are initialized with `0.0`. In the following example, braces in the
|
| 4033 |
+
*initializer-list* are elided; however the *initializer-list* has the
|
| 4034 |
+
same effect as the completely-braced *initializer-list* of the above
|
| 4035 |
+
example,
|
| 4036 |
+
|
| 4037 |
+
``` cpp
|
| 4038 |
+
float y[4][3] = {
|
| 4039 |
+
1, 3, 5, 2, 4, 6, 3, 5, 7
|
| 4040 |
+
};
|
| 4041 |
+
```
|
| 4042 |
+
|
| 4043 |
+
The initializer for `y` begins with a left brace, but the one for `y[0]`
|
| 4044 |
+
does not, therefore three elements from the list are used. Likewise the
|
| 4045 |
+
next three are taken successively for `y[1]` and `y[2]`.
|
| 4046 |
+
|
| 4047 |
+
— *end example*]
|
| 4048 |
+
|
| 4049 |
+
All implicit type conversions [[conv]] are considered when initializing
|
| 4050 |
+
the element with an *assignment-expression*. If the
|
| 4051 |
+
*assignment-expression* can initialize an element, the element is
|
| 4052 |
+
initialized. Otherwise, if the element is itself a subaggregate, brace
|
| 4053 |
+
elision is assumed and the *assignment-expression* is considered for the
|
| 4054 |
+
initialization of the first element of the subaggregate.
|
| 4055 |
+
|
| 4056 |
+
[*Note 6*: As specified above, brace elision cannot apply to
|
| 4057 |
+
subaggregates with no elements; an *initializer-clause* for the entire
|
| 4058 |
+
subobject is required. — *end note*]
|
| 4059 |
+
|
| 4060 |
+
[*Example 12*:
|
| 4061 |
+
|
| 4062 |
+
``` cpp
|
| 4063 |
+
struct A {
|
| 4064 |
+
int i;
|
| 4065 |
+
operator int();
|
| 4066 |
+
};
|
| 4067 |
+
struct B {
|
| 4068 |
+
A a1, a2;
|
| 4069 |
+
int z;
|
| 4070 |
+
};
|
| 4071 |
+
A a;
|
| 4072 |
+
B b = { 4, a, a };
|
| 4073 |
+
```
|
| 4074 |
+
|
| 4075 |
+
Braces are elided around the *initializer-clause* for `b.a1.i`. `b.a1.i`
|
| 4076 |
+
is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
|
| 4077 |
+
initialized with whatever `a.operator int()` returns.
|
| 4078 |
+
|
| 4079 |
+
— *end example*]
|
| 4080 |
+
|
| 4081 |
+
[*Note 7*: An aggregate array or an aggregate class may contain
|
| 4082 |
+
elements of a class type with a user-declared constructor
|
| 4083 |
+
[[class.ctor]]. Initialization of these aggregate objects is described
|
| 4084 |
+
in [[class.expl.init]]. — *end note*]
|
| 4085 |
+
|
| 4086 |
+
[*Note 8*: Whether the initialization of aggregates with static storage
|
| 4087 |
+
duration is static or dynamic is specified in [[basic.start.static]],
|
| 4088 |
+
[[basic.start.dynamic]], and [[stmt.dcl]]. — *end note*]
|
| 4089 |
+
|
| 4090 |
+
When a union is initialized with an initializer list, there shall not be
|
| 4091 |
+
more than one explicitly initialized element.
|
| 4092 |
+
|
| 4093 |
+
[*Example 13*:
|
| 4094 |
+
|
| 4095 |
+
``` cpp
|
| 4096 |
+
union u { int a; const char* b; };
|
| 4097 |
+
u a = { 1 };
|
| 4098 |
+
u b = a;
|
| 4099 |
+
u c = 1; // error
|
| 4100 |
+
u d = { 0, "asdf" }; // error
|
| 4101 |
+
u e = { "asdf" }; // error
|
| 4102 |
+
u f = { .b = "asdf" };
|
| 4103 |
+
u g = { .a = 1, .b = "asdf" }; // error
|
| 4104 |
+
```
|
| 4105 |
+
|
| 4106 |
+
— *end example*]
|
| 4107 |
+
|
| 4108 |
+
[*Note 9*: As described above, the braces around the
|
| 4109 |
+
*initializer-clause* for a union member can be omitted if the union is a
|
| 4110 |
+
member of another aggregate. — *end note*]
|
| 4111 |
+
|
| 4112 |
+
### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
|
| 4113 |
+
|
| 4114 |
+
An array of ordinary character type [[basic.fundamental]], `char8_t`
|
| 4115 |
+
array, `char16_t` array, `char32_t` array, or `wchar_t` array can be
|
| 4116 |
+
initialized by an ordinary string literal, UTF-8 string literal, UTF-16
|
| 4117 |
+
string literal, UTF-32 string literal, or wide string literal,
|
| 4118 |
+
respectively, or by an appropriately-typed *string-literal* enclosed in
|
| 4119 |
+
braces [[lex.string]]. Successive characters of the value of the
|
| 4120 |
+
*string-literal* initialize the elements of the array.
|
| 4121 |
+
|
| 4122 |
+
[*Example 1*:
|
| 4123 |
+
|
| 4124 |
+
``` cpp
|
| 4125 |
+
char msg[] = "Syntax error on line %s\n";
|
| 4126 |
+
```
|
| 4127 |
+
|
| 4128 |
+
shows a character array whose members are initialized with a
|
| 4129 |
+
*string-literal*. Note that because `'\n'` is a single character and
|
| 4130 |
+
because a trailing `'\0'` is appended, `sizeof(msg)` is `25`.
|
| 4131 |
+
|
| 4132 |
+
— *end example*]
|
| 4133 |
+
|
| 4134 |
+
There shall not be more initializers than there are array elements.
|
| 4135 |
+
|
| 4136 |
+
[*Example 2*:
|
| 4137 |
+
|
| 4138 |
+
``` cpp
|
| 4139 |
+
char cv[4] = "asdf"; // error
|
| 4140 |
+
```
|
| 4141 |
+
|
| 4142 |
+
is ill-formed since there is no space for the implied trailing `'\0'`.
|
| 4143 |
+
|
| 4144 |
+
— *end example*]
|
| 4145 |
+
|
| 4146 |
+
If there are fewer initializers than there are array elements, each
|
| 4147 |
+
element not explicitly initialized shall be zero-initialized
|
| 4148 |
+
[[dcl.init]].
|
| 4149 |
+
|
| 4150 |
+
### References <a id="dcl.init.ref">[[dcl.init.ref]]</a>
|
| 4151 |
+
|
| 4152 |
+
A variable whose declared type is “reference to type `T`” [[dcl.ref]]
|
| 4153 |
+
shall be initialized.
|
| 4154 |
+
|
| 4155 |
+
[*Example 1*:
|
| 4156 |
+
|
| 4157 |
+
``` cpp
|
| 4158 |
+
int g(int) noexcept;
|
| 4159 |
+
void f() {
|
| 4160 |
+
int i;
|
| 4161 |
+
int& r = i; // r refers to i
|
| 4162 |
+
r = 1; // the value of i becomes 1
|
| 4163 |
+
int* p = &r; // p points to i
|
| 4164 |
+
int& rr = r; // rr refers to what r refers to, that is, to i
|
| 4165 |
+
int (&rg)(int) = g; // rg refers to the function g
|
| 4166 |
+
rg(i); // calls function g
|
| 4167 |
+
int a[3];
|
| 4168 |
+
int (&ra)[3] = a; // ra refers to the array a
|
| 4169 |
+
ra[1] = i; // modifies a[1]
|
| 4170 |
+
}
|
| 4171 |
+
```
|
| 4172 |
+
|
| 4173 |
+
— *end example*]
|
| 4174 |
+
|
| 4175 |
+
A reference cannot be changed to refer to another object after
|
| 4176 |
+
initialization.
|
| 4177 |
+
|
| 4178 |
+
[*Note 1*: Assignment to a reference assigns to the object referred to
|
| 4179 |
+
by the reference [[expr.ass]]. — *end note*]
|
| 4180 |
+
|
| 4181 |
+
Argument passing [[expr.call]] and function value return [[stmt.return]]
|
| 4182 |
+
are initializations.
|
| 4183 |
+
|
| 4184 |
+
The initializer can be omitted for a reference only in a parameter
|
| 4185 |
+
declaration [[dcl.fct]], in the declaration of a function return type,
|
| 4186 |
+
in the declaration of a class member within its class definition
|
| 4187 |
+
[[class.mem]], and where the `extern` specifier is explicitly used.
|
| 4188 |
+
|
| 4189 |
+
[*Example 2*:
|
| 4190 |
+
|
| 4191 |
+
``` cpp
|
| 4192 |
+
int& r1; // error: initializer missing
|
| 4193 |
+
extern int& r2; // OK
|
| 4194 |
+
```
|
| 4195 |
+
|
| 4196 |
+
— *end example*]
|
| 4197 |
+
|
| 4198 |
+
Given types “*cv1* `T1`” and “*cv2* `T2`”, “*cv1* `T1`” is
|
| 4199 |
+
*reference-related* to “*cv2* `T2`” if `T1` is similar [[conv.qual]] to
|
| 4200 |
+
`T2`, or `T1` is a base class of `T2`. “*cv1* `T1`” is
|
| 4201 |
+
*reference-compatible* with “*cv2* `T2`” if a prvalue of type “pointer
|
| 4202 |
+
to *cv2* `T2`” can be converted to the type “pointer to *cv1* `T1`” via
|
| 4203 |
+
a standard conversion sequence [[conv]]. In all cases where the
|
| 4204 |
+
reference-compatible relationship of two types is used to establish the
|
| 4205 |
+
validity of a reference binding and the standard conversion sequence
|
| 4206 |
+
would be ill-formed, a program that necessitates such a binding is
|
| 4207 |
+
ill-formed.
|
| 4208 |
+
|
| 4209 |
+
A reference to type “*cv1* `T1`” is initialized by an expression of type
|
| 4210 |
+
“*cv2* `T2`” as follows:
|
| 4211 |
+
|
| 4212 |
+
- If the reference is an lvalue reference and the initializer expression
|
| 4213 |
+
- is an lvalue (but is not a bit-field), and “*cv1* `T1`” is
|
| 4214 |
+
reference-compatible with “*cv2* `T2`”, or
|
| 4215 |
+
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 4216 |
+
reference-related to `T2`, and can be converted to an lvalue of type
|
| 4217 |
+
“*cv3* `T3`”, where “*cv1* `T1`” is reference-compatible with “*cv3*
|
| 4218 |
+
`T3`”[^7] (this conversion is selected by enumerating the applicable
|
| 4219 |
+
conversion functions [[over.match.ref]] and choosing the best one
|
| 4220 |
+
through overload resolution [[over.match]]),
|
| 4221 |
+
|
| 4222 |
+
then the reference is bound to the initializer expression lvalue in
|
| 4223 |
+
the first case and to the lvalue result of the conversion in the
|
| 4224 |
+
second case (or, in either case, to the appropriate base class
|
| 4225 |
+
subobject of the object).
|
| 4226 |
+
\[*Note 2*: The usual lvalue-to-rvalue [[conv.lval]], array-to-pointer
|
| 4227 |
+
[[conv.array]], and function-to-pointer [[conv.func]] standard
|
| 4228 |
+
conversions are not needed, and therefore are suppressed, when such
|
| 4229 |
+
direct bindings to lvalues are done. — *end note*]
|
| 4230 |
+
\[*Example 3*:
|
| 4231 |
+
``` cpp
|
| 4232 |
+
double d = 2.0;
|
| 4233 |
+
double& rd = d; // rd refers to d
|
| 4234 |
+
const double& rcd = d; // rcd refers to d
|
| 4235 |
+
|
| 4236 |
+
struct A { };
|
| 4237 |
+
struct B : A { operator int&(); } b;
|
| 4238 |
+
A& ra = b; // ra refers to A subobject in b
|
| 4239 |
+
const A& rca = b; // rca refers to A subobject in b
|
| 4240 |
+
int& ir = B(); // ir refers to the result of B::operator int&
|
| 4241 |
+
```
|
| 4242 |
+
|
| 4243 |
+
— *end example*]
|
| 4244 |
+
- Otherwise, if the reference is an lvalue reference to a type that is
|
| 4245 |
+
not const-qualified or is volatile-qualified, the program is
|
| 4246 |
+
ill-formed.
|
| 4247 |
+
\[*Example 4*:
|
| 4248 |
+
``` cpp
|
| 4249 |
+
double& rd2 = 2.0; // error: not an lvalue and reference not const
|
| 4250 |
+
int i = 2;
|
| 4251 |
+
double& rd3 = i; // error: type mismatch and reference not const
|
| 4252 |
+
```
|
| 4253 |
+
|
| 4254 |
+
— *end example*]
|
| 4255 |
+
- Otherwise, if the initializer expression
|
| 4256 |
+
- is an rvalue (but not a bit-field) or function lvalue and “*cv1*
|
| 4257 |
+
`T1`” is reference-compatible with “*cv2* `T2`”, or
|
| 4258 |
+
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 4259 |
+
reference-related to `T2`, and can be converted to an rvalue or
|
| 4260 |
+
function lvalue of type “*cv3* `T3`”, where “*cv1* `T1`” is
|
| 4261 |
+
reference-compatible with “*cv3* `T3`” (see [[over.match.ref]]),
|
| 4262 |
+
|
| 4263 |
+
then the value of the initializer expression in the first case and the
|
| 4264 |
+
result of the conversion in the second case is called the converted
|
| 4265 |
+
initializer. If the converted initializer is a prvalue, its type `T4`
|
| 4266 |
+
is adjusted to type “*cv1* `T4`” [[conv.qual]] and the temporary
|
| 4267 |
+
materialization conversion [[conv.rval]] is applied. In any case, the
|
| 4268 |
+
reference is bound to the resulting glvalue (or to an appropriate base
|
| 4269 |
+
class subobject).
|
| 4270 |
+
\[*Example 5*:
|
| 4271 |
+
``` cpp
|
| 4272 |
+
struct A { };
|
| 4273 |
+
struct B : A { } b;
|
| 4274 |
+
extern B f();
|
| 4275 |
+
const A& rca2 = f(); // bound to the A subobject of the B rvalue.
|
| 4276 |
+
A&& rra = f(); // same as above
|
| 4277 |
+
struct X {
|
| 4278 |
+
operator B();
|
| 4279 |
+
operator int&();
|
| 4280 |
+
} x;
|
| 4281 |
+
const A& r = x; // bound to the A subobject of the result of the conversion
|
| 4282 |
+
int i2 = 42;
|
| 4283 |
+
int&& rri = static_cast<int&&>(i2); // bound directly to i2
|
| 4284 |
+
B&& rrb = x; // bound directly to the result of operator B
|
| 4285 |
+
```
|
| 4286 |
+
|
| 4287 |
+
— *end example*]
|
| 4288 |
+
- Otherwise:
|
| 4289 |
+
- If `T1` or `T2` is a class type and `T1` is not reference-related to
|
| 4290 |
+
`T2`, user-defined conversions are considered using the rules for
|
| 4291 |
+
copy-initialization of an object of type “*cv1* `T1`” by
|
| 4292 |
+
user-defined conversion ([[dcl.init]], [[over.match.copy]],
|
| 4293 |
+
[[over.match.conv]]); the program is ill-formed if the corresponding
|
| 4294 |
+
non-reference copy-initialization would be ill-formed. The result of
|
| 4295 |
+
the call to the conversion function, as described for the
|
| 4296 |
+
non-reference copy-initialization, is then used to direct-initialize
|
| 4297 |
+
the reference. For this direct-initialization, user-defined
|
| 4298 |
+
conversions are not considered.
|
| 4299 |
+
- Otherwise, the initializer expression is implicitly converted to a
|
| 4300 |
+
prvalue of type “*cv1* `T1`”. The temporary materialization
|
| 4301 |
+
conversion is applied and the reference is bound to the result.
|
| 4302 |
+
|
| 4303 |
+
If `T1` is reference-related to `T2`:
|
| 4304 |
+
- *cv1* shall be the same cv-qualification as, or greater
|
| 4305 |
+
cv-qualification than, *cv2*; and
|
| 4306 |
+
- if the reference is an rvalue reference, the initializer expression
|
| 4307 |
+
shall not be an lvalue.
|
| 4308 |
+
|
| 4309 |
+
\[*Example 6*:
|
| 4310 |
+
``` cpp
|
| 4311 |
+
struct Banana { };
|
| 4312 |
+
struct Enigma { operator const Banana(); };
|
| 4313 |
+
struct Alaska { operator Banana&(); };
|
| 4314 |
+
void enigmatic() {
|
| 4315 |
+
typedef const Banana ConstBanana;
|
| 4316 |
+
Banana &&banana1 = ConstBanana(); // error
|
| 4317 |
+
Banana &&banana2 = Enigma(); // error
|
| 4318 |
+
Banana &&banana3 = Alaska(); // error
|
| 4319 |
+
}
|
| 4320 |
+
|
| 4321 |
+
const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
|
| 4322 |
+
double&& rrd = 2; // rrd refers to temporary with value 2.0
|
| 4323 |
+
const volatile int cvi = 1;
|
| 4324 |
+
const int& r2 = cvi; // error: cv-qualifier dropped
|
| 4325 |
+
struct A { operator volatile int&(); } a;
|
| 4326 |
+
const int& r3 = a; // error: cv-qualifier dropped
|
| 4327 |
+
// from result of conversion function
|
| 4328 |
+
double d2 = 1.0;
|
| 4329 |
+
double&& rrd2 = d2; // error: initializer is lvalue of related type
|
| 4330 |
+
struct X { operator int&(); };
|
| 4331 |
+
int&& rri2 = X(); // error: result of conversion function is lvalue of related type
|
| 4332 |
+
int i3 = 2;
|
| 4333 |
+
double&& rrd3 = i3; // rrd3 refers to temporary with value 2.0
|
| 4334 |
+
```
|
| 4335 |
+
|
| 4336 |
+
— *end example*]
|
| 4337 |
+
|
| 4338 |
+
In all cases except the last (i.e., implicitly converting the
|
| 4339 |
+
initializer expression to the referenced type), the reference is said to
|
| 4340 |
+
*bind directly* to the initializer expression.
|
| 4341 |
+
|
| 4342 |
+
[*Note 3*: [[class.temporary]] describes the lifetime of temporaries
|
| 4343 |
+
bound to references. — *end note*]
|
| 4344 |
+
|
| 4345 |
+
### List-initialization <a id="dcl.init.list">[[dcl.init.list]]</a>
|
| 4346 |
+
|
| 4347 |
+
*List-initialization* is initialization of an object or reference from a
|
| 4348 |
+
*braced-init-list*. Such an initializer is called an *initializer list*,
|
| 4349 |
+
and the comma-separated *initializer-clause*s of the *initializer-list*
|
| 4350 |
+
or *designated-initializer-clause*s of the *designated-initializer-list*
|
| 4351 |
+
are called the *elements* of the initializer list. An initializer list
|
| 4352 |
+
may be empty. List-initialization can occur in direct-initialization or
|
| 4353 |
+
copy-initialization contexts; list-initialization in a
|
| 4354 |
+
direct-initialization context is called *direct-list-initialization* and
|
| 4355 |
+
list-initialization in a copy-initialization context is called
|
| 4356 |
+
*copy-list-initialization*.
|
| 4357 |
+
|
| 4358 |
+
[*Note 1*:
|
| 4359 |
+
|
| 4360 |
+
List-initialization can be used
|
| 4361 |
+
|
| 4362 |
+
- as the initializer in a variable definition [[dcl.init]]
|
| 4363 |
+
- as the initializer in a *new-expression* [[expr.new]]
|
| 4364 |
+
- in a `return` statement [[stmt.return]]
|
| 4365 |
+
- as a *for-range-initializer* [[stmt.iter]]
|
| 4366 |
+
- as a function argument [[expr.call]]
|
| 4367 |
+
- as a subscript [[expr.sub]]
|
| 4368 |
+
- as an argument to a constructor invocation ([[dcl.init]],
|
| 4369 |
+
[[expr.type.conv]])
|
| 4370 |
+
- as an initializer for a non-static data member [[class.mem]]
|
| 4371 |
+
- in a *mem-initializer* [[class.base.init]]
|
| 4372 |
+
- on the right-hand side of an assignment [[expr.ass]]
|
| 4373 |
+
|
| 4374 |
+
[*Example 1*:
|
| 4375 |
+
|
| 4376 |
+
``` cpp
|
| 4377 |
+
int a = {1};
|
| 4378 |
+
std::complex<double> z{1,2};
|
| 4379 |
+
new std::vector<std::string>{"once", "upon", "a", "time"}; // 4 string elements
|
| 4380 |
+
f( {"Nicholas","Annemarie"} ); // pass list of two elements
|
| 4381 |
+
return { "Norah" }; // return list of one element
|
| 4382 |
+
int* e {}; // initialization to zero / null pointer
|
| 4383 |
+
x = double{1}; // explicitly construct a double
|
| 4384 |
+
std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
|
| 4385 |
+
```
|
| 4386 |
+
|
| 4387 |
+
— *end example*]
|
| 4388 |
+
|
| 4389 |
+
— *end note*]
|
| 4390 |
+
|
| 4391 |
+
A constructor is an *initializer-list constructor* if its first
|
| 4392 |
+
parameter is of type `std::initializer_list<E>` or reference to
|
| 4393 |
+
cv `std::initializer_list<E>` for some type `E`, and either there are no
|
| 4394 |
+
other parameters or else all other parameters have default arguments
|
| 4395 |
+
[[dcl.fct.default]].
|
| 4396 |
+
|
| 4397 |
+
[*Note 2*: Initializer-list constructors are favored over other
|
| 4398 |
+
constructors in list-initialization [[over.match.list]]. Passing an
|
| 4399 |
+
initializer list as the argument to the constructor template
|
| 4400 |
+
`template<class T> C(T)` of a class `C` does not create an
|
| 4401 |
+
initializer-list constructor, because an initializer list argument
|
| 4402 |
+
causes the corresponding parameter to be a non-deduced context
|
| 4403 |
+
[[temp.deduct.call]]. — *end note*]
|
| 4404 |
+
|
| 4405 |
+
The template `std::initializer_list` is not predefined; if the header
|
| 4406 |
+
`<initializer_list>` is not imported or included prior to a use of
|
| 4407 |
+
`std::initializer_list` — even an implicit use in which the type is not
|
| 4408 |
+
named [[dcl.spec.auto]] — the program is ill-formed.
|
| 4409 |
+
|
| 4410 |
+
List-initialization of an object or reference of type `T` is defined as
|
| 4411 |
+
follows:
|
| 4412 |
+
|
| 4413 |
+
- If the *braced-init-list* contains a *designated-initializer-list*,
|
| 4414 |
+
`T` shall be an aggregate class. The ordered *identifier*s in the
|
| 4415 |
+
*designator*s of the *designated-initializer-list* shall form a
|
| 4416 |
+
subsequence of the ordered *identifier*s in the direct non-static data
|
| 4417 |
+
members of `T`. Aggregate initialization is performed
|
| 4418 |
+
[[dcl.init.aggr]].
|
| 4419 |
+
\[*Example 2*:
|
| 4420 |
+
``` cpp
|
| 4421 |
+
struct A { int x; int y; int z; };
|
| 4422 |
+
A a{.y = 2, .x = 1}; // error: designator order does not match declaration order
|
| 4423 |
+
A b{.x = 1, .z = 2}; // OK, b.y initialized to 0
|
| 4424 |
+
```
|
| 4425 |
+
|
| 4426 |
+
— *end example*]
|
| 4427 |
+
- If `T` is an aggregate class and the initializer list has a single
|
| 4428 |
+
element of type *cv* `U`, where `U` is `T` or a class derived from
|
| 4429 |
+
`T`, the object is initialized from that element (by
|
| 4430 |
+
copy-initialization for copy-list-initialization, or by
|
| 4431 |
+
direct-initialization for direct-list-initialization).
|
| 4432 |
+
- Otherwise, if `T` is a character array and the initializer list has a
|
| 4433 |
+
single element that is an appropriately-typed *string-literal*
|
| 4434 |
+
[[dcl.init.string]], initialization is performed as described in that
|
| 4435 |
+
subclause.
|
| 4436 |
+
- Otherwise, if `T` is an aggregate, aggregate initialization is
|
| 4437 |
+
performed [[dcl.init.aggr]].
|
| 4438 |
+
\[*Example 3*:
|
| 4439 |
+
``` cpp
|
| 4440 |
+
double ad[] = { 1, 2.0 }; // OK
|
| 4441 |
+
int ai[] = { 1, 2.0 }; // error: narrowing
|
| 4442 |
+
|
| 4443 |
+
struct S2 {
|
| 4444 |
+
int m1;
|
| 4445 |
+
double m2, m3;
|
| 4446 |
+
};
|
| 4447 |
+
S2 s21 = { 1, 2, 3.0 }; // OK
|
| 4448 |
+
S2 s22 { 1.0, 2, 3 }; // error: narrowing
|
| 4449 |
+
S2 s23 { }; // OK: default to 0,0,0
|
| 4450 |
+
```
|
| 4451 |
+
|
| 4452 |
+
— *end example*]
|
| 4453 |
+
- Otherwise, if the initializer list has no elements and `T` is a class
|
| 4454 |
+
type with a default constructor, the object is value-initialized.
|
| 4455 |
+
- Otherwise, if `T` is a specialization of `std::initializer_list<E>`,
|
| 4456 |
+
the object is constructed as described below.
|
| 4457 |
+
- Otherwise, if `T` is a class type, constructors are considered. The
|
| 4458 |
+
applicable constructors are enumerated and the best one is chosen
|
| 4459 |
+
through overload resolution ([[over.match]], [[over.match.list]]). If
|
| 4460 |
+
a narrowing conversion (see below) is required to convert any of the
|
| 4461 |
+
arguments, the program is ill-formed.
|
| 4462 |
+
\[*Example 4*:
|
| 4463 |
+
``` cpp
|
| 4464 |
+
struct S {
|
| 4465 |
+
S(std::initializer_list<double>); // #1
|
| 4466 |
+
S(std::initializer_list<int>); // #2
|
| 4467 |
+
S(); // #3
|
| 4468 |
+
// ...
|
| 4469 |
+
};
|
| 4470 |
+
S s1 = { 1.0, 2.0, 3.0 }; // invoke #1
|
| 4471 |
+
S s2 = { 1, 2, 3 }; // invoke #2
|
| 4472 |
+
S s3 = { }; // invoke #3
|
| 4473 |
+
```
|
| 4474 |
+
|
| 4475 |
+
— *end example*]
|
| 4476 |
+
\[*Example 5*:
|
| 4477 |
+
``` cpp
|
| 4478 |
+
struct Map {
|
| 4479 |
+
Map(std::initializer_list<std::pair<std::string,int>>);
|
| 4480 |
+
};
|
| 4481 |
+
Map ship = {{"Sophie",14}, {"Surprise",28}};
|
| 4482 |
+
```
|
| 4483 |
+
|
| 4484 |
+
— *end example*]
|
| 4485 |
+
\[*Example 6*:
|
| 4486 |
+
``` cpp
|
| 4487 |
+
struct S {
|
| 4488 |
+
// no initializer-list constructors
|
| 4489 |
+
S(int, double, double); // #1
|
| 4490 |
+
S(); // #2
|
| 4491 |
+
// ...
|
| 4492 |
+
};
|
| 4493 |
+
S s1 = { 1, 2, 3.0 }; // OK: invoke #1
|
| 4494 |
+
S s2 { 1.0, 2, 3 }; // error: narrowing
|
| 4495 |
+
S s3 { }; // OK: invoke #2
|
| 4496 |
+
```
|
| 4497 |
+
|
| 4498 |
+
— *end example*]
|
| 4499 |
+
- Otherwise, if `T` is an enumeration with a fixed underlying type
|
| 4500 |
+
[[dcl.enum]] `U`, the *initializer-list* has a single element `v`, `v`
|
| 4501 |
+
can be implicitly converted to `U`, and the initialization is
|
| 4502 |
+
direct-list-initialization, the object is initialized with the value
|
| 4503 |
+
`T(v)` [[expr.type.conv]]; if a narrowing conversion is required to
|
| 4504 |
+
convert `v` to `U`, the program is ill-formed.
|
| 4505 |
+
\[*Example 7*:
|
| 4506 |
+
``` cpp
|
| 4507 |
+
enum byte : unsigned char { };
|
| 4508 |
+
byte b { 42 }; // OK
|
| 4509 |
+
byte c = { 42 }; // error
|
| 4510 |
+
byte d = byte{ 42 }; // OK; same value as b
|
| 4511 |
+
byte e { -1 }; // error
|
| 4512 |
+
|
| 4513 |
+
struct A { byte b; };
|
| 4514 |
+
A a1 = { { 42 } }; // error
|
| 4515 |
+
A a2 = { byte{ 42 } }; // OK
|
| 4516 |
+
|
| 4517 |
+
void f(byte);
|
| 4518 |
+
f({ 42 }); // error
|
| 4519 |
+
|
| 4520 |
+
enum class Handle : uint32_t { Invalid = 0 };
|
| 4521 |
+
Handle h { 42 }; // OK
|
| 4522 |
+
```
|
| 4523 |
+
|
| 4524 |
+
— *end example*]
|
| 4525 |
+
- Otherwise, if the initializer list has a single element of type `E`
|
| 4526 |
+
and either `T` is not a reference type or its referenced type is
|
| 4527 |
+
reference-related to `E`, the object or reference is initialized from
|
| 4528 |
+
that element (by copy-initialization for copy-list-initialization, or
|
| 4529 |
+
by direct-initialization for direct-list-initialization); if a
|
| 4530 |
+
narrowing conversion (see below) is required to convert the element to
|
| 4531 |
+
`T`, the program is ill-formed.
|
| 4532 |
+
\[*Example 8*:
|
| 4533 |
+
``` cpp
|
| 4534 |
+
int x1 {2}; // OK
|
| 4535 |
+
int x2 {2.0}; // error: narrowing
|
| 4536 |
+
```
|
| 4537 |
+
|
| 4538 |
+
— *end example*]
|
| 4539 |
+
- Otherwise, if `T` is a reference type, a prvalue is generated. The
|
| 4540 |
+
prvalue initializes its result object by copy-list-initialization. The
|
| 4541 |
+
prvalue is then used to direct-initialize the reference. The type of
|
| 4542 |
+
the temporary is the type referenced by `T`, unless `T` is “reference
|
| 4543 |
+
to array of unknown bound of `U`”, in which case the type of the
|
| 4544 |
+
temporary is the type of `x` in the declaration `U x[] H`, where H is
|
| 4545 |
+
the initializer list.
|
| 4546 |
+
\[*Note 3*: As usual, the binding will fail and the program is
|
| 4547 |
+
ill-formed if the reference type is an lvalue reference to a non-const
|
| 4548 |
+
type. — *end note*]
|
| 4549 |
+
\[*Example 9*:
|
| 4550 |
+
``` cpp
|
| 4551 |
+
struct S {
|
| 4552 |
+
S(std::initializer_list<double>); // #1
|
| 4553 |
+
S(const std::string&); // #2
|
| 4554 |
+
// ...
|
| 4555 |
+
};
|
| 4556 |
+
const S& r1 = { 1, 2, 3.0 }; // OK: invoke #1
|
| 4557 |
+
const S& r2 { "Spinach" }; // OK: invoke #2
|
| 4558 |
+
S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
|
| 4559 |
+
const int& i1 = { 1 }; // OK
|
| 4560 |
+
const int& i2 = { 1.1 }; // error: narrowing
|
| 4561 |
+
const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
|
| 4562 |
+
|
| 4563 |
+
struct A { } a;
|
| 4564 |
+
struct B { explicit B(const A&); };
|
| 4565 |
+
const B& b2{a}; // error: cannot copy-list-initialize B temporary from A
|
| 4566 |
+
```
|
| 4567 |
+
|
| 4568 |
+
— *end example*]
|
| 4569 |
+
- Otherwise, if the initializer list has no elements, the object is
|
| 4570 |
+
value-initialized.
|
| 4571 |
+
\[*Example 10*:
|
| 4572 |
+
``` cpp
|
| 4573 |
+
int** pp {}; // initialized to null pointer
|
| 4574 |
+
```
|
| 4575 |
+
|
| 4576 |
+
— *end example*]
|
| 4577 |
+
- Otherwise, the program is ill-formed.
|
| 4578 |
+
\[*Example 11*:
|
| 4579 |
+
``` cpp
|
| 4580 |
+
struct A { int i; int j; };
|
| 4581 |
+
A a1 { 1, 2 }; // aggregate initialization
|
| 4582 |
+
A a2 { 1.2 }; // error: narrowing
|
| 4583 |
+
struct B {
|
| 4584 |
+
B(std::initializer_list<int>);
|
| 4585 |
+
};
|
| 4586 |
+
B b1 { 1, 2 }; // creates initializer_list<int> and calls constructor
|
| 4587 |
+
B b2 { 1, 2.0 }; // error: narrowing
|
| 4588 |
+
struct C {
|
| 4589 |
+
C(int i, double j);
|
| 4590 |
+
};
|
| 4591 |
+
C c1 = { 1, 2.2 }; // calls constructor with arguments (1, 2.2)
|
| 4592 |
+
C c2 = { 1.1, 2 }; // error: narrowing
|
| 4593 |
+
|
| 4594 |
+
int j { 1 }; // initialize to 1
|
| 4595 |
+
int k { }; // initialize to 0
|
| 4596 |
+
```
|
| 4597 |
+
|
| 4598 |
+
— *end example*]
|
| 4599 |
+
|
| 4600 |
+
Within the *initializer-list* of a *braced-init-list*, the
|
| 4601 |
+
*initializer-clause*s, including any that result from pack expansions
|
| 4602 |
+
[[temp.variadic]], are evaluated in the order in which they appear. That
|
| 4603 |
+
is, every value computation and side effect associated with a given
|
| 4604 |
+
*initializer-clause* is sequenced before every value computation and
|
| 4605 |
+
side effect associated with any *initializer-clause* that follows it in
|
| 4606 |
+
the comma-separated list of the *initializer-list*.
|
| 4607 |
+
|
| 4608 |
+
[*Note 4*: This evaluation ordering holds regardless of the semantics
|
| 4609 |
+
of the initialization; for example, it applies when the elements of the
|
| 4610 |
+
*initializer-list* are interpreted as arguments of a constructor call,
|
| 4611 |
+
even though ordinarily there are no sequencing constraints on the
|
| 4612 |
+
arguments of a call. — *end note*]
|
| 4613 |
+
|
| 4614 |
+
An object of type `std::initializer_list<E>` is constructed from an
|
| 4615 |
+
initializer list as if the implementation generated and materialized
|
| 4616 |
+
[[conv.rval]] a prvalue of type “array of N `const E`”, where N is the
|
| 4617 |
+
number of elements in the initializer list. Each element of that array
|
| 4618 |
+
is copy-initialized with the corresponding element of the initializer
|
| 4619 |
+
list, and the `std::initializer_list<E>` object is constructed to refer
|
| 4620 |
+
to that array.
|
| 4621 |
+
|
| 4622 |
+
[*Note 5*: A constructor or conversion function selected for the copy
|
| 4623 |
+
is required to be accessible [[class.access]] in the context of the
|
| 4624 |
+
initializer list. — *end note*]
|
| 4625 |
+
|
| 4626 |
+
If a narrowing conversion is required to initialize any of the elements,
|
| 4627 |
+
the program is ill-formed.
|
| 4628 |
+
|
| 4629 |
+
[*Example 12*:
|
| 4630 |
+
|
| 4631 |
+
``` cpp
|
| 4632 |
+
struct X {
|
| 4633 |
+
X(std::initializer_list<double> v);
|
| 4634 |
+
};
|
| 4635 |
+
X x{ 1,2,3 };
|
| 4636 |
+
```
|
| 4637 |
+
|
| 4638 |
+
The initialization will be implemented in a way roughly equivalent to
|
| 4639 |
+
this:
|
| 4640 |
+
|
| 4641 |
+
``` cpp
|
| 4642 |
+
const double __a[3] = {double{1}, double{2}, double{3}};
|
| 4643 |
+
X x(std::initializer_list<double>(__a, __a+3));
|
| 4644 |
+
```
|
| 4645 |
+
|
| 4646 |
+
assuming that the implementation can construct an `initializer_list`
|
| 4647 |
+
object with a pair of pointers.
|
| 4648 |
+
|
| 4649 |
+
— *end example*]
|
| 4650 |
+
|
| 4651 |
+
The array has the same lifetime as any other temporary object
|
| 4652 |
+
[[class.temporary]], except that initializing an `initializer_list`
|
| 4653 |
+
object from the array extends the lifetime of the array exactly like
|
| 4654 |
+
binding a reference to a temporary.
|
| 4655 |
+
|
| 4656 |
+
[*Example 13*:
|
| 4657 |
+
|
| 4658 |
+
``` cpp
|
| 4659 |
+
typedef std::complex<double> cmplx;
|
| 4660 |
+
std::vector<cmplx> v1 = { 1, 2, 3 };
|
| 4661 |
+
|
| 4662 |
+
void f() {
|
| 4663 |
+
std::vector<cmplx> v2{ 1, 2, 3 };
|
| 4664 |
+
std::initializer_list<int> i3 = { 1, 2, 3 };
|
| 4665 |
+
}
|
| 4666 |
+
|
| 4667 |
+
struct A {
|
| 4668 |
+
std::initializer_list<int> i4;
|
| 4669 |
+
A() : i4{ 1, 2, 3 } {} // ill-formed, would create a dangling reference
|
| 4670 |
+
};
|
| 4671 |
+
```
|
| 4672 |
+
|
| 4673 |
+
For `v1` and `v2`, the `initializer_list` object is a parameter in a
|
| 4674 |
+
function call, so the array created for `{ 1, 2, 3 }` has
|
| 4675 |
+
full-expression lifetime. For `i3`, the `initializer_list` object is a
|
| 4676 |
+
variable, so the array persists for the lifetime of the variable. For
|
| 4677 |
+
`i4`, the `initializer_list` object is initialized in the constructor’s
|
| 4678 |
+
*ctor-initializer* as if by binding a temporary array to a reference
|
| 4679 |
+
member, so the program is ill-formed [[class.base.init]].
|
| 4680 |
+
|
| 4681 |
+
— *end example*]
|
| 4682 |
+
|
| 4683 |
+
[*Note 6*: The implementation is free to allocate the array in
|
| 4684 |
+
read-only memory if an explicit array with the same initializer could be
|
| 4685 |
+
so allocated. — *end note*]
|
| 4686 |
+
|
| 4687 |
+
A *narrowing conversion* is an implicit conversion
|
| 4688 |
+
|
| 4689 |
+
- from a floating-point type to an integer type, or
|
| 4690 |
+
- from `long double` to `double` or `float`, or from `double` to
|
| 4691 |
+
`float`, except where the source is a constant expression and the
|
| 4692 |
+
actual value after conversion is within the range of values that can
|
| 4693 |
+
be represented (even if it cannot be represented exactly), or
|
| 4694 |
+
- from an integer type or unscoped enumeration type to a floating-point
|
| 4695 |
+
type, except where the source is a constant expression and the actual
|
| 4696 |
+
value after conversion will fit into the target type and will produce
|
| 4697 |
+
the original value when converted back to the original type, or
|
| 4698 |
+
- from an integer type or unscoped enumeration type to an integer type
|
| 4699 |
+
that cannot represent all the values of the original type, except
|
| 4700 |
+
where the source is a constant expression whose value after integral
|
| 4701 |
+
promotions will fit into the target type, or
|
| 4702 |
+
- from a pointer type or a pointer-to-member type to `bool`.
|
| 4703 |
+
|
| 4704 |
+
[*Note 7*: As indicated above, such conversions are not allowed at the
|
| 4705 |
+
top level in list-initializations. — *end note*]
|
| 4706 |
+
|
| 4707 |
+
[*Example 14*:
|
| 4708 |
+
|
| 4709 |
+
``` cpp
|
| 4710 |
+
int x = 999; // x is not a constant expression
|
| 4711 |
+
const int y = 999;
|
| 4712 |
+
const int z = 99;
|
| 4713 |
+
char c1 = x; // OK, though it might narrow (in this case, it does narrow)
|
| 4714 |
+
char c2{x}; // error: might narrow
|
| 4715 |
+
char c3{y}; // error: narrows (assuming char is 8 bits)
|
| 4716 |
+
char c4{z}; // OK: no narrowing needed
|
| 4717 |
+
unsigned char uc1 = {5}; // OK: no narrowing needed
|
| 4718 |
+
unsigned char uc2 = {-1}; // error: narrows
|
| 4719 |
+
unsigned int ui1 = {-1}; // error: narrows
|
| 4720 |
+
signed int si1 =
|
| 4721 |
+
{ (unsigned int)-1 }; // error: narrows
|
| 4722 |
+
int ii = {2.0}; // error: narrows
|
| 4723 |
+
float f1 { x }; // error: might narrow
|
| 4724 |
+
float f2 { 7 }; // OK: 7 can be exactly represented as a float
|
| 4725 |
+
bool b = {"meow"}; // error: narrows
|
| 4726 |
+
int f(int);
|
| 4727 |
+
int a[] = { 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level
|
| 4728 |
+
```
|
| 4729 |
+
|
| 4730 |
+
— *end example*]
|
| 4731 |
+
|
| 4732 |
+
## Function definitions <a id="dcl.fct.def">[[dcl.fct.def]]</a>
|
| 4733 |
+
|
| 4734 |
+
### In general <a id="dcl.fct.def.general">[[dcl.fct.def.general]]</a>
|
| 4735 |
+
|
| 4736 |
+
Function definitions have the form
|
| 4737 |
+
|
| 4738 |
+
``` bnf
|
| 4739 |
+
function-definition:
|
| 4740 |
+
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator virt-specifier-seqₒₚₜ function-body
|
| 4741 |
+
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator requires-clause function-body
|
| 4742 |
+
```
|
| 4743 |
+
|
| 4744 |
+
``` bnf
|
| 4745 |
+
function-body:
|
| 4746 |
+
ctor-initializerₒₚₜ compound-statement
|
| 4747 |
+
function-try-block
|
| 4748 |
+
'=' default ';'
|
| 4749 |
+
'=' delete ';'
|
| 4750 |
+
```
|
| 4751 |
+
|
| 4752 |
+
Any informal reference to the body of a function should be interpreted
|
| 4753 |
+
as a reference to the non-terminal *function-body*. The optional
|
| 4754 |
+
*attribute-specifier-seq* in a *function-definition* appertains to the
|
| 4755 |
+
function. A *virt-specifier-seq* can be part of a *function-definition*
|
| 4756 |
+
only if it is a *member-declaration* [[class.mem]].
|
| 4757 |
+
|
| 4758 |
+
In a *function-definition*, either `void` *declarator* `;` or
|
| 4759 |
+
*declarator* `;` shall be a well-formed function declaration as
|
| 4760 |
+
described in [[dcl.fct]]. A function shall be defined only in namespace
|
| 4761 |
+
or class scope. The type of a parameter or the return type for a
|
| 4762 |
+
function definition shall not be a (possibly cv-qualified) class type
|
| 4763 |
+
that is incomplete or abstract within the function body unless the
|
| 4764 |
+
function is deleted [[dcl.fct.def.delete]].
|
| 4765 |
+
|
| 4766 |
+
[*Example 1*:
|
| 4767 |
+
|
| 4768 |
+
A simple example of a complete function definition is
|
| 4769 |
+
|
| 4770 |
+
``` cpp
|
| 4771 |
+
int max(int a, int b, int c) {
|
| 4772 |
+
int m = (a > b) ? a : b;
|
| 4773 |
+
return (m > c) ? m : c;
|
| 4774 |
+
}
|
| 4775 |
+
```
|
| 4776 |
+
|
| 4777 |
+
Here `int` is the *decl-specifier-seq*; `max(int` `a,` `int` `b,` `int`
|
| 4778 |
+
`c)` is the *declarator*; `{ /* ... */ }` is the *function-body*.
|
| 4779 |
+
|
| 4780 |
+
— *end example*]
|
| 4781 |
+
|
| 4782 |
+
A *ctor-initializer* is used only in a constructor; see [[class.ctor]]
|
| 4783 |
+
and [[class.init]].
|
| 4784 |
+
|
| 4785 |
+
[*Note 1*: A *cv-qualifier-seq* affects the type of `this` in the body
|
| 4786 |
+
of a member function; see [[dcl.ref]]. — *end note*]
|
| 4787 |
+
|
| 4788 |
+
[*Note 2*:
|
| 4789 |
+
|
| 4790 |
+
Unused parameters need not be named. For example,
|
| 4791 |
+
|
| 4792 |
+
``` cpp
|
| 4793 |
+
void print(int a, int) {
|
| 4794 |
+
std::printf("a = %d\n",a);
|
| 4795 |
+
}
|
| 4796 |
+
```
|
| 4797 |
+
|
| 4798 |
+
— *end note*]
|
| 4799 |
+
|
| 4800 |
+
In the *function-body*, a *function-local predefined variable* denotes a
|
| 4801 |
+
block-scope object of static storage duration that is implicitly defined
|
| 4802 |
+
(see [[basic.scope.block]]).
|
| 4803 |
+
|
| 4804 |
+
The function-local predefined variable `__func__` is defined as if a
|
| 4805 |
+
definition of the form
|
| 4806 |
+
|
| 4807 |
+
``` cpp
|
| 4808 |
+
static const char __func__[] = "function-name";
|
| 4809 |
+
```
|
| 4810 |
+
|
| 4811 |
+
had been provided, where `function-name` is an *implementation-defined*
|
| 4812 |
+
string. It is unspecified whether such a variable has an address
|
| 4813 |
+
distinct from that of any other object in the program.[^8]
|
| 4814 |
+
|
| 4815 |
+
[*Example 2*:
|
| 4816 |
+
|
| 4817 |
+
``` cpp
|
| 4818 |
+
struct S {
|
| 4819 |
+
S() : s(__func__) { } // OK
|
| 4820 |
+
const char* s;
|
| 4821 |
+
};
|
| 4822 |
+
void f(const char* s = __func__); // error: __func__ is undeclared
|
| 4823 |
+
```
|
| 4824 |
+
|
| 4825 |
+
— *end example*]
|
| 4826 |
+
|
| 4827 |
+
### Explicitly-defaulted functions <a id="dcl.fct.def.default">[[dcl.fct.def.default]]</a>
|
| 4828 |
+
|
| 4829 |
+
A function definition whose *function-body* is of the form `= default ;`
|
| 4830 |
+
is called an *explicitly-defaulted* definition. A function that is
|
| 4831 |
+
explicitly defaulted shall
|
| 4832 |
+
|
| 4833 |
+
- be a special member function or a comparison operator function
|
| 4834 |
+
[[over.binary]], and
|
| 4835 |
+
- not have default arguments.
|
| 4836 |
+
|
| 4837 |
+
The type `T`₁ of an explicitly defaulted special member function `F` is
|
| 4838 |
+
allowed to differ from the type `T`₂ it would have had if it were
|
| 4839 |
+
implicitly declared, as follows:
|
| 4840 |
+
|
| 4841 |
+
- `T`₁ and `T`₂ may have differing *ref-qualifier*s;
|
| 4842 |
+
- `T`₁ and `T`₂ may have differing exception specifications; and
|
| 4843 |
+
- if `T`₂ has a parameter of type `const C&`, the corresponding
|
| 4844 |
+
parameter of `T`₁ may be of type `C&`.
|
| 4845 |
+
|
| 4846 |
+
If `T`₁ differs from `T`₂ in any other way, then:
|
| 4847 |
+
|
| 4848 |
+
- if `F` is an assignment operator, and the return type of `T`₁ differs
|
| 4849 |
+
from the return type of `T`₂ or `T`₁’s parameter type is not a
|
| 4850 |
+
reference, the program is ill-formed;
|
| 4851 |
+
- otherwise, if `F` is explicitly defaulted on its first declaration, it
|
| 4852 |
+
is defined as deleted;
|
| 4853 |
+
- otherwise, the program is ill-formed.
|
| 4854 |
+
|
| 4855 |
+
An explicitly-defaulted function that is not defined as deleted may be
|
| 4856 |
+
declared `constexpr` or `consteval` only if it is constexpr-compatible (
|
| 4857 |
+
[[special]], [[class.compare.default]]). A function explicitly defaulted
|
| 4858 |
+
on its first declaration is implicitly inline [[dcl.inline]], and is
|
| 4859 |
+
implicitly constexpr [[dcl.constexpr]] if it is constexpr-compatible.
|
| 4860 |
+
|
| 4861 |
+
[*Example 1*:
|
| 4862 |
+
|
| 4863 |
+
``` cpp
|
| 4864 |
+
struct S {
|
| 4865 |
+
constexpr S() = default; // error: implicit S() is not constexpr
|
| 4866 |
+
S(int a = 0) = default; // error: default argument
|
| 4867 |
+
void operator=(const S&) = default; // error: non-matching return type
|
| 4868 |
+
~S() noexcept(false) = default; // OK, despite mismatched exception specification
|
| 4869 |
+
private:
|
| 4870 |
+
int i;
|
| 4871 |
+
S(S&); // OK: private copy constructor
|
| 4872 |
+
};
|
| 4873 |
+
S::S(S&) = default; // OK: defines copy constructor
|
| 4874 |
+
|
| 4875 |
+
struct T {
|
| 4876 |
+
T();
|
| 4877 |
+
T(T &&) noexcept(false);
|
| 4878 |
+
};
|
| 4879 |
+
struct U {
|
| 4880 |
+
T t;
|
| 4881 |
+
U();
|
| 4882 |
+
U(U &&) noexcept = default;
|
| 4883 |
+
};
|
| 4884 |
+
U u1;
|
| 4885 |
+
U u2 = static_cast<U&&>(u1); // OK, calls std::terminate if T::T(T&&) throws
|
| 4886 |
+
```
|
| 4887 |
+
|
| 4888 |
+
— *end example*]
|
| 4889 |
+
|
| 4890 |
+
Explicitly-defaulted functions and implicitly-declared functions are
|
| 4891 |
+
collectively called *defaulted* functions, and the implementation shall
|
| 4892 |
+
provide implicit definitions for them ([[class.ctor]], [[class.dtor]],
|
| 4893 |
+
[[class.copy.ctor]], [[class.copy.assign]]), which might mean defining
|
| 4894 |
+
them as deleted. A defaulted prospective destructor [[class.dtor]] that
|
| 4895 |
+
is not a destructor is defined as deleted. A defaulted special member
|
| 4896 |
+
function that is neither a prospective destructor nor an eligible
|
| 4897 |
+
special member function [[special]] is defined as deleted. A function is
|
| 4898 |
+
*user-provided* if it is user-declared and not explicitly defaulted or
|
| 4899 |
+
deleted on its first declaration. A user-provided explicitly-defaulted
|
| 4900 |
+
function (i.e., explicitly defaulted after its first declaration) is
|
| 4901 |
+
defined at the point where it is explicitly defaulted; if such a
|
| 4902 |
+
function is implicitly defined as deleted, the program is ill-formed.
|
| 4903 |
+
|
| 4904 |
+
[*Note 1*: Declaring a function as defaulted after its first
|
| 4905 |
+
declaration can provide efficient execution and concise definition while
|
| 4906 |
+
enabling a stable binary interface to an evolving code
|
| 4907 |
+
base. — *end note*]
|
| 4908 |
+
|
| 4909 |
+
[*Example 2*:
|
| 4910 |
+
|
| 4911 |
+
``` cpp
|
| 4912 |
+
struct trivial {
|
| 4913 |
+
trivial() = default;
|
| 4914 |
+
trivial(const trivial&) = default;
|
| 4915 |
+
trivial(trivial&&) = default;
|
| 4916 |
+
trivial& operator=(const trivial&) = default;
|
| 4917 |
+
trivial& operator=(trivial&&) = default;
|
| 4918 |
+
~trivial() = default;
|
| 4919 |
+
};
|
| 4920 |
+
|
| 4921 |
+
struct nontrivial1 {
|
| 4922 |
+
nontrivial1();
|
| 4923 |
+
};
|
| 4924 |
+
nontrivial1::nontrivial1() = default; // not first declaration
|
| 4925 |
+
```
|
| 4926 |
+
|
| 4927 |
+
— *end example*]
|
| 4928 |
+
|
| 4929 |
+
### Deleted definitions <a id="dcl.fct.def.delete">[[dcl.fct.def.delete]]</a>
|
| 4930 |
+
|
| 4931 |
+
A function definition whose *function-body* is of the form `= delete ;`
|
| 4932 |
+
is called a *deleted definition*. A function with a deleted definition
|
| 4933 |
+
is also called a *deleted function*.
|
| 4934 |
+
|
| 4935 |
+
A program that refers to a deleted function implicitly or explicitly,
|
| 4936 |
+
other than to declare it, is ill-formed.
|
| 4937 |
+
|
| 4938 |
+
[*Note 1*: This includes calling the function implicitly or explicitly
|
| 4939 |
+
and forming a pointer or pointer-to-member to the function. It applies
|
| 4940 |
+
even for references in expressions that are not potentially-evaluated.
|
| 4941 |
+
If a function is overloaded, it is referenced only if the function is
|
| 4942 |
+
selected by overload resolution. The implicit odr-use [[basic.def.odr]]
|
| 4943 |
+
of a virtual function does not, by itself, constitute a
|
| 4944 |
+
reference. — *end note*]
|
| 4945 |
+
|
| 4946 |
+
[*Example 1*:
|
| 4947 |
+
|
| 4948 |
+
One can prevent default initialization and initialization by
|
| 4949 |
+
non-`double`s with
|
| 4950 |
+
|
| 4951 |
+
``` cpp
|
| 4952 |
+
struct onlydouble {
|
| 4953 |
+
onlydouble() = delete; // OK, but redundant
|
| 4954 |
+
template<class T>
|
| 4955 |
+
onlydouble(T) = delete;
|
| 4956 |
+
onlydouble(double);
|
| 4957 |
+
};
|
| 4958 |
+
```
|
| 4959 |
+
|
| 4960 |
+
— *end example*]
|
| 4961 |
+
|
| 4962 |
+
[*Example 2*:
|
| 4963 |
+
|
| 4964 |
+
One can prevent use of a class in certain *new-expression*s by using
|
| 4965 |
+
deleted definitions of a user-declared `operator new` for that class.
|
| 4966 |
+
|
| 4967 |
+
``` cpp
|
| 4968 |
+
struct sometype {
|
| 4969 |
+
void* operator new(std::size_t) = delete;
|
| 4970 |
+
void* operator new[](std::size_t) = delete;
|
| 4971 |
+
};
|
| 4972 |
+
sometype* p = new sometype; // error: deleted class operator new
|
| 4973 |
+
sometype* q = new sometype[3]; // error: deleted class operator new[]
|
| 4974 |
+
```
|
| 4975 |
+
|
| 4976 |
+
— *end example*]
|
| 4977 |
+
|
| 4978 |
+
[*Example 3*:
|
| 4979 |
+
|
| 4980 |
+
One can make a class uncopyable, i.e., move-only, by using deleted
|
| 4981 |
+
definitions of the copy constructor and copy assignment operator, and
|
| 4982 |
+
then providing defaulted definitions of the move constructor and move
|
| 4983 |
+
assignment operator.
|
| 4984 |
+
|
| 4985 |
+
``` cpp
|
| 4986 |
+
struct moveonly {
|
| 4987 |
+
moveonly() = default;
|
| 4988 |
+
moveonly(const moveonly&) = delete;
|
| 4989 |
+
moveonly(moveonly&&) = default;
|
| 4990 |
+
moveonly& operator=(const moveonly&) = delete;
|
| 4991 |
+
moveonly& operator=(moveonly&&) = default;
|
| 4992 |
+
~moveonly() = default;
|
| 4993 |
+
};
|
| 4994 |
+
moveonly* p;
|
| 4995 |
+
moveonly q(*p); // error: deleted copy constructor
|
| 4996 |
+
```
|
| 4997 |
+
|
| 4998 |
+
— *end example*]
|
| 4999 |
+
|
| 5000 |
+
A deleted function is implicitly an inline function [[dcl.inline]].
|
| 5001 |
+
|
| 5002 |
+
[*Note 2*: The one-definition rule [[basic.def.odr]] applies to deleted
|
| 5003 |
+
definitions. — *end note*]
|
| 5004 |
+
|
| 5005 |
+
A deleted definition of a function shall be the first declaration of the
|
| 5006 |
+
function or, for an explicit specialization of a function template, the
|
| 5007 |
+
first declaration of that specialization. An implicitly declared
|
| 5008 |
+
allocation or deallocation function [[basic.stc.dynamic]] shall not be
|
| 5009 |
+
defined as deleted.
|
| 5010 |
+
|
| 5011 |
+
[*Example 4*:
|
| 5012 |
+
|
| 5013 |
+
``` cpp
|
| 5014 |
+
struct sometype {
|
| 5015 |
+
sometype();
|
| 5016 |
+
};
|
| 5017 |
+
sometype::sometype() = delete; // error: not first declaration
|
| 5018 |
+
```
|
| 5019 |
+
|
| 5020 |
+
— *end example*]
|
| 5021 |
+
|
| 5022 |
+
### Coroutine definitions <a id="dcl.fct.def.coroutine">[[dcl.fct.def.coroutine]]</a>
|
| 5023 |
+
|
| 5024 |
+
A function is a *coroutine* if its *function-body* encloses a
|
| 5025 |
+
*coroutine-return-statement* [[stmt.return.coroutine]], an
|
| 5026 |
+
*await-expression* [[expr.await]], or a *yield-expression*
|
| 5027 |
+
[[expr.yield]]. The *parameter-declaration-clause* of the coroutine
|
| 5028 |
+
shall not terminate with an ellipsis that is not part of a
|
| 5029 |
+
*parameter-declaration*.
|
| 5030 |
+
|
| 5031 |
+
[*Example 1*:
|
| 5032 |
+
|
| 5033 |
+
``` cpp
|
| 5034 |
+
task<int> f();
|
| 5035 |
+
|
| 5036 |
+
task<void> g1() {
|
| 5037 |
+
int i = co_await f();
|
| 5038 |
+
std::cout << "f() => " << i << std::endl;
|
| 5039 |
+
}
|
| 5040 |
+
|
| 5041 |
+
template <typename... Args>
|
| 5042 |
+
task<void> g2(Args&&...) { // OK, ellipsis is a pack expansion
|
| 5043 |
+
int i = co_await f();
|
| 5044 |
+
std::cout << "f() => " << i << std::endl;
|
| 5045 |
+
}
|
| 5046 |
+
|
| 5047 |
+
task<void> g3(int a, ...) { // error: variable parameter list not allowed
|
| 5048 |
+
int i = co_await f();
|
| 5049 |
+
std::cout << "f() => " << i << std::endl;
|
| 5050 |
+
}
|
| 5051 |
+
```
|
| 5052 |
+
|
| 5053 |
+
— *end example*]
|
| 5054 |
+
|
| 5055 |
+
The *promise type* of a coroutine is
|
| 5056 |
+
`std::coroutine_traits<R, P₁, …, Pₙ>::promise_type`, where `R` is the
|
| 5057 |
+
return type of the function, and `P₁` … `Pₙ` are the sequence of types
|
| 5058 |
+
of the function parameters, preceded by the type of the implicit object
|
| 5059 |
+
parameter [[over.match.funcs]] if the coroutine is a non-static member
|
| 5060 |
+
function. The promise type shall be a class type.
|
| 5061 |
+
|
| 5062 |
+
In the following, `pᵢ` is an lvalue of type `Pᵢ`, where `p₁` denotes
|
| 5063 |
+
`*this` and `p_i+1` denotes the $i^\textrm{th}$ function parameter for a
|
| 5064 |
+
non-static member function, and `pᵢ` denotes the $i^\textrm{th}$
|
| 5065 |
+
function parameter otherwise.
|
| 5066 |
+
|
| 5067 |
+
A coroutine behaves as if its *function-body* were replaced by:
|
| 5068 |
+
|
| 5069 |
+
``` bnf
|
| 5070 |
+
'{'
|
| 5071 |
+
*promise-type* promise *promise-constructor-arguments* ';'
|
| 5072 |
+
% FIXME: promise'.get_return_object()' ';'
|
| 5073 |
+
% ... except that it's not a discarded-value expression
|
| 5074 |
+
'try' '{'
|
| 5075 |
+
'co_await' 'promise.initial_suspend()' ';'
|
| 5076 |
+
function-body
|
| 5077 |
+
'} catch ( ... ) {'
|
| 5078 |
+
'if (!initial-await-resume-called)'
|
| 5079 |
+
'throw' ';'
|
| 5080 |
+
'promise.unhandled_exception()' ';'
|
| 5081 |
+
'}'
|
| 5082 |
+
final-suspend ':'
|
| 5083 |
+
'co_await' 'promise.final_suspend()' ';'
|
| 5084 |
+
'}'
|
| 5085 |
+
```
|
| 5086 |
+
|
| 5087 |
+
where
|
| 5088 |
+
|
| 5089 |
+
- the *await-expression* containing the call to `initial_suspend` is the
|
| 5090 |
+
*initial suspend point*, and
|
| 5091 |
+
- the *await-expression* containing the call to `final_suspend` is the
|
| 5092 |
+
*final suspend point*, and
|
| 5093 |
+
- *initial-await-resume-called* is initially `false` and is set to
|
| 5094 |
+
`true` immediately before the evaluation of the *await-resume*
|
| 5095 |
+
expression [[expr.await]] of the initial suspend point, and
|
| 5096 |
+
- *promise-type* denotes the promise type, and
|
| 5097 |
+
- the object denoted by the exposition-only name *`promise`* is the
|
| 5098 |
+
*promise object* of the coroutine, and
|
| 5099 |
+
- the label denoted by the name *`final-suspend`* is defined for
|
| 5100 |
+
exposition only [[stmt.return.coroutine]], and
|
| 5101 |
+
- *promise-constructor-arguments* is determined as follows: overload
|
| 5102 |
+
resolution is performed on a promise constructor call created by
|
| 5103 |
+
assembling an argument list with lvalues `p₁` … `pₙ`. If a viable
|
| 5104 |
+
constructor is found [[over.match.viable]], then
|
| 5105 |
+
*promise-constructor-arguments* is `(p₁, …, pₙ)`, otherwise
|
| 5106 |
+
*promise-constructor-arguments* is empty.
|
| 5107 |
+
|
| 5108 |
+
The *unqualified-id*s `return_void` and `return_value` are looked up in
|
| 5109 |
+
the scope of the promise type. If both are found, the program is
|
| 5110 |
+
ill-formed.
|
| 5111 |
+
|
| 5112 |
+
[*Note 1*: If the *unqualified-id* `return_void` is found, flowing off
|
| 5113 |
+
the end of a coroutine is equivalent to a `co_return` with no operand.
|
| 5114 |
+
Otherwise, flowing off the end of a coroutine results in undefined
|
| 5115 |
+
behavior [[stmt.return.coroutine]]. — *end note*]
|
| 5116 |
+
|
| 5117 |
+
The expression `promise.get_return_object()` is used to initialize the
|
| 5118 |
+
glvalue result or prvalue result object of a call to a coroutine. The
|
| 5119 |
+
call to `get_return_object` is sequenced before the call to
|
| 5120 |
+
`initial_suspend` and is invoked at most once.
|
| 5121 |
+
|
| 5122 |
+
A suspended coroutine can be resumed to continue execution by invoking a
|
| 5123 |
+
resumption member function [[coroutine.handle.resumption]] of a
|
| 5124 |
+
coroutine handle [[coroutine.handle]] that refers to the coroutine. The
|
| 5125 |
+
function that invoked a resumption member function is called the
|
| 5126 |
+
*resumer*. Invoking a resumption member function for a coroutine that is
|
| 5127 |
+
not suspended results in undefined behavior.
|
| 5128 |
+
|
| 5129 |
+
An implementation may need to allocate additional storage for a
|
| 5130 |
+
coroutine. This storage is known as the *coroutine state* and is
|
| 5131 |
+
obtained by calling a non-array allocation function
|
| 5132 |
+
[[basic.stc.dynamic.allocation]]. The allocation function’s name is
|
| 5133 |
+
looked up in the scope of the promise type. If this lookup fails, the
|
| 5134 |
+
allocation function’s name is looked up in the global scope. If the
|
| 5135 |
+
lookup finds an allocation function in the scope of the promise type,
|
| 5136 |
+
overload resolution is performed on a function call created by
|
| 5137 |
+
assembling an argument list. The first argument is the amount of space
|
| 5138 |
+
requested, and has type `std::size_t`. The lvalues `p₁` … `pₙ` are the
|
| 5139 |
+
succeeding arguments. If no viable function is found
|
| 5140 |
+
[[over.match.viable]], overload resolution is performed again on a
|
| 5141 |
+
function call created by passing just the amount of space required as an
|
| 5142 |
+
argument of type `std::size_t`.
|
| 5143 |
+
|
| 5144 |
+
The *unqualified-id* `get_return_object_on_allocation_failure` is looked
|
| 5145 |
+
up in the scope of the promise type by class member access lookup
|
| 5146 |
+
[[basic.lookup.classref]]. If any declarations are found, then the
|
| 5147 |
+
result of a call to an allocation function used to obtain storage for
|
| 5148 |
+
the coroutine state is assumed to return `nullptr` if it fails to obtain
|
| 5149 |
+
storage, and if a global allocation function is selected, the
|
| 5150 |
+
`::operator new(size_t, nothrow_t)` form is used. The allocation
|
| 5151 |
+
function used in this case shall have a non-throwing
|
| 5152 |
+
*noexcept-specification*. If the allocation function returns `nullptr`,
|
| 5153 |
+
the coroutine returns control to the caller of the coroutine and the
|
| 5154 |
+
return value is obtained by a call to
|
| 5155 |
+
`T::get_return_object_on_allocation_failure()`, where `T` is the promise
|
| 5156 |
+
type.
|
| 5157 |
+
|
| 5158 |
+
[*Example 2*:
|
| 5159 |
+
|
| 5160 |
+
``` cpp
|
| 5161 |
+
#include <iostream>
|
| 5162 |
+
#include <coroutine>
|
| 5163 |
+
|
| 5164 |
+
// ::operator new(size_t, nothrow_t) will be used if allocation is needed
|
| 5165 |
+
struct generator {
|
| 5166 |
+
struct promise_type;
|
| 5167 |
+
using handle = std::coroutine_handle<promise_type>;
|
| 5168 |
+
struct promise_type {
|
| 5169 |
+
int current_value;
|
| 5170 |
+
static auto get_return_object_on_allocation_failure() { return generator{nullptr}; }
|
| 5171 |
+
auto get_return_object() { return generator{handle::from_promise(*this)}; }
|
| 5172 |
+
auto initial_suspend() { return std::suspend_always{}; }
|
| 5173 |
+
auto final_suspend() { return std::suspend_always{}; }
|
| 5174 |
+
void unhandled_exception() { std::terminate(); }
|
| 5175 |
+
void return_void() {}
|
| 5176 |
+
auto yield_value(int value) {
|
| 5177 |
+
current_value = value;
|
| 5178 |
+
return std::suspend_always{};
|
| 5179 |
+
}
|
| 5180 |
+
};
|
| 5181 |
+
bool move_next() { return coro ? (coro.resume(), !coro.done()) : false; }
|
| 5182 |
+
int current_value() { return coro.promise().current_value; }
|
| 5183 |
+
generator(generator const&) = delete;
|
| 5184 |
+
generator(generator && rhs) : coro(rhs.coro) { rhs.coro = nullptr; }
|
| 5185 |
+
~generator() { if (coro) coro.destroy(); }
|
| 5186 |
+
private:
|
| 5187 |
+
generator(handle h) : coro(h) {}
|
| 5188 |
+
handle coro;
|
| 5189 |
+
};
|
| 5190 |
+
generator f() { co_yield 1; co_yield 2; }
|
| 5191 |
+
int main() {
|
| 5192 |
+
auto g = f();
|
| 5193 |
+
while (g.move_next()) std::cout << g.current_value() << std::endl;
|
| 5194 |
+
}
|
| 5195 |
+
```
|
| 5196 |
+
|
| 5197 |
+
— *end example*]
|
| 5198 |
+
|
| 5199 |
+
The coroutine state is destroyed when control flows off the end of the
|
| 5200 |
+
coroutine or the `destroy` member function
|
| 5201 |
+
[[coroutine.handle.resumption]] of a coroutine handle
|
| 5202 |
+
[[coroutine.handle]] that refers to the coroutine is invoked. In the
|
| 5203 |
+
latter case objects with automatic storage duration that are in scope at
|
| 5204 |
+
the suspend point are destroyed in the reverse order of the
|
| 5205 |
+
construction. The storage for the coroutine state is released by calling
|
| 5206 |
+
a non-array deallocation function [[basic.stc.dynamic.deallocation]]. If
|
| 5207 |
+
`destroy` is called for a coroutine that is not suspended, the program
|
| 5208 |
+
has undefined behavior.
|
| 5209 |
+
|
| 5210 |
+
The deallocation function’s name is looked up in the scope of the
|
| 5211 |
+
promise type. If this lookup fails, the deallocation function’s name is
|
| 5212 |
+
looked up in the global scope. If deallocation function lookup finds
|
| 5213 |
+
both a usual deallocation function with only a pointer parameter and a
|
| 5214 |
+
usual deallocation function with both a pointer parameter and a size
|
| 5215 |
+
parameter, then the selected deallocation function shall be the one with
|
| 5216 |
+
two parameters. Otherwise, the selected deallocation function shall be
|
| 5217 |
+
the function with one parameter. If no usual deallocation function is
|
| 5218 |
+
found, the program is ill-formed. The selected deallocation function
|
| 5219 |
+
shall be called with the address of the block of storage to be reclaimed
|
| 5220 |
+
as its first argument. If a deallocation function with a parameter of
|
| 5221 |
+
type `std::size_t` is used, the size of the block is passed as the
|
| 5222 |
+
corresponding argument.
|
| 5223 |
+
|
| 5224 |
+
When a coroutine is invoked, after initializing its parameters
|
| 5225 |
+
[[expr.call]], a copy is created for each coroutine parameter. For a
|
| 5226 |
+
parameter of type cv `T`, the copy is a variable of type cv `T` with
|
| 5227 |
+
automatic storage duration that is direct-initialized from an xvalue of
|
| 5228 |
+
type `T` referring to the parameter.
|
| 5229 |
+
|
| 5230 |
+
[*Note 2*: An original parameter object is never a const or volatile
|
| 5231 |
+
object [[basic.type.qualifier]]. — *end note*]
|
| 5232 |
+
|
| 5233 |
+
The initialization and destruction of each parameter copy occurs in the
|
| 5234 |
+
context of the called coroutine. Initializations of parameter copies are
|
| 5235 |
+
sequenced before the call to the coroutine promise constructor and
|
| 5236 |
+
indeterminately sequenced with respect to each other. The lifetime of
|
| 5237 |
+
parameter copies ends immediately after the lifetime of the coroutine
|
| 5238 |
+
promise object ends.
|
| 5239 |
+
|
| 5240 |
+
[*Note 3*: If a coroutine has a parameter passed by reference, resuming
|
| 5241 |
+
the coroutine after the lifetime of the entity referred to by that
|
| 5242 |
+
parameter has ended is likely to result in undefined
|
| 5243 |
+
behavior. — *end note*]
|
| 5244 |
+
|
| 5245 |
+
If the evaluation of the expression `promise.unhandled_exception()`
|
| 5246 |
+
exits via an exception, the coroutine is considered suspended at the
|
| 5247 |
+
final suspend point.
|
| 5248 |
+
|
| 5249 |
+
The expression `co_await` `promise.final_suspend()` shall not be
|
| 5250 |
+
potentially-throwing [[except.spec]].
|
| 5251 |
+
|
| 5252 |
+
## Structured binding declarations <a id="dcl.struct.bind">[[dcl.struct.bind]]</a>
|
| 5253 |
+
|
| 5254 |
+
A structured binding declaration introduces the *identifier*s `v₀`,
|
| 5255 |
+
`v₁`, `v₂`, … of the *identifier-list* as names
|
| 5256 |
+
[[basic.scope.declarative]] of *structured binding*s. Let cv denote the
|
| 5257 |
+
*cv-qualifier*s in the *decl-specifier-seq* and *S* consist of the
|
| 5258 |
+
*storage-class-specifier*s of the *decl-specifier-seq* (if any). A cv
|
| 5259 |
+
that includes `volatile` is deprecated; see [[depr.volatile.type]].
|
| 5260 |
+
First, a variable with a unique name *`e`* is introduced. If the
|
| 5261 |
+
*assignment-expression* in the *initializer* has array type `A` and no
|
| 5262 |
+
*ref-qualifier* is present, *`e`* is defined by
|
| 5263 |
+
|
| 5264 |
+
``` bnf
|
| 5265 |
+
attribute-specifier-seqₒₚₜ *S* cv 'A' e ';'
|
| 5266 |
+
```
|
| 5267 |
+
|
| 5268 |
+
and each element is copy-initialized or direct-initialized from the
|
| 5269 |
+
corresponding element of the *assignment-expression* as specified by the
|
| 5270 |
+
form of the *initializer*. Otherwise, *`e`* is defined as-if by
|
| 5271 |
+
|
| 5272 |
+
``` bnf
|
| 5273 |
+
attribute-specifier-seqₒₚₜ decl-specifier-seq ref-qualifierₒₚₜ e initializer ';'
|
| 5274 |
+
```
|
| 5275 |
+
|
| 5276 |
+
where the declaration is never interpreted as a function declaration and
|
| 5277 |
+
the parts of the declaration other than the *declarator-id* are taken
|
| 5278 |
+
from the corresponding structured binding declaration. The type of the
|
| 5279 |
+
*id-expression* *`e`* is called `E`.
|
| 5280 |
+
|
| 5281 |
+
[*Note 1*: `E` is never a reference type [[expr.prop]]. — *end note*]
|
| 5282 |
+
|
| 5283 |
+
If the *initializer* refers to one of the names introduced by the
|
| 5284 |
+
structured binding declaration, the program is ill-formed.
|
| 5285 |
+
|
| 5286 |
+
If `E` is an array type with element type `T`, the number of elements in
|
| 5287 |
+
the *identifier-list* shall be equal to the number of elements of `E`.
|
| 5288 |
+
Each `v`ᵢ is the name of an lvalue that refers to the element i of the
|
| 5289 |
+
array and whose type is `T`; the referenced type is `T`.
|
| 5290 |
+
|
| 5291 |
+
[*Note 2*: The top-level cv-qualifiers of `T` are cv. — *end note*]
|
| 5292 |
+
|
| 5293 |
+
[*Example 1*:
|
| 5294 |
+
|
| 5295 |
+
``` cpp
|
| 5296 |
+
auto f() -> int(&)[2];
|
| 5297 |
+
auto [ x, y ] = f(); // x and y refer to elements in a copy of the array return value
|
| 5298 |
+
auto& [ xr, yr ] = f(); // xr and yr refer to elements in the array referred to by f's return value
|
| 5299 |
+
```
|
| 5300 |
+
|
| 5301 |
+
— *end example*]
|
| 5302 |
+
|
| 5303 |
+
Otherwise, if the *qualified-id* `std::tuple_size<E>` names a complete
|
| 5304 |
+
class type with a member named `value`, the expression
|
| 5305 |
+
`std::tuple_size<E>::value` shall be a well-formed integral constant
|
| 5306 |
+
expression and the number of elements in the *identifier-list* shall be
|
| 5307 |
+
equal to the value of that expression. Let `i` be an index prvalue of
|
| 5308 |
+
type `std::size_t` corresponding to `v`_`i`. The *unqualified-id* `get`
|
| 5309 |
+
is looked up in the scope of `E` by class member access lookup
|
| 5310 |
+
[[basic.lookup.classref]], and if that finds at least one declaration
|
| 5311 |
+
that is a function template whose first template parameter is a non-type
|
| 5312 |
+
parameter, the initializer is `e.get<i>()`. Otherwise, the initializer
|
| 5313 |
+
is `get<i>(e)`, where `get` is looked up in the associated namespaces
|
| 5314 |
+
[[basic.lookup.argdep]]. In either case, `get<i>` is interpreted as a
|
| 5315 |
+
*template-id*.
|
| 5316 |
+
|
| 5317 |
+
[*Note 3*: Ordinary unqualified lookup [[basic.lookup.unqual]] is not
|
| 5318 |
+
performed. — *end note*]
|
| 5319 |
+
|
| 5320 |
+
In either case, *`e`* is an lvalue if the type of the entity *`e`* is an
|
| 5321 |
+
lvalue reference and an xvalue otherwise. Given the type `Tᵢ` designated
|
| 5322 |
+
by `std::tuple_element<i, E>::type` and the type `Uᵢ` designated by
|
| 5323 |
+
either `Tᵢ&` or `Tᵢ&&`, where `Uᵢ` is an lvalue reference if the
|
| 5324 |
+
initializer is an lvalue and an rvalue reference otherwise, variables
|
| 5325 |
+
are introduced with unique names `rᵢ` as follows:
|
| 5326 |
+
|
| 5327 |
+
``` bnf
|
| 5328 |
+
*S* 'Uᵢ rᵢ =' initializer ';'
|
| 5329 |
+
```
|
| 5330 |
+
|
| 5331 |
+
Each `vᵢ` is the name of an lvalue of type `Tᵢ` that refers to the
|
| 5332 |
+
object bound to `rᵢ`; the referenced type is `Tᵢ`.
|
| 5333 |
+
|
| 5334 |
+
Otherwise, all of `E`’s non-static data members shall be direct members
|
| 5335 |
+
of `E` or of the same base class of `E`, well-formed when named as
|
| 5336 |
+
`e.name` in the context of the structured binding, `E` shall not have an
|
| 5337 |
+
anonymous union member, and the number of elements in the
|
| 5338 |
+
*identifier-list* shall be equal to the number of non-static data
|
| 5339 |
+
members of `E`. Designating the non-static data members of `E` as `m₀`,
|
| 5340 |
+
`m₁`, `m₂`, … (in declaration order), each `v`ᵢ is the name of an lvalue
|
| 5341 |
+
that refers to the member `m`ᵢ of *`e`* and whose type is cv `Tᵢ`, where
|
| 5342 |
+
`Tᵢ` is the declared type of that member; the referenced type is
|
| 5343 |
+
cv `Tᵢ`. The lvalue is a bit-field if that member is a bit-field.
|
| 5344 |
+
|
| 5345 |
+
[*Example 2*:
|
| 5346 |
+
|
| 5347 |
+
``` cpp
|
| 5348 |
+
struct S { int x1 : 2; volatile double y1; };
|
| 5349 |
+
S f();
|
| 5350 |
+
const auto [ x, y ] = f();
|
| 5351 |
+
```
|
| 5352 |
+
|
| 5353 |
+
The type of the *id-expression* `x` is “`const int`”, the type of the
|
| 5354 |
+
*id-expression* `y` is “`const volatile double`”.
|
| 5355 |
+
|
| 5356 |
+
— *end example*]
|
| 5357 |
+
|
| 5358 |
+
## Enumerations <a id="enum">[[enum]]</a>
|
| 5359 |
+
|
| 5360 |
+
### Enumeration declarations <a id="dcl.enum">[[dcl.enum]]</a>
|
| 5361 |
+
|
| 5362 |
+
An enumeration is a distinct type [[basic.compound]] with named
|
| 5363 |
constants. Its name becomes an *enum-name* within its scope.
|
| 5364 |
|
| 5365 |
``` bnf
|
| 5366 |
enum-name:
|
| 5367 |
identifier
|
| 5368 |
```
|
| 5369 |
|
| 5370 |
``` bnf
|
| 5371 |
enum-specifier:
|
| 5372 |
enum-head '{' enumerator-listₒₚₜ '}'
|
| 5373 |
+
enum-head '{' enumerator-list ',' '}'
|
| 5374 |
```
|
| 5375 |
|
| 5376 |
``` bnf
|
| 5377 |
enum-head:
|
| 5378 |
enum-key attribute-specifier-seqₒₚₜ enum-head-nameₒₚₜ enum-baseₒₚₜ
|
|
|
|
| 5383 |
nested-name-specifierₒₚₜ identifier
|
| 5384 |
```
|
| 5385 |
|
| 5386 |
``` bnf
|
| 5387 |
opaque-enum-declaration:
|
| 5388 |
+
enum-key attribute-specifier-seqₒₚₜ enum-head-name enum-baseₒₚₜ ';'
|
| 5389 |
```
|
| 5390 |
|
| 5391 |
``` bnf
|
| 5392 |
enum-key:
|
| 5393 |
+
enum
|
| 5394 |
+
enum class
|
| 5395 |
+
enum struct
|
| 5396 |
```
|
| 5397 |
|
| 5398 |
``` bnf
|
| 5399 |
enum-base:
|
| 5400 |
':' type-specifier-seq
|
|
|
|
| 5442 |
|
| 5443 |
— *end example*]
|
| 5444 |
|
| 5445 |
— *end note*]
|
| 5446 |
|
| 5447 |
+
If the *enum-head-name* of an *opaque-enum-declaration* contains a
|
| 5448 |
+
*nested-name-specifier*, the declaration shall be an explicit
|
| 5449 |
+
specialization [[temp.expl.spec]].
|
| 5450 |
|
| 5451 |
The enumeration type declared with an *enum-key* of only `enum` is an
|
| 5452 |
*unscoped enumeration*, and its *enumerator*s are *unscoped
|
| 5453 |
enumerators*. The *enum-key*s `enum class` and `enum struct` are
|
| 5454 |
semantically equivalent; an enumeration type declared with one of these
|
| 5455 |
is a *scoped enumeration*, and its *enumerator*s are *scoped
|
| 5456 |
+
enumerators*. The optional *enum-head-name* shall not be omitted in the
|
| 5457 |
declaration of a scoped enumeration. The *type-specifier-seq* of an
|
| 5458 |
*enum-base* shall name an integral type; any cv-qualification is
|
| 5459 |
ignored. An *opaque-enum-declaration* declaring an unscoped enumeration
|
| 5460 |
shall not omit the *enum-base*. The identifiers in an *enumerator-list*
|
| 5461 |
are declared as constants, and can appear wherever constants are
|
|
|
|
| 5483 |
|
| 5484 |
An *opaque-enum-declaration* is either a redeclaration of an enumeration
|
| 5485 |
in the current scope or a declaration of a new enumeration.
|
| 5486 |
|
| 5487 |
[*Note 2*: An enumeration declared by an *opaque-enum-declaration* has
|
| 5488 |
+
a fixed underlying type and is a complete type. The list of enumerators
|
| 5489 |
can be provided in a later redeclaration with an
|
| 5490 |
*enum-specifier*. — *end note*]
|
| 5491 |
|
| 5492 |
A scoped enumeration shall not be later redeclared as unscoped or with a
|
| 5493 |
different underlying type. An unscoped enumeration shall not be later
|
| 5494 |
redeclared as scoped and each redeclaration shall include an *enum-base*
|
| 5495 |
specifying the same underlying type as in the original declaration.
|
| 5496 |
|
| 5497 |
+
If an *enum-head-name* contains a *nested-name-specifier*, it shall not
|
| 5498 |
+
begin with a *decltype-specifier* and the enclosing *enum-specifier* or
|
| 5499 |
+
*opaque-enum-declaration* shall refer to an enumeration that was
|
| 5500 |
+
previously declared directly in the class or namespace to which the
|
| 5501 |
+
*nested-name-specifier* refers, or in an element of the inline namespace
|
| 5502 |
+
set [[namespace.def]] of that namespace (i.e., neither inherited nor
|
| 5503 |
+
introduced by a *using-declaration*), and the *enum-specifier* or
|
| 5504 |
+
*opaque-enum-declaration* shall appear in a namespace enclosing the
|
| 5505 |
+
previous declaration.
|
| 5506 |
|
| 5507 |
Each enumeration defines a type that is different from all other types.
|
| 5508 |
Each enumeration also has an *underlying type*. The underlying type can
|
| 5509 |
be explicitly specified using an *enum-base*. For a scoped enumeration
|
| 5510 |
type, the underlying type is `int` if it is not explicitly specified. In
|
| 5511 |
both of these cases, the underlying type is said to be *fixed*.
|
| 5512 |
Following the closing brace of an *enum-specifier*, each enumerator has
|
| 5513 |
the type of its enumeration. If the underlying type is fixed, the type
|
| 5514 |
of each enumerator prior to the closing brace is the underlying type and
|
| 5515 |
the *constant-expression* in the *enumerator-definition* shall be a
|
| 5516 |
+
converted constant expression of the underlying type [[expr.const]]. If
|
| 5517 |
+
the underlying type is not fixed, the type of each enumerator prior to
|
| 5518 |
+
the closing brace is determined as follows:
|
| 5519 |
|
| 5520 |
- If an initializer is specified for an enumerator, the
|
| 5521 |
+
*constant-expression* shall be an integral constant expression
|
| 5522 |
+
[[expr.const]]. If the expression has unscoped enumeration type, the
|
| 5523 |
enumerator has the underlying type of that enumeration type, otherwise
|
| 5524 |
it has the same type as the expression.
|
| 5525 |
- If no initializer is specified for the first enumerator, its type is
|
| 5526 |
an unspecified signed integral type.
|
| 5527 |
- Otherwise the type of the enumerator is the same as that of the
|
|
|
|
| 5529 |
in that type, in which case the type is an unspecified integral type
|
| 5530 |
sufficient to contain the incremented value. If no such type exists,
|
| 5531 |
the program is ill-formed.
|
| 5532 |
|
| 5533 |
An enumeration whose underlying type is fixed is an incomplete type from
|
| 5534 |
+
its point of declaration [[basic.scope.pdecl]] to immediately after its
|
| 5535 |
+
*enum-base* (if any), at which point it becomes a complete type. An
|
| 5536 |
enumeration whose underlying type is not fixed is an incomplete type
|
| 5537 |
from its point of declaration to immediately after the closing `}` of
|
| 5538 |
its *enum-specifier*, at which point it becomes a complete type.
|
| 5539 |
|
| 5540 |
For an enumeration whose underlying type is not fixed, the underlying
|
|
|
|
| 5546 |
unless the value of an enumerator cannot fit in an `int` or
|
| 5547 |
`unsigned int`. If the *enumerator-list* is empty, the underlying type
|
| 5548 |
is as if the enumeration had a single enumerator with value 0.
|
| 5549 |
|
| 5550 |
For an enumeration whose underlying type is fixed, the values of the
|
| 5551 |
+
enumeration are the values of the underlying type. Otherwise, the values
|
| 5552 |
+
of the enumeration are the values representable by a hypothetical
|
| 5553 |
+
integer type with minimal width M such that all enumerators can be
|
| 5554 |
+
represented. The width of the smallest bit-field large enough to hold
|
| 5555 |
+
all the values of the enumeration type is M. It is possible to define an
|
| 5556 |
+
enumeration that has values not defined by any of its enumerators. If
|
| 5557 |
+
the *enumerator-list* is empty, the values of the enumeration are as if
|
| 5558 |
+
the enumeration had a single enumerator with value 0.[^9]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5559 |
|
| 5560 |
Two enumeration types are *layout-compatible enumerations* if they have
|
| 5561 |
the same underlying type.
|
| 5562 |
|
| 5563 |
The value of an enumerator or an object of an unscoped enumeration type
|
| 5564 |
+
is converted to an integer by integral promotion [[conv.prom]].
|
| 5565 |
|
| 5566 |
[*Example 3*:
|
| 5567 |
|
| 5568 |
``` cpp
|
| 5569 |
enum color { red, yellow, green=20, blue };
|
|
|
|
| 5596 |
|
| 5597 |
— *end example*]
|
| 5598 |
|
| 5599 |
Each *enum-name* and each unscoped *enumerator* is declared in the scope
|
| 5600 |
that immediately contains the *enum-specifier*. Each scoped *enumerator*
|
| 5601 |
+
is declared in the scope of the enumeration. An unnamed enumeration that
|
| 5602 |
+
does not have a typedef name for linkage purposes [[dcl.typedef]] and
|
| 5603 |
+
that has a first enumerator is denoted, for linkage purposes
|
| 5604 |
+
[[basic.link]], by its underlying type and its first enumerator; such an
|
| 5605 |
+
enumeration is said to have an enumerator as a name for linkage
|
| 5606 |
+
purposes. These names obey the scope rules defined for all names in
|
| 5607 |
+
[[basic.scope]] and [[basic.lookup]].
|
| 5608 |
+
|
| 5609 |
+
[*Note 3*: Each unnamed enumeration with no enumerators is a distinct
|
| 5610 |
+
type. — *end note*]
|
| 5611 |
|
| 5612 |
[*Example 4*:
|
| 5613 |
|
| 5614 |
``` cpp
|
| 5615 |
enum direction { left='l', right='r' };
|
|
|
|
| 5653 |
}
|
| 5654 |
```
|
| 5655 |
|
| 5656 |
— *end example*]
|
| 5657 |
|
| 5658 |
+
### The `using enum` declaration <a id="enum.udecl">[[enum.udecl]]</a>
|
| 5659 |
+
|
| 5660 |
+
``` bnf
|
| 5661 |
+
using-enum-declaration:
|
| 5662 |
+
'using' elaborated-enum-specifier ';'
|
| 5663 |
+
```
|
| 5664 |
+
|
| 5665 |
+
The *elaborated-enum-specifier* shall not name a dependent type and the
|
| 5666 |
+
type shall have a reachable *enum-specifier*.
|
| 5667 |
+
|
| 5668 |
+
A *using-enum-declaration* introduces the enumerator names of the named
|
| 5669 |
+
enumeration as if by a *using-declaration* for each enumerator.
|
| 5670 |
+
|
| 5671 |
+
[*Note 1*:
|
| 5672 |
+
|
| 5673 |
+
A *using-enum-declaration* in class scope adds the enumerators of the
|
| 5674 |
+
named enumeration as members to the scope. This means they are
|
| 5675 |
+
accessible for member lookup.
|
| 5676 |
+
|
| 5677 |
+
[*Example 1*:
|
| 5678 |
+
|
| 5679 |
+
``` cpp
|
| 5680 |
+
enum class fruit { orange, apple };
|
| 5681 |
+
struct S {
|
| 5682 |
+
using enum fruit; // OK, introduces orange and apple into S
|
| 5683 |
+
};
|
| 5684 |
+
void f() {
|
| 5685 |
+
S s;
|
| 5686 |
+
s.orange; // OK, names fruit::orange
|
| 5687 |
+
S::orange; // OK, names fruit::orange
|
| 5688 |
+
}
|
| 5689 |
+
```
|
| 5690 |
+
|
| 5691 |
+
— *end example*]
|
| 5692 |
+
|
| 5693 |
+
— *end note*]
|
| 5694 |
+
|
| 5695 |
+
[*Note 2*:
|
| 5696 |
+
|
| 5697 |
+
Two *using-enum-declaration*s that introduce two enumerators of the same
|
| 5698 |
+
name conflict.
|
| 5699 |
+
|
| 5700 |
+
[*Example 2*:
|
| 5701 |
+
|
| 5702 |
+
``` cpp
|
| 5703 |
+
enum class fruit { orange, apple };
|
| 5704 |
+
enum class color { red, orange };
|
| 5705 |
+
void f() {
|
| 5706 |
+
using enum fruit; // OK
|
| 5707 |
+
using enum color; // error: color::orange and fruit::orange conflict
|
| 5708 |
+
}
|
| 5709 |
+
```
|
| 5710 |
+
|
| 5711 |
+
— *end example*]
|
| 5712 |
+
|
| 5713 |
+
— *end note*]
|
| 5714 |
|
| 5715 |
## Namespaces <a id="basic.namespace">[[basic.namespace]]</a>
|
| 5716 |
|
| 5717 |
A namespace is an optionally-named declarative region. The name of a
|
| 5718 |
namespace can be used to access entities declared in that namespace;
|
| 5719 |
that is, the members of the namespace. Unlike other declarative regions,
|
| 5720 |
the definition of a namespace can be split over several parts of one or
|
| 5721 |
more translation units.
|
| 5722 |
|
| 5723 |
+
[*Note 1*: A namespace name with external linkage is exported if any of
|
| 5724 |
+
its *namespace-definition*s is exported, or if it contains any
|
| 5725 |
+
*export-declaration*s [[module.interface]]. A namespace is never
|
| 5726 |
+
attached to a module, and never has module linkage even if it is not
|
| 5727 |
+
exported. — *end note*]
|
| 5728 |
+
|
| 5729 |
+
[*Example 1*:
|
| 5730 |
+
|
| 5731 |
+
``` cpp
|
| 5732 |
+
export module M;
|
| 5733 |
+
namespace N1 {} // N1 is not exported
|
| 5734 |
+
export namespace N2 {} // N2 is exported
|
| 5735 |
+
namespace N3 { export int n; } // N3 is exported
|
| 5736 |
+
```
|
| 5737 |
+
|
| 5738 |
+
— *end example*]
|
| 5739 |
+
|
| 5740 |
The outermost declarative region of a translation unit is a namespace;
|
| 5741 |
see [[basic.scope.namespace]].
|
| 5742 |
|
| 5743 |
### Namespace definition <a id="namespace.def">[[namespace.def]]</a>
|
| 5744 |
|
|
|
|
| 5755 |
nested-namespace-definition
|
| 5756 |
```
|
| 5757 |
|
| 5758 |
``` bnf
|
| 5759 |
named-namespace-definition:
|
| 5760 |
+
inlineₒₚₜ namespace attribute-specifier-seqₒₚₜ identifier '{' namespace-body '}'
|
| 5761 |
```
|
| 5762 |
|
| 5763 |
``` bnf
|
| 5764 |
unnamed-namespace-definition:
|
| 5765 |
+
inlineₒₚₜ namespace attribute-specifier-seqₒₚₜ '{' namespace-body '}'
|
| 5766 |
```
|
| 5767 |
|
| 5768 |
``` bnf
|
| 5769 |
nested-namespace-definition:
|
| 5770 |
+
namespace enclosing-namespace-specifier '::' inlineₒₚₜ identifier '{' namespace-body '}'
|
| 5771 |
```
|
| 5772 |
|
| 5773 |
``` bnf
|
| 5774 |
enclosing-namespace-specifier:
|
| 5775 |
identifier
|
| 5776 |
+
enclosing-namespace-specifier '::' inlineₒₚₜ identifier
|
| 5777 |
```
|
| 5778 |
|
| 5779 |
``` bnf
|
| 5780 |
namespace-body:
|
| 5781 |
declaration-seqₒₚₜ
|
| 5782 |
```
|
| 5783 |
|
| 5784 |
+
Every *namespace-definition* shall appear at namespace scope
|
| 5785 |
+
[[basic.scope.namespace]].
|
| 5786 |
|
| 5787 |
In a *named-namespace-definition*, the *identifier* is the name of the
|
| 5788 |
+
namespace. If the *identifier*, when looked up [[basic.lookup.unqual]],
|
| 5789 |
+
refers to a *namespace-name* (but not a *namespace-alias*) that was
|
| 5790 |
+
introduced in the namespace in which the *named-namespace-definition*
|
| 5791 |
+
appears or that was introduced in a member of the inline namespace set
|
| 5792 |
+
of that namespace, the *namespace-definition* *extends* the
|
| 5793 |
+
previously-declared namespace. Otherwise, the *identifier* is introduced
|
| 5794 |
+
as a *namespace-name* into the declarative region in which the
|
| 5795 |
+
*named-namespace-definition* appears.
|
| 5796 |
|
| 5797 |
Because a *namespace-definition* contains *declaration*s in its
|
| 5798 |
*namespace-body* and a *namespace-definition* is itself a *declaration*,
|
| 5799 |
it follows that *namespace-definition*s can be nested.
|
| 5800 |
|
|
|
|
| 5848 |
appertains to the namespace being defined or extended.
|
| 5849 |
|
| 5850 |
Members of an inline namespace can be used in most respects as though
|
| 5851 |
they were members of the enclosing namespace. Specifically, the inline
|
| 5852 |
namespace and its enclosing namespace are both added to the set of
|
| 5853 |
+
associated namespaces used in argument-dependent lookup
|
| 5854 |
+
[[basic.lookup.argdep]] whenever one of them is, and a *using-directive*
|
| 5855 |
+
[[namespace.udir]] that names the inline namespace is implicitly
|
| 5856 |
+
inserted into the enclosing namespace as for an unnamed namespace
|
| 5857 |
+
[[namespace.unnamed]]. Furthermore, each member of the inline namespace
|
| 5858 |
+
can subsequently be partially specialized [[temp.class.spec]],
|
| 5859 |
+
explicitly instantiated [[temp.explicit]], or explicitly specialized
|
| 5860 |
+
[[temp.expl.spec]] as though it were a member of the enclosing
|
| 5861 |
+
namespace. Finally, looking up a name in the enclosing namespace via
|
| 5862 |
+
explicit qualification [[namespace.qual]] will include members of the
|
| 5863 |
+
inline namespace brought in by the *using-directive* even if there are
|
| 5864 |
+
declarations of that name in the enclosing namespace.
|
| 5865 |
|
| 5866 |
These properties are transitive: if a namespace `N` contains an inline
|
| 5867 |
namespace `M`, which in turn contains an inline namespace `O`, then the
|
| 5868 |
members of `O` can be used as though they were members of `M` or `N`.
|
| 5869 |
The *inline namespace set* of `N` is the transitive closure of all
|
|
|
|
| 5874 |
|
| 5875 |
A *nested-namespace-definition* with an *enclosing-namespace-specifier*
|
| 5876 |
`E`, *identifier* `I` and *namespace-body* `B` is equivalent to
|
| 5877 |
|
| 5878 |
``` cpp
|
| 5879 |
+
namespace E { \opt{inline} namespace I { B } }
|
| 5880 |
```
|
| 5881 |
|
| 5882 |
+
where the optional `inline` is present if and only if the *identifier*
|
| 5883 |
+
`I` is preceded by `inline`.
|
| 5884 |
+
|
| 5885 |
[*Example 3*:
|
| 5886 |
|
| 5887 |
``` cpp
|
| 5888 |
+
namespace A::inline B::C {
|
| 5889 |
int i;
|
| 5890 |
}
|
| 5891 |
```
|
| 5892 |
|
| 5893 |
The above has the same effect as:
|
| 5894 |
|
| 5895 |
``` cpp
|
| 5896 |
namespace A {
|
| 5897 |
+
inline namespace B {
|
| 5898 |
namespace C {
|
| 5899 |
int i;
|
| 5900 |
}
|
| 5901 |
}
|
| 5902 |
}
|
|
|
|
| 5907 |
#### Unnamed namespaces <a id="namespace.unnamed">[[namespace.unnamed]]</a>
|
| 5908 |
|
| 5909 |
An *unnamed-namespace-definition* behaves as if it were replaced by
|
| 5910 |
|
| 5911 |
``` bnf
|
| 5912 |
+
inlineₒₚₜ namespace unique '{' '/* empty body */' '}'
|
| 5913 |
+
using namespace unique ';'
|
| 5914 |
+
namespace unique '{' namespace-body '}'
|
| 5915 |
```
|
| 5916 |
|
| 5917 |
where `inline` appears if and only if it appears in the
|
| 5918 |
+
*unnamed-namespace-definition* and all occurrences of *`unique`* in a
|
| 5919 |
translation unit are replaced by the same identifier, and this
|
| 5920 |
identifier differs from all other identifiers in the translation unit.
|
| 5921 |
The optional *attribute-specifier-seq* in the
|
| 5922 |
+
*unnamed-namespace-definition* appertains to *`unique`*.
|
| 5923 |
|
| 5924 |
[*Example 1*:
|
| 5925 |
|
| 5926 |
``` cpp
|
| 5927 |
namespace { int i; } // unique::i
|
|
|
|
| 5946 |
— *end example*]
|
| 5947 |
|
| 5948 |
#### Namespace member definitions <a id="namespace.memdef">[[namespace.memdef]]</a>
|
| 5949 |
|
| 5950 |
A declaration in a namespace `N` (excluding declarations in nested
|
| 5951 |
+
scopes) whose *declarator-id* is an *unqualified-id* [[dcl.meaning]],
|
| 5952 |
+
whose *class-head-name* [[class.pre]] or *enum-head-name* [[dcl.enum]]
|
| 5953 |
+
is an *identifier*, or whose *elaborated-type-specifier* is of the form
|
| 5954 |
+
*class-key* *attribute-specifier-seq*ₒₚₜ *identifier*
|
| 5955 |
+
[[dcl.type.elab]], or that is an *opaque-enum-declaration*, declares (or
|
| 5956 |
+
redeclares) its *unqualified-id* or *identifier* as a member of `N`.
|
| 5957 |
|
| 5958 |
+
[*Note 1*: An explicit instantiation [[temp.explicit]] or explicit
|
| 5959 |
+
specialization [[temp.expl.spec]] of a template does not introduce a
|
| 5960 |
name and thus may be declared using an *unqualified-id* in a member of
|
| 5961 |
the enclosing namespace set, if the primary template is declared in an
|
| 5962 |
inline namespace. — *end note*]
|
| 5963 |
|
| 5964 |
[*Example 1*:
|
|
|
|
| 5976 |
```
|
| 5977 |
|
| 5978 |
— *end example*]
|
| 5979 |
|
| 5980 |
Members of a named namespace can also be defined outside that namespace
|
| 5981 |
+
by explicit qualification [[namespace.qual]] of the name being defined,
|
| 5982 |
+
provided that the entity being defined was already declared in the
|
| 5983 |
+
namespace and the definition appears after the point of declaration in a
|
| 5984 |
+
namespace that encloses the declaration’s namespace.
|
| 5985 |
|
| 5986 |
[*Example 2*:
|
| 5987 |
|
| 5988 |
``` cpp
|
| 5989 |
namespace Q {
|
|
|
|
| 6002 |
}
|
| 6003 |
```
|
| 6004 |
|
| 6005 |
— *end example*]
|
| 6006 |
|
| 6007 |
+
If a friend declaration in a non-local class first declares a class,
|
| 6008 |
+
function, class template or function template[^10] the friend is a
|
| 6009 |
+
member of the innermost enclosing namespace. The friend declaration does
|
| 6010 |
+
not by itself make the name visible to unqualified lookup
|
| 6011 |
+
[[basic.lookup.unqual]] or qualified lookup [[basic.lookup.qual]].
|
| 6012 |
|
| 6013 |
[*Note 2*: The name of the friend will be visible in its namespace if a
|
| 6014 |
matching declaration is provided at namespace scope (either before or
|
| 6015 |
after the class definition granting friendship). — *end note*]
|
| 6016 |
|
| 6017 |
If a friend function or function template is called, its name may be
|
| 6018 |
found by the name lookup that considers functions from namespaces and
|
| 6019 |
+
classes associated with the types of the function arguments
|
| 6020 |
+
[[basic.lookup.argdep]]. If the name in a friend declaration is neither
|
| 6021 |
+
qualified nor a *template-id* and the declaration is a function or an
|
| 6022 |
+
*elaborated-type-specifier*, the lookup to determine whether the entity
|
| 6023 |
+
has been previously declared shall not consider any scopes outside the
|
| 6024 |
+
innermost enclosing namespace.
|
| 6025 |
|
| 6026 |
+
[*Note 3*: The other forms of friend declarations cannot declare a new
|
| 6027 |
+
member of the innermost enclosing namespace and thus follow the usual
|
| 6028 |
+
lookup rules. — *end note*]
|
| 6029 |
|
| 6030 |
[*Example 3*:
|
| 6031 |
|
| 6032 |
``` cpp
|
| 6033 |
// Assume f and g have not yet been declared.
|
|
|
|
| 6073 |
identifier
|
| 6074 |
```
|
| 6075 |
|
| 6076 |
``` bnf
|
| 6077 |
namespace-alias-definition:
|
| 6078 |
+
namespace identifier '=' qualified-namespace-specifier ';'
|
| 6079 |
```
|
| 6080 |
|
| 6081 |
``` bnf
|
| 6082 |
qualified-namespace-specifier:
|
| 6083 |
nested-name-specifierₒₚₜ namespace-name
|
|
|
|
| 6106 |
namespace CWVLN = CWVLN;
|
| 6107 |
```
|
| 6108 |
|
| 6109 |
— *end example*]
|
| 6110 |
|
| 6111 |
+
### Using namespace directive <a id="namespace.udir">[[namespace.udir]]</a>
|
| 6112 |
+
|
| 6113 |
+
``` bnf
|
| 6114 |
+
using-directive:
|
| 6115 |
+
attribute-specifier-seqₒₚₜ using namespace nested-name-specifierₒₚₜ namespace-name ';'
|
| 6116 |
+
```
|
| 6117 |
+
|
| 6118 |
+
A *using-directive* shall not appear in class scope, but may appear in
|
| 6119 |
+
namespace scope or in block scope.
|
| 6120 |
+
|
| 6121 |
+
[*Note 1*: When looking up a *namespace-name* in a *using-directive*,
|
| 6122 |
+
only namespace names are considered, see
|
| 6123 |
+
[[basic.lookup.udir]]. — *end note*]
|
| 6124 |
+
|
| 6125 |
+
The optional *attribute-specifier-seq* appertains to the
|
| 6126 |
+
*using-directive*.
|
| 6127 |
+
|
| 6128 |
+
A *using-directive* specifies that the names in the nominated namespace
|
| 6129 |
+
can be used in the scope in which the *using-directive* appears after
|
| 6130 |
+
the *using-directive*. During unqualified name lookup
|
| 6131 |
+
[[basic.lookup.unqual]], the names appear as if they were declared in
|
| 6132 |
+
the nearest enclosing namespace which contains both the
|
| 6133 |
+
*using-directive* and the nominated namespace.
|
| 6134 |
+
|
| 6135 |
+
[*Note 2*: In this context, “contains” means “contains directly or
|
| 6136 |
+
indirectly”. — *end note*]
|
| 6137 |
+
|
| 6138 |
+
A *using-directive* does not add any members to the declarative region
|
| 6139 |
+
in which it appears.
|
| 6140 |
+
|
| 6141 |
+
[*Example 1*:
|
| 6142 |
+
|
| 6143 |
+
``` cpp
|
| 6144 |
+
namespace A {
|
| 6145 |
+
int i;
|
| 6146 |
+
namespace B {
|
| 6147 |
+
namespace C {
|
| 6148 |
+
int i;
|
| 6149 |
+
}
|
| 6150 |
+
using namespace A::B::C;
|
| 6151 |
+
void f1() {
|
| 6152 |
+
i = 5; // OK, C::i visible in B and hides A::i
|
| 6153 |
+
}
|
| 6154 |
+
}
|
| 6155 |
+
namespace D {
|
| 6156 |
+
using namespace B;
|
| 6157 |
+
using namespace C;
|
| 6158 |
+
void f2() {
|
| 6159 |
+
i = 5; // ambiguous, B::C::i or A::i?
|
| 6160 |
+
}
|
| 6161 |
+
}
|
| 6162 |
+
void f3() {
|
| 6163 |
+
i = 5; // uses A::i
|
| 6164 |
+
}
|
| 6165 |
+
}
|
| 6166 |
+
void f4() {
|
| 6167 |
+
i = 5; // error: neither i is visible
|
| 6168 |
+
}
|
| 6169 |
+
```
|
| 6170 |
+
|
| 6171 |
+
— *end example*]
|
| 6172 |
+
|
| 6173 |
+
For unqualified lookup [[basic.lookup.unqual]], the *using-directive* is
|
| 6174 |
+
transitive: if a scope contains a *using-directive* that nominates a
|
| 6175 |
+
second namespace that itself contains *using-directive*s, the effect is
|
| 6176 |
+
as if the *using-directive*s from the second namespace also appeared in
|
| 6177 |
+
the first.
|
| 6178 |
+
|
| 6179 |
+
[*Note 3*: For qualified lookup, see
|
| 6180 |
+
[[namespace.qual]]. — *end note*]
|
| 6181 |
+
|
| 6182 |
+
[*Example 2*:
|
| 6183 |
+
|
| 6184 |
+
``` cpp
|
| 6185 |
+
namespace M {
|
| 6186 |
+
int i;
|
| 6187 |
+
}
|
| 6188 |
+
|
| 6189 |
+
namespace N {
|
| 6190 |
+
int i;
|
| 6191 |
+
using namespace M;
|
| 6192 |
+
}
|
| 6193 |
+
|
| 6194 |
+
void f() {
|
| 6195 |
+
using namespace N;
|
| 6196 |
+
i = 7; // error: both M::i and N::i are visible
|
| 6197 |
+
}
|
| 6198 |
+
```
|
| 6199 |
+
|
| 6200 |
+
For another example,
|
| 6201 |
+
|
| 6202 |
+
``` cpp
|
| 6203 |
+
namespace A {
|
| 6204 |
+
int i;
|
| 6205 |
+
}
|
| 6206 |
+
namespace B {
|
| 6207 |
+
int i;
|
| 6208 |
+
int j;
|
| 6209 |
+
namespace C {
|
| 6210 |
+
namespace D {
|
| 6211 |
+
using namespace A;
|
| 6212 |
+
int j;
|
| 6213 |
+
int k;
|
| 6214 |
+
int a = i; // B::i hides A::i
|
| 6215 |
+
}
|
| 6216 |
+
using namespace D;
|
| 6217 |
+
int k = 89; // no problem yet
|
| 6218 |
+
int l = k; // ambiguous: C::k or D::k
|
| 6219 |
+
int m = i; // B::i hides A::i
|
| 6220 |
+
int n = j; // D::j hides B::j
|
| 6221 |
+
}
|
| 6222 |
+
}
|
| 6223 |
+
```
|
| 6224 |
+
|
| 6225 |
+
— *end example*]
|
| 6226 |
+
|
| 6227 |
+
If a namespace is extended [[namespace.def]] after a *using-directive*
|
| 6228 |
+
for that namespace is given, the additional members of the extended
|
| 6229 |
+
namespace and the members of namespaces nominated by *using-directive*s
|
| 6230 |
+
in the extending *namespace-definition* can be used after the extending
|
| 6231 |
+
*namespace-definition*.
|
| 6232 |
+
|
| 6233 |
+
[*Note 4*:
|
| 6234 |
+
|
| 6235 |
+
If name lookup finds a declaration for a name in two different
|
| 6236 |
+
namespaces, and the declarations do not declare the same entity and do
|
| 6237 |
+
not declare functions or function templates, the use of the name is
|
| 6238 |
+
ill-formed [[basic.lookup]]. In particular, the name of a variable,
|
| 6239 |
+
function or enumerator does not hide the name of a class or enumeration
|
| 6240 |
+
declared in a different namespace. For example,
|
| 6241 |
+
|
| 6242 |
+
``` cpp
|
| 6243 |
+
namespace A {
|
| 6244 |
+
class X { };
|
| 6245 |
+
extern "C" int g();
|
| 6246 |
+
extern "C++" int h();
|
| 6247 |
+
}
|
| 6248 |
+
namespace B {
|
| 6249 |
+
void X(int);
|
| 6250 |
+
extern "C" int g();
|
| 6251 |
+
extern "C++" int h(int);
|
| 6252 |
+
}
|
| 6253 |
+
using namespace A;
|
| 6254 |
+
using namespace B;
|
| 6255 |
+
|
| 6256 |
+
void f() {
|
| 6257 |
+
X(1); // error: name X found in two namespaces
|
| 6258 |
+
g(); // OK: name g refers to the same entity
|
| 6259 |
+
h(); // OK: overload resolution selects A::h
|
| 6260 |
+
}
|
| 6261 |
+
```
|
| 6262 |
+
|
| 6263 |
+
— *end note*]
|
| 6264 |
+
|
| 6265 |
+
During overload resolution, all functions from the transitive search are
|
| 6266 |
+
considered for argument matching. The set of declarations found by the
|
| 6267 |
+
transitive search is unordered.
|
| 6268 |
+
|
| 6269 |
+
[*Note 5*: In particular, the order in which namespaces were considered
|
| 6270 |
+
and the relationships among the namespaces implied by the
|
| 6271 |
+
*using-directive*s do not cause preference to be given to any of the
|
| 6272 |
+
declarations found by the search. — *end note*]
|
| 6273 |
+
|
| 6274 |
+
An ambiguity exists if the best match finds two functions with the same
|
| 6275 |
+
signature, even if one is in a namespace reachable through
|
| 6276 |
+
*using-directive*s in the namespace of the other.[^11]
|
| 6277 |
+
|
| 6278 |
+
[*Example 3*:
|
| 6279 |
+
|
| 6280 |
+
``` cpp
|
| 6281 |
+
namespace D {
|
| 6282 |
+
int d1;
|
| 6283 |
+
void f(char);
|
| 6284 |
+
}
|
| 6285 |
+
using namespace D;
|
| 6286 |
+
|
| 6287 |
+
int d1; // OK: no conflict with D::d1
|
| 6288 |
+
|
| 6289 |
+
namespace E {
|
| 6290 |
+
int e;
|
| 6291 |
+
void f(int);
|
| 6292 |
+
}
|
| 6293 |
+
|
| 6294 |
+
namespace D { // namespace extension
|
| 6295 |
+
int d2;
|
| 6296 |
+
using namespace E;
|
| 6297 |
+
void f(int);
|
| 6298 |
+
}
|
| 6299 |
+
|
| 6300 |
+
void f() {
|
| 6301 |
+
d1++; // error: ambiguous ::d1 or D::d1?
|
| 6302 |
+
::d1++; // OK
|
| 6303 |
+
D::d1++; // OK
|
| 6304 |
+
d2++; // OK: D::d2
|
| 6305 |
+
e++; // OK: E::e
|
| 6306 |
+
f(1); // error: ambiguous: D::f(int) or E::f(int)?
|
| 6307 |
+
f('a'); // OK: D::f(char)
|
| 6308 |
+
}
|
| 6309 |
+
```
|
| 6310 |
+
|
| 6311 |
+
— *end example*]
|
| 6312 |
+
|
| 6313 |
+
## The `using` declaration <a id="namespace.udecl">[[namespace.udecl]]</a>
|
| 6314 |
|
| 6315 |
``` bnf
|
| 6316 |
using-declaration:
|
| 6317 |
+
using using-declarator-list ';'
|
| 6318 |
```
|
| 6319 |
|
| 6320 |
``` bnf
|
| 6321 |
using-declarator-list:
|
| 6322 |
using-declarator '...'ₒₚₜ
|
| 6323 |
using-declarator-list ',' using-declarator '...'ₒₚₜ
|
| 6324 |
```
|
| 6325 |
|
| 6326 |
``` bnf
|
| 6327 |
using-declarator:
|
| 6328 |
+
typenameₒₚₜ nested-name-specifier unqualified-id
|
| 6329 |
```
|
| 6330 |
|
| 6331 |
+
Each *using-declarator* in a *using-declaration* [^12] introduces a set
|
| 6332 |
of declarations into the declarative region in which the
|
| 6333 |
*using-declaration* appears. The set of declarations introduced by the
|
| 6334 |
*using-declarator* is found by performing qualified name lookup (
|
| 6335 |
[[basic.lookup.qual]], [[class.member.lookup]]) for the name in the
|
| 6336 |
*using-declarator*, excluding functions that are hidden as described
|
|
|
|
| 6369 |
```
|
| 6370 |
|
| 6371 |
— *end example*]
|
| 6372 |
|
| 6373 |
In a *using-declaration* used as a *member-declaration*, each
|
| 6374 |
+
*using-declarator* shall either name an enumerator or have a
|
| 6375 |
+
*nested-name-specifier* naming a base class of the class being defined.
|
|
|
|
|
|
|
| 6376 |
|
| 6377 |
[*Example 2*:
|
| 6378 |
|
| 6379 |
+
``` cpp
|
| 6380 |
+
enum class button { up, down };
|
| 6381 |
+
struct S {
|
| 6382 |
+
using button::up;
|
| 6383 |
+
button b = up; // OK
|
| 6384 |
+
};
|
| 6385 |
+
```
|
| 6386 |
+
|
| 6387 |
+
— *end example*]
|
| 6388 |
+
|
| 6389 |
+
If a *using-declarator* names a constructor, its *nested-name-specifier*
|
| 6390 |
+
shall name a direct base class of the class being defined.
|
| 6391 |
+
|
| 6392 |
+
[*Example 3*:
|
| 6393 |
+
|
| 6394 |
``` cpp
|
| 6395 |
template <typename... bases>
|
| 6396 |
struct X : bases... {
|
| 6397 |
using bases::g...;
|
| 6398 |
};
|
|
|
|
| 6400 |
X<B, D> x; // OK: B::g and D::g introduced
|
| 6401 |
```
|
| 6402 |
|
| 6403 |
— *end example*]
|
| 6404 |
|
| 6405 |
+
[*Example 4*:
|
| 6406 |
|
| 6407 |
``` cpp
|
| 6408 |
class C {
|
| 6409 |
int g();
|
| 6410 |
};
|
|
|
|
| 6421 |
|
| 6422 |
[*Note 2*: Since destructors do not have names, a *using-declaration*
|
| 6423 |
cannot refer to a destructor for a base class. Since specializations of
|
| 6424 |
member templates for conversion functions are not found by name lookup,
|
| 6425 |
they are not considered when a *using-declaration* specifies a
|
| 6426 |
+
conversion function [[temp.mem]]. — *end note*]
|
| 6427 |
|
| 6428 |
If a constructor or assignment operator brought from a base class into a
|
| 6429 |
derived class has the signature of a copy/move constructor or assignment
|
| 6430 |
+
operator for the derived class ([[class.copy.ctor]],
|
| 6431 |
+
[[class.copy.assign]]), the *using-declaration* does not by itself
|
| 6432 |
+
suppress the implicit declaration of the derived class member; the
|
| 6433 |
+
member from the base class is hidden or overridden by the
|
| 6434 |
+
implicitly-declared copy/move constructor or assignment operator of the
|
| 6435 |
+
derived class, as described below.
|
| 6436 |
|
| 6437 |
A *using-declaration* shall not name a *template-id*.
|
| 6438 |
|
| 6439 |
+
[*Example 5*:
|
| 6440 |
|
| 6441 |
``` cpp
|
| 6442 |
struct A {
|
| 6443 |
template <class T> void f(T);
|
| 6444 |
template <class T> struct X { };
|
| 6445 |
};
|
| 6446 |
struct B : A {
|
| 6447 |
+
using A::f<double>; // error
|
| 6448 |
+
using A::X<int>; // error
|
| 6449 |
};
|
| 6450 |
```
|
| 6451 |
|
| 6452 |
— *end example*]
|
| 6453 |
|
| 6454 |
A *using-declaration* shall not name a namespace.
|
| 6455 |
|
| 6456 |
+
A *using-declaration* that names a class member other than an enumerator
|
| 6457 |
+
shall be a *member-declaration*.
|
| 6458 |
|
| 6459 |
+
[*Example 6*:
|
|
|
|
|
|
|
|
|
|
| 6460 |
|
| 6461 |
``` cpp
|
| 6462 |
struct X {
|
| 6463 |
int i;
|
| 6464 |
static int s;
|
|
|
|
| 6471 |
```
|
| 6472 |
|
| 6473 |
— *end example*]
|
| 6474 |
|
| 6475 |
Members declared by a *using-declaration* can be referred to by explicit
|
| 6476 |
+
qualification just like other member names [[namespace.qual]].
|
| 6477 |
|
| 6478 |
+
[*Example 7*:
|
| 6479 |
|
| 6480 |
``` cpp
|
| 6481 |
void f();
|
| 6482 |
|
| 6483 |
namespace A {
|
|
|
|
| 6499 |
— *end example*]
|
| 6500 |
|
| 6501 |
A *using-declaration* is a *declaration* and can therefore be used
|
| 6502 |
repeatedly where (and only where) multiple declarations are allowed.
|
| 6503 |
|
| 6504 |
+
[*Example 8*:
|
| 6505 |
|
| 6506 |
``` cpp
|
| 6507 |
namespace A {
|
| 6508 |
int i;
|
| 6509 |
}
|
|
|
|
| 6526 |
[*Note 3*: For a *using-declaration* whose *nested-name-specifier*
|
| 6527 |
names a namespace, members added to the namespace after the
|
| 6528 |
*using-declaration* are not in the set of introduced declarations, so
|
| 6529 |
they are not considered when a use of the name is made. Thus, additional
|
| 6530 |
overloads added after the *using-declaration* are ignored, but default
|
| 6531 |
+
function arguments [[dcl.fct.default]], default template arguments
|
| 6532 |
+
[[temp.param]], and template specializations ([[temp.class.spec]],
|
| 6533 |
[[temp.expl.spec]]) are considered. — *end note*]
|
| 6534 |
|
| 6535 |
+
[*Example 9*:
|
| 6536 |
|
| 6537 |
``` cpp
|
| 6538 |
namespace A {
|
| 6539 |
void f(int);
|
| 6540 |
}
|
|
|
|
| 6559 |
[*Note 4*: Partial specializations of class templates are found by
|
| 6560 |
looking up the primary class template and then considering all partial
|
| 6561 |
specializations of that template. If a *using-declaration* names a class
|
| 6562 |
template, partial specializations introduced after the
|
| 6563 |
*using-declaration* are effectively visible because the primary template
|
| 6564 |
+
is visible [[temp.class.spec]]. — *end note*]
|
| 6565 |
|
| 6566 |
Since a *using-declaration* is a declaration, the restrictions on
|
| 6567 |
+
declarations of the same name in the same declarative region
|
| 6568 |
+
[[basic.scope]] also apply to *using-declaration*s.
|
| 6569 |
|
| 6570 |
+
[*Example 10*:
|
| 6571 |
|
| 6572 |
``` cpp
|
| 6573 |
namespace A {
|
| 6574 |
int x;
|
| 6575 |
}
|
|
|
|
| 6600 |
```
|
| 6601 |
|
| 6602 |
— *end example*]
|
| 6603 |
|
| 6604 |
If a function declaration in namespace scope or block scope has the same
|
| 6605 |
+
name and the same parameter-type-list [[dcl.fct]] as a function
|
| 6606 |
introduced by a *using-declaration*, and the declarations do not declare
|
| 6607 |
the same function, the program is ill-formed. If a function template
|
| 6608 |
declaration in namespace scope has the same name, parameter-type-list,
|
| 6609 |
+
trailing *requires-clause* (if any), return type, and *template-head*,
|
| 6610 |
+
as a function template introduced by a *using-declaration*, the program
|
| 6611 |
+
is ill-formed.
|
| 6612 |
|
| 6613 |
[*Note 5*:
|
| 6614 |
|
| 6615 |
Two *using-declaration*s may introduce functions with the same name and
|
| 6616 |
the same parameter-type-list. If, for a call to an unqualified function
|
| 6617 |
name, function overload resolution selects the functions introduced by
|
| 6618 |
such *using-declaration*s, the function call is ill-formed.
|
| 6619 |
|
| 6620 |
+
[*Example 11*:
|
| 6621 |
|
| 6622 |
``` cpp
|
| 6623 |
namespace B {
|
| 6624 |
void f(int);
|
| 6625 |
void f(double);
|
|
|
|
| 6644 |
— *end note*]
|
| 6645 |
|
| 6646 |
When a *using-declarator* brings declarations from a base class into a
|
| 6647 |
derived class, member functions and member function templates in the
|
| 6648 |
derived class override and/or hide member functions and member function
|
| 6649 |
+
templates with the same name, parameter-type-list [[dcl.fct]], trailing
|
| 6650 |
+
*requires-clause* (if any), cv-qualification, and *ref-qualifier* (if
|
| 6651 |
+
any), in a base class (rather than conflicting). Such hidden or
|
| 6652 |
+
overridden declarations are excluded from the set of declarations
|
| 6653 |
+
introduced by the *using-declarator*.
|
| 6654 |
|
| 6655 |
+
[*Example 12*:
|
| 6656 |
|
| 6657 |
``` cpp
|
| 6658 |
struct B {
|
| 6659 |
virtual void f(int);
|
| 6660 |
virtual void f(char);
|
|
|
|
| 6691 |
|
| 6692 |
struct D1 : B1, B2 {
|
| 6693 |
using B1::B1;
|
| 6694 |
using B2::B2;
|
| 6695 |
};
|
| 6696 |
+
D1 d1(0); // error: ambiguous
|
| 6697 |
|
| 6698 |
struct D2 : B1, B2 {
|
| 6699 |
using B1::B1;
|
| 6700 |
using B2::B2;
|
| 6701 |
D2(int); // OK: D2::D2(int) hides B1::B1(int) and B2::B2(int)
|
|
|
|
| 6703 |
D2 d2(0); // calls D2::D2(int)
|
| 6704 |
```
|
| 6705 |
|
| 6706 |
— *end example*]
|
| 6707 |
|
| 6708 |
+
[*Note 6*: For the purpose of forming a set of candidates during
|
| 6709 |
+
overload resolution, the functions that are introduced by a
|
| 6710 |
+
*using-declaration* into a derived class are treated as though they were
|
| 6711 |
+
members of the derived class [[class.member.lookup]]. In particular, the
|
| 6712 |
+
implicit object parameter is treated as if it were a reference to the
|
| 6713 |
+
derived class rather than to the base class [[over.match.funcs]]. This
|
| 6714 |
+
has no effect on the type of the function, and in all other respects the
|
| 6715 |
+
function remains a member of the base class. — *end note*]
|
| 6716 |
+
|
| 6717 |
+
Constructors that are introduced by a *using-declaration* are treated as
|
| 6718 |
+
though they were constructors of the derived class when looking up the
|
| 6719 |
+
constructors of the derived class [[class.qual]] or forming a set of
|
| 6720 |
+
overload candidates ([[over.match.ctor]], [[over.match.copy]],
|
| 6721 |
+
[[over.match.list]]).
|
| 6722 |
+
|
| 6723 |
+
[*Note 7*: If such a constructor is selected to perform the
|
| 6724 |
+
initialization of an object of class type, all subobjects other than the
|
| 6725 |
+
base class from which the constructor originated are implicitly
|
| 6726 |
+
initialized [[class.inhctor.init]]. A constructor of a derived class is
|
| 6727 |
+
sometimes preferred to a constructor of a base class if they would
|
| 6728 |
+
otherwise be ambiguous [[over.match.best]]. — *end note*]
|
| 6729 |
|
| 6730 |
In a *using-declarator* that does not name a constructor, all members of
|
| 6731 |
the set of introduced declarations shall be accessible. In a
|
| 6732 |
*using-declarator* that names a constructor, no access check is
|
| 6733 |
performed. In particular, if a derived class uses a *using-declarator*
|
|
|
|
| 6736 |
named shall be accessible. The base class members mentioned by a
|
| 6737 |
*using-declarator* shall be visible in the scope of at least one of the
|
| 6738 |
direct base classes of the class where the *using-declarator* is
|
| 6739 |
specified.
|
| 6740 |
|
| 6741 |
+
[*Note 8*:
|
| 6742 |
|
| 6743 |
Because a *using-declarator* designates a base class member (and not a
|
| 6744 |
member subobject or a member function of a base class subobject), a
|
| 6745 |
*using-declarator* cannot be used to resolve inherited member
|
| 6746 |
ambiguities.
|
| 6747 |
|
| 6748 |
+
[*Example 13*:
|
| 6749 |
|
| 6750 |
``` cpp
|
| 6751 |
struct A { int x(); };
|
| 6752 |
struct B : A { };
|
| 6753 |
struct C : A {
|
|
|
|
| 6773 |
constructor does not create a synonym; instead, the additional
|
| 6774 |
constructors are accessible if they would be accessible when used to
|
| 6775 |
construct an object of the corresponding base class, and the
|
| 6776 |
accessibility of the *using-declaration* is ignored.
|
| 6777 |
|
| 6778 |
+
[*Example 14*:
|
| 6779 |
|
| 6780 |
``` cpp
|
| 6781 |
class A {
|
| 6782 |
private:
|
| 6783 |
void f(char);
|
|
|
|
| 6795 |
```
|
| 6796 |
|
| 6797 |
— *end example*]
|
| 6798 |
|
| 6799 |
If a *using-declarator* uses the keyword `typename` and specifies a
|
| 6800 |
+
dependent name [[temp.dep]], the name introduced by the
|
| 6801 |
+
*using-declaration* is treated as a *typedef-name* [[dcl.typedef]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6802 |
|
| 6803 |
## The `asm` declaration <a id="dcl.asm">[[dcl.asm]]</a>
|
| 6804 |
|
| 6805 |
An `asm` declaration has the form
|
| 6806 |
|
| 6807 |
``` bnf
|
| 6808 |
+
asm-declaration:
|
| 6809 |
+
attribute-specifier-seqₒₚₜ asm '(' string-literal ')' ';'
|
| 6810 |
```
|
| 6811 |
|
| 6812 |
The `asm` declaration is conditionally-supported; its meaning is
|
| 6813 |
*implementation-defined*. The optional *attribute-specifier-seq* in an
|
| 6814 |
+
*asm-declaration* appertains to the `asm` declaration.
|
| 6815 |
|
| 6816 |
[*Note 1*: Typically it is used to pass information through the
|
| 6817 |
implementation to an assembler. — *end note*]
|
| 6818 |
|
| 6819 |
## Linkage specifications <a id="dcl.link">[[dcl.link]]</a>
|
|
|
|
| 6827 |
associated with a particular form of representing names of objects and
|
| 6828 |
functions with external linkage, or with a particular calling
|
| 6829 |
convention, etc. — *end note*]
|
| 6830 |
|
| 6831 |
The default language linkage of all function types, function names, and
|
| 6832 |
+
variable names is C++ language linkage. Two function types with
|
| 6833 |
+
different language linkages are distinct types even if they are
|
| 6834 |
+
otherwise identical.
|
| 6835 |
|
| 6836 |
+
Linkage [[basic.link]] between C++ and non-C++ code fragments can be
|
| 6837 |
achieved using a *linkage-specification*:
|
| 6838 |
|
| 6839 |
``` bnf
|
| 6840 |
linkage-specification:
|
| 6841 |
+
extern string-literal '{' declaration-seqₒₚₜ '}'
|
| 6842 |
+
extern string-literal declaration
|
| 6843 |
```
|
| 6844 |
|
| 6845 |
The *string-literal* indicates the required language linkage. This
|
| 6846 |
+
document specifies the semantics for the *string-literal*s `"C"` and
|
| 6847 |
+
`"C++"`. Use of a *string-literal* other than `"C"` or `"C++"` is
|
| 6848 |
+
conditionally-supported, with *implementation-defined* semantics.
|
| 6849 |
|
| 6850 |
[*Note 2*: Therefore, a linkage-specification with a *string-literal*
|
| 6851 |
that is unknown to the implementation requires a
|
| 6852 |
diagnostic. — *end note*]
|
| 6853 |
|
|
|
|
| 6855 |
be taken from the document defining that language. For example, `Ada`
|
| 6856 |
(not `ADA`) and `Fortran` or `FORTRAN`, depending on the
|
| 6857 |
vintage. — *end note*]
|
| 6858 |
|
| 6859 |
Every implementation shall provide for linkage to functions written in
|
| 6860 |
+
the C programming language, `"C"`, and linkage to C++ functions,
|
| 6861 |
+
`"C++"`.
|
| 6862 |
|
| 6863 |
[*Example 1*:
|
| 6864 |
|
| 6865 |
``` cpp
|
| 6866 |
+
complex sqrt(complex); // C++{} linkage by default
|
| 6867 |
extern "C" {
|
| 6868 |
double sqrt(double); // C linkage
|
| 6869 |
}
|
| 6870 |
```
|
| 6871 |
|
| 6872 |
— *end example*]
|
| 6873 |
|
| 6874 |
+
A *module-import-declaration* shall not be directly contained in a
|
| 6875 |
+
*linkage-specification*. A *module-import-declaration* appearing in a
|
| 6876 |
+
linkage specification with other than C++ language linkage is
|
| 6877 |
+
conditionally-supported with *implementation-defined* semantics.
|
| 6878 |
+
|
| 6879 |
Linkage specifications nest. When linkage specifications nest, the
|
| 6880 |
innermost one determines the language linkage. A linkage specification
|
| 6881 |
does not establish a scope. A *linkage-specification* shall occur only
|
| 6882 |
+
in namespace scope [[basic.scope]]. In a *linkage-specification*, the
|
| 6883 |
specified language linkage applies to the function types of all function
|
| 6884 |
declarators, function names with external linkage, and variable names
|
| 6885 |
with external linkage declared within the *linkage-specification*.
|
| 6886 |
|
| 6887 |
[*Example 2*:
|
|
|
|
| 6889 |
``` cpp
|
| 6890 |
extern "C" // the name f1 and its function type have C language linkage;
|
| 6891 |
void f1(void(*pf)(int)); // pf is a pointer to a C function
|
| 6892 |
|
| 6893 |
extern "C" typedef void FUNC();
|
| 6894 |
+
FUNC f2; // the name f2 has C++{} language linkage and the
|
| 6895 |
// function's type has C language linkage
|
| 6896 |
|
| 6897 |
extern "C" FUNC f3; // the name of function f3 and the function's type have C language linkage
|
| 6898 |
|
| 6899 |
+
void (*pf2)(FUNC*); // the name of the variable pf2 has C++{} linkage and the type
|
| 6900 |
+
// of pf2 is ``pointer to C++{} function that takes one parameter of type
|
| 6901 |
// pointer to C function''
|
| 6902 |
extern "C" {
|
| 6903 |
static void f4(); // the name of the function f4 has internal linkage (not C language linkage)
|
| 6904 |
// and the function's type has C language linkage.
|
| 6905 |
}
|
|
|
|
| 6929 |
``` cpp
|
| 6930 |
extern "C" typedef void FUNC_c();
|
| 6931 |
|
| 6932 |
class C {
|
| 6933 |
void mf1(FUNC_c*); // the name of the function mf1 and the member function's type have
|
| 6934 |
+
// C++{} language linkage; the parameter has type ``pointer to C function''
|
| 6935 |
|
| 6936 |
FUNC_c mf2; // the name of the function mf2 and the member function's type have
|
| 6937 |
+
// C++{} language linkage
|
| 6938 |
|
| 6939 |
+
static FUNC_c* q; // the name of the data member q has C++{} language linkage and
|
| 6940 |
// the data member's type is ``pointer to C function''
|
| 6941 |
};
|
| 6942 |
|
| 6943 |
extern "C" {
|
| 6944 |
class X {
|
| 6945 |
void mf(); // the name of the function mf and the member function's type have
|
| 6946 |
+
// C++{} language linkage
|
| 6947 |
+
void mf2(void(*)()); // the name of the function mf2 has C++{} language linkage;
|
| 6948 |
// the parameter has type ``pointer to C function''
|
| 6949 |
};
|
| 6950 |
}
|
| 6951 |
```
|
| 6952 |
|
| 6953 |
— *end example*]
|
| 6954 |
|
| 6955 |
If two declarations declare functions with the same name and
|
| 6956 |
+
parameter-type-list [[dcl.fct]] to be members of the same namespace or
|
| 6957 |
+
declare objects with the same name to be members of the same namespace
|
| 6958 |
+
and the declarations give the names different language linkages, the
|
| 6959 |
+
program is ill-formed; no diagnostic is required if the declarations
|
| 6960 |
+
appear in different translation units. Except for functions with C++
|
| 6961 |
+
linkage, a function declaration without a linkage specification shall
|
| 6962 |
+
not precede the first linkage specification for that function. A
|
| 6963 |
function can be declared without a linkage specification after an
|
| 6964 |
explicit linkage specification has been seen; the linkage explicitly
|
| 6965 |
specified in the earlier declaration is not affected by such a function
|
| 6966 |
declaration.
|
| 6967 |
|
|
|
|
| 6991 |
int x;
|
| 6992 |
namespace A {
|
| 6993 |
extern "C" int f();
|
| 6994 |
extern "C" int g() { return 1; }
|
| 6995 |
extern "C" int h();
|
| 6996 |
+
extern "C" int x(); // error: same name as global-space object x
|
| 6997 |
}
|
| 6998 |
|
| 6999 |
namespace B {
|
| 7000 |
extern "C" int f(); // A::f and B::f refer to the same function
|
| 7001 |
+
extern "C" int g() { return 1; } // error: the function g with C language linkage has two definitions
|
| 7002 |
}
|
| 7003 |
|
| 7004 |
int A::f() { return 98; } // definition for the function f with C language linkage
|
| 7005 |
extern "C" int h() { return 97; } // definition for the function h with C language linkage
|
| 7006 |
// A::h and ::h refer to the same function
|
| 7007 |
```
|
| 7008 |
|
| 7009 |
— *end example*]
|
| 7010 |
|
| 7011 |
A declaration directly contained in a *linkage-specification* is treated
|
| 7012 |
+
as if it contains the `extern` specifier [[dcl.stc]] for the purpose of
|
| 7013 |
+
determining the linkage of the declared name and whether it is a
|
| 7014 |
definition. Such a declaration shall not specify a storage class.
|
| 7015 |
|
| 7016 |
[*Example 5*:
|
| 7017 |
|
| 7018 |
``` cpp
|
|
|
|
| 7055 |
alignment-specifier
|
| 7056 |
```
|
| 7057 |
|
| 7058 |
``` bnf
|
| 7059 |
alignment-specifier:
|
| 7060 |
+
alignas '(' type-id '...'ₒₚₜ ')'
|
| 7061 |
+
alignas '(' constant-expression '...'ₒₚₜ ')'
|
| 7062 |
```
|
| 7063 |
|
| 7064 |
``` bnf
|
| 7065 |
attribute-using-prefix:
|
| 7066 |
+
using attribute-namespace ':'
|
| 7067 |
```
|
| 7068 |
|
| 7069 |
``` bnf
|
| 7070 |
attribute-list:
|
| 7071 |
attributeₒₚₜ
|
|
|
|
| 7141 |
[*Note 2*: For each individual attribute, the form of the
|
| 7142 |
*balanced-token-seq* will be specified. — *end note*]
|
| 7143 |
|
| 7144 |
In an *attribute-list*, an ellipsis may appear only if that
|
| 7145 |
*attribute*’s specification permits it. An *attribute* followed by an
|
| 7146 |
+
ellipsis is a pack expansion [[temp.variadic]]. An *attribute-specifier*
|
| 7147 |
+
that contains no *attribute*s has no effect. The order in which the
|
| 7148 |
+
*attribute-token*s appear in an *attribute-list* is not significant. If
|
| 7149 |
+
a keyword [[lex.key]] or an alternative token [[lex.digraph]] that
|
| 7150 |
+
satisfies the syntactic requirements of an *identifier* [[lex.name]] is
|
| 7151 |
+
contained in an *attribute-token*, it is considered an identifier. No
|
| 7152 |
+
name lookup [[basic.lookup]] is performed on any of the identifiers
|
| 7153 |
+
contained in an *attribute-token*. The *attribute-token* determines
|
| 7154 |
+
additional requirements on the *attribute-argument-clause* (if any).
|
|
|
|
| 7155 |
|
| 7156 |
Each *attribute-specifier-seq* is said to *appertain* to some entity or
|
| 7157 |
+
statement, identified by the syntactic context where it appears (
|
| 7158 |
+
[[stmt.stmt]], [[dcl.dcl]], [[dcl.decl]]). If an
|
| 7159 |
*attribute-specifier-seq* that appertains to some entity or statement
|
| 7160 |
contains an *attribute* or *alignment-specifier* that is not allowed to
|
| 7161 |
apply to that entity or statement, the program is ill-formed. If an
|
| 7162 |
+
*attribute-specifier-seq* appertains to a friend declaration
|
| 7163 |
+
[[class.friend]], that declaration shall be a definition.
|
| 7164 |
+
|
| 7165 |
+
[*Note 3*: An *attribute-specifier-seq* cannot appeartain to an
|
| 7166 |
+
explicit instantiation [[temp.explicit]]. — *end note*]
|
| 7167 |
|
| 7168 |
For an *attribute-token* (including an *attribute-scoped-token*) not
|
| 7169 |
+
specified in this document, the behavior is *implementation-defined*.
|
| 7170 |
+
Any *attribute-token* that is not recognized by the implementation is
|
| 7171 |
+
ignored. An *attribute-token* is reserved for future standardization if
|
| 7172 |
|
| 7173 |
+
- it is not an *attribute-scoped-token* and is not specified in this
|
| 7174 |
+
document, or
|
| 7175 |
+
- it is an *attribute-scoped-token* and its *attribute-namespace* is
|
| 7176 |
+
`std` followed by zero or more digits.
|
| 7177 |
+
|
| 7178 |
+
[*Note 4*: Each implementation should choose a distinctive name for the
|
| 7179 |
*attribute-namespace* in an *attribute-scoped-token*. — *end note*]
|
| 7180 |
|
| 7181 |
Two consecutive left square bracket tokens shall appear only when
|
| 7182 |
introducing an *attribute-specifier* or within the *balanced-token-seq*
|
| 7183 |
of an *attribute-argument-clause*.
|
| 7184 |
|
| 7185 |
+
[*Note 5*: If two consecutive left square brackets appear where an
|
| 7186 |
*attribute-specifier* is not allowed, the program is ill-formed even if
|
| 7187 |
the brackets match an alternative grammar production. — *end note*]
|
| 7188 |
|
| 7189 |
[*Example 2*:
|
| 7190 |
|
|
|
|
| 7203 |
|
| 7204 |
### Alignment specifier <a id="dcl.align">[[dcl.align]]</a>
|
| 7205 |
|
| 7206 |
An *alignment-specifier* may be applied to a variable or to a class data
|
| 7207 |
member, but it shall not be applied to a bit-field, a function
|
| 7208 |
+
parameter, or an *exception-declaration* [[except.handle]]. An
|
| 7209 |
+
*alignment-specifier* may also be applied to the declaration of a class
|
| 7210 |
+
(in an *elaborated-type-specifier* [[dcl.type.elab]] or *class-head*
|
| 7211 |
+
[[class]], respectively). An *alignment-specifier* with an ellipsis is a
|
| 7212 |
+
pack expansion [[temp.variadic]].
|
|
|
|
|
|
|
|
|
|
| 7213 |
|
| 7214 |
When the *alignment-specifier* is of the form `alignas(`
|
| 7215 |
*constant-expression* `)`:
|
| 7216 |
|
| 7217 |
- the *constant-expression* shall be an integral constant expression
|
| 7218 |
+
- if the constant expression does not evaluate to an alignment value
|
| 7219 |
+
[[basic.align]], or evaluates to an extended alignment and the
|
| 7220 |
implementation does not support that alignment in the context of the
|
| 7221 |
declaration, the program is ill-formed.
|
| 7222 |
|
| 7223 |
An *alignment-specifier* of the form `alignas(` *type-id* `)` has the
|
| 7224 |
+
same effect as `alignas({}alignof(` *type-id* `))` [[expr.alignof]].
|
| 7225 |
|
| 7226 |
The alignment requirement of an entity is the strictest nonzero
|
| 7227 |
alignment specified by its *alignment-specifier*s, if any; otherwise,
|
| 7228 |
the *alignment-specifier*s have no effect.
|
| 7229 |
|
|
|
|
| 7256 |
``` cpp
|
| 7257 |
// Translation unit #1:
|
| 7258 |
struct S { int x; } s, *p = &s;
|
| 7259 |
|
| 7260 |
// Translation unit #2:
|
| 7261 |
+
struct alignas(16) S; // ill-formed, no diagnostic required: definition of S lacks alignment
|
| 7262 |
extern S* p;
|
| 7263 |
```
|
| 7264 |
|
| 7265 |
— *end example*]
|
| 7266 |
|
|
|
|
| 7297 |
propagation into and out of functions. It shall appear at most once in
|
| 7298 |
each *attribute-list* and no *attribute-argument-clause* shall be
|
| 7299 |
present. The attribute may be applied to the *declarator-id* of a
|
| 7300 |
*parameter-declaration* in a function declaration or lambda, in which
|
| 7301 |
case it specifies that the initialization of the parameter carries a
|
| 7302 |
+
dependency to [[intro.multithread]] each lvalue-to-rvalue conversion
|
| 7303 |
+
[[conv.lval]] of that object. The attribute may also be applied to the
|
| 7304 |
+
*declarator-id* of a function declaration, in which case it specifies
|
| 7305 |
+
that the return value, if any, carries a dependency to the evaluation of
|
| 7306 |
+
the function call expression.
|
| 7307 |
|
| 7308 |
The first declaration of a function shall specify the
|
| 7309 |
`carries_dependency` attribute for its *declarator-id* if any
|
| 7310 |
declaration of the function specifies the `carries_dependency`
|
| 7311 |
attribute. Furthermore, the first declaration of a function shall
|
|
|
|
| 7330 |
struct foo { int* a; int* b; };
|
| 7331 |
std::atomic<struct foo *> foo_head[10];
|
| 7332 |
int foo_array[10][10];
|
| 7333 |
|
| 7334 |
[[carries_dependency]] struct foo* f(int i) {
|
| 7335 |
+
return foo_head[i].load(memory_order::consume);
|
| 7336 |
}
|
| 7337 |
|
| 7338 |
int g(int* x, int* y [[carries_dependency]]) {
|
| 7339 |
return kill_dependency(foo_array[*x][*y]);
|
| 7340 |
}
|
|
|
|
| 7357 |
|
| 7358 |
The `carries_dependency` attribute on function `f` means that the return
|
| 7359 |
value carries a dependency out of `f`, so that the implementation need
|
| 7360 |
not constrain ordering upon return from `f`. Implementations of `f` and
|
| 7361 |
its caller may choose to preserve dependencies instead of emitting
|
| 7362 |
+
hardware memory ordering instructions (a.k.a. fences). Function `g`’s
|
| 7363 |
+
second parameter has a `carries_dependency` attribute, but its first
|
| 7364 |
+
parameter does not. Therefore, function `h`’s first call to `g` carries
|
| 7365 |
+
a dependency into `g`, but its second call does not. The implementation
|
| 7366 |
+
might need to insert a fence prior to the second call to `g`.
|
|
|
|
|
|
|
| 7367 |
|
| 7368 |
— *end example*]
|
| 7369 |
|
| 7370 |
### Deprecated attribute <a id="dcl.attr.deprecated">[[dcl.attr.deprecated]]</a>
|
| 7371 |
|
|
|
|
| 7377 |
|
| 7378 |
It shall appear at most once in each *attribute-list*. An
|
| 7379 |
*attribute-argument-clause* may be present and, if present, it shall
|
| 7380 |
have the form:
|
| 7381 |
|
| 7382 |
+
``` bnf
|
| 7383 |
+
'(' string-literal ')'
|
| 7384 |
```
|
| 7385 |
|
| 7386 |
[*Note 2*: The *string-literal* in the *attribute-argument-clause*
|
| 7387 |
could be used to explain the rationale for deprecation and/or to suggest
|
| 7388 |
a replacing entity. — *end note*]
|
|
|
|
| 7401 |
|
| 7402 |
Redeclarations using different forms of the attribute (with or without
|
| 7403 |
the *attribute-argument-clause* or with different
|
| 7404 |
*attribute-argument-clause*s) are allowed.
|
| 7405 |
|
| 7406 |
+
*Recommended practice:* Implementations should use the `deprecated`
|
| 7407 |
+
attribute to produce a diagnostic message in case the program refers to
|
| 7408 |
+
a name or entity other than to declare it, after a declaration that
|
| 7409 |
+
specifies the attribute. The diagnostic message should include the text
|
| 7410 |
+
provided within the *attribute-argument-clause* of any `deprecated`
|
| 7411 |
+
attribute applied to the name or entity.
|
| 7412 |
|
| 7413 |
### Fallthrough attribute <a id="dcl.attr.fallthrough">[[dcl.attr.fallthrough]]</a>
|
| 7414 |
|
| 7415 |
+
The *attribute-token* `fallthrough` may be applied to a null statement
|
| 7416 |
+
[[stmt.expr]]; such a statement is a fallthrough statement. The
|
| 7417 |
*attribute-token* `fallthrough` shall appear at most once in each
|
| 7418 |
*attribute-list* and no *attribute-argument-clause* shall be present. A
|
| 7419 |
fallthrough statement may only appear within an enclosing `switch`
|
| 7420 |
+
statement [[stmt.switch]]. The next statement that would be executed
|
| 7421 |
after a fallthrough statement shall be a labeled statement whose label
|
| 7422 |
+
is a case label or default label for the same `switch` statement and, if
|
| 7423 |
+
the fallthrough statement is contained in an iteration statement, the
|
| 7424 |
+
next statement shall be part of the same execution of the substatement
|
| 7425 |
+
of the innermost enclosing iteration statement. The program is
|
| 7426 |
+
ill-formed if there is no such statement.
|
| 7427 |
|
| 7428 |
+
*Recommended practice:* The use of a fallthrough statement should
|
| 7429 |
+
suppress a warning that an implementation might otherwise issue for a
|
| 7430 |
+
case or default label that is reachable from another case or default
|
| 7431 |
+
label along some path of execution. Implementations should issue a
|
| 7432 |
+
warning if a fallthrough statement is not dynamically reachable.
|
|
|
|
| 7433 |
|
| 7434 |
[*Example 1*:
|
| 7435 |
|
| 7436 |
``` cpp
|
| 7437 |
void f(int n) {
|
|
|
|
| 7440 |
case 1:
|
| 7441 |
case 2:
|
| 7442 |
g();
|
| 7443 |
[[fallthrough]];
|
| 7444 |
case 3: // warning on fallthrough discouraged
|
| 7445 |
+
do {
|
| 7446 |
+
[[fallthrough]]; // error: next statement is not part of the same substatement execution
|
| 7447 |
+
} while (false);
|
| 7448 |
+
case 6:
|
| 7449 |
+
do {
|
| 7450 |
+
[[fallthrough]]; // error: next statement is not part of the same substatement execution
|
| 7451 |
+
} while (n--);
|
| 7452 |
+
case 7:
|
| 7453 |
+
while (false) {
|
| 7454 |
+
[[fallthrough]]; // error: next statement is not part of the same substatement execution
|
| 7455 |
+
}
|
| 7456 |
+
case 5:
|
| 7457 |
h();
|
| 7458 |
case 4: // implementation may warn on fallthrough
|
| 7459 |
i();
|
| 7460 |
+
[[fallthrough]]; // error
|
| 7461 |
}
|
| 7462 |
}
|
| 7463 |
```
|
| 7464 |
|
| 7465 |
— *end example*]
|
| 7466 |
|
| 7467 |
+
### Likelihood attributes <a id="dcl.attr.likelihood">[[dcl.attr.likelihood]]</a>
|
| 7468 |
+
|
| 7469 |
+
The *attribute-token*s `likely` and `unlikely` may be applied to labels
|
| 7470 |
+
or statements. The *attribute-token*s `likely` and `unlikely` shall
|
| 7471 |
+
appear at most once in each *attribute-list* and no
|
| 7472 |
+
*attribute-argument-clause* shall be present. The *attribute-token*
|
| 7473 |
+
`likely` shall not appear in an *attribute-specifier-seq* that contains
|
| 7474 |
+
the *attribute-token* `unlikely`.
|
| 7475 |
+
|
| 7476 |
+
*Recommended practice:* The use of the `likely` attribute is intended to
|
| 7477 |
+
allow implementations to optimize for the case where paths of execution
|
| 7478 |
+
including it are arbitrarily more likely than any alternative path of
|
| 7479 |
+
execution that does not include such an attribute on a statement or
|
| 7480 |
+
label. The use of the `unlikely` attribute is intended to allow
|
| 7481 |
+
implementations to optimize for the case where paths of execution
|
| 7482 |
+
including it are arbitrarily more unlikely than any alternative path of
|
| 7483 |
+
execution that does not include such an attribute on a statement or
|
| 7484 |
+
label. A path of execution includes a label if and only if it contains a
|
| 7485 |
+
jump to that label.
|
| 7486 |
+
|
| 7487 |
+
[*Note 1*: Excessive usage of either of these attributes is liable to
|
| 7488 |
+
result in performance degradation. — *end note*]
|
| 7489 |
+
|
| 7490 |
+
[*Example 1*:
|
| 7491 |
+
|
| 7492 |
+
``` cpp
|
| 7493 |
+
void g(int);
|
| 7494 |
+
int f(int n) {
|
| 7495 |
+
if (n > 5) [[unlikely]] { // n > 5 is considered to be arbitrarily unlikely
|
| 7496 |
+
g(0);
|
| 7497 |
+
return n * 2 + 1;
|
| 7498 |
+
}
|
| 7499 |
+
|
| 7500 |
+
switch (n) {
|
| 7501 |
+
case 1:
|
| 7502 |
+
g(1);
|
| 7503 |
+
[[fallthrough]];
|
| 7504 |
+
|
| 7505 |
+
[[likely]] case 2: // n == 2 is considered to be arbitrarily more
|
| 7506 |
+
g(2); // likely than any other value of n
|
| 7507 |
+
break;
|
| 7508 |
+
}
|
| 7509 |
+
return 3;
|
| 7510 |
+
}
|
| 7511 |
+
```
|
| 7512 |
+
|
| 7513 |
+
— *end example*]
|
| 7514 |
+
|
| 7515 |
### Maybe unused attribute <a id="dcl.attr.unused">[[dcl.attr.unused]]</a>
|
| 7516 |
|
| 7517 |
The *attribute-token* `maybe_unused` indicates that a name or entity is
|
| 7518 |
possibly intentionally unused. It shall appear at most once in each
|
| 7519 |
*attribute-list* and no *attribute-argument-clause* shall be present.
|
| 7520 |
|
| 7521 |
The attribute may be applied to the declaration of a class, a
|
| 7522 |
+
*typedef-name*, a variable (including a structured binding declaration),
|
| 7523 |
+
a non-static data member, a function, an enumeration, or an enumerator.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7524 |
|
| 7525 |
A name or entity declared without the `maybe_unused` attribute can later
|
| 7526 |
be redeclared with the attribute and vice versa. An entity is considered
|
| 7527 |
marked after the first declaration that marks it.
|
| 7528 |
|
| 7529 |
+
*Recommended practice:* For an entity marked `maybe_unused`,
|
| 7530 |
+
implementations should not emit a warning that the entity or its
|
| 7531 |
+
structured bindings (if any) are used or unused. For a structured
|
| 7532 |
+
binding declaration not marked `maybe_unused`, implementations should
|
| 7533 |
+
not emit such a warning unless all of its structured bindings are
|
| 7534 |
+
unused.
|
| 7535 |
+
|
| 7536 |
[*Example 1*:
|
| 7537 |
|
| 7538 |
``` cpp
|
| 7539 |
[[maybe_unused]] void f([[maybe_unused]] bool thing1,
|
| 7540 |
[[maybe_unused]] bool thing2) {
|
| 7541 |
[[maybe_unused]] bool b = thing1 && thing2;
|
| 7542 |
assert(b);
|
| 7543 |
}
|
| 7544 |
```
|
| 7545 |
|
| 7546 |
+
Implementations should not warn that `b` is unused, whether or not
|
| 7547 |
+
`NDEBUG` is defined.
|
| 7548 |
|
| 7549 |
— *end example*]
|
| 7550 |
|
| 7551 |
### Nodiscard attribute <a id="dcl.attr.nodiscard">[[dcl.attr.nodiscard]]</a>
|
| 7552 |
|
| 7553 |
The *attribute-token* `nodiscard` may be applied to the *declarator-id*
|
| 7554 |
in a function declaration or to the declaration of a class or
|
| 7555 |
+
enumeration. It shall appear at most once in each *attribute-list*. An
|
| 7556 |
+
*attribute-argument-clause* may be present and, if present, shall have
|
| 7557 |
+
the form:
|
| 7558 |
+
|
| 7559 |
+
``` bnf
|
| 7560 |
+
'(' string-literal ')'
|
| 7561 |
+
```
|
| 7562 |
+
|
| 7563 |
+
A name or entity declared without the `nodiscard` attribute can later be
|
| 7564 |
+
redeclared with the attribute and vice-versa.
|
| 7565 |
+
|
| 7566 |
+
[*Note 1*: Thus, an entity initially declared without the attribute can
|
| 7567 |
+
be marked as `nodiscard` by a subsequent redeclaration. However, after
|
| 7568 |
+
an entity is marked as `nodiscard`, later redeclarations do not remove
|
| 7569 |
+
the `nodiscard` from the entity. — *end note*]
|
| 7570 |
+
|
| 7571 |
+
Redeclarations using different forms of the attribute (with or without
|
| 7572 |
+
the *attribute-argument-clause* or with different
|
| 7573 |
+
*attribute-argument-clause*s) are allowed.
|
| 7574 |
+
|
| 7575 |
+
A *nodiscard type* is a (possibly cv-qualified) class or enumeration
|
| 7576 |
+
type marked `nodiscard` in a reachable declaration. A *nodiscard call*
|
| 7577 |
+
is either
|
| 7578 |
+
|
| 7579 |
+
- a function call expression [[expr.call]] that calls a function
|
| 7580 |
+
declared `nodiscard` in a reachable declaration or whose return type
|
| 7581 |
+
is a nodiscard type, or
|
| 7582 |
+
- an explicit type conversion ([[expr.type.conv]],
|
| 7583 |
+
[[expr.static.cast]], [[expr.cast]]) that constructs an object through
|
| 7584 |
+
a constructor declared `nodiscard` in a reachable declaration, or that
|
| 7585 |
+
initializes an object of a nodiscard type.
|
| 7586 |
+
|
| 7587 |
+
*Recommended practice:* Appearance of a nodiscard call as a
|
| 7588 |
+
potentially-evaluated discarded-value expression [[expr.prop]] is
|
| 7589 |
+
discouraged unless explicitly cast to `void`. Implementations should
|
| 7590 |
+
issue a warning in such cases.
|
| 7591 |
+
|
| 7592 |
+
[*Note 2*: This is typically because discarding the return value of a
|
| 7593 |
+
nodiscard call has surprising consequences. — *end note*]
|
| 7594 |
+
|
| 7595 |
+
The *string-literal* in a `nodiscard` *attribute-argument-clause* should
|
| 7596 |
+
be used in the message of the warning as the rationale for why the
|
| 7597 |
+
result should not be discarded.
|
| 7598 |
|
| 7599 |
[*Example 1*:
|
| 7600 |
|
| 7601 |
``` cpp
|
| 7602 |
+
struct [[nodiscard]] my_scopeguard { ... };
|
| 7603 |
+
struct my_unique {
|
| 7604 |
+
my_unique() = default; // does not acquire resource
|
| 7605 |
+
[[nodiscard]] my_unique(int fd) { ... } // acquires resource
|
| 7606 |
+
~my_unique() noexcept { ... } // releases resource, if any
|
| 7607 |
+
...
|
| 7608 |
+
};
|
| 7609 |
struct [[nodiscard]] error_info { ... };
|
| 7610 |
error_info enable_missile_safety_mode();
|
| 7611 |
void launch_missiles();
|
| 7612 |
void test_missiles() {
|
| 7613 |
+
my_scopeguard(); // warning encouraged
|
| 7614 |
+
(void)my_scopeguard(), // warning not encouraged, cast to void
|
| 7615 |
+
launch_missiles(); // comma operator, statement continues
|
| 7616 |
+
my_unique(42); // warning encouraged
|
| 7617 |
+
my_unique(); // warning not encouraged
|
| 7618 |
enable_missile_safety_mode(); // warning encouraged
|
| 7619 |
launch_missiles();
|
| 7620 |
}
|
| 7621 |
error_info &foo();
|
| 7622 |
void f() { foo(); } // warning not encouraged: not a nodiscard call, because neither
|
|
|
|
| 7643 |
undefined.
|
| 7644 |
|
| 7645 |
[*Note 1*: The function may terminate by throwing an
|
| 7646 |
exception. — *end note*]
|
| 7647 |
|
| 7648 |
+
*Recommended practice:* Implementations should issue a warning if a
|
| 7649 |
+
function marked `[[noreturn]]` might return.
|
| 7650 |
|
| 7651 |
[*Example 1*:
|
| 7652 |
|
| 7653 |
``` cpp
|
| 7654 |
[[ noreturn ]] void f() {
|
|
|
|
| 7661 |
}
|
| 7662 |
```
|
| 7663 |
|
| 7664 |
— *end example*]
|
| 7665 |
|
| 7666 |
+
### No unique address attribute <a id="dcl.attr.nouniqueaddr">[[dcl.attr.nouniqueaddr]]</a>
|
| 7667 |
+
|
| 7668 |
+
The *attribute-token* `no_unique_address` specifies that a non-static
|
| 7669 |
+
data member is a potentially-overlapping subobject [[intro.object]]. It
|
| 7670 |
+
shall appear at most once in each *attribute-list* and no
|
| 7671 |
+
*attribute-argument-clause* shall be present. The attribute may
|
| 7672 |
+
appertain to a non-static data member other than a bit-field.
|
| 7673 |
+
|
| 7674 |
+
[*Note 1*: The non-static data member can share the address of another
|
| 7675 |
+
non-static data member or that of a base class, and any padding that
|
| 7676 |
+
would normally be inserted at the end of the object can be reused as
|
| 7677 |
+
storage for other members. — *end note*]
|
| 7678 |
+
|
| 7679 |
+
[*Example 1*:
|
| 7680 |
+
|
| 7681 |
+
``` cpp
|
| 7682 |
+
template<typename Key, typename Value,
|
| 7683 |
+
typename Hash, typename Pred, typename Allocator>
|
| 7684 |
+
class hash_map {
|
| 7685 |
+
[[no_unique_address]] Hash hasher;
|
| 7686 |
+
[[no_unique_address]] Pred pred;
|
| 7687 |
+
[[no_unique_address]] Allocator alloc;
|
| 7688 |
+
Bucket *buckets;
|
| 7689 |
+
// ...
|
| 7690 |
+
public:
|
| 7691 |
+
// ...
|
| 7692 |
+
};
|
| 7693 |
+
```
|
| 7694 |
+
|
| 7695 |
+
Here, `hasher`, `pred`, and `alloc` could have the same address as
|
| 7696 |
+
`buckets` if their respective types are all empty.
|
| 7697 |
+
|
| 7698 |
+
— *end example*]
|
| 7699 |
+
|
| 7700 |
+
<!-- Link reference definitions -->
|
| 7701 |
+
[basic.align]: basic.md#basic.align
|
| 7702 |
+
[basic.compound]: basic.md#basic.compound
|
| 7703 |
+
[basic.def]: basic.md#basic.def
|
| 7704 |
+
[basic.def.odr]: basic.md#basic.def.odr
|
| 7705 |
+
[basic.fundamental]: basic.md#basic.fundamental
|
| 7706 |
+
[basic.life]: basic.md#basic.life
|
| 7707 |
+
[basic.link]: basic.md#basic.link
|
| 7708 |
+
[basic.lookup]: basic.md#basic.lookup
|
| 7709 |
+
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 7710 |
+
[basic.lookup.classref]: basic.md#basic.lookup.classref
|
| 7711 |
+
[basic.lookup.elab]: basic.md#basic.lookup.elab
|
| 7712 |
+
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
| 7713 |
+
[basic.lookup.udir]: basic.md#basic.lookup.udir
|
| 7714 |
+
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 7715 |
+
[basic.namespace]: #basic.namespace
|
| 7716 |
+
[basic.scope]: basic.md#basic.scope
|
| 7717 |
+
[basic.scope.block]: basic.md#basic.scope.block
|
| 7718 |
+
[basic.scope.declarative]: basic.md#basic.scope.declarative
|
| 7719 |
+
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
| 7720 |
+
[basic.scope.param]: basic.md#basic.scope.param
|
| 7721 |
+
[basic.scope.pdecl]: basic.md#basic.scope.pdecl
|
| 7722 |
+
[basic.start]: basic.md#basic.start
|
| 7723 |
+
[basic.start.dynamic]: basic.md#basic.start.dynamic
|
| 7724 |
+
[basic.start.static]: basic.md#basic.start.static
|
| 7725 |
+
[basic.stc]: basic.md#basic.stc
|
| 7726 |
+
[basic.stc.auto]: basic.md#basic.stc.auto
|
| 7727 |
+
[basic.stc.dynamic]: basic.md#basic.stc.dynamic
|
| 7728 |
+
[basic.stc.dynamic.allocation]: basic.md#basic.stc.dynamic.allocation
|
| 7729 |
+
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 7730 |
+
[basic.stc.static]: basic.md#basic.stc.static
|
| 7731 |
+
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 7732 |
+
[basic.type.qualifier]: basic.md#basic.type.qualifier
|
| 7733 |
+
[basic.types]: basic.md#basic.types
|
| 7734 |
+
[class]: class.md#class
|
| 7735 |
+
[class.access]: class.md#class.access
|
| 7736 |
+
[class.base.init]: class.md#class.base.init
|
| 7737 |
+
[class.bit]: class.md#class.bit
|
| 7738 |
+
[class.compare.default]: class.md#class.compare.default
|
| 7739 |
+
[class.conv]: class.md#class.conv
|
| 7740 |
+
[class.conv.ctor]: class.md#class.conv.ctor
|
| 7741 |
+
[class.conv.fct]: class.md#class.conv.fct
|
| 7742 |
+
[class.copy.assign]: class.md#class.copy.assign
|
| 7743 |
+
[class.copy.ctor]: class.md#class.copy.ctor
|
| 7744 |
+
[class.copy.elision]: class.md#class.copy.elision
|
| 7745 |
+
[class.ctor]: class.md#class.ctor
|
| 7746 |
+
[class.default.ctor]: class.md#class.default.ctor
|
| 7747 |
+
[class.dtor]: class.md#class.dtor
|
| 7748 |
+
[class.expl.init]: class.md#class.expl.init
|
| 7749 |
+
[class.friend]: class.md#class.friend
|
| 7750 |
+
[class.inhctor.init]: class.md#class.inhctor.init
|
| 7751 |
+
[class.init]: class.md#class.init
|
| 7752 |
+
[class.mem]: class.md#class.mem
|
| 7753 |
+
[class.member.lookup]: class.md#class.member.lookup
|
| 7754 |
+
[class.mfct]: class.md#class.mfct
|
| 7755 |
+
[class.mi]: class.md#class.mi
|
| 7756 |
+
[class.name]: class.md#class.name
|
| 7757 |
+
[class.pre]: class.md#class.pre
|
| 7758 |
+
[class.qual]: basic.md#class.qual
|
| 7759 |
+
[class.static]: class.md#class.static
|
| 7760 |
+
[class.static.data]: class.md#class.static.data
|
| 7761 |
+
[class.temporary]: basic.md#class.temporary
|
| 7762 |
+
[class.union]: class.md#class.union
|
| 7763 |
+
[class.union.anon]: class.md#class.union.anon
|
| 7764 |
+
[class.virtual]: class.md#class.virtual
|
| 7765 |
+
[conv]: expr.md#conv
|
| 7766 |
+
[conv.array]: expr.md#conv.array
|
| 7767 |
+
[conv.func]: expr.md#conv.func
|
| 7768 |
+
[conv.lval]: expr.md#conv.lval
|
| 7769 |
+
[conv.prom]: expr.md#conv.prom
|
| 7770 |
+
[conv.ptr]: expr.md#conv.ptr
|
| 7771 |
+
[conv.qual]: expr.md#conv.qual
|
| 7772 |
+
[conv.rval]: expr.md#conv.rval
|
| 7773 |
+
[coroutine.handle]: support.md#coroutine.handle
|
| 7774 |
+
[coroutine.handle.resumption]: support.md#coroutine.handle.resumption
|
| 7775 |
+
[dcl.align]: #dcl.align
|
| 7776 |
+
[dcl.ambig.res]: #dcl.ambig.res
|
| 7777 |
+
[dcl.array]: #dcl.array
|
| 7778 |
+
[dcl.asm]: #dcl.asm
|
| 7779 |
+
[dcl.attr]: #dcl.attr
|
| 7780 |
+
[dcl.attr.depend]: #dcl.attr.depend
|
| 7781 |
+
[dcl.attr.deprecated]: #dcl.attr.deprecated
|
| 7782 |
+
[dcl.attr.fallthrough]: #dcl.attr.fallthrough
|
| 7783 |
+
[dcl.attr.grammar]: #dcl.attr.grammar
|
| 7784 |
+
[dcl.attr.likelihood]: #dcl.attr.likelihood
|
| 7785 |
+
[dcl.attr.nodiscard]: #dcl.attr.nodiscard
|
| 7786 |
+
[dcl.attr.noreturn]: #dcl.attr.noreturn
|
| 7787 |
+
[dcl.attr.nouniqueaddr]: #dcl.attr.nouniqueaddr
|
| 7788 |
+
[dcl.attr.unused]: #dcl.attr.unused
|
| 7789 |
+
[dcl.constexpr]: #dcl.constexpr
|
| 7790 |
+
[dcl.constinit]: #dcl.constinit
|
| 7791 |
+
[dcl.dcl]: #dcl.dcl
|
| 7792 |
+
[dcl.decl]: #dcl.decl
|
| 7793 |
+
[dcl.enum]: #dcl.enum
|
| 7794 |
+
[dcl.fct]: #dcl.fct
|
| 7795 |
+
[dcl.fct.def]: #dcl.fct.def
|
| 7796 |
+
[dcl.fct.def.coroutine]: #dcl.fct.def.coroutine
|
| 7797 |
+
[dcl.fct.def.default]: #dcl.fct.def.default
|
| 7798 |
+
[dcl.fct.def.delete]: #dcl.fct.def.delete
|
| 7799 |
+
[dcl.fct.def.general]: #dcl.fct.def.general
|
| 7800 |
+
[dcl.fct.default]: #dcl.fct.default
|
| 7801 |
+
[dcl.fct.spec]: #dcl.fct.spec
|
| 7802 |
+
[dcl.friend]: #dcl.friend
|
| 7803 |
+
[dcl.init]: #dcl.init
|
| 7804 |
+
[dcl.init.aggr]: #dcl.init.aggr
|
| 7805 |
+
[dcl.init.list]: #dcl.init.list
|
| 7806 |
+
[dcl.init.ref]: #dcl.init.ref
|
| 7807 |
+
[dcl.init.string]: #dcl.init.string
|
| 7808 |
+
[dcl.inline]: #dcl.inline
|
| 7809 |
+
[dcl.link]: #dcl.link
|
| 7810 |
+
[dcl.meaning]: #dcl.meaning
|
| 7811 |
+
[dcl.mptr]: #dcl.mptr
|
| 7812 |
+
[dcl.name]: #dcl.name
|
| 7813 |
+
[dcl.pre]: #dcl.pre
|
| 7814 |
+
[dcl.ptr]: #dcl.ptr
|
| 7815 |
+
[dcl.ref]: #dcl.ref
|
| 7816 |
+
[dcl.spec]: #dcl.spec
|
| 7817 |
+
[dcl.spec.auto]: #dcl.spec.auto
|
| 7818 |
+
[dcl.stc]: #dcl.stc
|
| 7819 |
+
[dcl.struct.bind]: #dcl.struct.bind
|
| 7820 |
+
[dcl.type]: #dcl.type
|
| 7821 |
+
[dcl.type.auto.deduct]: #dcl.type.auto.deduct
|
| 7822 |
+
[dcl.type.class.deduct]: #dcl.type.class.deduct
|
| 7823 |
+
[dcl.type.cv]: #dcl.type.cv
|
| 7824 |
+
[dcl.type.decltype]: #dcl.type.decltype
|
| 7825 |
+
[dcl.type.elab]: #dcl.type.elab
|
| 7826 |
+
[dcl.type.simple]: #dcl.type.simple
|
| 7827 |
+
[dcl.typedef]: #dcl.typedef
|
| 7828 |
+
[depr.volatile.type]: future.md#depr.volatile.type
|
| 7829 |
+
[enum]: #enum
|
| 7830 |
+
[enum.udecl]: #enum.udecl
|
| 7831 |
+
[except.ctor]: except.md#except.ctor
|
| 7832 |
+
[except.handle]: except.md#except.handle
|
| 7833 |
+
[except.spec]: except.md#except.spec
|
| 7834 |
+
[except.throw]: except.md#except.throw
|
| 7835 |
+
[expr.alignof]: expr.md#expr.alignof
|
| 7836 |
+
[expr.ass]: expr.md#expr.ass
|
| 7837 |
+
[expr.await]: expr.md#expr.await
|
| 7838 |
+
[expr.call]: expr.md#expr.call
|
| 7839 |
+
[expr.cast]: expr.md#expr.cast
|
| 7840 |
+
[expr.const]: expr.md#expr.const
|
| 7841 |
+
[expr.const.cast]: expr.md#expr.const.cast
|
| 7842 |
+
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 7843 |
+
[expr.new]: expr.md#expr.new
|
| 7844 |
+
[expr.post.incr]: expr.md#expr.post.incr
|
| 7845 |
+
[expr.pre.incr]: expr.md#expr.pre.incr
|
| 7846 |
+
[expr.prim.lambda]: expr.md#expr.prim.lambda
|
| 7847 |
+
[expr.prim.this]: expr.md#expr.prim.this
|
| 7848 |
+
[expr.prop]: expr.md#expr.prop
|
| 7849 |
+
[expr.ref]: expr.md#expr.ref
|
| 7850 |
+
[expr.static.cast]: expr.md#expr.static.cast
|
| 7851 |
+
[expr.sub]: expr.md#expr.sub
|
| 7852 |
+
[expr.type.conv]: expr.md#expr.type.conv
|
| 7853 |
+
[expr.unary]: expr.md#expr.unary
|
| 7854 |
+
[expr.unary.op]: expr.md#expr.unary.op
|
| 7855 |
+
[expr.yield]: expr.md#expr.yield
|
| 7856 |
+
[intro.compliance]: intro.md#intro.compliance
|
| 7857 |
+
[intro.execution]: basic.md#intro.execution
|
| 7858 |
+
[intro.multithread]: basic.md#intro.multithread
|
| 7859 |
+
[intro.object]: basic.md#intro.object
|
| 7860 |
+
[lex.charset]: lex.md#lex.charset
|
| 7861 |
+
[lex.digraph]: lex.md#lex.digraph
|
| 7862 |
+
[lex.key]: lex.md#lex.key
|
| 7863 |
+
[lex.name]: lex.md#lex.name
|
| 7864 |
+
[lex.string]: lex.md#lex.string
|
| 7865 |
+
[module.interface]: module.md#module.interface
|
| 7866 |
+
[namespace.alias]: #namespace.alias
|
| 7867 |
+
[namespace.def]: #namespace.def
|
| 7868 |
+
[namespace.memdef]: #namespace.memdef
|
| 7869 |
+
[namespace.qual]: basic.md#namespace.qual
|
| 7870 |
+
[namespace.udecl]: #namespace.udecl
|
| 7871 |
+
[namespace.udir]: #namespace.udir
|
| 7872 |
+
[namespace.unnamed]: #namespace.unnamed
|
| 7873 |
+
[over]: over.md#over
|
| 7874 |
+
[over.binary]: over.md#over.binary
|
| 7875 |
+
[over.match]: over.md#over.match
|
| 7876 |
+
[over.match.best]: over.md#over.match.best
|
| 7877 |
+
[over.match.class.deduct]: over.md#over.match.class.deduct
|
| 7878 |
+
[over.match.conv]: over.md#over.match.conv
|
| 7879 |
+
[over.match.copy]: over.md#over.match.copy
|
| 7880 |
+
[over.match.ctor]: over.md#over.match.ctor
|
| 7881 |
+
[over.match.funcs]: over.md#over.match.funcs
|
| 7882 |
+
[over.match.list]: over.md#over.match.list
|
| 7883 |
+
[over.match.ref]: over.md#over.match.ref
|
| 7884 |
+
[over.match.viable]: over.md#over.match.viable
|
| 7885 |
+
[over.oper]: over.md#over.oper
|
| 7886 |
+
[over.sub]: over.md#over.sub
|
| 7887 |
+
[special]: class.md#special
|
| 7888 |
+
[stmt.ambig]: stmt.md#stmt.ambig
|
| 7889 |
+
[stmt.dcl]: stmt.md#stmt.dcl
|
| 7890 |
+
[stmt.expr]: stmt.md#stmt.expr
|
| 7891 |
+
[stmt.if]: stmt.md#stmt.if
|
| 7892 |
+
[stmt.iter]: stmt.md#stmt.iter
|
| 7893 |
+
[stmt.label]: stmt.md#stmt.label
|
| 7894 |
+
[stmt.pre]: stmt.md#stmt.pre
|
| 7895 |
+
[stmt.return]: stmt.md#stmt.return
|
| 7896 |
+
[stmt.return.coroutine]: stmt.md#stmt.return.coroutine
|
| 7897 |
+
[stmt.select]: stmt.md#stmt.select
|
| 7898 |
+
[stmt.stmt]: stmt.md#stmt.stmt
|
| 7899 |
+
[stmt.switch]: stmt.md#stmt.switch
|
| 7900 |
+
[support.runtime]: support.md#support.runtime
|
| 7901 |
+
[temp.arg.type]: temp.md#temp.arg.type
|
| 7902 |
+
[temp.class.spec]: temp.md#temp.class.spec
|
| 7903 |
+
[temp.deduct]: temp.md#temp.deduct
|
| 7904 |
+
[temp.deduct.call]: temp.md#temp.deduct.call
|
| 7905 |
+
[temp.deduct.guide]: temp.md#temp.deduct.guide
|
| 7906 |
+
[temp.dep]: temp.md#temp.dep
|
| 7907 |
+
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 7908 |
+
[temp.explicit]: temp.md#temp.explicit
|
| 7909 |
+
[temp.fct]: temp.md#temp.fct
|
| 7910 |
+
[temp.inst]: temp.md#temp.inst
|
| 7911 |
+
[temp.local]: temp.md#temp.local
|
| 7912 |
+
[temp.mem]: temp.md#temp.mem
|
| 7913 |
+
[temp.names]: temp.md#temp.names
|
| 7914 |
+
[temp.over.link]: temp.md#temp.over.link
|
| 7915 |
+
[temp.param]: temp.md#temp.param
|
| 7916 |
+
[temp.pre]: temp.md#temp.pre
|
| 7917 |
+
[temp.res]: temp.md#temp.res
|
| 7918 |
+
[temp.spec]: temp.md#temp.spec
|
| 7919 |
+
[temp.variadic]: temp.md#temp.variadic
|
| 7920 |
+
|
| 7921 |
+
[^1]: There is no special provision for a *decl-specifier-seq* that
|
| 7922 |
+
lacks a *type-specifier* or that has a *type-specifier* that only
|
| 7923 |
+
specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
|
| 7924 |
+
supported.
|
| 7925 |
+
|
| 7926 |
+
[^2]: As indicated by syntax, cv-qualifiers are a significant component
|
| 7927 |
+
in function return types.
|
| 7928 |
+
|
| 7929 |
+
[^3]: One can explicitly disambiguate the parse either by introducing a
|
| 7930 |
+
comma (so the ellipsis will be parsed as part of the
|
| 7931 |
+
*parameter-declaration-clause*) or by introducing a name for the
|
| 7932 |
+
parameter (so the ellipsis will be parsed as part of the
|
| 7933 |
+
*declarator-id*).
|
| 7934 |
+
|
| 7935 |
+
[^4]: This means that default arguments cannot appear, for example, in
|
| 7936 |
+
declarations of pointers to functions, references to functions, or
|
| 7937 |
+
`typedef` declarations.
|
| 7938 |
+
|
| 7939 |
+
[^5]: As specified in [[conv.ptr]], converting an integer literal whose
|
| 7940 |
+
value is `0` to a pointer type results in a null pointer value.
|
| 7941 |
+
|
| 7942 |
+
[^6]: The syntax provides for empty *braced-init-list*s, but nonetheless
|
| 7943 |
+
C++ does not have zero length arrays.
|
| 7944 |
+
|
| 7945 |
+
[^7]: This requires a conversion function [[class.conv.fct]] returning a
|
| 7946 |
+
reference type.
|
| 7947 |
+
|
| 7948 |
+
[^8]: Implementations are permitted to provide additional predefined
|
| 7949 |
+
variables with names that are reserved to the implementation
|
| 7950 |
+
[[lex.name]]. If a predefined variable is not odr-used
|
| 7951 |
+
[[basic.def.odr]], its string value need not be present in the
|
| 7952 |
+
program image.
|
| 7953 |
+
|
| 7954 |
+
[^9]: This set of values is used to define promotion and conversion
|
| 7955 |
+
semantics for the enumeration type. It does not preclude an
|
| 7956 |
+
expression of enumeration type from having a value that falls
|
| 7957 |
+
outside this range.
|
| 7958 |
+
|
| 7959 |
+
[^10]: this implies that the name of the class or function is
|
| 7960 |
+
unqualified.
|
| 7961 |
+
|
| 7962 |
+
[^11]: During name lookup in a class hierarchy, some ambiguities may be
|
| 7963 |
+
resolved by considering whether one member hides the other along
|
| 7964 |
+
some paths [[class.member.lookup]]. There is no such disambiguation
|
| 7965 |
+
when considering the set of names found as a result of following
|
| 7966 |
+
*using-directive*s.
|
| 7967 |
+
|
| 7968 |
+
[^12]: A *using-declaration* with more than one *using-declarator* is
|
| 7969 |
+
equivalent to a corresponding sequence of *using-declaration*s with
|
| 7970 |
+
one *using-declarator* each.
|