- tmp/tmpfj61gxc4/{from.md → to.md} +624 -474
tmp/tmpfj61gxc4/{from.md → to.md}
RENAMED
|
@@ -5,30 +5,34 @@ The specifiers that can be used in a declaration are
|
|
| 5 |
``` bnf
|
| 6 |
decl-specifier:
|
| 7 |
storage-class-specifier
|
| 8 |
defining-type-specifier
|
| 9 |
function-specifier
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
```
|
| 15 |
|
| 16 |
``` bnf
|
| 17 |
decl-specifier-seq:
|
| 18 |
decl-specifier attribute-specifier-seqₒₚₜ
|
| 19 |
decl-specifier decl-specifier-seq
|
| 20 |
```
|
| 21 |
|
| 22 |
The optional *attribute-specifier-seq* in a *decl-specifier-seq*
|
| 23 |
-
appertains to the type determined by the preceding *decl-specifier*s
|
| 24 |
-
[[dcl.meaning]]
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
Each *decl-specifier* shall appear at most once in a complete
|
| 29 |
-
*decl-specifier-seq*, except that `long` may appear twice.
|
|
|
|
|
|
|
| 30 |
|
| 31 |
If a *type-name* is encountered while parsing a *decl-specifier-seq*, it
|
| 32 |
is interpreted as part of the *decl-specifier-seq* if and only if there
|
| 33 |
is no previous *defining-type-specifier* other than a *cv-qualifier* in
|
| 34 |
the *decl-specifier-seq*. The sequence shall be self-consistent as
|
|
@@ -76,69 +80,71 @@ void k(unsigned int Pc); // void k(unsigned int)
|
|
| 76 |
|
| 77 |
The storage class specifiers are
|
| 78 |
|
| 79 |
``` bnf
|
| 80 |
storage-class-specifier:
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
```
|
| 86 |
|
| 87 |
At most one *storage-class-specifier* shall appear in a given
|
| 88 |
*decl-specifier-seq*, except that `thread_local` may appear with
|
| 89 |
`static` or `extern`. If `thread_local` appears in any declaration of a
|
| 90 |
variable it shall be present in all declarations of that entity. If a
|
| 91 |
*storage-class-specifier* appears in a *decl-specifier-seq*, there can
|
| 92 |
be no `typedef` specifier in the same *decl-specifier-seq* and the
|
| 93 |
*init-declarator-list* or *member-declarator-list* of the declaration
|
| 94 |
shall not be empty (except for an anonymous union declared in a named
|
| 95 |
-
namespace or in the global namespace, which shall be declared `static`
|
| 96 |
-
[[class.union.anon]])
|
| 97 |
-
|
| 98 |
-
declared by other specifiers.
|
| 99 |
-
`thread_local` shall not be specified in an explicit specialization (
|
| 100 |
-
[[temp.expl.spec]]) or an explicit instantiation ([[temp.explicit]])
|
| 101 |
-
directive.
|
| 102 |
|
| 103 |
-
[*Note 1*:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
block scope or declared as a function parameter has automatic storage
|
| 105 |
-
duration by default
|
| 106 |
|
| 107 |
The `thread_local` specifier indicates that the named entity has thread
|
| 108 |
-
storage duration
|
| 109 |
-
|
| 110 |
-
|
|
|
|
| 111 |
block scope the *storage-class-specifier* `static` is implied if no
|
| 112 |
other *storage-class-specifier* appears in the *decl-specifier-seq*.
|
| 113 |
|
| 114 |
-
The `static` specifier
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
| 125 |
|
| 126 |
-
The `extern` specifier
|
| 127 |
-
|
| 128 |
-
class
|
| 129 |
-
with an `extern` specifier, see [[basic.link]].
|
| 130 |
|
| 131 |
-
[*Note
|
| 132 |
*explicit-instantiation*s and *linkage-specification*s, but it is not a
|
| 133 |
*storage-class-specifier* in such contexts. — *end note*]
|
| 134 |
|
| 135 |
The linkages implied by successive declarations for a given entity shall
|
| 136 |
agree. That is, within a given scope, each declaration declaring the
|
| 137 |
same variable name or the same overloading of a function name shall
|
| 138 |
-
imply the same linkage.
|
| 139 |
-
functions can have a different linkage, however.
|
| 140 |
|
| 141 |
[*Example 1*:
|
| 142 |
|
| 143 |
``` cpp
|
| 144 |
static char* f(); // f() has internal linkage
|
|
@@ -195,71 +201,86 @@ void h() {
|
|
| 195 |
```
|
| 196 |
|
| 197 |
— *end example*]
|
| 198 |
|
| 199 |
The `mutable` specifier shall appear only in the declaration of a
|
| 200 |
-
non-static data member
|
| 201 |
const-qualified nor a reference type.
|
| 202 |
|
| 203 |
[*Example 3*:
|
| 204 |
|
| 205 |
``` cpp
|
| 206 |
class X {
|
| 207 |
mutable const int* p; // OK
|
| 208 |
-
mutable int* const q; //
|
| 209 |
};
|
| 210 |
```
|
| 211 |
|
| 212 |
— *end example*]
|
| 213 |
|
| 214 |
-
The `mutable` specifier on a class data member nullifies a
|
| 215 |
-
specifier applied to the containing class object and permits
|
| 216 |
modification of the mutable class member even though the rest of the
|
| 217 |
-
object is
|
|
|
|
| 218 |
|
| 219 |
### Function specifiers <a id="dcl.fct.spec">[[dcl.fct.spec]]</a>
|
| 220 |
|
| 221 |
-
can be used only in function
|
| 222 |
|
| 223 |
``` bnf
|
| 224 |
function-specifier:
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
```
|
| 228 |
|
| 229 |
The `virtual` specifier shall be used only in the initial declaration of
|
| 230 |
a non-static class member function; see [[class.virtual]].
|
| 231 |
|
| 232 |
-
|
| 233 |
constructor or conversion function within its class definition; see
|
| 234 |
[[class.conv.ctor]] and [[class.conv.fct]].
|
| 235 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
### The `typedef` specifier <a id="dcl.typedef">[[dcl.typedef]]</a>
|
| 237 |
|
| 238 |
Declarations containing the *decl-specifier* `typedef` declare
|
| 239 |
-
identifiers that can be used later for naming fundamental
|
| 240 |
-
[[basic.fundamental]]
|
| 241 |
`typedef` specifier shall not be combined in a *decl-specifier-seq* with
|
| 242 |
any other kind of specifier except a *defining-type-specifier*, and it
|
| 243 |
shall not be used in the *decl-specifier-seq* of a
|
| 244 |
-
*parameter-declaration*
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
ill-formed.
|
| 248 |
|
| 249 |
``` bnf
|
| 250 |
typedef-name:
|
| 251 |
identifier
|
|
|
|
| 252 |
```
|
| 253 |
|
| 254 |
-
A name declared with the `typedef` specifier becomes a *typedef-name*.
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
or enum declaration does.
|
| 261 |
|
| 262 |
[*Example 1*:
|
| 263 |
|
| 264 |
After
|
| 265 |
|
|
@@ -290,21 +311,21 @@ particular, it does not define a new type.
|
|
| 290 |
|
| 291 |
``` cpp
|
| 292 |
using handler_t = void (*)(int);
|
| 293 |
extern handler_t ignore;
|
| 294 |
extern void (*ignore)(int); // redeclare ignore
|
| 295 |
-
using cell = pair<void*, cell*>; //
|
| 296 |
```
|
| 297 |
|
| 298 |
— *end example*]
|
| 299 |
|
| 300 |
The *defining-type-specifier-seq* of the *defining-type-id* shall not
|
| 301 |
define a class or enumeration if the *alias-declaration* is the
|
| 302 |
*declaration* of a *template-declaration*.
|
| 303 |
|
| 304 |
In a given non-class scope, a `typedef` specifier can be used to
|
| 305 |
-
|
| 306 |
type to which it already refers.
|
| 307 |
|
| 308 |
[*Example 3*:
|
| 309 |
|
| 310 |
``` cpp
|
|
@@ -314,11 +335,11 @@ typedef int I;
|
|
| 314 |
typedef I I;
|
| 315 |
```
|
| 316 |
|
| 317 |
— *end example*]
|
| 318 |
|
| 319 |
-
In a given class scope, a `typedef` specifier can be used to
|
| 320 |
any *class-name* declared in that scope that is not also a
|
| 321 |
*typedef-name* to refer to the type to which it already refers.
|
| 322 |
|
| 323 |
[*Example 4*:
|
| 324 |
|
|
@@ -330,11 +351,11 @@ struct S {
|
|
| 330 |
};
|
| 331 |
```
|
| 332 |
|
| 333 |
— *end example*]
|
| 334 |
|
| 335 |
-
If a `typedef` specifier is used to
|
| 336 |
that can be referenced using an *elaborated-type-specifier*, the entity
|
| 337 |
can continue to be referenced by an *elaborated-type-specifier* or as an
|
| 338 |
enumeration or class name in an enumeration or class definition
|
| 339 |
respectively.
|
| 340 |
|
|
@@ -349,11 +370,11 @@ int main() {
|
|
| 349 |
struct S { }; // OK
|
| 350 |
```
|
| 351 |
|
| 352 |
— *end example*]
|
| 353 |
|
| 354 |
-
In a given scope, a `typedef` specifier shall not be used to
|
| 355 |
the name of any type declared in that scope to refer to a different
|
| 356 |
type.
|
| 357 |
|
| 358 |
[*Example 6*:
|
| 359 |
|
|
@@ -375,17 +396,19 @@ typedef int complex;
|
|
| 375 |
class complex { ... }; // error: redefinition
|
| 376 |
```
|
| 377 |
|
| 378 |
— *end example*]
|
| 379 |
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
*
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
|
|
|
|
|
|
| 387 |
|
| 388 |
[*Example 8*:
|
| 389 |
|
| 390 |
``` cpp
|
| 391 |
struct S {
|
|
@@ -399,40 +422,67 @@ S a = T(); // OK
|
|
| 399 |
struct T * p; // error
|
| 400 |
```
|
| 401 |
|
| 402 |
— *end example*]
|
| 403 |
|
| 404 |
-
If the typedef declaration defines an unnamed class
|
| 405 |
-
*typedef-name* declared by the declaration to be that
|
| 406 |
-
|
| 407 |
-
|
|
|
|
|
|
|
|
|
|
| 408 |
|
| 409 |
[*Example 9*:
|
| 410 |
|
| 411 |
``` cpp
|
| 412 |
typedef struct { } *ps, S; // S is the class name for linkage purposes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
```
|
| 414 |
|
| 415 |
— *end example*]
|
| 416 |
|
| 417 |
### The `friend` specifier <a id="dcl.friend">[[dcl.friend]]</a>
|
| 418 |
|
| 419 |
The `friend` specifier is used to specify access to class members; see
|
| 420 |
[[class.friend]].
|
| 421 |
|
| 422 |
-
### The `constexpr`
|
| 423 |
|
| 424 |
The `constexpr` specifier shall be applied only to the definition of a
|
| 425 |
variable or variable template or the declaration of a function or
|
| 426 |
-
function template.
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
`constexpr`
|
|
|
|
|
|
|
| 431 |
|
| 432 |
[*Note 1*: An explicit specialization can differ from the template
|
| 433 |
-
declaration with respect to the `constexpr`
|
|
|
|
| 434 |
|
| 435 |
[*Note 2*: Function parameters cannot be declared
|
| 436 |
`constexpr`. — *end note*]
|
| 437 |
|
| 438 |
[*Example 1*:
|
|
@@ -447,11 +497,11 @@ constexpr struct pixel { // error: pixel is a type
|
|
| 447 |
};
|
| 448 |
constexpr pixel::pixel(int a)
|
| 449 |
: x(a), y(x) // OK: definition
|
| 450 |
{ square(x); }
|
| 451 |
constexpr pixel small(2); // error: square not defined, so small(2)
|
| 452 |
-
// not constant
|
| 453 |
|
| 454 |
constexpr void square(int &x) { // OK: definition
|
| 455 |
x *= x;
|
| 456 |
}
|
| 457 |
constexpr pixel large(4); // OK: square defined
|
|
@@ -461,29 +511,33 @@ int next(constexpr int x) { // error: not for parameters
|
|
| 461 |
extern constexpr int memsz; // error: not a definition
|
| 462 |
```
|
| 463 |
|
| 464 |
— *end example*]
|
| 465 |
|
| 466 |
-
A `constexpr` specifier used in the declaration of a
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
|
|
|
|
|
|
| 470 |
|
| 471 |
The definition of a constexpr function shall satisfy the following
|
| 472 |
requirements:
|
| 473 |
|
| 474 |
-
-
|
| 475 |
-
- its return type shall be a literal type;
|
| 476 |
- each of its parameter types shall be a literal type;
|
| 477 |
-
-
|
| 478 |
-
|
| 479 |
-
|
|
|
|
| 480 |
- a `goto` statement,
|
| 481 |
-
- an identifier label
|
| 482 |
-
- a *try-block*, or
|
| 483 |
- a definition of a variable of non-literal type or of static or
|
| 484 |
-
thread storage duration
|
|
|
|
|
|
|
|
|
|
| 485 |
|
| 486 |
[*Example 2*:
|
| 487 |
|
| 488 |
``` cpp
|
| 489 |
constexpr int square(int x)
|
|
@@ -498,12 +552,12 @@ constexpr int abs(int x) {
|
|
| 498 |
constexpr int first(int n) {
|
| 499 |
static int value = n; // error: variable has static storage duration
|
| 500 |
return value;
|
| 501 |
}
|
| 502 |
constexpr int uninit() {
|
| 503 |
-
int a;
|
| 504 |
-
return a;
|
| 505 |
}
|
| 506 |
constexpr int prev(int x)
|
| 507 |
{ return --x; } // OK
|
| 508 |
constexpr int g(int x, int n) { // OK
|
| 509 |
int r = 1;
|
|
@@ -512,30 +566,13 @@ constexpr int g(int x, int n) { // OK
|
|
| 512 |
}
|
| 513 |
```
|
| 514 |
|
| 515 |
— *end example*]
|
| 516 |
|
| 517 |
-
The definition of a constexpr constructor
|
| 518 |
-
requirements:
|
| 519 |
|
| 520 |
-
- the class shall not have any virtual base classes;
|
| 521 |
-
- each of the parameter types shall be a literal type;
|
| 522 |
-
- its *function-body* shall not be a *function-try-block*.
|
| 523 |
-
|
| 524 |
-
In addition, either its *function-body* shall be `= delete`, or it shall
|
| 525 |
-
satisfy the following requirements:
|
| 526 |
-
|
| 527 |
-
- either its *function-body* shall be `= default`, or the
|
| 528 |
-
*compound-statement* of its *function-body* shall satisfy the
|
| 529 |
-
requirements for a *function-body* of a constexpr function;
|
| 530 |
-
- every non-variant non-static data member and base class subobject
|
| 531 |
-
shall be initialized ([[class.base.init]]);
|
| 532 |
-
- if the class is a union having variant members ([[class.union]]),
|
| 533 |
-
exactly one of them shall be initialized;
|
| 534 |
-
- if the class is a union-like class, but is not a union, for each of
|
| 535 |
-
its anonymous union members having variant members, exactly one of
|
| 536 |
-
them shall be initialized;
|
| 537 |
- for a non-delegating constructor, every constructor selected to
|
| 538 |
initialize non-static data members and base class subobjects shall be
|
| 539 |
a constexpr constructor;
|
| 540 |
- for a delegating constructor, the target constructor shall be a
|
| 541 |
constexpr constructor.
|
|
@@ -550,16 +587,23 @@ private:
|
|
| 550 |
};
|
| 551 |
```
|
| 552 |
|
| 553 |
— *end example*]
|
| 554 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 555 |
For a constexpr function or constexpr constructor that is neither
|
| 556 |
defaulted nor a template, if no argument values exist such that an
|
| 557 |
invocation of the function or constructor could be an evaluated
|
| 558 |
-
subexpression of a core constant expression
|
| 559 |
-
constructor,
|
| 560 |
-
|
|
|
|
| 561 |
required.
|
| 562 |
|
| 563 |
[*Example 4*:
|
| 564 |
|
| 565 |
``` cpp
|
|
@@ -582,27 +626,33 @@ struct D : B {
|
|
| 582 |
|
| 583 |
— *end example*]
|
| 584 |
|
| 585 |
If the instantiated template specialization of a constexpr function
|
| 586 |
template or member function of a class template would fail to satisfy
|
| 587 |
-
the requirements for a constexpr function
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
ill-formed, no diagnostic required.
|
| 594 |
|
| 595 |
-
|
| 596 |
-
equivalent non-constexpr function in
|
|
|
|
| 597 |
|
| 598 |
-
-
|
| 599 |
-
[[expr.const]]
|
| 600 |
-
- copy elision is
|
|
|
|
| 601 |
|
| 602 |
-
|
| 603 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 604 |
|
| 605 |
[*Example 5*:
|
| 606 |
|
| 607 |
``` cpp
|
| 608 |
constexpr int bar(int x, int y) // OK
|
|
@@ -613,14 +663,14 @@ int bar(int x, int y) // error: redefinition of bar
|
|
| 613 |
```
|
| 614 |
|
| 615 |
— *end example*]
|
| 616 |
|
| 617 |
A `constexpr` specifier used in an object declaration declares the
|
| 618 |
-
object as
|
| 619 |
initialized. In any `constexpr` variable declaration, the
|
| 620 |
-
full-expression of the initialization shall be a constant expression
|
| 621 |
-
[[expr.const]]
|
| 622 |
|
| 623 |
[*Example 6*:
|
| 624 |
|
| 625 |
``` cpp
|
| 626 |
struct pixel {
|
|
@@ -630,54 +680,86 @@ constexpr pixel ur = { 1294, 1024 }; // OK
|
|
| 630 |
constexpr pixel origin; // error: initializer missing
|
| 631 |
```
|
| 632 |
|
| 633 |
— *end example*]
|
| 634 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 635 |
### The `inline` specifier <a id="dcl.inline">[[dcl.inline]]</a>
|
| 636 |
|
| 637 |
-
The `inline` specifier
|
| 638 |
-
|
| 639 |
|
| 640 |
-
A function declaration ([[dcl.fct]],
|
| 641 |
with an `inline` specifier declares an *inline function*. The inline
|
| 642 |
specifier indicates to the implementation that inline substitution of
|
| 643 |
the function body at the point of call is to be preferred to the usual
|
| 644 |
function call mechanism. An implementation is not required to perform
|
| 645 |
this inline substitution at the point of call; however, even if this
|
| 646 |
inline substitution is omitted, the other rules for inline functions
|
| 647 |
-
specified in this
|
| 648 |
|
| 649 |
-
|
| 650 |
-
|
|
|
|
| 651 |
|
| 652 |
-
A
|
|
|
|
| 653 |
|
| 654 |
-
The `inline` specifier shall not appear on a block scope
|
| 655 |
-
declaration.
|
| 656 |
-
declaration, that declaration shall be a
|
| 657 |
-
shall have previously been declared inline.
|
| 658 |
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
|
|
|
|
|
|
|
|
|
| 662 |
|
| 663 |
-
[*Note
|
| 664 |
-
|
| 665 |
translation unit. — *end note*]
|
| 666 |
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
|
|
|
| 679 |
|
| 680 |
### Type specifiers <a id="dcl.type">[[dcl.type]]</a>
|
| 681 |
|
| 682 |
The type-specifiers are
|
| 683 |
|
|
@@ -708,14 +790,14 @@ defining-type-specifier-seq:
|
|
| 708 |
defining-type-specifier defining-type-specifier-seq
|
| 709 |
```
|
| 710 |
|
| 711 |
The optional *attribute-specifier-seq* in a *type-specifier-seq* or a
|
| 712 |
*defining-type-specifier-seq* appertains to the type denoted by the
|
| 713 |
-
preceding *type-specifier*s or *defining-type-specifier*s
|
| 714 |
-
[[dcl.meaning]]
|
| 715 |
-
|
| 716 |
-
|
| 717 |
|
| 718 |
As a general rule, at most one *defining-type-specifier* is allowed in
|
| 719 |
the complete *decl-specifier-seq* of a *declaration* or in a
|
| 720 |
*defining-type-specifier-seq*, and at most one *type-specifier* is
|
| 721 |
allowed in a *type-specifier-seq*. The only exceptions to this rule are
|
|
@@ -730,16 +812,16 @@ the following:
|
|
| 730 |
- `long` can be combined with `long`.
|
| 731 |
|
| 732 |
Except in a declaration of a constructor, destructor, or conversion
|
| 733 |
function, at least one *defining-type-specifier* that is not a
|
| 734 |
*cv-qualifier* shall appear in a complete *type-specifier-seq* or a
|
| 735 |
-
complete *decl-specifier-seq*.[^
|
| 736 |
|
| 737 |
[*Note 1*: *enum-specifier*s, *class-specifier*s, and
|
| 738 |
-
*typename-specifier*s are discussed in [[dcl.enum]],
|
| 739 |
-
|
| 740 |
-
discussed in the rest of this
|
| 741 |
|
| 742 |
#### The *cv-qualifier*s <a id="dcl.type.cv">[[dcl.type.cv]]</a>
|
| 743 |
|
| 744 |
There are two *cv-qualifier*s, `const` and `volatile`. Each
|
| 745 |
*cv-qualifier* shall appear at most once in a *cv-qualifier-seq*. If a
|
|
@@ -753,48 +835,47 @@ cv-qualifiers affect object and function types. — *end note*]
|
|
| 753 |
Redundant cv-qualifications are ignored.
|
| 754 |
|
| 755 |
[*Note 2*: For example, these could be introduced by
|
| 756 |
typedefs. — *end note*]
|
| 757 |
|
| 758 |
-
[*Note 3*: Declaring a variable `const` can affect its linkage
|
| 759 |
-
[[dcl.stc]]
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
|
| 764 |
A pointer or reference to a cv-qualified type need not actually point or
|
| 765 |
refer to a cv-qualified object, but it is treated as if it does; a
|
| 766 |
const-qualified access path cannot be used to modify an object even if
|
| 767 |
the object referenced is a non-const object and can be modified through
|
| 768 |
some other access path.
|
| 769 |
|
| 770 |
[*Note 4*: Cv-qualifiers are supported by the type system so that they
|
| 771 |
-
cannot be subverted without casting
|
| 772 |
-
[[expr.const.cast]]). — *end note*]
|
| 773 |
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
[[basic.life]]
|
| 777 |
|
| 778 |
[*Example 1*:
|
| 779 |
|
| 780 |
``` cpp
|
| 781 |
const int ci = 3; // cv-qualified (initialized as required)
|
| 782 |
-
ci = 4; //
|
| 783 |
|
| 784 |
int i = 2; // not cv-qualified
|
| 785 |
const int* cip; // pointer to const int
|
| 786 |
cip = &i; // OK: cv-qualified access path to unqualified
|
| 787 |
-
*cip = 4; //
|
| 788 |
|
| 789 |
int* ip;
|
| 790 |
ip = const_cast<int*>(cip); // cast needed to convert const int* to int*
|
| 791 |
*ip = 4; // defined: *ip points to i, a non-const object
|
| 792 |
|
| 793 |
const int* ciq = new const int (3); // initialized as required
|
| 794 |
int* iq = const_cast<int*>(ciq); // cast required
|
| 795 |
-
*iq = 4; // undefined: modifies a const object
|
| 796 |
```
|
| 797 |
|
| 798 |
For another example,
|
| 799 |
|
| 800 |
``` cpp
|
|
@@ -807,14 +888,14 @@ struct Y {
|
|
| 807 |
Y();
|
| 808 |
};
|
| 809 |
|
| 810 |
const Y y;
|
| 811 |
y.x.i++; // well-formed: mutable member can be modified
|
| 812 |
-
y.x.j++; //
|
| 813 |
Y* p = const_cast<Y*>(&y); // cast away const-ness of y
|
| 814 |
p->x.i = 99; // well-formed: mutable member can be modified
|
| 815 |
-
p->x.j = 99; // undefined: modifies a const
|
| 816 |
```
|
| 817 |
|
| 818 |
— *end example*]
|
| 819 |
|
| 820 |
The semantics of an access through a volatile glvalue are
|
|
@@ -836,126 +917,226 @@ C. — *end note*]
|
|
| 836 |
The simple type specifiers are
|
| 837 |
|
| 838 |
``` bnf
|
| 839 |
simple-type-specifier:
|
| 840 |
nested-name-specifierₒₚₜ type-name
|
| 841 |
-
nested-name-specifier
|
| 842 |
-
nested-name-specifierₒₚₜ template-name
|
| 843 |
-
'char'
|
| 844 |
-
'char16_t'
|
| 845 |
-
'char32_t'
|
| 846 |
-
'wchar_t'
|
| 847 |
-
'bool'
|
| 848 |
-
'short'
|
| 849 |
-
'int'
|
| 850 |
-
'long'
|
| 851 |
-
'signed'
|
| 852 |
-
'unsigned'
|
| 853 |
-
'float'
|
| 854 |
-
'double'
|
| 855 |
-
'void'
|
| 856 |
-
'auto'
|
| 857 |
decltype-specifier
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 858 |
```
|
| 859 |
|
| 860 |
``` bnf
|
| 861 |
type-name:
|
| 862 |
class-name
|
| 863 |
enum-name
|
| 864 |
typedef-name
|
| 865 |
-
simple-template-id
|
| 866 |
```
|
| 867 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 868 |
``` bnf
|
| 869 |
-
|
| 870 |
-
'decltype' '(' expression ')'
|
| 871 |
-
'decltype' '(' 'auto' ')'
|
| 872 |
```
|
| 873 |
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
`typename`ₒₚₜ *nested-name-specifier*ₒₚₜ *template-name* is a
|
| 877 |
-
placeholder for a deduced class type ([[dcl.type.class.deduct]]). The
|
| 878 |
-
*template-name* shall name a class template that is not an
|
| 879 |
-
injected-class-name. The other *simple-type-specifier*s specify either a
|
| 880 |
-
previously-declared type, a type determined from an expression, or one
|
| 881 |
-
of the fundamental types ([[basic.fundamental]]). Table
|
| 882 |
-
[[tab:simple.type.specifiers]] summarizes the valid combinations of
|
| 883 |
-
*simple-type-specifier*s and the types they specify.
|
| 884 |
|
| 885 |
-
**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 886 |
|
| 887 |
| Specifier(s) | Type |
|
| 888 |
-
| ---------------------- | -------------------------------------- |
|
| 889 |
| *type-name* | the type named |
|
| 890 |
| *simple-template-id* | the type as defined in~ [[temp.names]] |
|
| 891 |
-
| *
|
| 892 |
-
|
|
| 893 |
-
|
|
| 894 |
-
|
|
| 895 |
-
|
|
| 896 |
-
|
|
| 897 |
-
|
|
| 898 |
-
|
|
| 899 |
-
|
|
| 900 |
-
|
|
| 901 |
-
|
|
| 902 |
-
| int
|
| 903 |
-
|
|
| 904 |
-
|
|
| 905 |
-
|
|
| 906 |
-
| unsigned
|
| 907 |
-
| unsigned
|
| 908 |
-
| unsigned long
|
| 909 |
-
|
|
| 910 |
-
|
|
| 911 |
-
|
|
| 912 |
-
| signed long
|
| 913 |
-
|
|
| 914 |
-
| long long
|
| 915 |
-
| long
|
| 916 |
-
| long
|
| 917 |
-
|
|
| 918 |
-
|
|
| 919 |
-
|
|
| 920 |
-
| short
|
| 921 |
-
|
|
| 922 |
-
|
|
| 923 |
-
|
|
| 924 |
-
|
|
| 925 |
-
|
|
| 926 |
-
|
|
| 927 |
-
|
|
| 928 |
-
|
|
| 929 |
|
| 930 |
|
| 931 |
When multiple *simple-type-specifier*s are allowed, they can be freely
|
| 932 |
intermixed with other *decl-specifier*s in any order.
|
| 933 |
|
| 934 |
-
[*Note
|
| 935 |
type are represented as signed or unsigned quantities. The `signed`
|
| 936 |
specifier forces `char` objects to be signed; it is redundant in other
|
| 937 |
contexts. — *end note*]
|
| 938 |
|
| 939 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 940 |
follows:
|
| 941 |
|
| 942 |
-
- if
|
| 943 |
-
|
| 944 |
-
|
| 945 |
-
- otherwise, if
|
| 946 |
-
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
- otherwise, if
|
| 950 |
-
|
| 951 |
-
|
| 952 |
-
|
| 953 |
-
- otherwise, `decltype(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 954 |
|
| 955 |
The operand of the `decltype` specifier is an unevaluated operand
|
| 956 |
-
|
| 957 |
|
| 958 |
[*Example 1*:
|
| 959 |
|
| 960 |
``` cpp
|
| 961 |
const int&& foo();
|
|
@@ -968,29 +1149,30 @@ decltype(a->x) x3; // type is double
|
|
| 968 |
decltype((a->x)) x4 = x3; // type is const double&
|
| 969 |
```
|
| 970 |
|
| 971 |
— *end example*]
|
| 972 |
|
| 973 |
-
[*Note
|
| 974 |
are specified in [[dcl.spec.auto]]. — *end note*]
|
| 975 |
|
| 976 |
-
If the operand of a *decltype-specifier* is a prvalue
|
| 977 |
-
|
| 978 |
-
|
| 979 |
-
|
|
|
|
| 980 |
|
| 981 |
-
[*Note
|
| 982 |
is not destroyed. Thus, a class type is not instantiated as a result of
|
| 983 |
being the type of a function call in this context. In this context, the
|
| 984 |
common purpose of writing the expression is merely to refer to its type.
|
| 985 |
In that sense, a *decltype-specifier* is analogous to a use of a
|
| 986 |
*typedef-name*, so the usual reasons for requiring a complete type do
|
| 987 |
not apply. In particular, it is not necessary to allocate storage for a
|
| 988 |
temporary object or to enforce the semantic constraints associated with
|
| 989 |
invoking the type’s destructor. — *end note*]
|
| 990 |
|
| 991 |
-
[*Note
|
| 992 |
meaning in this context. — *end note*]
|
| 993 |
|
| 994 |
[*Example 2*:
|
| 995 |
|
| 996 |
``` cpp
|
|
@@ -1005,149 +1187,74 @@ template<class T> auto f(T) // #1
|
|
| 1005 |
// (A temporary is not introduced as a result of the use of i().)
|
| 1006 |
template<class T> auto f(T) // #2
|
| 1007 |
-> void;
|
| 1008 |
auto g() -> void {
|
| 1009 |
f(42); // OK: calls #2. (#1 is not a viable candidate: type deduction
|
| 1010 |
-
// fails
|
| 1011 |
// decltype-specifier)
|
| 1012 |
}
|
| 1013 |
template<class T> auto q(T)
|
| 1014 |
-> decltype((h<T>())); // does not force completion of A<T>; A<T>::~A() is not implicitly
|
| 1015 |
// used within the context of this decltype-specifier
|
| 1016 |
void r() {
|
| 1017 |
-
q(42); //
|
| 1018 |
-
// the specialization ``q(T) -> decltype((h<T>()))
|
| 1019 |
-
//
|
| 1020 |
-
// destructor is used, so the program is ill-formed
|
| 1021 |
}
|
| 1022 |
```
|
| 1023 |
|
| 1024 |
— *end example*]
|
| 1025 |
|
| 1026 |
-
####
|
| 1027 |
|
| 1028 |
``` bnf
|
| 1029 |
-
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
class-key nested-name-specifier 'template'ₒₚₜ simple-template-id
|
| 1033 |
-
'enum' nested-name-specifierₒₚₜ identifier
|
| 1034 |
```
|
| 1035 |
|
| 1036 |
-
|
| 1037 |
-
|
| 1038 |
-
a declaration. If an *elaborated-type-specifier* is the sole constituent
|
| 1039 |
-
of a declaration, the declaration is ill-formed unless it is an explicit
|
| 1040 |
-
specialization ([[temp.expl.spec]]), an explicit instantiation (
|
| 1041 |
-
[[temp.explicit]]) or it has one of the following forms:
|
| 1042 |
|
| 1043 |
-
``
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
```
|
| 1050 |
-
|
| 1051 |
-
In the first case, the *attribute-specifier-seq*, if any, appertains to
|
| 1052 |
-
the class being declared; the attributes in the
|
| 1053 |
-
*attribute-specifier-seq* are thereafter considered attributes of the
|
| 1054 |
-
class whenever it is named.
|
| 1055 |
-
|
| 1056 |
-
[[basic.lookup.elab]] describes how name lookup proceeds for the
|
| 1057 |
-
*identifier* in an *elaborated-type-specifier*. If the *identifier*
|
| 1058 |
-
resolves to a *class-name* or *enum-name*, the
|
| 1059 |
-
*elaborated-type-specifier* introduces it into the declaration the same
|
| 1060 |
-
way a *simple-type-specifier* introduces its *type-name*. If the
|
| 1061 |
-
*identifier* resolves to a *typedef-name* or the *simple-template-id*
|
| 1062 |
-
resolves to an alias template specialization, the
|
| 1063 |
-
*elaborated-type-specifier* is ill-formed.
|
| 1064 |
-
|
| 1065 |
-
[*Note 1*:
|
| 1066 |
-
|
| 1067 |
-
This implies that, within a class template with a template
|
| 1068 |
-
*type-parameter* `T`, the declaration
|
| 1069 |
-
|
| 1070 |
-
``` cpp
|
| 1071 |
-
friend class T;
|
| 1072 |
-
```
|
| 1073 |
-
|
| 1074 |
-
is ill-formed. However, the similar declaration `friend T;` is allowed (
|
| 1075 |
-
[[class.friend]]).
|
| 1076 |
-
|
| 1077 |
-
— *end note*]
|
| 1078 |
-
|
| 1079 |
-
The *class-key* or `enum` keyword present in the
|
| 1080 |
-
*elaborated-type-specifier* shall agree in kind with the declaration to
|
| 1081 |
-
which the name in the *elaborated-type-specifier* refers. This rule also
|
| 1082 |
-
applies to the form of *elaborated-type-specifier* that declares a
|
| 1083 |
-
*class-name* or `friend` class since it can be construed as referring to
|
| 1084 |
-
the definition of the class. Thus, in any *elaborated-type-specifier*,
|
| 1085 |
-
the `enum` keyword shall be used to refer to an enumeration (
|
| 1086 |
-
[[dcl.enum]]), the `union` *class-key* shall be used to refer to a union
|
| 1087 |
-
(Clause [[class]]), and either the `class` or `struct` *class-key*
|
| 1088 |
-
shall be used to refer to a class (Clause [[class]]) declared using the
|
| 1089 |
-
`class` or `struct` *class-key*.
|
| 1090 |
-
|
| 1091 |
-
[*Example 1*:
|
| 1092 |
-
|
| 1093 |
-
``` cpp
|
| 1094 |
-
enum class E { a, b };
|
| 1095 |
-
enum E x = E::a; // OK
|
| 1096 |
-
```
|
| 1097 |
-
|
| 1098 |
-
— *end example*]
|
| 1099 |
-
|
| 1100 |
-
#### The `auto` specifier <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
|
| 1101 |
|
| 1102 |
-
|
| 1103 |
-
|
| 1104 |
-
|
| 1105 |
-
function type having a *trailing-return-type* or to signify that a
|
| 1106 |
-
lambda is a generic lambda ([[expr.prim.lambda.closure]]). The `auto`
|
| 1107 |
-
*type-specifier* is also used to introduce a structured binding
|
| 1108 |
-
declaration ([[dcl.struct.bind]]).
|
| 1109 |
|
| 1110 |
The placeholder type can appear with a function declarator in the
|
| 1111 |
*decl-specifier-seq*, *type-specifier-seq*, *conversion-function-id*, or
|
| 1112 |
*trailing-return-type*, in any context where such a declarator is valid.
|
| 1113 |
-
If the function declarator includes a *trailing-return-type*
|
| 1114 |
-
[[dcl.fct]]
|
| 1115 |
type of the function. Otherwise, the function declarator shall declare a
|
| 1116 |
function. If the declared return type of the function contains a
|
| 1117 |
placeholder type, the return type of the function is deduced from
|
| 1118 |
-
non-discarded `return` statements, if any, in the body of the function
|
| 1119 |
-
[[stmt.if]]
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
| 1128 |
-
``` cpp
|
| 1129 |
-
auto glambda = [](int i, auto a) { return i; }; // OK: a generic lambda
|
| 1130 |
-
```
|
| 1131 |
-
|
| 1132 |
-
— *end example*]
|
| 1133 |
-
|
| 1134 |
-
The type of a variable declared using `auto` or `decltype(auto)` is
|
| 1135 |
-
deduced from its initializer. This use is allowed in an initializing
|
| 1136 |
-
declaration ([[dcl.init]]) of a variable. `auto` or `decltype(auto)`
|
| 1137 |
-
shall appear as one of the *decl-specifier*s in the *decl-specifier-seq*
|
| 1138 |
-
and the *decl-specifier-seq* shall be followed by one or more
|
| 1139 |
-
*declarator*s, each of which shall be followed by a non-empty
|
| 1140 |
-
*initializer*. In an *initializer* of the form
|
| 1141 |
|
| 1142 |
``` cpp
|
| 1143 |
( expression-list )
|
| 1144 |
```
|
| 1145 |
|
| 1146 |
the *expression-list* shall be a single *assignment-expression*.
|
| 1147 |
|
| 1148 |
-
[*Example
|
| 1149 |
|
| 1150 |
``` cpp
|
| 1151 |
auto x = 5; // OK: x has type int
|
| 1152 |
const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
|
| 1153 |
static auto y = 0.0; // OK: y has type double
|
|
@@ -1157,25 +1264,28 @@ auto g() { return 0.0; } // OK: g returns double
|
|
| 1157 |
auto h(); // OK: h's return type will be deduced when it is defined
|
| 1158 |
```
|
| 1159 |
|
| 1160 |
— *end example*]
|
| 1161 |
|
|
|
|
|
|
|
|
|
|
| 1162 |
A placeholder type can also be used in the *type-specifier-seq* in the
|
| 1163 |
-
*new-type-id* or *type-id* of a *new-expression*
|
| 1164 |
-
|
| 1165 |
-
in a *template-parameter*
|
| 1166 |
|
| 1167 |
-
A program that uses
|
| 1168 |
-
|
| 1169 |
|
| 1170 |
If the *init-declarator-list* contains more than one *init-declarator*,
|
| 1171 |
they shall all form declarations of variables. The type of each declared
|
| 1172 |
-
variable is determined by placeholder type deduction
|
| 1173 |
-
[[dcl.type.auto.deduct]]
|
| 1174 |
type is not the same in each deduction, the program is ill-formed.
|
| 1175 |
|
| 1176 |
-
[*Example
|
| 1177 |
|
| 1178 |
``` cpp
|
| 1179 |
auto x = 5, *y = &x; // OK: auto is int
|
| 1180 |
auto a = 5, b = { 1, 2 }; // error: different types for auto
|
| 1181 |
```
|
|
@@ -1190,53 +1300,60 @@ same in each deduction, the program is ill-formed.
|
|
| 1190 |
If a function with a declared return type that uses a placeholder type
|
| 1191 |
has no non-discarded `return` statements, the return type is deduced as
|
| 1192 |
though from a `return` statement with no operand at the closing brace of
|
| 1193 |
the function body.
|
| 1194 |
|
| 1195 |
-
[*Example
|
| 1196 |
|
| 1197 |
``` cpp
|
| 1198 |
auto f() { } // OK, return type is void
|
| 1199 |
-
auto* g() { } // error
|
| 1200 |
```
|
| 1201 |
|
| 1202 |
— *end example*]
|
| 1203 |
|
| 1204 |
-
|
| 1205 |
-
|
| 1206 |
-
|
| 1207 |
-
the return type deduced from that statement can be used in the rest of
|
| 1208 |
-
the function, including in other `return` statements.
|
| 1209 |
|
| 1210 |
-
[*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1211 |
|
| 1212 |
``` cpp
|
| 1213 |
-
auto n = n; // error
|
| 1214 |
auto f();
|
| 1215 |
-
void g() { &f; } // error
|
| 1216 |
auto sum(int i) {
|
| 1217 |
if (i == 1)
|
| 1218 |
return i; // sum's return type is int
|
| 1219 |
else
|
| 1220 |
return sum(i-1)+i; // OK, sum's return type has been deduced
|
| 1221 |
}
|
| 1222 |
```
|
| 1223 |
|
| 1224 |
— *end example*]
|
| 1225 |
|
| 1226 |
-
Return type deduction for a
|
| 1227 |
-
|
| 1228 |
-
|
| 1229 |
-
operand.
|
| 1230 |
|
| 1231 |
-
[*Note
|
| 1232 |
template will cause an implicit instantiation. Any errors that arise
|
| 1233 |
from this instantiation are not in the immediate context of the function
|
| 1234 |
-
type and can result in the program being ill-formed
|
| 1235 |
-
[[temp.deduct]]
|
| 1236 |
|
| 1237 |
-
[*Example
|
| 1238 |
|
| 1239 |
``` cpp
|
| 1240 |
template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
|
| 1241 |
typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type
|
| 1242 |
template<class T> auto f(T* t) { return *t; }
|
|
@@ -1246,49 +1363,61 @@ void g() { int (*p)(int*) = &f; } // instantiates both fs to deter
|
|
| 1246 |
|
| 1247 |
— *end example*]
|
| 1248 |
|
| 1249 |
Redeclarations or specializations of a function or function template
|
| 1250 |
with a declared return type that uses a placeholder type shall also use
|
| 1251 |
-
that placeholder, not a deduced type.
|
|
|
|
|
|
|
|
|
|
| 1252 |
|
| 1253 |
-
[*Example
|
| 1254 |
|
| 1255 |
``` cpp
|
| 1256 |
auto f();
|
| 1257 |
auto f() { return 42; } // return type is int
|
| 1258 |
auto f(); // OK
|
| 1259 |
-
int f(); // error
|
| 1260 |
-
decltype(auto) f(); // error
|
| 1261 |
|
| 1262 |
template <typename T> auto g(T t) { return t; } // #1
|
| 1263 |
template auto g(int); // OK, return type is int
|
| 1264 |
-
template char g(char); // error
|
| 1265 |
template<> auto g(double); // OK, forward declaration with unknown return type
|
| 1266 |
|
| 1267 |
template <class T> T g(T t) { return t; } // OK, not functionally equivalent to #1
|
| 1268 |
template char g(char); // OK, now there is a matching template
|
| 1269 |
template auto g(float); // still matches #1
|
| 1270 |
|
| 1271 |
-
void h() { return g(42); } // error
|
| 1272 |
|
| 1273 |
template <typename T> struct A {
|
| 1274 |
friend T frf(T);
|
| 1275 |
};
|
| 1276 |
auto frf(int i) { return i; } // not a friend of A<int>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1277 |
```
|
| 1278 |
|
| 1279 |
— *end example*]
|
| 1280 |
|
| 1281 |
A function declared with a return type that uses a placeholder type
|
| 1282 |
-
shall not be `virtual`
|
| 1283 |
|
| 1284 |
-
|
| 1285 |
-
|
| 1286 |
-
but it also does not prevent that entity from being instantiated as
|
| 1287 |
-
needed to determine its type.
|
| 1288 |
|
| 1289 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1290 |
|
| 1291 |
``` cpp
|
| 1292 |
template <typename T> auto f(T t) { return t; }
|
| 1293 |
extern template auto f(int); // does not instantiate f<int>
|
| 1294 |
int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit
|
|
@@ -1301,45 +1430,46 @@ int (*p)(int) = f; // instantiates f<int> to determine its return t
|
|
| 1301 |
|
| 1302 |
*Placeholder type deduction* is the process by which a type containing a
|
| 1303 |
placeholder type is replaced by a deduced type.
|
| 1304 |
|
| 1305 |
A type `T` containing a placeholder type, and a corresponding
|
| 1306 |
-
initializer
|
| 1307 |
|
| 1308 |
- for a non-discarded `return` statement that occurs in a function
|
| 1309 |
declared with a return type that contains a placeholder type, `T` is
|
| 1310 |
-
the declared return type and
|
| 1311 |
-
statement. If the `return` statement has no operand, then
|
| 1312 |
`void()`;
|
| 1313 |
- for a variable declared with a type that contains a placeholder type,
|
| 1314 |
-
`T` is the declared type of the variable and
|
| 1315 |
-
|
| 1316 |
shall be a *braced-init-list* containing only a single
|
| 1317 |
-
*assignment-expression* and
|
| 1318 |
- for a non-type template parameter declared with a type that contains a
|
| 1319 |
placeholder type, `T` is the declared type of the non-type template
|
| 1320 |
-
parameter and
|
| 1321 |
|
| 1322 |
In the case of a `return` statement with no operand or with an operand
|
| 1323 |
-
of type `void`, `T` shall be either
|
|
|
|
| 1324 |
|
| 1325 |
-
If the deduction is for a `return` statement and
|
| 1326 |
-
*braced-init-list*
|
| 1327 |
|
| 1328 |
-
If the placeholder is the
|
| 1329 |
-
replacing `T` is determined using the rules
|
| 1330 |
-
deduction. Obtain `P` from `T` by replacing the
|
| 1331 |
-
|
| 1332 |
-
|
| 1333 |
-
`std::initializer_list<U>`. Deduce a
|
| 1334 |
-
template argument deduction from a
|
| 1335 |
-
[[temp.deduct.call]]
|
| 1336 |
-
and the corresponding argument is
|
| 1337 |
-
declaration is ill-formed. Otherwise, T' is obtained by
|
| 1338 |
-
deduced `U` into `P`.
|
| 1339 |
|
| 1340 |
-
[*Example
|
| 1341 |
|
| 1342 |
``` cpp
|
| 1343 |
auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
|
| 1344 |
auto x2 = { 1, 2.0 }; // error: cannot deduce element type
|
| 1345 |
auto x3{ 1, 2 }; // error: not a single element
|
|
@@ -1347,11 +1477,11 @@ auto x4 = { 3 }; // decltype(x4) is std::initializer_list<int>
|
|
| 1347 |
auto x5{ 3 }; // decltype(x5) is int
|
| 1348 |
```
|
| 1349 |
|
| 1350 |
— *end example*]
|
| 1351 |
|
| 1352 |
-
[*Example
|
| 1353 |
|
| 1354 |
``` cpp
|
| 1355 |
const auto &i = expr;
|
| 1356 |
```
|
| 1357 |
|
|
@@ -1362,16 +1492,16 @@ The type of `i` is the deduced type of the parameter `u` in the call
|
|
| 1362 |
template <class U> void f(const U& u);
|
| 1363 |
```
|
| 1364 |
|
| 1365 |
— *end example*]
|
| 1366 |
|
| 1367 |
-
If the placeholder is the
|
| 1368 |
-
be the placeholder alone. The type deduced
|
| 1369 |
-
described in [[dcl.type.simple]], as though
|
| 1370 |
-
the `decltype`.
|
| 1371 |
|
| 1372 |
-
[*Example
|
| 1373 |
|
| 1374 |
``` cpp
|
| 1375 |
int i;
|
| 1376 |
int&& f();
|
| 1377 |
auto x2a(i); // decltype(x2a) is int
|
|
@@ -1381,36 +1511,56 @@ decltype(auto) x3d = i; // decltype(x3d) is int
|
|
| 1381 |
auto x4a = (i); // decltype(x4a) is int
|
| 1382 |
decltype(auto) x4d = (i); // decltype(x4d) is int&
|
| 1383 |
auto x5a = f(); // decltype(x5a) is int
|
| 1384 |
decltype(auto) x5d = f(); // decltype(x5d) is int&&
|
| 1385 |
auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
|
| 1386 |
-
decltype(auto) x6d = { 1, 2 }; // error
|
| 1387 |
auto *x7a = &i; // decltype(x7a) is int*
|
| 1388 |
-
decltype(auto)*x7d = &i; // error
|
| 1389 |
```
|
| 1390 |
|
| 1391 |
— *end example*]
|
| 1392 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1393 |
#### Deduced class template specialization types <a id="dcl.type.class.deduct">[[dcl.type.class.deduct]]</a>
|
| 1394 |
|
| 1395 |
If a placeholder for a deduced class type appears as a *decl-specifier*
|
| 1396 |
-
in the *decl-specifier-seq* of an initializing declaration
|
| 1397 |
-
|
| 1398 |
-
|
| 1399 |
-
|
| 1400 |
-
|
| 1401 |
-
|
| 1402 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1403 |
|
| 1404 |
A placeholder for a deduced class type can also be used in the
|
| 1405 |
*type-specifier-seq* in the *new-type-id* or *type-id* of a
|
| 1406 |
-
*new-expression*
|
| 1407 |
-
|
| 1408 |
-
|
| 1409 |
-
|
|
|
|
| 1410 |
|
| 1411 |
-
[*Example
|
| 1412 |
|
| 1413 |
``` cpp
|
| 1414 |
template<class T> struct container {
|
| 1415 |
container(T t) {}
|
| 1416 |
template<class Iter> container(Iter beg, Iter end);
|
|
@@ -1419,10 +1569,10 @@ template<class Iter>
|
|
| 1419 |
container(Iter b, Iter e) -> container<typename std::iterator_traits<Iter>::value_type>;
|
| 1420 |
std::vector<double> v = { ... };
|
| 1421 |
|
| 1422 |
container c(7); // OK, deduces int for T
|
| 1423 |
auto d = container(v.begin(), v.end()); // OK, deduces double for T
|
| 1424 |
-
container e{5, 6}; // error
|
| 1425 |
```
|
| 1426 |
|
| 1427 |
— *end example*]
|
| 1428 |
|
|
|
|
| 5 |
``` bnf
|
| 6 |
decl-specifier:
|
| 7 |
storage-class-specifier
|
| 8 |
defining-type-specifier
|
| 9 |
function-specifier
|
| 10 |
+
friend
|
| 11 |
+
typedef
|
| 12 |
+
constexpr
|
| 13 |
+
consteval
|
| 14 |
+
constinit
|
| 15 |
+
inline
|
| 16 |
```
|
| 17 |
|
| 18 |
``` bnf
|
| 19 |
decl-specifier-seq:
|
| 20 |
decl-specifier attribute-specifier-seqₒₚₜ
|
| 21 |
decl-specifier decl-specifier-seq
|
| 22 |
```
|
| 23 |
|
| 24 |
The optional *attribute-specifier-seq* in a *decl-specifier-seq*
|
| 25 |
+
appertains to the type determined by the preceding *decl-specifier*s
|
| 26 |
+
[[dcl.meaning]]. The *attribute-specifier-seq* affects the type only for
|
| 27 |
+
the declaration it appears in, not other declarations involving the same
|
| 28 |
+
type.
|
| 29 |
|
| 30 |
Each *decl-specifier* shall appear at most once in a complete
|
| 31 |
+
*decl-specifier-seq*, except that `long` may appear twice. At most one
|
| 32 |
+
of the `constexpr`, `consteval`, and `constinit` keywords shall appear
|
| 33 |
+
in a *decl-specifier-seq*.
|
| 34 |
|
| 35 |
If a *type-name* is encountered while parsing a *decl-specifier-seq*, it
|
| 36 |
is interpreted as part of the *decl-specifier-seq* if and only if there
|
| 37 |
is no previous *defining-type-specifier* other than a *cv-qualifier* in
|
| 38 |
the *decl-specifier-seq*. The sequence shall be self-consistent as
|
|
|
|
| 80 |
|
| 81 |
The storage class specifiers are
|
| 82 |
|
| 83 |
``` bnf
|
| 84 |
storage-class-specifier:
|
| 85 |
+
static
|
| 86 |
+
thread_local
|
| 87 |
+
extern
|
| 88 |
+
mutable
|
| 89 |
```
|
| 90 |
|
| 91 |
At most one *storage-class-specifier* shall appear in a given
|
| 92 |
*decl-specifier-seq*, except that `thread_local` may appear with
|
| 93 |
`static` or `extern`. If `thread_local` appears in any declaration of a
|
| 94 |
variable it shall be present in all declarations of that entity. If a
|
| 95 |
*storage-class-specifier* appears in a *decl-specifier-seq*, there can
|
| 96 |
be no `typedef` specifier in the same *decl-specifier-seq* and the
|
| 97 |
*init-declarator-list* or *member-declarator-list* of the declaration
|
| 98 |
shall not be empty (except for an anonymous union declared in a named
|
| 99 |
+
namespace or in the global namespace, which shall be declared `static`
|
| 100 |
+
[[class.union.anon]]). The *storage-class-specifier* applies to the name
|
| 101 |
+
declared by each *init-declarator* in the list and not to any names
|
| 102 |
+
declared by other specifiers.
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
[*Note 1*: See [[temp.expl.spec]] and [[temp.explicit]] for
|
| 105 |
+
restrictions in explicit specializations and explicit instantiations,
|
| 106 |
+
respectively. — *end note*]
|
| 107 |
+
|
| 108 |
+
[*Note 2*: A variable declared without a *storage-class-specifier* at
|
| 109 |
block scope or declared as a function parameter has automatic storage
|
| 110 |
+
duration by default [[basic.stc.auto]]. — *end note*]
|
| 111 |
|
| 112 |
The `thread_local` specifier indicates that the named entity has thread
|
| 113 |
+
storage duration [[basic.stc.thread]]. It shall be applied only to the
|
| 114 |
+
declaration of a variable of namespace or block scope, to a structured
|
| 115 |
+
binding declaration [[dcl.struct.bind]], or to the declaration of a
|
| 116 |
+
static data member. When `thread_local` is applied to a variable of
|
| 117 |
block scope the *storage-class-specifier* `static` is implied if no
|
| 118 |
other *storage-class-specifier* appears in the *decl-specifier-seq*.
|
| 119 |
|
| 120 |
+
The `static` specifier shall be applied only to the declaration of a
|
| 121 |
+
variable or function, to a structured binding declaration
|
| 122 |
+
[[dcl.struct.bind]], or to the declaration of an anonymous union
|
| 123 |
+
[[class.union.anon]]. There can be no `static` function declarations
|
| 124 |
+
within a block, nor any `static` function parameters. A `static`
|
| 125 |
+
specifier used in the declaration of a variable declares the variable to
|
| 126 |
+
have static storage duration [[basic.stc.static]], unless accompanied by
|
| 127 |
+
the `thread_local` specifier, which declares the variable to have thread
|
| 128 |
+
storage duration [[basic.stc.thread]]. A `static` specifier can be used
|
| 129 |
+
in declarations of class members; [[class.static]] describes its
|
| 130 |
+
effect. For the linkage of a name declared with a `static` specifier,
|
| 131 |
+
see [[basic.link]].
|
| 132 |
|
| 133 |
+
The `extern` specifier shall be applied only to the declaration of a
|
| 134 |
+
variable or function. The `extern` specifier shall not be used in the
|
| 135 |
+
declaration of a class member or function parameter. For the linkage of
|
| 136 |
+
a name declared with an `extern` specifier, see [[basic.link]].
|
| 137 |
|
| 138 |
+
[*Note 3*: The `extern` keyword can also be used in
|
| 139 |
*explicit-instantiation*s and *linkage-specification*s, but it is not a
|
| 140 |
*storage-class-specifier* in such contexts. — *end note*]
|
| 141 |
|
| 142 |
The linkages implied by successive declarations for a given entity shall
|
| 143 |
agree. That is, within a given scope, each declaration declaring the
|
| 144 |
same variable name or the same overloading of a function name shall
|
| 145 |
+
imply the same linkage.
|
|
|
|
| 146 |
|
| 147 |
[*Example 1*:
|
| 148 |
|
| 149 |
``` cpp
|
| 150 |
static char* f(); // f() has internal linkage
|
|
|
|
| 201 |
```
|
| 202 |
|
| 203 |
— *end example*]
|
| 204 |
|
| 205 |
The `mutable` specifier shall appear only in the declaration of a
|
| 206 |
+
non-static data member [[class.mem]] whose type is neither
|
| 207 |
const-qualified nor a reference type.
|
| 208 |
|
| 209 |
[*Example 3*:
|
| 210 |
|
| 211 |
``` cpp
|
| 212 |
class X {
|
| 213 |
mutable const int* p; // OK
|
| 214 |
+
mutable int* const q; // error
|
| 215 |
};
|
| 216 |
```
|
| 217 |
|
| 218 |
— *end example*]
|
| 219 |
|
| 220 |
+
[*Note 4*: The `mutable` specifier on a class data member nullifies a
|
| 221 |
+
`const` specifier applied to the containing class object and permits
|
| 222 |
modification of the mutable class member even though the rest of the
|
| 223 |
+
object is const ([[basic.type.qualifier]],
|
| 224 |
+
[[dcl.type.cv]]). — *end note*]
|
| 225 |
|
| 226 |
### Function specifiers <a id="dcl.fct.spec">[[dcl.fct.spec]]</a>
|
| 227 |
|
| 228 |
+
A *function-specifier* can be used only in a function declaration.
|
| 229 |
|
| 230 |
``` bnf
|
| 231 |
function-specifier:
|
| 232 |
+
virtual
|
| 233 |
+
explicit-specifier
|
| 234 |
+
```
|
| 235 |
+
|
| 236 |
+
``` bnf
|
| 237 |
+
explicit-specifier:
|
| 238 |
+
explicit '(' constant-expression ')'
|
| 239 |
+
explicit
|
| 240 |
```
|
| 241 |
|
| 242 |
The `virtual` specifier shall be used only in the initial declaration of
|
| 243 |
a non-static class member function; see [[class.virtual]].
|
| 244 |
|
| 245 |
+
An *explicit-specifier* shall be used only in the declaration of a
|
| 246 |
constructor or conversion function within its class definition; see
|
| 247 |
[[class.conv.ctor]] and [[class.conv.fct]].
|
| 248 |
|
| 249 |
+
In an *explicit-specifier*, the *constant-expression*, if supplied,
|
| 250 |
+
shall be a contextually converted constant expression of type `bool`
|
| 251 |
+
[[expr.const]]. The *explicit-specifier* `explicit` without a
|
| 252 |
+
*constant-expression* is equivalent to the *explicit-specifier*
|
| 253 |
+
`explicit(true)`. If the constant expression evaluates to `true`, the
|
| 254 |
+
function is explicit. Otherwise, the function is not explicit. A `(`
|
| 255 |
+
token that follows `explicit` is parsed as part of the
|
| 256 |
+
*explicit-specifier*.
|
| 257 |
+
|
| 258 |
### The `typedef` specifier <a id="dcl.typedef">[[dcl.typedef]]</a>
|
| 259 |
|
| 260 |
Declarations containing the *decl-specifier* `typedef` declare
|
| 261 |
+
identifiers that can be used later for naming fundamental
|
| 262 |
+
[[basic.fundamental]] or compound [[basic.compound]] types. The
|
| 263 |
`typedef` specifier shall not be combined in a *decl-specifier-seq* with
|
| 264 |
any other kind of specifier except a *defining-type-specifier*, and it
|
| 265 |
shall not be used in the *decl-specifier-seq* of a
|
| 266 |
+
*parameter-declaration* [[dcl.fct]] nor in the *decl-specifier-seq* of a
|
| 267 |
+
*function-definition* [[dcl.fct.def]]. If a `typedef` specifier appears
|
| 268 |
+
in a declaration without a *declarator*, the program is ill-formed.
|
|
|
|
| 269 |
|
| 270 |
``` bnf
|
| 271 |
typedef-name:
|
| 272 |
identifier
|
| 273 |
+
simple-template-id
|
| 274 |
```
|
| 275 |
|
| 276 |
+
A name declared with the `typedef` specifier becomes a *typedef-name*. A
|
| 277 |
+
*typedef-name* names the type associated with the *identifier*
|
| 278 |
+
[[dcl.decl]] or *simple-template-id* [[temp.pre]]; a *typedef-name* is
|
| 279 |
+
thus a synonym for another type. A *typedef-name* does not introduce a
|
| 280 |
+
new type the way a class declaration [[class.name]] or enum declaration
|
| 281 |
+
[[dcl.enum]] does.
|
|
|
|
| 282 |
|
| 283 |
[*Example 1*:
|
| 284 |
|
| 285 |
After
|
| 286 |
|
|
|
|
| 311 |
|
| 312 |
``` cpp
|
| 313 |
using handler_t = void (*)(int);
|
| 314 |
extern handler_t ignore;
|
| 315 |
extern void (*ignore)(int); // redeclare ignore
|
| 316 |
+
using cell = pair<void*, cell*>; // error
|
| 317 |
```
|
| 318 |
|
| 319 |
— *end example*]
|
| 320 |
|
| 321 |
The *defining-type-specifier-seq* of the *defining-type-id* shall not
|
| 322 |
define a class or enumeration if the *alias-declaration* is the
|
| 323 |
*declaration* of a *template-declaration*.
|
| 324 |
|
| 325 |
In a given non-class scope, a `typedef` specifier can be used to
|
| 326 |
+
redeclare the name of any type declared in that scope to refer to the
|
| 327 |
type to which it already refers.
|
| 328 |
|
| 329 |
[*Example 3*:
|
| 330 |
|
| 331 |
``` cpp
|
|
|
|
| 335 |
typedef I I;
|
| 336 |
```
|
| 337 |
|
| 338 |
— *end example*]
|
| 339 |
|
| 340 |
+
In a given class scope, a `typedef` specifier can be used to redeclare
|
| 341 |
any *class-name* declared in that scope that is not also a
|
| 342 |
*typedef-name* to refer to the type to which it already refers.
|
| 343 |
|
| 344 |
[*Example 4*:
|
| 345 |
|
|
|
|
| 351 |
};
|
| 352 |
```
|
| 353 |
|
| 354 |
— *end example*]
|
| 355 |
|
| 356 |
+
If a `typedef` specifier is used to redeclare in a given scope an entity
|
| 357 |
that can be referenced using an *elaborated-type-specifier*, the entity
|
| 358 |
can continue to be referenced by an *elaborated-type-specifier* or as an
|
| 359 |
enumeration or class name in an enumeration or class definition
|
| 360 |
respectively.
|
| 361 |
|
|
|
|
| 370 |
struct S { }; // OK
|
| 371 |
```
|
| 372 |
|
| 373 |
— *end example*]
|
| 374 |
|
| 375 |
+
In a given scope, a `typedef` specifier shall not be used to redeclare
|
| 376 |
the name of any type declared in that scope to refer to a different
|
| 377 |
type.
|
| 378 |
|
| 379 |
[*Example 6*:
|
| 380 |
|
|
|
|
| 396 |
class complex { ... }; // error: redefinition
|
| 397 |
```
|
| 398 |
|
| 399 |
— *end example*]
|
| 400 |
|
| 401 |
+
A *simple-template-id* is only a *typedef-name* if its *template-name*
|
| 402 |
+
names an alias template or a template *template-parameter*.
|
| 403 |
+
|
| 404 |
+
[*Note 1*: A *simple-template-id* that names a class template
|
| 405 |
+
specialization is a *class-name* [[class.name]]. If a *typedef-name* is
|
| 406 |
+
used to identify the subject of an *elaborated-type-specifier*
|
| 407 |
+
[[dcl.type.elab]], a class definition [[class]], a constructor
|
| 408 |
+
declaration [[class.ctor]], or a destructor declaration [[class.dtor]],
|
| 409 |
+
the program is ill-formed. — *end note*]
|
| 410 |
|
| 411 |
[*Example 8*:
|
| 412 |
|
| 413 |
``` cpp
|
| 414 |
struct S {
|
|
|
|
| 422 |
struct T * p; // error
|
| 423 |
```
|
| 424 |
|
| 425 |
— *end example*]
|
| 426 |
|
| 427 |
+
If the typedef declaration defines an unnamed class or enumeration, the
|
| 428 |
+
first *typedef-name* declared by the declaration to be that type is used
|
| 429 |
+
to denote the type for linkage purposes only [[basic.link]].
|
| 430 |
+
|
| 431 |
+
[*Note 2*: A typedef declaration involving a *lambda-expression* does
|
| 432 |
+
not itself define the associated closure type, and so the closure type
|
| 433 |
+
is not given a name for linkage purposes. — *end note*]
|
| 434 |
|
| 435 |
[*Example 9*:
|
| 436 |
|
| 437 |
``` cpp
|
| 438 |
typedef struct { } *ps, S; // S is the class name for linkage purposes
|
| 439 |
+
typedef decltype([]{}) C; // the closure type has no name for linkage purposes
|
| 440 |
+
```
|
| 441 |
+
|
| 442 |
+
— *end example*]
|
| 443 |
+
|
| 444 |
+
An unnamed class with a typedef name for linkage purposes shall not
|
| 445 |
+
|
| 446 |
+
- declare any members other than non-static data members, member
|
| 447 |
+
enumerations, or member classes,
|
| 448 |
+
- have any base classes or default member initializers, or
|
| 449 |
+
- contain a *lambda-expression*,
|
| 450 |
+
|
| 451 |
+
and all member classes shall also satisfy these requirements
|
| 452 |
+
(recursively).
|
| 453 |
+
|
| 454 |
+
[*Example 10*:
|
| 455 |
+
|
| 456 |
+
``` cpp
|
| 457 |
+
typedef struct {
|
| 458 |
+
int f() {}
|
| 459 |
+
} X; // error: struct with typedef name for linkage has member functions
|
| 460 |
```
|
| 461 |
|
| 462 |
— *end example*]
|
| 463 |
|
| 464 |
### The `friend` specifier <a id="dcl.friend">[[dcl.friend]]</a>
|
| 465 |
|
| 466 |
The `friend` specifier is used to specify access to class members; see
|
| 467 |
[[class.friend]].
|
| 468 |
|
| 469 |
+
### The `constexpr` and `consteval` specifiers <a id="dcl.constexpr">[[dcl.constexpr]]</a>
|
| 470 |
|
| 471 |
The `constexpr` specifier shall be applied only to the definition of a
|
| 472 |
variable or variable template or the declaration of a function or
|
| 473 |
+
function template. The `consteval` specifier shall be applied only to
|
| 474 |
+
the declaration of a function or function template. A function or static
|
| 475 |
+
data member declared with the `constexpr` or `consteval` specifier is
|
| 476 |
+
implicitly an inline function or variable [[dcl.inline]]. If any
|
| 477 |
+
declaration of a function or function template has a `constexpr` or
|
| 478 |
+
`consteval` specifier, then all its declarations shall contain the same
|
| 479 |
+
specifier.
|
| 480 |
|
| 481 |
[*Note 1*: An explicit specialization can differ from the template
|
| 482 |
+
declaration with respect to the `constexpr` or `consteval`
|
| 483 |
+
specifier. — *end note*]
|
| 484 |
|
| 485 |
[*Note 2*: Function parameters cannot be declared
|
| 486 |
`constexpr`. — *end note*]
|
| 487 |
|
| 488 |
[*Example 1*:
|
|
|
|
| 497 |
};
|
| 498 |
constexpr pixel::pixel(int a)
|
| 499 |
: x(a), y(x) // OK: definition
|
| 500 |
{ square(x); }
|
| 501 |
constexpr pixel small(2); // error: square not defined, so small(2)
|
| 502 |
+
// not constant[expr.const] so constexpr not satisfied
|
| 503 |
|
| 504 |
constexpr void square(int &x) { // OK: definition
|
| 505 |
x *= x;
|
| 506 |
}
|
| 507 |
constexpr pixel large(4); // OK: square defined
|
|
|
|
| 511 |
extern constexpr int memsz; // error: not a definition
|
| 512 |
```
|
| 513 |
|
| 514 |
— *end example*]
|
| 515 |
|
| 516 |
+
A `constexpr` or `consteval` specifier used in the declaration of a
|
| 517 |
+
function declares that function to be a *constexpr function*. A function
|
| 518 |
+
or constructor declared with the `consteval` specifier is called an
|
| 519 |
+
*immediate function*. A destructor, an allocation function, or a
|
| 520 |
+
deallocation function shall not be declared with the `consteval`
|
| 521 |
+
specifier.
|
| 522 |
|
| 523 |
The definition of a constexpr function shall satisfy the following
|
| 524 |
requirements:
|
| 525 |
|
| 526 |
+
- its return type (if any) shall be a literal type;
|
|
|
|
| 527 |
- each of its parameter types shall be a literal type;
|
| 528 |
+
- it shall not be a coroutine [[dcl.fct.def.coroutine]];
|
| 529 |
+
- if the function is a constructor or destructor, its class shall not
|
| 530 |
+
have any virtual base classes;
|
| 531 |
+
- its *function-body* shall not enclose [[stmt.pre]]
|
| 532 |
- a `goto` statement,
|
| 533 |
+
- an identifier label [[stmt.label]],
|
|
|
|
| 534 |
- a definition of a variable of non-literal type or of static or
|
| 535 |
+
thread storage duration.
|
| 536 |
+
|
| 537 |
+
\[*Note 3*: A *function-body* that is `= delete` or `= default`
|
| 538 |
+
encloses none of the above. — *end note*]
|
| 539 |
|
| 540 |
[*Example 2*:
|
| 541 |
|
| 542 |
``` cpp
|
| 543 |
constexpr int square(int x)
|
|
|
|
| 552 |
constexpr int first(int n) {
|
| 553 |
static int value = n; // error: variable has static storage duration
|
| 554 |
return value;
|
| 555 |
}
|
| 556 |
constexpr int uninit() {
|
| 557 |
+
struct { int a; } s;
|
| 558 |
+
return s.a; // error: uninitialized read of s.a
|
| 559 |
}
|
| 560 |
constexpr int prev(int x)
|
| 561 |
{ return --x; } // OK
|
| 562 |
constexpr int g(int x, int n) { // OK
|
| 563 |
int r = 1;
|
|
|
|
| 566 |
}
|
| 567 |
```
|
| 568 |
|
| 569 |
— *end example*]
|
| 570 |
|
| 571 |
+
The definition of a constexpr constructor whose *function-body* is not
|
| 572 |
+
`= delete` shall additionally satisfy the following requirements:
|
| 573 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 574 |
- for a non-delegating constructor, every constructor selected to
|
| 575 |
initialize non-static data members and base class subobjects shall be
|
| 576 |
a constexpr constructor;
|
| 577 |
- for a delegating constructor, the target constructor shall be a
|
| 578 |
constexpr constructor.
|
|
|
|
| 587 |
};
|
| 588 |
```
|
| 589 |
|
| 590 |
— *end example*]
|
| 591 |
|
| 592 |
+
The definition of a constexpr destructor whose *function-body* is not
|
| 593 |
+
`= delete` shall additionally satisfy the following requirement:
|
| 594 |
+
|
| 595 |
+
- for every subobject of class type or (possibly multi-dimensional)
|
| 596 |
+
array thereof, that class type shall have a constexpr destructor.
|
| 597 |
+
|
| 598 |
For a constexpr function or constexpr constructor that is neither
|
| 599 |
defaulted nor a template, if no argument values exist such that an
|
| 600 |
invocation of the function or constructor could be an evaluated
|
| 601 |
+
subexpression of a core constant expression [[expr.const]], or, for a
|
| 602 |
+
constructor, an evaluated subexpression of the initialization
|
| 603 |
+
full-expression of some constant-initialized object
|
| 604 |
+
[[basic.start.static]], the program is ill-formed, no diagnostic
|
| 605 |
required.
|
| 606 |
|
| 607 |
[*Example 4*:
|
| 608 |
|
| 609 |
``` cpp
|
|
|
|
| 626 |
|
| 627 |
— *end example*]
|
| 628 |
|
| 629 |
If the instantiated template specialization of a constexpr function
|
| 630 |
template or member function of a class template would fail to satisfy
|
| 631 |
+
the requirements for a constexpr function, that specialization is still
|
| 632 |
+
a constexpr function, even though a call to such a function cannot
|
| 633 |
+
appear in a constant expression. If no specialization of the template
|
| 634 |
+
would satisfy the requirements for a constexpr function when considered
|
| 635 |
+
as a non-template function, the template is ill-formed, no diagnostic
|
| 636 |
+
required.
|
|
|
|
| 637 |
|
| 638 |
+
An invocation of a constexpr function in a given context produces the
|
| 639 |
+
same result as an invocation of an equivalent non-constexpr function in
|
| 640 |
+
the same context in all respects except that
|
| 641 |
|
| 642 |
+
- an invocation of a constexpr function can appear in a constant
|
| 643 |
+
expression [[expr.const]] and
|
| 644 |
+
- copy elision is not performed in a constant expression
|
| 645 |
+
[[class.copy.elision]].
|
| 646 |
|
| 647 |
+
[*Note 4*: Declaring a function constexpr can change whether an
|
| 648 |
+
expression is a constant expression. This can indirectly cause calls to
|
| 649 |
+
`std::is_constant_evaluated` within an invocation of the function to
|
| 650 |
+
produce a different value. — *end note*]
|
| 651 |
+
|
| 652 |
+
The `constexpr` and `consteval` specifiers have no effect on the type of
|
| 653 |
+
a constexpr function.
|
| 654 |
|
| 655 |
[*Example 5*:
|
| 656 |
|
| 657 |
``` cpp
|
| 658 |
constexpr int bar(int x, int y) // OK
|
|
|
|
| 663 |
```
|
| 664 |
|
| 665 |
— *end example*]
|
| 666 |
|
| 667 |
A `constexpr` specifier used in an object declaration declares the
|
| 668 |
+
object as const. Such an object shall have literal type and shall be
|
| 669 |
initialized. In any `constexpr` variable declaration, the
|
| 670 |
+
full-expression of the initialization shall be a constant expression
|
| 671 |
+
[[expr.const]]. A `constexpr` variable shall have constant destruction.
|
| 672 |
|
| 673 |
[*Example 6*:
|
| 674 |
|
| 675 |
``` cpp
|
| 676 |
struct pixel {
|
|
|
|
| 680 |
constexpr pixel origin; // error: initializer missing
|
| 681 |
```
|
| 682 |
|
| 683 |
— *end example*]
|
| 684 |
|
| 685 |
+
### The `constinit` specifier <a id="dcl.constinit">[[dcl.constinit]]</a>
|
| 686 |
+
|
| 687 |
+
The `constinit` specifier shall be applied only to a declaration of a
|
| 688 |
+
variable with static or thread storage duration. If the specifier is
|
| 689 |
+
applied to any declaration of a variable, it shall be applied to the
|
| 690 |
+
initializing declaration. No diagnostic is required if no `constinit`
|
| 691 |
+
declaration is reachable at the point of the initializing declaration.
|
| 692 |
+
|
| 693 |
+
If a variable declared with the `constinit` specifier has dynamic
|
| 694 |
+
initialization [[basic.start.dynamic]], the program is ill-formed.
|
| 695 |
+
|
| 696 |
+
[*Note 1*: The `constinit` specifier ensures that the variable is
|
| 697 |
+
initialized during static initialization
|
| 698 |
+
[[basic.start.static]]. — *end note*]
|
| 699 |
+
|
| 700 |
+
[*Example 1*:
|
| 701 |
+
|
| 702 |
+
``` cpp
|
| 703 |
+
const char * g() { return "dynamic initialization"; }
|
| 704 |
+
constexpr const char * f(bool p) { return p ? "constant initializer" : g(); }
|
| 705 |
+
constinit const char * c = f(true); // OK
|
| 706 |
+
constinit const char * d = f(false); // error
|
| 707 |
+
```
|
| 708 |
+
|
| 709 |
+
— *end example*]
|
| 710 |
+
|
| 711 |
### The `inline` specifier <a id="dcl.inline">[[dcl.inline]]</a>
|
| 712 |
|
| 713 |
+
The `inline` specifier shall be applied only to the declaration of a
|
| 714 |
+
variable or function.
|
| 715 |
|
| 716 |
+
A function declaration ([[dcl.fct]], [[class.mfct]], [[class.friend]])
|
| 717 |
with an `inline` specifier declares an *inline function*. The inline
|
| 718 |
specifier indicates to the implementation that inline substitution of
|
| 719 |
the function body at the point of call is to be preferred to the usual
|
| 720 |
function call mechanism. An implementation is not required to perform
|
| 721 |
this inline substitution at the point of call; however, even if this
|
| 722 |
inline substitution is omitted, the other rules for inline functions
|
| 723 |
+
specified in this subclause shall still be respected.
|
| 724 |
|
| 725 |
+
[*Note 1*: The `inline` keyword has no effect on the linkage of a
|
| 726 |
+
function. In certain cases, an inline function cannot use names with
|
| 727 |
+
internal linkage; see [[basic.link]]. — *end note*]
|
| 728 |
|
| 729 |
+
A variable declaration with an `inline` specifier declares an
|
| 730 |
+
*inline variable*.
|
| 731 |
|
| 732 |
+
The `inline` specifier shall not appear on a block scope declaration or
|
| 733 |
+
on the declaration of a function parameter. If the `inline` specifier is
|
| 734 |
+
used in a friend function declaration, that declaration shall be a
|
| 735 |
+
definition or the function shall have previously been declared inline.
|
| 736 |
|
| 737 |
+
If a definition of a function or variable is reachable at the point of
|
| 738 |
+
its first declaration as inline, the program is ill-formed. If a
|
| 739 |
+
function or variable with external or module linkage is declared inline
|
| 740 |
+
in one definition domain, an inline declaration of it shall be reachable
|
| 741 |
+
from the end of every definition domain in which it is declared; no
|
| 742 |
+
diagnostic is required.
|
| 743 |
|
| 744 |
+
[*Note 2*: A call to an inline function or a use of an inline variable
|
| 745 |
+
may be encountered before its definition becomes reachable in a
|
| 746 |
translation unit. — *end note*]
|
| 747 |
|
| 748 |
+
[*Note 3*: An inline function or variable with external or module
|
| 749 |
+
linkage has the same address in all translation units. A `static` local
|
| 750 |
+
variable in an inline function with external or module linkage always
|
| 751 |
+
refers to the same object. A type defined within the body of an inline
|
| 752 |
+
function with external or module linkage is the same type in every
|
| 753 |
+
translation unit. — *end note*]
|
| 754 |
+
|
| 755 |
+
If an inline function or variable that is attached to a named module is
|
| 756 |
+
declared in a definition domain, it shall be defined in that domain.
|
| 757 |
+
|
| 758 |
+
[*Note 4*: A constexpr function [[dcl.constexpr]] is implicitly inline.
|
| 759 |
+
In the global module, a function defined within a class definition is
|
| 760 |
+
implicitly inline ([[class.mfct]], [[class.friend]]). — *end note*]
|
| 761 |
|
| 762 |
### Type specifiers <a id="dcl.type">[[dcl.type]]</a>
|
| 763 |
|
| 764 |
The type-specifiers are
|
| 765 |
|
|
|
|
| 790 |
defining-type-specifier defining-type-specifier-seq
|
| 791 |
```
|
| 792 |
|
| 793 |
The optional *attribute-specifier-seq* in a *type-specifier-seq* or a
|
| 794 |
*defining-type-specifier-seq* appertains to the type denoted by the
|
| 795 |
+
preceding *type-specifier*s or *defining-type-specifier*s
|
| 796 |
+
[[dcl.meaning]]. The *attribute-specifier-seq* affects the type only for
|
| 797 |
+
the declaration it appears in, not other declarations involving the same
|
| 798 |
+
type.
|
| 799 |
|
| 800 |
As a general rule, at most one *defining-type-specifier* is allowed in
|
| 801 |
the complete *decl-specifier-seq* of a *declaration* or in a
|
| 802 |
*defining-type-specifier-seq*, and at most one *type-specifier* is
|
| 803 |
allowed in a *type-specifier-seq*. The only exceptions to this rule are
|
|
|
|
| 812 |
- `long` can be combined with `long`.
|
| 813 |
|
| 814 |
Except in a declaration of a constructor, destructor, or conversion
|
| 815 |
function, at least one *defining-type-specifier* that is not a
|
| 816 |
*cv-qualifier* shall appear in a complete *type-specifier-seq* or a
|
| 817 |
+
complete *decl-specifier-seq*.[^1]
|
| 818 |
|
| 819 |
[*Note 1*: *enum-specifier*s, *class-specifier*s, and
|
| 820 |
+
*typename-specifier*s are discussed in [[dcl.enum]], [[class]], and
|
| 821 |
+
[[temp.res]], respectively. The remaining *type-specifier*s are
|
| 822 |
+
discussed in the rest of this subclause. — *end note*]
|
| 823 |
|
| 824 |
#### The *cv-qualifier*s <a id="dcl.type.cv">[[dcl.type.cv]]</a>
|
| 825 |
|
| 826 |
There are two *cv-qualifier*s, `const` and `volatile`. Each
|
| 827 |
*cv-qualifier* shall appear at most once in a *cv-qualifier-seq*. If a
|
|
|
|
| 835 |
Redundant cv-qualifications are ignored.
|
| 836 |
|
| 837 |
[*Note 2*: For example, these could be introduced by
|
| 838 |
typedefs. — *end note*]
|
| 839 |
|
| 840 |
+
[*Note 3*: Declaring a variable `const` can affect its linkage
|
| 841 |
+
[[dcl.stc]] and its usability in constant expressions [[expr.const]]. As
|
| 842 |
+
described in [[dcl.init]], the definition of an object or subobject of
|
| 843 |
+
const-qualified type must specify an initializer or be subject to
|
| 844 |
+
default-initialization. — *end note*]
|
| 845 |
|
| 846 |
A pointer or reference to a cv-qualified type need not actually point or
|
| 847 |
refer to a cv-qualified object, but it is treated as if it does; a
|
| 848 |
const-qualified access path cannot be used to modify an object even if
|
| 849 |
the object referenced is a non-const object and can be modified through
|
| 850 |
some other access path.
|
| 851 |
|
| 852 |
[*Note 4*: Cv-qualifiers are supported by the type system so that they
|
| 853 |
+
cannot be subverted without casting [[expr.const.cast]]. — *end note*]
|
|
|
|
| 854 |
|
| 855 |
+
Any attempt to modify ([[expr.ass]], [[expr.post.incr]],
|
| 856 |
+
[[expr.pre.incr]]) a const object [[basic.type.qualifier]] during its
|
| 857 |
+
lifetime [[basic.life]] results in undefined behavior.
|
| 858 |
|
| 859 |
[*Example 1*:
|
| 860 |
|
| 861 |
``` cpp
|
| 862 |
const int ci = 3; // cv-qualified (initialized as required)
|
| 863 |
+
ci = 4; // error: attempt to modify const
|
| 864 |
|
| 865 |
int i = 2; // not cv-qualified
|
| 866 |
const int* cip; // pointer to const int
|
| 867 |
cip = &i; // OK: cv-qualified access path to unqualified
|
| 868 |
+
*cip = 4; // error: attempt to modify through ptr to const
|
| 869 |
|
| 870 |
int* ip;
|
| 871 |
ip = const_cast<int*>(cip); // cast needed to convert const int* to int*
|
| 872 |
*ip = 4; // defined: *ip points to i, a non-const object
|
| 873 |
|
| 874 |
const int* ciq = new const int (3); // initialized as required
|
| 875 |
int* iq = const_cast<int*>(ciq); // cast required
|
| 876 |
+
*iq = 4; // undefined behavior: modifies a const object
|
| 877 |
```
|
| 878 |
|
| 879 |
For another example,
|
| 880 |
|
| 881 |
``` cpp
|
|
|
|
| 888 |
Y();
|
| 889 |
};
|
| 890 |
|
| 891 |
const Y y;
|
| 892 |
y.x.i++; // well-formed: mutable member can be modified
|
| 893 |
+
y.x.j++; // error: const-qualified member modified
|
| 894 |
Y* p = const_cast<Y*>(&y); // cast away const-ness of y
|
| 895 |
p->x.i = 99; // well-formed: mutable member can be modified
|
| 896 |
+
p->x.j = 99; // undefined behavior: modifies a const subobject
|
| 897 |
```
|
| 898 |
|
| 899 |
— *end example*]
|
| 900 |
|
| 901 |
The semantics of an access through a volatile glvalue are
|
|
|
|
| 917 |
The simple type specifiers are
|
| 918 |
|
| 919 |
``` bnf
|
| 920 |
simple-type-specifier:
|
| 921 |
nested-name-specifierₒₚₜ type-name
|
| 922 |
+
nested-name-specifier template simple-template-id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 923 |
decltype-specifier
|
| 924 |
+
placeholder-type-specifier
|
| 925 |
+
nested-name-specifierₒₚₜ template-name
|
| 926 |
+
char
|
| 927 |
+
char8_t
|
| 928 |
+
char16_t
|
| 929 |
+
char32_t
|
| 930 |
+
wchar_t
|
| 931 |
+
bool
|
| 932 |
+
short
|
| 933 |
+
int
|
| 934 |
+
long
|
| 935 |
+
signed
|
| 936 |
+
unsigned
|
| 937 |
+
float
|
| 938 |
+
double
|
| 939 |
+
void
|
| 940 |
```
|
| 941 |
|
| 942 |
``` bnf
|
| 943 |
type-name:
|
| 944 |
class-name
|
| 945 |
enum-name
|
| 946 |
typedef-name
|
|
|
|
| 947 |
```
|
| 948 |
|
| 949 |
+
A *placeholder-type-specifier* is a placeholder for a type to be deduced
|
| 950 |
+
[[dcl.spec.auto]]. A *type-specifier* of the form `typename`ₒₚₜ
|
| 951 |
+
*nested-name-specifier*ₒₚₜ *template-name* is a placeholder for a
|
| 952 |
+
deduced class type [[dcl.type.class.deduct]]. The
|
| 953 |
+
*nested-name-specifier*, if any, shall be non-dependent and the
|
| 954 |
+
*template-name* shall name a deducible template. A *deducible template*
|
| 955 |
+
is either a class template or is an alias template whose
|
| 956 |
+
*defining-type-id* is of the form
|
| 957 |
+
|
| 958 |
``` bnf
|
| 959 |
+
typenameₒₚₜ nested-name-specifierₒₚₜ templateₒₚₜ simple-template-id
|
|
|
|
|
|
|
| 960 |
```
|
| 961 |
|
| 962 |
+
where the *nested-name-specifier* (if any) is non-dependent and the
|
| 963 |
+
*template-name* of the *simple-template-id* names a deducible template.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 964 |
|
| 965 |
+
[*Note 1*: An injected-class-name is never interpreted as a
|
| 966 |
+
*template-name* in contexts where class template argument deduction
|
| 967 |
+
would be performed [[temp.local]]. — *end note*]
|
| 968 |
+
|
| 969 |
+
The other *simple-type-specifier*s specify either a previously-declared
|
| 970 |
+
type, a type determined from an expression, or one of the fundamental
|
| 971 |
+
types [[basic.fundamental]]. [[dcl.type.simple]] summarizes the valid
|
| 972 |
+
combinations of *simple-type-specifier*s and the types they specify.
|
| 973 |
+
|
| 974 |
+
**Table: *simple-type-specifier*{s} and the types they specify** <a id="dcl.type.simple">[dcl.type.simple]</a>
|
| 975 |
|
| 976 |
| Specifier(s) | Type |
|
| 977 |
+
| ---------------------------- | ------------------------------------------------- |
|
| 978 |
| *type-name* | the type named |
|
| 979 |
| *simple-template-id* | the type as defined in~ [[temp.names]] |
|
| 980 |
+
| *decltype-specifier* | the type as defined in~ [[dcl.type.decltype]] |
|
| 981 |
+
| *placeholder-type-specifier* | the type as defined in~ [[dcl.spec.auto]] |
|
| 982 |
+
| *template-name* | the type as defined in~ [[dcl.type.class.deduct]] |
|
| 983 |
+
| `char` | ```char`'' |
|
| 984 |
+
| `unsigned char` | ```unsigned char`'' |
|
| 985 |
+
| `signed char` | ```signed char`'' |
|
| 986 |
+
| `char8_t` | ```char8_t`'' |
|
| 987 |
+
| `char16_t` | ```char16_t`'' |
|
| 988 |
+
| `char32_t` | ```char32_t`'' |
|
| 989 |
+
| `bool` | ```bool`'' |
|
| 990 |
+
| `unsigned` | ```unsigned int`'' |
|
| 991 |
+
| `unsigned int` | ```unsigned int`'' |
|
| 992 |
+
| `signed` | ```int`'' |
|
| 993 |
+
| `signed int` | ```int`'' |
|
| 994 |
+
| `int` | ```int`'' |
|
| 995 |
+
| `unsigned short int` | ```unsigned short int`'' |
|
| 996 |
+
| `unsigned short` | ```unsigned short int`'' |
|
| 997 |
+
| `unsigned long int` | ```unsigned long int`'' |
|
| 998 |
+
| `unsigned long` | ```unsigned long int`'' |
|
| 999 |
+
| `unsigned long long int` | ```unsigned long long int`'' |
|
| 1000 |
+
| `unsigned long long` | ```unsigned long long int`'' |
|
| 1001 |
+
| `signed long int` | ```long int`'' |
|
| 1002 |
+
| `signed long` | ```long int`'' |
|
| 1003 |
+
| `signed long long int` | ```long long int`'' |
|
| 1004 |
+
| `signed long long` | ```long long int`'' |
|
| 1005 |
+
| `long long int` | ```long long int`'' |
|
| 1006 |
+
| `long long` | ```long long int`'' |
|
| 1007 |
+
| `long int` | ```long int`'' |
|
| 1008 |
+
| `long` | ```long int`'' |
|
| 1009 |
+
| `signed short int` | ```short int`'' |
|
| 1010 |
+
| `signed short` | ```short int`'' |
|
| 1011 |
+
| `short int` | ```short int`'' |
|
| 1012 |
+
| `short` | ```short int`'' |
|
| 1013 |
+
| `wchar_t` | ```wchar_t`'' |
|
| 1014 |
+
| `float` | ```float`'' |
|
| 1015 |
+
| `double` | ```double`'' |
|
| 1016 |
+
| `long double` | ```long double`'' |
|
| 1017 |
+
| `void` | ```void`'' |
|
| 1018 |
|
| 1019 |
|
| 1020 |
When multiple *simple-type-specifier*s are allowed, they can be freely
|
| 1021 |
intermixed with other *decl-specifier*s in any order.
|
| 1022 |
|
| 1023 |
+
[*Note 2*: It is *implementation-defined* whether objects of `char`
|
| 1024 |
type are represented as signed or unsigned quantities. The `signed`
|
| 1025 |
specifier forces `char` objects to be signed; it is redundant in other
|
| 1026 |
contexts. — *end note*]
|
| 1027 |
|
| 1028 |
+
#### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
|
| 1029 |
+
|
| 1030 |
+
``` bnf
|
| 1031 |
+
elaborated-type-specifier:
|
| 1032 |
+
class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
|
| 1033 |
+
class-key simple-template-id
|
| 1034 |
+
class-key nested-name-specifier templateₒₚₜ simple-template-id
|
| 1035 |
+
elaborated-enum-specifier
|
| 1036 |
+
```
|
| 1037 |
+
|
| 1038 |
+
``` bnf
|
| 1039 |
+
elaborated-enum-specifier:
|
| 1040 |
+
enum nested-name-specifierₒₚₜ identifier
|
| 1041 |
+
```
|
| 1042 |
+
|
| 1043 |
+
An *attribute-specifier-seq* shall not appear in an
|
| 1044 |
+
*elaborated-type-specifier* unless the latter is the sole constituent of
|
| 1045 |
+
a declaration. If an *elaborated-type-specifier* is the sole constituent
|
| 1046 |
+
of a declaration, the declaration is ill-formed unless it is an explicit
|
| 1047 |
+
specialization [[temp.expl.spec]], an explicit instantiation
|
| 1048 |
+
[[temp.explicit]] or it has one of the following forms:
|
| 1049 |
+
|
| 1050 |
+
``` bnf
|
| 1051 |
+
class-key attribute-specifier-seqₒₚₜ identifier ';'
|
| 1052 |
+
friend class-key '::ₒₚₜ ' identifier ';'
|
| 1053 |
+
friend class-key '::ₒₚₜ ' simple-template-id ';'
|
| 1054 |
+
friend class-key nested-name-specifier identifier ';'
|
| 1055 |
+
friend class-key nested-name-specifier templateₒₚₜ simple-template-id ';'
|
| 1056 |
+
```
|
| 1057 |
+
|
| 1058 |
+
In the first case, the *attribute-specifier-seq*, if any, appertains to
|
| 1059 |
+
the class being declared; the attributes in the
|
| 1060 |
+
*attribute-specifier-seq* are thereafter considered attributes of the
|
| 1061 |
+
class whenever it is named.
|
| 1062 |
+
|
| 1063 |
+
[*Note 1*: [[basic.lookup.elab]] describes how name lookup proceeds
|
| 1064 |
+
for the *identifier* in an *elaborated-type-specifier*. — *end note*]
|
| 1065 |
+
|
| 1066 |
+
If the *identifier* or *simple-template-id* resolves to a *class-name*
|
| 1067 |
+
or *enum-name*, the *elaborated-type-specifier* introduces it into the
|
| 1068 |
+
declaration the same way a *simple-type-specifier* introduces its
|
| 1069 |
+
*type-name* [[dcl.type.simple]]. If the *identifier* or
|
| 1070 |
+
*simple-template-id* resolves to a *typedef-name* ([[dcl.typedef]],
|
| 1071 |
+
[[temp.names]]), the *elaborated-type-specifier* is ill-formed.
|
| 1072 |
+
|
| 1073 |
+
[*Note 2*:
|
| 1074 |
+
|
| 1075 |
+
This implies that, within a class template with a template
|
| 1076 |
+
*type-parameter* `T`, the declaration
|
| 1077 |
+
|
| 1078 |
+
``` cpp
|
| 1079 |
+
friend class T;
|
| 1080 |
+
```
|
| 1081 |
+
|
| 1082 |
+
is ill-formed. However, the similar declaration `friend T;` is allowed
|
| 1083 |
+
[[class.friend]].
|
| 1084 |
+
|
| 1085 |
+
— *end note*]
|
| 1086 |
+
|
| 1087 |
+
The *class-key* or `enum` keyword present in the
|
| 1088 |
+
*elaborated-type-specifier* shall agree in kind with the declaration to
|
| 1089 |
+
which the name in the *elaborated-type-specifier* refers. This rule also
|
| 1090 |
+
applies to the form of *elaborated-type-specifier* that declares a
|
| 1091 |
+
*class-name* or friend class since it can be construed as referring to
|
| 1092 |
+
the definition of the class. Thus, in any *elaborated-type-specifier*,
|
| 1093 |
+
the `enum` keyword shall be used to refer to an enumeration
|
| 1094 |
+
[[dcl.enum]], the `union` *class-key* shall be used to refer to a union
|
| 1095 |
+
[[class.union]], and either the `class` or `struct` *class-key* shall be
|
| 1096 |
+
used to refer to a non-union class [[class.pre]].
|
| 1097 |
+
|
| 1098 |
+
[*Example 1*:
|
| 1099 |
+
|
| 1100 |
+
``` cpp
|
| 1101 |
+
enum class E { a, b };
|
| 1102 |
+
enum E x = E::a; // OK
|
| 1103 |
+
struct S { } s;
|
| 1104 |
+
class S* p = &s; // OK
|
| 1105 |
+
```
|
| 1106 |
+
|
| 1107 |
+
— *end example*]
|
| 1108 |
+
|
| 1109 |
+
#### Decltype specifiers <a id="dcl.type.decltype">[[dcl.type.decltype]]</a>
|
| 1110 |
+
|
| 1111 |
+
``` bnf
|
| 1112 |
+
decltype-specifier:
|
| 1113 |
+
decltype '(' expression ')'
|
| 1114 |
+
```
|
| 1115 |
+
|
| 1116 |
+
For an expression E, the type denoted by `decltype(E)` is defined as
|
| 1117 |
follows:
|
| 1118 |
|
| 1119 |
+
- if E is an unparenthesized *id-expression* naming a structured binding
|
| 1120 |
+
[[dcl.struct.bind]], `decltype(E)` is the referenced type as given in
|
| 1121 |
+
the specification of the structured binding declaration;
|
| 1122 |
+
- otherwise, if E is an unparenthesized *id-expression* naming a
|
| 1123 |
+
non-type *template-parameter* [[temp.param]], `decltype(E)` is the
|
| 1124 |
+
type of the *template-parameter* after performing any necessary type
|
| 1125 |
+
deduction ([[dcl.spec.auto]], [[dcl.type.class.deduct]]);
|
| 1126 |
+
- otherwise, if E is an unparenthesized *id-expression* or an
|
| 1127 |
+
unparenthesized class member access [[expr.ref]], `decltype(E)` is the
|
| 1128 |
+
type of the entity named by E. If there is no such entity, or if E
|
| 1129 |
+
names a set of overloaded functions, the program is ill-formed;
|
| 1130 |
+
- otherwise, if E is an xvalue, `decltype(E)` is `T&&`, where `T` is the
|
| 1131 |
+
type of E;
|
| 1132 |
+
- otherwise, if E is an lvalue, `decltype(E)` is `T&`, where `T` is the
|
| 1133 |
+
type of E;
|
| 1134 |
+
- otherwise, `decltype(E)` is the type of E.
|
| 1135 |
|
| 1136 |
The operand of the `decltype` specifier is an unevaluated operand
|
| 1137 |
+
[[expr.prop]].
|
| 1138 |
|
| 1139 |
[*Example 1*:
|
| 1140 |
|
| 1141 |
``` cpp
|
| 1142 |
const int&& foo();
|
|
|
|
| 1149 |
decltype((a->x)) x4 = x3; // type is const double&
|
| 1150 |
```
|
| 1151 |
|
| 1152 |
— *end example*]
|
| 1153 |
|
| 1154 |
+
[*Note 1*: The rules for determining types involving `decltype(auto)`
|
| 1155 |
are specified in [[dcl.spec.auto]]. — *end note*]
|
| 1156 |
|
| 1157 |
+
If the operand of a *decltype-specifier* is a prvalue and is not a
|
| 1158 |
+
(possibly parenthesized) immediate invocation [[expr.const]], the
|
| 1159 |
+
temporary materialization conversion is not applied [[conv.rval]] and no
|
| 1160 |
+
result object is provided for the prvalue. The type of the prvalue may
|
| 1161 |
+
be incomplete or an abstract class type.
|
| 1162 |
|
| 1163 |
+
[*Note 2*: As a result, storage is not allocated for the prvalue and it
|
| 1164 |
is not destroyed. Thus, a class type is not instantiated as a result of
|
| 1165 |
being the type of a function call in this context. In this context, the
|
| 1166 |
common purpose of writing the expression is merely to refer to its type.
|
| 1167 |
In that sense, a *decltype-specifier* is analogous to a use of a
|
| 1168 |
*typedef-name*, so the usual reasons for requiring a complete type do
|
| 1169 |
not apply. In particular, it is not necessary to allocate storage for a
|
| 1170 |
temporary object or to enforce the semantic constraints associated with
|
| 1171 |
invoking the type’s destructor. — *end note*]
|
| 1172 |
|
| 1173 |
+
[*Note 3*: Unlike the preceding rule, parentheses have no special
|
| 1174 |
meaning in this context. — *end note*]
|
| 1175 |
|
| 1176 |
[*Example 2*:
|
| 1177 |
|
| 1178 |
``` cpp
|
|
|
|
| 1187 |
// (A temporary is not introduced as a result of the use of i().)
|
| 1188 |
template<class T> auto f(T) // #2
|
| 1189 |
-> void;
|
| 1190 |
auto g() -> void {
|
| 1191 |
f(42); // OK: calls #2. (#1 is not a viable candidate: type deduction
|
| 1192 |
+
// fails[temp.deduct] because A<int>::~A() is implicitly used in its
|
| 1193 |
// decltype-specifier)
|
| 1194 |
}
|
| 1195 |
template<class T> auto q(T)
|
| 1196 |
-> decltype((h<T>())); // does not force completion of A<T>; A<T>::~A() is not implicitly
|
| 1197 |
// used within the context of this decltype-specifier
|
| 1198 |
void r() {
|
| 1199 |
+
q(42); // error: deduction against q succeeds, so overload resolution selects
|
| 1200 |
+
// the specialization ``q(T) -> decltype((h<T>()))'' with T=int;
|
| 1201 |
+
// the return type is A<int>, so a temporary is introduced and its
|
| 1202 |
+
// destructor is used, so the program is ill-formed
|
| 1203 |
}
|
| 1204 |
```
|
| 1205 |
|
| 1206 |
— *end example*]
|
| 1207 |
|
| 1208 |
+
#### Placeholder type specifiers <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
|
| 1209 |
|
| 1210 |
``` bnf
|
| 1211 |
+
placeholder-type-specifier:
|
| 1212 |
+
type-constraintₒₚₜ auto
|
| 1213 |
+
type-constraintₒₚₜ decltype '(' auto ')'
|
|
|
|
|
|
|
| 1214 |
```
|
| 1215 |
|
| 1216 |
+
A *placeholder-type-specifier* designates a placeholder type that will
|
| 1217 |
+
be replaced later by deduction from an initializer.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1218 |
|
| 1219 |
+
A *placeholder-type-specifier* of the form *type-constraint*ₒₚₜ `auto`
|
| 1220 |
+
can be used as a *decl-specifier* of the *decl-specifier-seq* of a
|
| 1221 |
+
*parameter-declaration* of a function declaration or *lambda-expression*
|
| 1222 |
+
and, if it is not the `auto` *type-specifier* introducing a
|
| 1223 |
+
*trailing-return-type* (see below), is a *generic parameter type
|
| 1224 |
+
placeholder* of the function declaration or *lambda-expression*.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1225 |
|
| 1226 |
+
[*Note 1*: Having a generic parameter type placeholder signifies that
|
| 1227 |
+
the function is an abbreviated function template [[dcl.fct]] or the
|
| 1228 |
+
lambda is a generic lambda [[expr.prim.lambda]]. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1229 |
|
| 1230 |
The placeholder type can appear with a function declarator in the
|
| 1231 |
*decl-specifier-seq*, *type-specifier-seq*, *conversion-function-id*, or
|
| 1232 |
*trailing-return-type*, in any context where such a declarator is valid.
|
| 1233 |
+
If the function declarator includes a *trailing-return-type*
|
| 1234 |
+
[[dcl.fct]], that *trailing-return-type* specifies the declared return
|
| 1235 |
type of the function. Otherwise, the function declarator shall declare a
|
| 1236 |
function. If the declared return type of the function contains a
|
| 1237 |
placeholder type, the return type of the function is deduced from
|
| 1238 |
+
non-discarded `return` statements, if any, in the body of the function
|
| 1239 |
+
[[stmt.if]].
|
| 1240 |
+
|
| 1241 |
+
The type of a variable declared using a placeholder type is deduced from
|
| 1242 |
+
its initializer. This use is allowed in an initializing declaration
|
| 1243 |
+
[[dcl.init]] of a variable. The placeholder type shall appear as one of
|
| 1244 |
+
the *decl-specifier*s in the *decl-specifier-seq* and the
|
| 1245 |
+
*decl-specifier-seq* shall be followed by one or more *declarator*s,
|
| 1246 |
+
each of which shall be followed by a non-empty *initializer*. In an
|
| 1247 |
+
*initializer* of the form
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1248 |
|
| 1249 |
``` cpp
|
| 1250 |
( expression-list )
|
| 1251 |
```
|
| 1252 |
|
| 1253 |
the *expression-list* shall be a single *assignment-expression*.
|
| 1254 |
|
| 1255 |
+
[*Example 1*:
|
| 1256 |
|
| 1257 |
``` cpp
|
| 1258 |
auto x = 5; // OK: x has type int
|
| 1259 |
const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
|
| 1260 |
static auto y = 0.0; // OK: y has type double
|
|
|
|
| 1264 |
auto h(); // OK: h's return type will be deduced when it is defined
|
| 1265 |
```
|
| 1266 |
|
| 1267 |
— *end example*]
|
| 1268 |
|
| 1269 |
+
The `auto` *type-specifier* can also be used to introduce a structured
|
| 1270 |
+
binding declaration [[dcl.struct.bind]].
|
| 1271 |
+
|
| 1272 |
A placeholder type can also be used in the *type-specifier-seq* in the
|
| 1273 |
+
*new-type-id* or *type-id* of a *new-expression* [[expr.new]] and as a
|
| 1274 |
+
*decl-specifier* of the *parameter-declaration*'s *decl-specifier-seq*
|
| 1275 |
+
in a *template-parameter* [[temp.param]].
|
| 1276 |
|
| 1277 |
+
A program that uses a placeholder type in a context not explicitly
|
| 1278 |
+
allowed in this subclause is ill-formed.
|
| 1279 |
|
| 1280 |
If the *init-declarator-list* contains more than one *init-declarator*,
|
| 1281 |
they shall all form declarations of variables. The type of each declared
|
| 1282 |
+
variable is determined by placeholder type deduction
|
| 1283 |
+
[[dcl.type.auto.deduct]], and if the type that replaces the placeholder
|
| 1284 |
type is not the same in each deduction, the program is ill-formed.
|
| 1285 |
|
| 1286 |
+
[*Example 2*:
|
| 1287 |
|
| 1288 |
``` cpp
|
| 1289 |
auto x = 5, *y = &x; // OK: auto is int
|
| 1290 |
auto a = 5, b = { 1, 2 }; // error: different types for auto
|
| 1291 |
```
|
|
|
|
| 1300 |
If a function with a declared return type that uses a placeholder type
|
| 1301 |
has no non-discarded `return` statements, the return type is deduced as
|
| 1302 |
though from a `return` statement with no operand at the closing brace of
|
| 1303 |
the function body.
|
| 1304 |
|
| 1305 |
+
[*Example 3*:
|
| 1306 |
|
| 1307 |
``` cpp
|
| 1308 |
auto f() { } // OK, return type is void
|
| 1309 |
+
auto* g() { } // error: cannot deduce auto* from void()
|
| 1310 |
```
|
| 1311 |
|
| 1312 |
— *end example*]
|
| 1313 |
|
| 1314 |
+
An exported function with a declared return type that uses a placeholder
|
| 1315 |
+
type shall be defined in the translation unit containing its exported
|
| 1316 |
+
declaration, outside the *private-module-fragment* (if any).
|
|
|
|
|
|
|
| 1317 |
|
| 1318 |
+
[*Note 2*: The deduced return type cannot have a name with internal
|
| 1319 |
+
linkage [[basic.link]]. — *end note*]
|
| 1320 |
+
|
| 1321 |
+
If the name of an entity with an undeduced placeholder type appears in
|
| 1322 |
+
an expression, the program is ill-formed. Once a non-discarded `return`
|
| 1323 |
+
statement has been seen in a function, however, the return type deduced
|
| 1324 |
+
from that statement can be used in the rest of the function, including
|
| 1325 |
+
in other `return` statements.
|
| 1326 |
+
|
| 1327 |
+
[*Example 4*:
|
| 1328 |
|
| 1329 |
``` cpp
|
| 1330 |
+
auto n = n; // error: n's initializer refers to n
|
| 1331 |
auto f();
|
| 1332 |
+
void g() { &f; } // error: f's return type is unknown
|
| 1333 |
auto sum(int i) {
|
| 1334 |
if (i == 1)
|
| 1335 |
return i; // sum's return type is int
|
| 1336 |
else
|
| 1337 |
return sum(i-1)+i; // OK, sum's return type has been deduced
|
| 1338 |
}
|
| 1339 |
```
|
| 1340 |
|
| 1341 |
— *end example*]
|
| 1342 |
|
| 1343 |
+
Return type deduction for a templated entity that is a function or
|
| 1344 |
+
function template with a placeholder in its declared type occurs when
|
| 1345 |
+
the definition is instantiated even if the function body contains a
|
| 1346 |
+
`return` statement with a non-type-dependent operand.
|
| 1347 |
|
| 1348 |
+
[*Note 3*: Therefore, any use of a specialization of the function
|
| 1349 |
template will cause an implicit instantiation. Any errors that arise
|
| 1350 |
from this instantiation are not in the immediate context of the function
|
| 1351 |
+
type and can result in the program being ill-formed
|
| 1352 |
+
[[temp.deduct]]. — *end note*]
|
| 1353 |
|
| 1354 |
+
[*Example 5*:
|
| 1355 |
|
| 1356 |
``` cpp
|
| 1357 |
template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
|
| 1358 |
typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type
|
| 1359 |
template<class T> auto f(T* t) { return *t; }
|
|
|
|
| 1363 |
|
| 1364 |
— *end example*]
|
| 1365 |
|
| 1366 |
Redeclarations or specializations of a function or function template
|
| 1367 |
with a declared return type that uses a placeholder type shall also use
|
| 1368 |
+
that placeholder, not a deduced type. Similarly, redeclarations or
|
| 1369 |
+
specializations of a function or function template with a declared
|
| 1370 |
+
return type that does not use a placeholder type shall not use a
|
| 1371 |
+
placeholder.
|
| 1372 |
|
| 1373 |
+
[*Example 6*:
|
| 1374 |
|
| 1375 |
``` cpp
|
| 1376 |
auto f();
|
| 1377 |
auto f() { return 42; } // return type is int
|
| 1378 |
auto f(); // OK
|
| 1379 |
+
int f(); // error: cannot be overloaded with auto f()
|
| 1380 |
+
decltype(auto) f(); // error: auto and decltype(auto) don't match
|
| 1381 |
|
| 1382 |
template <typename T> auto g(T t) { return t; } // #1
|
| 1383 |
template auto g(int); // OK, return type is int
|
| 1384 |
+
template char g(char); // error: no matching template
|
| 1385 |
template<> auto g(double); // OK, forward declaration with unknown return type
|
| 1386 |
|
| 1387 |
template <class T> T g(T t) { return t; } // OK, not functionally equivalent to #1
|
| 1388 |
template char g(char); // OK, now there is a matching template
|
| 1389 |
template auto g(float); // still matches #1
|
| 1390 |
|
| 1391 |
+
void h() { return g(42); } // error: ambiguous
|
| 1392 |
|
| 1393 |
template <typename T> struct A {
|
| 1394 |
friend T frf(T);
|
| 1395 |
};
|
| 1396 |
auto frf(int i) { return i; } // not a friend of A<int>
|
| 1397 |
+
extern int v;
|
| 1398 |
+
auto v = 17; // OK, redeclares v
|
| 1399 |
+
struct S {
|
| 1400 |
+
static int i;
|
| 1401 |
+
};
|
| 1402 |
+
auto S::i = 23; // OK
|
| 1403 |
```
|
| 1404 |
|
| 1405 |
— *end example*]
|
| 1406 |
|
| 1407 |
A function declared with a return type that uses a placeholder type
|
| 1408 |
+
shall not be `virtual` [[class.virtual]].
|
| 1409 |
|
| 1410 |
+
A function declared with a return type that uses a placeholder type
|
| 1411 |
+
shall not be a coroutine [[dcl.fct.def.coroutine]].
|
|
|
|
|
|
|
| 1412 |
|
| 1413 |
+
An explicit instantiation declaration [[temp.explicit]] does not cause
|
| 1414 |
+
the instantiation of an entity declared using a placeholder type, but it
|
| 1415 |
+
also does not prevent that entity from being instantiated as needed to
|
| 1416 |
+
determine its type.
|
| 1417 |
+
|
| 1418 |
+
[*Example 7*:
|
| 1419 |
|
| 1420 |
``` cpp
|
| 1421 |
template <typename T> auto f(T t) { return t; }
|
| 1422 |
extern template auto f(int); // does not instantiate f<int>
|
| 1423 |
int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit
|
|
|
|
| 1430 |
|
| 1431 |
*Placeholder type deduction* is the process by which a type containing a
|
| 1432 |
placeholder type is replaced by a deduced type.
|
| 1433 |
|
| 1434 |
A type `T` containing a placeholder type, and a corresponding
|
| 1435 |
+
initializer E, are determined as follows:
|
| 1436 |
|
| 1437 |
- for a non-discarded `return` statement that occurs in a function
|
| 1438 |
declared with a return type that contains a placeholder type, `T` is
|
| 1439 |
+
the declared return type and E is the operand of the `return`
|
| 1440 |
+
statement. If the `return` statement has no operand, then E is
|
| 1441 |
`void()`;
|
| 1442 |
- for a variable declared with a type that contains a placeholder type,
|
| 1443 |
+
`T` is the declared type of the variable and E is the initializer. If
|
| 1444 |
+
the initialization is direct-list-initialization, the initializer
|
| 1445 |
shall be a *braced-init-list* containing only a single
|
| 1446 |
+
*assignment-expression* and E is the *assignment-expression*;
|
| 1447 |
- for a non-type template parameter declared with a type that contains a
|
| 1448 |
placeholder type, `T` is the declared type of the non-type template
|
| 1449 |
+
parameter and E is the corresponding template argument.
|
| 1450 |
|
| 1451 |
In the case of a `return` statement with no operand or with an operand
|
| 1452 |
+
of type `void`, `T` shall be either *type-constraint*ₒₚₜ
|
| 1453 |
+
`decltype(auto)` or cv *type-constraint*ₒₚₜ `auto`.
|
| 1454 |
|
| 1455 |
+
If the deduction is for a `return` statement and E is a
|
| 1456 |
+
*braced-init-list* [[dcl.init.list]], the program is ill-formed.
|
| 1457 |
|
| 1458 |
+
If the *placeholder-type-specifier* is of the form *type-constraint*ₒₚₜ
|
| 1459 |
+
`auto`, the deduced type T' replacing `T` is determined using the rules
|
| 1460 |
+
for template argument deduction. Obtain `P` from `T` by replacing the
|
| 1461 |
+
occurrences of *type-constraint*ₒₚₜ `auto` either with a new invented
|
| 1462 |
+
type template parameter `U` or, if the initialization is
|
| 1463 |
+
copy-list-initialization, with `std::initializer_list<U>`. Deduce a
|
| 1464 |
+
value for `U` using the rules of template argument deduction from a
|
| 1465 |
+
function call [[temp.deduct.call]], where `P` is a function template
|
| 1466 |
+
parameter type and the corresponding argument is E. If the deduction
|
| 1467 |
+
fails, the declaration is ill-formed. Otherwise, T' is obtained by
|
| 1468 |
+
substituting the deduced `U` into `P`.
|
| 1469 |
|
| 1470 |
+
[*Example 8*:
|
| 1471 |
|
| 1472 |
``` cpp
|
| 1473 |
auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
|
| 1474 |
auto x2 = { 1, 2.0 }; // error: cannot deduce element type
|
| 1475 |
auto x3{ 1, 2 }; // error: not a single element
|
|
|
|
| 1477 |
auto x5{ 3 }; // decltype(x5) is int
|
| 1478 |
```
|
| 1479 |
|
| 1480 |
— *end example*]
|
| 1481 |
|
| 1482 |
+
[*Example 9*:
|
| 1483 |
|
| 1484 |
``` cpp
|
| 1485 |
const auto &i = expr;
|
| 1486 |
```
|
| 1487 |
|
|
|
|
| 1492 |
template <class U> void f(const U& u);
|
| 1493 |
```
|
| 1494 |
|
| 1495 |
— *end example*]
|
| 1496 |
|
| 1497 |
+
If the *placeholder-type-specifier* is of the form *type-constraint*ₒₚₜ
|
| 1498 |
+
`decltype(auto)`, `T` shall be the placeholder alone. The type deduced
|
| 1499 |
+
for `T` is determined as described in [[dcl.type.simple]], as though E
|
| 1500 |
+
had been the operand of the `decltype`.
|
| 1501 |
|
| 1502 |
+
[*Example 10*:
|
| 1503 |
|
| 1504 |
``` cpp
|
| 1505 |
int i;
|
| 1506 |
int&& f();
|
| 1507 |
auto x2a(i); // decltype(x2a) is int
|
|
|
|
| 1511 |
auto x4a = (i); // decltype(x4a) is int
|
| 1512 |
decltype(auto) x4d = (i); // decltype(x4d) is int&
|
| 1513 |
auto x5a = f(); // decltype(x5a) is int
|
| 1514 |
decltype(auto) x5d = f(); // decltype(x5d) is int&&
|
| 1515 |
auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
|
| 1516 |
+
decltype(auto) x6d = { 1, 2 }; // error: { 1, 2 } is not an expression
|
| 1517 |
auto *x7a = &i; // decltype(x7a) is int*
|
| 1518 |
+
decltype(auto)*x7d = &i; // error: declared type is not plain decltype(auto)
|
| 1519 |
```
|
| 1520 |
|
| 1521 |
— *end example*]
|
| 1522 |
|
| 1523 |
+
For a *placeholder-type-specifier* with a *type-constraint*, the
|
| 1524 |
+
immediately-declared constraint [[temp.param]] of the *type-constraint*
|
| 1525 |
+
for the type deduced for the placeholder shall be satisfied.
|
| 1526 |
+
|
| 1527 |
#### Deduced class template specialization types <a id="dcl.type.class.deduct">[[dcl.type.class.deduct]]</a>
|
| 1528 |
|
| 1529 |
If a placeholder for a deduced class type appears as a *decl-specifier*
|
| 1530 |
+
in the *decl-specifier-seq* of an initializing declaration [[dcl.init]]
|
| 1531 |
+
of a variable, the declared type of the variable shall be cv `T`, where
|
| 1532 |
+
`T` is the placeholder.
|
| 1533 |
+
|
| 1534 |
+
[*Example 1*:
|
| 1535 |
+
|
| 1536 |
+
``` cpp
|
| 1537 |
+
template <class ...T> struct A {
|
| 1538 |
+
A(T...) {}
|
| 1539 |
+
};
|
| 1540 |
+
A x[29]{}; // error: no declarator operators allowed
|
| 1541 |
+
const A& y{}; // error: no declarator operators allowed
|
| 1542 |
+
```
|
| 1543 |
+
|
| 1544 |
+
— *end example*]
|
| 1545 |
+
|
| 1546 |
+
The placeholder is replaced by the return type of the function selected
|
| 1547 |
+
by overload resolution for class template deduction
|
| 1548 |
+
[[over.match.class.deduct]]. If the *decl-specifier-seq* is followed by
|
| 1549 |
+
an *init-declarator-list* or *member-declarator-list* containing more
|
| 1550 |
+
than one *declarator*, the type that replaces the placeholder shall be
|
| 1551 |
+
the same in each deduction.
|
| 1552 |
|
| 1553 |
A placeholder for a deduced class type can also be used in the
|
| 1554 |
*type-specifier-seq* in the *new-type-id* or *type-id* of a
|
| 1555 |
+
*new-expression* [[expr.new]], as the *simple-type-specifier* in an
|
| 1556 |
+
explicit type conversion (functional notation) [[expr.type.conv]], or as
|
| 1557 |
+
the *type-specifier* in the *parameter-declaration* of a
|
| 1558 |
+
*template-parameter* [[temp.param]]. A placeholder for a deduced class
|
| 1559 |
+
type shall not appear in any other context.
|
| 1560 |
|
| 1561 |
+
[*Example 2*:
|
| 1562 |
|
| 1563 |
``` cpp
|
| 1564 |
template<class T> struct container {
|
| 1565 |
container(T t) {}
|
| 1566 |
template<class Iter> container(Iter beg, Iter end);
|
|
|
|
| 1569 |
container(Iter b, Iter e) -> container<typename std::iterator_traits<Iter>::value_type>;
|
| 1570 |
std::vector<double> v = { ... };
|
| 1571 |
|
| 1572 |
container c(7); // OK, deduces int for T
|
| 1573 |
auto d = container(v.begin(), v.end()); // OK, deduces double for T
|
| 1574 |
+
container e{5, 6}; // error: int is not an iterator
|
| 1575 |
```
|
| 1576 |
|
| 1577 |
— *end example*]
|
| 1578 |
|