- tmp/tmpjsy99id6/{from.md → to.md} +292 -182
tmp/tmpjsy99id6/{from.md → to.md}
RENAMED
|
@@ -98,12 +98,12 @@ taken. This use is deprecated (see [[depr.register]]).
|
|
| 98 |
|
| 99 |
The `thread_local` specifier indicates that the named entity has thread
|
| 100 |
storage duration ([[basic.stc.thread]]). It shall be applied only to
|
| 101 |
the names of variables of namespace or block scope and to the names of
|
| 102 |
static data members. When `thread_local` is applied to a variable of
|
| 103 |
-
block scope the *storage-class-specifier* `static` is implied if
|
| 104 |
-
|
| 105 |
|
| 106 |
The `static` specifier can be applied only to names of variables and
|
| 107 |
functions and to anonymous unions ([[class.union]]). There can be no
|
| 108 |
`static` function declarations within a block, nor any `static` function
|
| 109 |
parameters. A `static` specifier used in the declaration of a variable
|
|
@@ -119,16 +119,10 @@ The `extern` specifier can be applied only to the names of variables and
|
|
| 119 |
functions. The `extern` specifier cannot be used in the declaration of
|
| 120 |
class members or function parameters. For the linkage of a name declared
|
| 121 |
with an `extern` specifier, see [[basic.link]]. The `extern` keyword
|
| 122 |
can also be used in s and s, but it is not a in such contexts.
|
| 123 |
|
| 124 |
-
A name declared in a namespace scope without a *storage-class-specifier*
|
| 125 |
-
has external linkage unless it has internal linkage because of a
|
| 126 |
-
previous declaration and provided it is not declared `const`. Objects
|
| 127 |
-
declared `const` and not explicitly declared `extern` have internal
|
| 128 |
-
linkage.
|
| 129 |
-
|
| 130 |
The linkages implied by successive declarations for a given entity shall
|
| 131 |
agree. That is, within a given scope, each declaration declaring the
|
| 132 |
same variable name or the same overloading of a function name shall
|
| 133 |
imply the same linkage. Each function in a given set of overloaded
|
| 134 |
functions can have a different linkage, however.
|
|
@@ -392,34 +386,35 @@ The `friend` specifier is used to specify access to class members; see
|
|
| 392 |
[[class.friend]].
|
| 393 |
|
| 394 |
### The `constexpr` specifier <a id="dcl.constexpr">[[dcl.constexpr]]</a>
|
| 395 |
|
| 396 |
The `constexpr` specifier shall be applied only to the definition of a
|
| 397 |
-
variable, the declaration of a function or function
|
| 398 |
-
declaration of a static data member of a literal type (
|
| 399 |
-
[[basic.types]]). If any declaration of a function
|
| 400 |
-
has `constexpr` specifier, then all its
|
| 401 |
-
`constexpr` specifier. An explicit
|
| 402 |
-
template declaration with respect to
|
| 403 |
-
parameters cannot be declared
|
|
|
|
| 404 |
|
| 405 |
``` cpp
|
| 406 |
-
constexpr
|
| 407 |
constexpr int bufsz = 1024; // OK: definition
|
| 408 |
constexpr struct pixel { // error: pixel is a type
|
| 409 |
int x;
|
| 410 |
int y;
|
| 411 |
constexpr pixel(int); // OK: declaration
|
| 412 |
};
|
| 413 |
constexpr pixel::pixel(int a)
|
| 414 |
-
: x(
|
| 415 |
-
{ }
|
| 416 |
constexpr pixel small(2); // error: square not defined, so small(2)
|
| 417 |
// not constant~([expr.const]) so constexpr not satisfied
|
| 418 |
|
| 419 |
-
constexpr
|
| 420 |
-
|
| 421 |
}
|
| 422 |
constexpr pixel large(4); // OK: square defined
|
| 423 |
int next(constexpr int x) { // error: not for parameters
|
| 424 |
return x + 1;
|
| 425 |
}
|
|
@@ -438,122 +433,89 @@ constraints:
|
|
| 438 |
|
| 439 |
- it shall not be virtual ([[class.virtual]]);
|
| 440 |
- its return type shall be a literal type;
|
| 441 |
- each of its parameter types shall be a literal type;
|
| 442 |
- its *function-body* shall be `= delete`, `= default`, or a
|
| 443 |
-
*compound-statement* that
|
| 444 |
-
-
|
| 445 |
-
|
| 446 |
-
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
classes or enumerations,
|
| 450 |
-
|
| 451 |
-
- *using-declaration*s,
|
| 452 |
-
|
| 453 |
-
- *using-directive*s,
|
| 454 |
-
|
| 455 |
-
- and exactly one return statement;
|
| 456 |
-
- every constructor call and implicit conversion used in initializing
|
| 457 |
-
the return value ([[stmt.return]], [[dcl.init]]) shall be one of
|
| 458 |
-
those allowed in a constant expression ([[expr.const]]).
|
| 459 |
|
| 460 |
``` cpp
|
| 461 |
constexpr int square(int x)
|
| 462 |
{ return x * x; } // OK
|
| 463 |
constexpr long long_max()
|
| 464 |
{ return 2147483647; } // OK
|
| 465 |
-
constexpr int abs(int x)
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
constexpr int prev(int x)
|
| 470 |
-
{ return --x; } //
|
| 471 |
-
constexpr int g(int x, int n) { //
|
| 472 |
int r = 1;
|
| 473 |
while (--n > 0) r *= x;
|
| 474 |
return r;
|
| 475 |
}
|
| 476 |
```
|
| 477 |
|
| 478 |
-
|
| 479 |
-
types shall be a literal type. In addition, either its *function-body*
|
| 480 |
-
shall be `= delete` or `= default` or it shall satisfy the following
|
| 481 |
constraints:
|
| 482 |
|
| 483 |
- the class shall not have any virtual base classes;
|
|
|
|
| 484 |
- its *function-body* shall not be a *function-try-block*;
|
| 485 |
-
- the *compound-statement* of its *function-body* shall contain only
|
| 486 |
-
- null statements,
|
| 487 |
|
| 488 |
-
|
|
|
|
| 489 |
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
the corresponding parameter type and converting a full-expression to
|
| 506 |
-
the corresponding member type shall be one of those allowed in a
|
| 507 |
-
constant expression.
|
| 508 |
|
| 509 |
``` cpp
|
| 510 |
struct Length {
|
| 511 |
-
|
| 512 |
private:
|
| 513 |
int val;
|
| 514 |
};
|
| 515 |
```
|
| 516 |
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
argument
|
| 520 |
-
|
| 521 |
-
use of the corresponding parameter in the *function-body*, and, for
|
| 522 |
-
`constexpr` functions, implicitly converting the resulting returned
|
| 523 |
-
expression or *braced-init-list* to the return type of the function as
|
| 524 |
-
if by copy-initialization. Such substitution does not change the
|
| 525 |
-
meaning.
|
| 526 |
-
|
| 527 |
-
``` cpp
|
| 528 |
-
constexpr int f(void *) { return 0; }
|
| 529 |
-
constexpr int f(...) { return 1; }
|
| 530 |
-
constexpr int g1() { return f(0); } // calls f(void *)
|
| 531 |
-
constexpr int g2(int n) { return f(n); } // calls f(...) even for n == 0
|
| 532 |
-
constexpr int g3(int n) { return f(n*0); } // calls f(...)
|
| 533 |
-
|
| 534 |
-
namespace N {
|
| 535 |
-
constexpr int c = 5;
|
| 536 |
-
constexpr int h() { return c; }
|
| 537 |
-
}
|
| 538 |
-
constexpr int c = 0;
|
| 539 |
-
constexpr int g4() { return N::h(); } // value is 5, c is not looked up again after the substitution
|
| 540 |
-
```
|
| 541 |
-
|
| 542 |
-
For a `constexpr` function, if no function argument values exist such
|
| 543 |
-
that the function invocation substitution would produce a constant
|
| 544 |
expression ([[expr.const]]), the program is ill-formed; no diagnostic
|
| 545 |
-
required.
|
| 546 |
-
such that after function invocation substitution, every constructor call
|
| 547 |
-
and full-expression in the *mem-initializer*s would be a constant
|
| 548 |
-
expression (including conversions), the program is ill-formed; no
|
| 549 |
-
diagnostic required.
|
| 550 |
|
| 551 |
``` cpp
|
| 552 |
constexpr int f(bool b)
|
| 553 |
{ return b ? throw 0 : 0; } // OK
|
| 554 |
-
constexpr int f() {
|
| 555 |
|
| 556 |
struct B {
|
| 557 |
constexpr B(int x) : i(0) { } // x is unused
|
| 558 |
int i;
|
| 559 |
};
|
|
@@ -567,37 +529,25 @@ struct D : B {
|
|
| 567 |
```
|
| 568 |
|
| 569 |
If the instantiated template specialization of a `constexpr` function
|
| 570 |
template or member function of a class template would fail to satisfy
|
| 571 |
the requirements for a `constexpr` function or `constexpr` constructor,
|
| 572 |
-
that specialization is
|
| 573 |
-
constructor
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
|
|
|
| 577 |
|
| 578 |
A call to a `constexpr` function produces the same result as a call to
|
| 579 |
an equivalent non-`constexpr` function in all respects except that a
|
| 580 |
call to a `constexpr` function can appear in a constant expression.
|
| 581 |
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
[[class.mfct.non-static]]). The `constexpr` specifier has no other
|
| 585 |
-
effect on the function type. The keyword `const` is ignored if it
|
| 586 |
-
appears in the *cv-qualifier-seq* of the function declarator of the
|
| 587 |
-
declaration of such a member function. The class of which that function
|
| 588 |
-
is a member shall be a literal type ([[basic.types]]).
|
| 589 |
|
| 590 |
``` cpp
|
| 591 |
-
class debug_flag {
|
| 592 |
-
public:
|
| 593 |
-
explicit debug_flag(bool);
|
| 594 |
-
constexpr bool is_on(); // error: debug_flag not
|
| 595 |
-
// literal type
|
| 596 |
-
private:
|
| 597 |
-
bool flag;
|
| 598 |
-
};
|
| 599 |
constexpr int bar(int x, int y) // OK
|
| 600 |
{ return x + y + x*y; }
|
| 601 |
// ...
|
| 602 |
int bar(int x, int y) // error: redefinition of bar
|
| 603 |
{ return x * 2 + 3 * y; }
|
|
@@ -608,12 +558,12 @@ object as `const`. Such an object shall have literal type and shall be
|
|
| 608 |
initialized. If it is initialized by a constructor call, that call shall
|
| 609 |
be a constant expression ([[expr.const]]). Otherwise, or if a
|
| 610 |
`constexpr` specifier is used in a reference declaration, every
|
| 611 |
full-expression that appears in its initializer shall be a constant
|
| 612 |
expression. Each implicit conversion used in converting the initializer
|
| 613 |
-
expressions and each constructor call used for the initialization
|
| 614 |
-
|
| 615 |
|
| 616 |
``` cpp
|
| 617 |
struct pixel {
|
| 618 |
int x, y;
|
| 619 |
};
|
|
@@ -669,25 +619,27 @@ exceptions to this rule are the following:
|
|
| 669 |
or `int`.
|
| 670 |
- `short` or `long` can be combined with `int`.
|
| 671 |
- `long` can be combined with `double`.
|
| 672 |
- `long` can be combined with `long`.
|
| 673 |
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
|
|
|
| 678 |
*alias-declaration* ([[dcl.typedef]]) that is not the *declaration* of
|
| 679 |
a *template-declaration*.
|
| 680 |
|
| 681 |
*enum-specifier*s, *class-specifier*s, and *typename-specifier*s are
|
| 682 |
-
discussed in [[dcl.enum]], [[class]], and [[temp.res]],
|
| 683 |
-
The remaining *type-specifier*s are discussed in the rest
|
| 684 |
-
section.
|
| 685 |
|
| 686 |
#### The *cv-qualifiers* <a id="dcl.type.cv">[[dcl.type.cv]]</a>
|
| 687 |
|
| 688 |
-
There are two *cv-qualifiers*, `const` and `volatile`.
|
|
|
|
| 689 |
*cv-qualifier* appears in a *decl-specifier-seq*, the
|
| 690 |
*init-declarator-list* of the declaration shall not be empty.
|
| 691 |
[[basic.type.qualifier]] and [[dcl.fct]] describe how cv-qualifiers
|
| 692 |
affect object and function types. Redundant cv-qualifications are
|
| 693 |
ignored. For example, these could be introduced by typedefs.
|
|
@@ -745,19 +697,22 @@ y.x.j++; // ill-formed: const-qualified member modified
|
|
| 745 |
Y* p = const_cast<Y*>(&y); // cast away const-ness of y
|
| 746 |
p->x.i = 99; // well-formed: mutable member can be modified
|
| 747 |
p->x.j = 99; // undefined: modifies a const member
|
| 748 |
```
|
| 749 |
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
|
|
|
| 753 |
|
| 754 |
`volatile` is a hint to the implementation to avoid aggressive
|
| 755 |
optimization involving the object because the value of the object might
|
| 756 |
-
be changed by means undetectable by an implementation.
|
| 757 |
-
|
| 758 |
-
|
|
|
|
|
|
|
| 759 |
|
| 760 |
#### Simple type specifiers <a id="dcl.type.simple">[[dcl.type.simple]]</a>
|
| 761 |
|
| 762 |
The simple type specifiers are
|
| 763 |
|
|
@@ -791,18 +746,19 @@ type-name:
|
|
| 791 |
```
|
| 792 |
|
| 793 |
``` bnf
|
| 794 |
decltype-specifier:
|
| 795 |
'decltype' '(' expression ')'
|
|
|
|
| 796 |
```
|
| 797 |
|
| 798 |
The `auto` specifier is a placeholder for a type to be deduced (
|
| 799 |
[[dcl.spec.auto]]). The other *simple-type-specifier*s specify either a
|
| 800 |
-
previously-declared
|
| 801 |
-
[[basic.fundamental]]). Table
|
| 802 |
-
|
| 803 |
-
specify.
|
| 804 |
|
| 805 |
**Table: *simple-type-specifier*{s} and the types they specify** <a id="tab:simple.type.specifiers">[tab:simple.type.specifiers]</a>
|
| 806 |
|
| 807 |
| | |
|
| 808 |
| ---------------------- | -------------------------------------- |
|
|
@@ -846,16 +802,16 @@ specify.
|
|
| 846 |
| decltype(*expression*) | the type as defined below |
|
| 847 |
|
| 848 |
|
| 849 |
When multiple *simple-type-specifiers* are allowed, they can be freely
|
| 850 |
intermixed with other *decl-specifiers* in any order. It is
|
| 851 |
-
implementation-defined whether objects of `char` type
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
to be signed; it is redundant in other contexts.
|
| 855 |
|
| 856 |
-
|
|
|
|
| 857 |
|
| 858 |
- if `e` is an unparenthesized *id-expression* or an unparenthesized
|
| 859 |
class member access ([[expr.ref]]), `decltype(e)` is the type of the
|
| 860 |
entity named by `e`. If there is no such entity, or if `e` names a set
|
| 861 |
of overloaded functions, the program is ill-formed;
|
|
@@ -871,16 +827,19 @@ The operand of the `decltype` specifier is an unevaluated operand
|
|
| 871 |
``` cpp
|
| 872 |
const int&& foo();
|
| 873 |
int i;
|
| 874 |
struct A { double x; };
|
| 875 |
const A* a = new A();
|
| 876 |
-
decltype(foo()) x1 =
|
| 877 |
decltype(i) x2; // type is int
|
| 878 |
decltype(a->x) x3; // type is double
|
| 879 |
decltype((a->x)) x4 = x3; // type is const double&
|
| 880 |
```
|
| 881 |
|
|
|
|
|
|
|
|
|
|
| 882 |
in the case where the operand of a *decltype-specifier* is a function
|
| 883 |
call and the return type of the function is a class type, a special
|
| 884 |
rule ([[expr.call]]) ensures that the return type is not required to be
|
| 885 |
complete (as it would be if the call appeared in a sub-expression or
|
| 886 |
outside of a *decltype-specifier*). In this context, the common purpose
|
|
@@ -923,11 +882,12 @@ void r() {
|
|
| 923 |
#### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
|
| 924 |
|
| 925 |
``` bnf
|
| 926 |
elaborated-type-specifier:
|
| 927 |
class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
|
| 928 |
-
class-key
|
|
|
|
| 929 |
'enum' nested-name-specifierₒₚₜ identifier
|
| 930 |
```
|
| 931 |
|
| 932 |
An *attribute-specifier-seq* shall not appear in an
|
| 933 |
*elaborated-type-specifier* unless the latter is the sole constituent of
|
|
@@ -983,68 +943,94 @@ enum class E { a, b };
|
|
| 983 |
enum E x = E::a; // OK
|
| 984 |
```
|
| 985 |
|
| 986 |
#### `auto` specifier <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
|
| 987 |
|
| 988 |
-
The `auto` *type-specifier*
|
| 989 |
-
|
| 990 |
-
|
|
|
|
|
|
|
| 991 |
|
| 992 |
-
The
|
| 993 |
-
*
|
| 994 |
-
declarator is valid.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 995 |
|
| 996 |
-
|
| 997 |
-
|
| 998 |
-
expression
|
| 999 |
-
|
| 1000 |
-
|
| 1001 |
-
|
| 1002 |
-
|
| 1003 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1004 |
|
| 1005 |
``` cpp
|
| 1006 |
auto x = 5; // OK: x has type int
|
| 1007 |
const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
|
| 1008 |
static auto y = 0.0; // OK: y has type double
|
| 1009 |
auto int r; // error: auto is not a storage-class-specifier
|
|
|
|
|
|
|
|
|
|
| 1010 |
```
|
| 1011 |
|
| 1012 |
-
|
| 1013 |
selection statement ([[stmt.select]]) or an iteration statement (
|
| 1014 |
[[stmt.iter]]), in the in the or of a ([[expr.new]]), in a
|
| 1015 |
*for-range-declaration*, and in declaring a static data member with a
|
| 1016 |
*brace-or-equal-initializer* that appears within the of a class
|
| 1017 |
definition ([[class.static.data]]).
|
| 1018 |
|
| 1019 |
-
A program that uses `auto` in a context not
|
| 1020 |
-
section is ill-formed.
|
| 1021 |
|
| 1022 |
-
|
| 1023 |
-
|
| 1024 |
-
|
| 1025 |
-
|
| 1026 |
-
|
| 1027 |
-
|
| 1028 |
-
*
|
| 1029 |
-
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
|
| 1033 |
-
|
| 1034 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1035 |
|
| 1036 |
``` cpp
|
| 1037 |
auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
|
| 1038 |
auto x2 = { 1, 2.0 }; // error: cannot deduce element type
|
| 1039 |
```
|
| 1040 |
|
| 1041 |
-
If the list of declarators contains more than one declarator, the type
|
| 1042 |
-
of each declared variable is determined as described above. If the type
|
| 1043 |
-
deduced for the template parameter `U` is not the same in each
|
| 1044 |
-
deduction, the program is ill-formed.
|
| 1045 |
-
|
| 1046 |
``` cpp
|
| 1047 |
const auto &i = expr;
|
| 1048 |
```
|
| 1049 |
|
| 1050 |
The type of `i` is the deduced type of the parameter `u` in the call
|
|
@@ -1052,5 +1038,129 @@ The type of `i` is the deduced type of the parameter `u` in the call
|
|
| 1052 |
|
| 1053 |
``` cpp
|
| 1054 |
template <class U> void f(const U& u);
|
| 1055 |
```
|
| 1056 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
The `thread_local` specifier indicates that the named entity has thread
|
| 100 |
storage duration ([[basic.stc.thread]]). It shall be applied only to
|
| 101 |
the names of variables of namespace or block scope and to the names of
|
| 102 |
static data members. When `thread_local` is applied to a variable of
|
| 103 |
+
block scope the *storage-class-specifier* `static` is implied if no
|
| 104 |
+
other *storage-class-specifier* appears in the *decl-specifier-seq*.
|
| 105 |
|
| 106 |
The `static` specifier can be applied only to names of variables and
|
| 107 |
functions and to anonymous unions ([[class.union]]). There can be no
|
| 108 |
`static` function declarations within a block, nor any `static` function
|
| 109 |
parameters. A `static` specifier used in the declaration of a variable
|
|
|
|
| 119 |
functions. The `extern` specifier cannot be used in the declaration of
|
| 120 |
class members or function parameters. For the linkage of a name declared
|
| 121 |
with an `extern` specifier, see [[basic.link]]. The `extern` keyword
|
| 122 |
can also be used in s and s, but it is not a in such contexts.
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
The linkages implied by successive declarations for a given entity shall
|
| 125 |
agree. That is, within a given scope, each declaration declaring the
|
| 126 |
same variable name or the same overloading of a function name shall
|
| 127 |
imply the same linkage. Each function in a given set of overloaded
|
| 128 |
functions can have a different linkage, however.
|
|
|
|
| 386 |
[[class.friend]].
|
| 387 |
|
| 388 |
### The `constexpr` specifier <a id="dcl.constexpr">[[dcl.constexpr]]</a>
|
| 389 |
|
| 390 |
The `constexpr` specifier shall be applied only to the definition of a
|
| 391 |
+
variable or variable template, the declaration of a function or function
|
| 392 |
+
template, or the declaration of a static data member of a literal type (
|
| 393 |
+
[[basic.types]]). If any declaration of a function, function template,
|
| 394 |
+
or variable template has a `constexpr` specifier, then all its
|
| 395 |
+
declarations shall contain the `constexpr` specifier. An explicit
|
| 396 |
+
specialization can differ from the template declaration with respect to
|
| 397 |
+
the `constexpr` specifier. Function parameters cannot be declared
|
| 398 |
+
`constexpr`.
|
| 399 |
|
| 400 |
``` cpp
|
| 401 |
+
constexpr void square(int &x); // OK: declaration
|
| 402 |
constexpr int bufsz = 1024; // OK: definition
|
| 403 |
constexpr struct pixel { // error: pixel is a type
|
| 404 |
int x;
|
| 405 |
int y;
|
| 406 |
constexpr pixel(int); // OK: declaration
|
| 407 |
};
|
| 408 |
constexpr pixel::pixel(int a)
|
| 409 |
+
: x(a), y(x) // OK: definition
|
| 410 |
+
{ square(x); }
|
| 411 |
constexpr pixel small(2); // error: square not defined, so small(2)
|
| 412 |
// not constant~([expr.const]) so constexpr not satisfied
|
| 413 |
|
| 414 |
+
constexpr void square(int &x) { // OK: definition
|
| 415 |
+
x *= x;
|
| 416 |
}
|
| 417 |
constexpr pixel large(4); // OK: square defined
|
| 418 |
int next(constexpr int x) { // error: not for parameters
|
| 419 |
return x + 1;
|
| 420 |
}
|
|
|
|
| 433 |
|
| 434 |
- it shall not be virtual ([[class.virtual]]);
|
| 435 |
- its return type shall be a literal type;
|
| 436 |
- each of its parameter types shall be a literal type;
|
| 437 |
- its *function-body* shall be `= delete`, `= default`, or a
|
| 438 |
+
*compound-statement* that does not contain
|
| 439 |
+
- an *asm-definition*,
|
| 440 |
+
- a `goto` statement,
|
| 441 |
+
- a *try-block*, or
|
| 442 |
+
- a definition of a variable of non-literal type or of static or
|
| 443 |
+
thread storage duration or for which no initialization is performed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
|
| 445 |
``` cpp
|
| 446 |
constexpr int square(int x)
|
| 447 |
{ return x * x; } // OK
|
| 448 |
constexpr long long_max()
|
| 449 |
{ return 2147483647; } // OK
|
| 450 |
+
constexpr int abs(int x) {
|
| 451 |
+
if (x < 0)
|
| 452 |
+
x = -x;
|
| 453 |
+
return x; // OK
|
| 454 |
+
}
|
| 455 |
+
constexpr int first(int n) {
|
| 456 |
+
static int value = n; // error: variable has static storage duration
|
| 457 |
+
return value;
|
| 458 |
+
}
|
| 459 |
+
constexpr int uninit() {
|
| 460 |
+
int a; // error: variable is uninitialized
|
| 461 |
+
return a;
|
| 462 |
+
}
|
| 463 |
constexpr int prev(int x)
|
| 464 |
+
{ return --x; } // OK
|
| 465 |
+
constexpr int g(int x, int n) { // OK
|
| 466 |
int r = 1;
|
| 467 |
while (--n > 0) r *= x;
|
| 468 |
return r;
|
| 469 |
}
|
| 470 |
```
|
| 471 |
|
| 472 |
+
The definition of a `constexpr` constructor shall satisfy the following
|
|
|
|
|
|
|
| 473 |
constraints:
|
| 474 |
|
| 475 |
- the class shall not have any virtual base classes;
|
| 476 |
+
- each of the parameter types shall be a literal type;
|
| 477 |
- its *function-body* shall not be a *function-try-block*;
|
|
|
|
|
|
|
| 478 |
|
| 479 |
+
In addition, either its *function-body* shall be `= delete`, or it shall
|
| 480 |
+
satisfy the following constraints:
|
| 481 |
|
| 482 |
+
- either its *function-body* shall be `= default`, or the
|
| 483 |
+
*compound-statement* of its *function-body* shall satisfy the
|
| 484 |
+
constraints for a *function-body* of a `constexpr` function;
|
| 485 |
+
- every non-variant non-static data member and base class sub-object
|
| 486 |
+
shall be initialized ([[class.base.init]]);
|
| 487 |
+
- if the class is a union having variant members ([[class.union]]),
|
| 488 |
+
exactly one of them shall be initialized;
|
| 489 |
+
- if the class is a union-like class, but is not a union, for each of
|
| 490 |
+
its anonymous union members having variant members, exactly one of
|
| 491 |
+
them shall be initialized;
|
| 492 |
+
- for a non-delegating constructor, every constructor selected to
|
| 493 |
+
initialize non-static data members and base class sub-objects shall be
|
| 494 |
+
a `constexpr` constructor;
|
| 495 |
+
- for a delegating constructor, the target constructor shall be a
|
| 496 |
+
`constexpr` constructor.
|
|
|
|
|
|
|
|
|
|
| 497 |
|
| 498 |
``` cpp
|
| 499 |
struct Length {
|
| 500 |
+
constexpr explicit Length(int i = 0) : val(i) { }
|
| 501 |
private:
|
| 502 |
int val;
|
| 503 |
};
|
| 504 |
```
|
| 505 |
|
| 506 |
+
For a non-template, non-defaulted `constexpr` function or a
|
| 507 |
+
non-template, non-defaulted, non-inheriting `constexpr` constructor, if
|
| 508 |
+
no argument values exist such that an invocation of the function or
|
| 509 |
+
constructor could be an evaluated subexpression of a core constant
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
expression ([[expr.const]]), the program is ill-formed; no diagnostic
|
| 511 |
+
required.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 512 |
|
| 513 |
``` cpp
|
| 514 |
constexpr int f(bool b)
|
| 515 |
{ return b ? throw 0 : 0; } // OK
|
| 516 |
+
constexpr int f() { return f(true); } // ill-formed, no diagnostic required
|
| 517 |
|
| 518 |
struct B {
|
| 519 |
constexpr B(int x) : i(0) { } // x is unused
|
| 520 |
int i;
|
| 521 |
};
|
|
|
|
| 529 |
```
|
| 530 |
|
| 531 |
If the instantiated template specialization of a `constexpr` function
|
| 532 |
template or member function of a class template would fail to satisfy
|
| 533 |
the requirements for a `constexpr` function or `constexpr` constructor,
|
| 534 |
+
that specialization is still a `constexpr` function or `constexpr`
|
| 535 |
+
constructor, even though a call to such a function cannot appear in a
|
| 536 |
+
constant expression. If no specialization of the template would satisfy
|
| 537 |
+
the requirements for a `constexpr` function or `constexpr` constructor
|
| 538 |
+
when considered as a non-template function or constructor, the template
|
| 539 |
+
is ill-formed; no diagnostic required.
|
| 540 |
|
| 541 |
A call to a `constexpr` function produces the same result as a call to
|
| 542 |
an equivalent non-`constexpr` function in all respects except that a
|
| 543 |
call to a `constexpr` function can appear in a constant expression.
|
| 544 |
|
| 545 |
+
The `constexpr` specifier has no effect on the type of a `constexpr`
|
| 546 |
+
function or a `constexpr` constructor.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 547 |
|
| 548 |
``` cpp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 549 |
constexpr int bar(int x, int y) // OK
|
| 550 |
{ return x + y + x*y; }
|
| 551 |
// ...
|
| 552 |
int bar(int x, int y) // error: redefinition of bar
|
| 553 |
{ return x * 2 + 3 * y; }
|
|
|
|
| 558 |
initialized. If it is initialized by a constructor call, that call shall
|
| 559 |
be a constant expression ([[expr.const]]). Otherwise, or if a
|
| 560 |
`constexpr` specifier is used in a reference declaration, every
|
| 561 |
full-expression that appears in its initializer shall be a constant
|
| 562 |
expression. Each implicit conversion used in converting the initializer
|
| 563 |
+
expressions and each constructor call used for the initialization is
|
| 564 |
+
part of such a full-expression.
|
| 565 |
|
| 566 |
``` cpp
|
| 567 |
struct pixel {
|
| 568 |
int x, y;
|
| 569 |
};
|
|
|
|
| 619 |
or `int`.
|
| 620 |
- `short` or `long` can be combined with `int`.
|
| 621 |
- `long` can be combined with `double`.
|
| 622 |
- `long` can be combined with `long`.
|
| 623 |
|
| 624 |
+
Except in a declaration of a constructor, destructor, or conversion
|
| 625 |
+
function, at least one *type-specifier* that is not a *cv-qualifier*
|
| 626 |
+
shall appear in a complete *type-specifier-seq* or a complete
|
| 627 |
+
*decl-specifier-seq*.[^3] A *type-specifier-seq* shall not define a
|
| 628 |
+
class or enumeration unless it appears in the *type-id* of an
|
| 629 |
*alias-declaration* ([[dcl.typedef]]) that is not the *declaration* of
|
| 630 |
a *template-declaration*.
|
| 631 |
|
| 632 |
*enum-specifier*s, *class-specifier*s, and *typename-specifier*s are
|
| 633 |
+
discussed in [[dcl.enum]], Clause [[class]], and [[temp.res]],
|
| 634 |
+
respectively. The remaining *type-specifier*s are discussed in the rest
|
| 635 |
+
of this section.
|
| 636 |
|
| 637 |
#### The *cv-qualifiers* <a id="dcl.type.cv">[[dcl.type.cv]]</a>
|
| 638 |
|
| 639 |
+
There are two *cv-qualifiers*, `const` and `volatile`. Each
|
| 640 |
+
*cv-qualifier* shall appear at most once in a *cv-qualifier-seq*. If a
|
| 641 |
*cv-qualifier* appears in a *decl-specifier-seq*, the
|
| 642 |
*init-declarator-list* of the declaration shall not be empty.
|
| 643 |
[[basic.type.qualifier]] and [[dcl.fct]] describe how cv-qualifiers
|
| 644 |
affect object and function types. Redundant cv-qualifications are
|
| 645 |
ignored. For example, these could be introduced by typedefs.
|
|
|
|
| 697 |
Y* p = const_cast<Y*>(&y); // cast away const-ness of y
|
| 698 |
p->x.i = 99; // well-formed: mutable member can be modified
|
| 699 |
p->x.j = 99; // undefined: modifies a const member
|
| 700 |
```
|
| 701 |
|
| 702 |
+
What constitutes an access to an object that has volatile-qualified type
|
| 703 |
+
is implementation-defined. If an attempt is made to refer to an object
|
| 704 |
+
defined with a volatile-qualified type through the use of a glvalue with
|
| 705 |
+
a non-volatile-qualified type, the program behavior is undefined.
|
| 706 |
|
| 707 |
`volatile` is a hint to the implementation to avoid aggressive
|
| 708 |
optimization involving the object because the value of the object might
|
| 709 |
+
be changed by means undetectable by an implementation. Furthermore, for
|
| 710 |
+
some implementations, `volatile` might indicate that special hardware
|
| 711 |
+
instructions are required to access the object. See [[intro.execution]]
|
| 712 |
+
for detailed semantics. In general, the semantics of `volatile` are
|
| 713 |
+
intended to be the same in C++as they are in C.
|
| 714 |
|
| 715 |
#### Simple type specifiers <a id="dcl.type.simple">[[dcl.type.simple]]</a>
|
| 716 |
|
| 717 |
The simple type specifiers are
|
| 718 |
|
|
|
|
| 746 |
```
|
| 747 |
|
| 748 |
``` bnf
|
| 749 |
decltype-specifier:
|
| 750 |
'decltype' '(' expression ')'
|
| 751 |
+
'decltype' '(' 'auto' ')'
|
| 752 |
```
|
| 753 |
|
| 754 |
The `auto` specifier is a placeholder for a type to be deduced (
|
| 755 |
[[dcl.spec.auto]]). The other *simple-type-specifier*s specify either a
|
| 756 |
+
previously-declared type, a type determined from an expression, or one
|
| 757 |
+
of the fundamental types ([[basic.fundamental]]). Table
|
| 758 |
+
[[tab:simple.type.specifiers]] summarizes the valid combinations of
|
| 759 |
+
*simple-type-specifier*s and the types they specify.
|
| 760 |
|
| 761 |
**Table: *simple-type-specifier*{s} and the types they specify** <a id="tab:simple.type.specifiers">[tab:simple.type.specifiers]</a>
|
| 762 |
|
| 763 |
| | |
|
| 764 |
| ---------------------- | -------------------------------------- |
|
|
|
|
| 802 |
| decltype(*expression*) | the type as defined below |
|
| 803 |
|
| 804 |
|
| 805 |
When multiple *simple-type-specifiers* are allowed, they can be freely
|
| 806 |
intermixed with other *decl-specifiers* in any order. It is
|
| 807 |
+
implementation-defined whether objects of `char` type are represented as
|
| 808 |
+
signed or unsigned quantities. The `signed` specifier forces `char`
|
| 809 |
+
objects to be signed; it is redundant in other contexts.
|
|
|
|
| 810 |
|
| 811 |
+
For an expression `e`, the type denoted by `decltype(e)` is defined as
|
| 812 |
+
follows:
|
| 813 |
|
| 814 |
- if `e` is an unparenthesized *id-expression* or an unparenthesized
|
| 815 |
class member access ([[expr.ref]]), `decltype(e)` is the type of the
|
| 816 |
entity named by `e`. If there is no such entity, or if `e` names a set
|
| 817 |
of overloaded functions, the program is ill-formed;
|
|
|
|
| 827 |
``` cpp
|
| 828 |
const int&& foo();
|
| 829 |
int i;
|
| 830 |
struct A { double x; };
|
| 831 |
const A* a = new A();
|
| 832 |
+
decltype(foo()) x1 = 0; // type is const int&&
|
| 833 |
decltype(i) x2; // type is int
|
| 834 |
decltype(a->x) x3; // type is double
|
| 835 |
decltype((a->x)) x4 = x3; // type is const double&
|
| 836 |
```
|
| 837 |
|
| 838 |
+
The rules for determining types involving `decltype(auto)` are specified
|
| 839 |
+
in [[dcl.spec.auto]].
|
| 840 |
+
|
| 841 |
in the case where the operand of a *decltype-specifier* is a function
|
| 842 |
call and the return type of the function is a class type, a special
|
| 843 |
rule ([[expr.call]]) ensures that the return type is not required to be
|
| 844 |
complete (as it would be if the call appeared in a sub-expression or
|
| 845 |
outside of a *decltype-specifier*). In this context, the common purpose
|
|
|
|
| 882 |
#### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
|
| 883 |
|
| 884 |
``` bnf
|
| 885 |
elaborated-type-specifier:
|
| 886 |
class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
|
| 887 |
+
class-key simple-template-id
|
| 888 |
+
class-key nested-name-specifier 'template'ₒₚₜ simple-template-id
|
| 889 |
'enum' nested-name-specifierₒₚₜ identifier
|
| 890 |
```
|
| 891 |
|
| 892 |
An *attribute-specifier-seq* shall not appear in an
|
| 893 |
*elaborated-type-specifier* unless the latter is the sole constituent of
|
|
|
|
| 943 |
enum E x = E::a; // OK
|
| 944 |
```
|
| 945 |
|
| 946 |
#### `auto` specifier <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
|
| 947 |
|
| 948 |
+
The `auto` and `decltype(auto)` *type-specifier*s designate a
|
| 949 |
+
placeholder type that will be replaced later, either by deduction from
|
| 950 |
+
an initializer or by explicit specification with a
|
| 951 |
+
*trailing-return-type*. The `auto` *type-specifier* is also used to
|
| 952 |
+
signify that a lambda is a generic lambda.
|
| 953 |
|
| 954 |
+
The placeholder type can appear with a function declarator in the
|
| 955 |
+
*decl-specifier-seq*, *type-specifier-seq*, *conversion-function-id*, or
|
| 956 |
+
*trailing-return-type*, in any context where such a declarator is valid.
|
| 957 |
+
If the function declarator includes a *trailing-return-type* (
|
| 958 |
+
[[dcl.fct]]), that specifies the declared return type of the function.
|
| 959 |
+
If the declared return type of the function contains a placeholder type,
|
| 960 |
+
the return type of the function is deduced from `return` statements in
|
| 961 |
+
the body of the function, if any.
|
| 962 |
|
| 963 |
+
If the `auto` *type-specifier* appears as one of the *decl-specifier*s
|
| 964 |
+
in the *decl-specifier-seq* of a *parameter-declaration* of a
|
| 965 |
+
*lambda-expression*, the lambda is a *generic lambda* (
|
| 966 |
+
[[expr.prim.lambda]]).
|
| 967 |
+
|
| 968 |
+
``` cpp
|
| 969 |
+
auto glambda = [](int i, auto a) { return i; }; // OK: a generic lambda
|
| 970 |
+
```
|
| 971 |
+
|
| 972 |
+
The type of a variable declared using `auto` or `decltype(auto)` is
|
| 973 |
+
deduced from its initializer. This use is allowed when declaring
|
| 974 |
+
variables in a block ([[stmt.block]]), in namespace scope (
|
| 975 |
+
[[basic.scope.namespace]]), and in a ([[stmt.for]]). `auto` or
|
| 976 |
+
`decltype(auto)` shall appear as one of the *decl-specifier*s in the
|
| 977 |
+
*decl-specifier-seq* and the *decl-specifier-seq* shall be followed by
|
| 978 |
+
one or more *init-declarator*s, each of which shall have a non-empty
|
| 979 |
+
*initializer*. In an *initializer* of the form
|
| 980 |
+
|
| 981 |
+
``` cpp
|
| 982 |
+
( expression-list )
|
| 983 |
+
```
|
| 984 |
+
|
| 985 |
+
the *expression-list* shall be a single *assignment-expression*.
|
| 986 |
|
| 987 |
``` cpp
|
| 988 |
auto x = 5; // OK: x has type int
|
| 989 |
const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
|
| 990 |
static auto y = 0.0; // OK: y has type double
|
| 991 |
auto int r; // error: auto is not a storage-class-specifier
|
| 992 |
+
auto f() -> int; // OK: f returns int
|
| 993 |
+
auto g() { return 0.0; } // OK: g returns double
|
| 994 |
+
auto h(); // OK: h's return type will be deduced when it is defined
|
| 995 |
```
|
| 996 |
|
| 997 |
+
A placeholder type can also be used in declaring a variable in the of a
|
| 998 |
selection statement ([[stmt.select]]) or an iteration statement (
|
| 999 |
[[stmt.iter]]), in the in the or of a ([[expr.new]]), in a
|
| 1000 |
*for-range-declaration*, and in declaring a static data member with a
|
| 1001 |
*brace-or-equal-initializer* that appears within the of a class
|
| 1002 |
definition ([[class.static.data]]).
|
| 1003 |
|
| 1004 |
+
A program that uses `auto` or `decltype(auto)` in a context not
|
| 1005 |
+
explicitly allowed in this section is ill-formed.
|
| 1006 |
|
| 1007 |
+
When a variable declared using a placeholder type is initialized, or a
|
| 1008 |
+
`return` statement occurs in a function declared with a return type that
|
| 1009 |
+
contains a placeholder type, the deduced return type or variable type is
|
| 1010 |
+
determined from the type of its initializer. In the case of a `return`
|
| 1011 |
+
with no operand, the initializer is considered to be `void()`. Let `T`
|
| 1012 |
+
be the declared type of the variable or return type of the function. If
|
| 1013 |
+
the placeholder is the `auto` *type-specifier*, the deduced type is
|
| 1014 |
+
determined using the rules for template argument deduction. If the
|
| 1015 |
+
deduction is for a `return` statement and the initializer is a
|
| 1016 |
+
*braced-init-list* ([[dcl.init.list]]), the program is ill-formed.
|
| 1017 |
+
Otherwise, obtain `P` from `T` by replacing the occurrences of `auto`
|
| 1018 |
+
with either a new invented type template parameter `U` or, if the
|
| 1019 |
+
initializer is a *braced-init-list*, with `std::initializer_list<U>`.
|
| 1020 |
+
Deduce a value for `U` using the rules of template argument deduction
|
| 1021 |
+
from a function call ([[temp.deduct.call]]), where `P` is a function
|
| 1022 |
+
template parameter type and the initializer is the corresponding
|
| 1023 |
+
argument. If the deduction fails, the declaration is ill-formed.
|
| 1024 |
+
Otherwise, the type deduced for the variable or return type is obtained
|
| 1025 |
+
by substituting the deduced `U` into `P`.
|
| 1026 |
|
| 1027 |
``` cpp
|
| 1028 |
auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
|
| 1029 |
auto x2 = { 1, 2.0 }; // error: cannot deduce element type
|
| 1030 |
```
|
| 1031 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1032 |
``` cpp
|
| 1033 |
const auto &i = expr;
|
| 1034 |
```
|
| 1035 |
|
| 1036 |
The type of `i` is the deduced type of the parameter `u` in the call
|
|
|
|
| 1038 |
|
| 1039 |
``` cpp
|
| 1040 |
template <class U> void f(const U& u);
|
| 1041 |
```
|
| 1042 |
|
| 1043 |
+
If the placeholder is the `decltype(auto)` *type-specifier*, the
|
| 1044 |
+
declared type of the variable or return type of the function shall be
|
| 1045 |
+
the placeholder alone. The type deduced for the variable or return type
|
| 1046 |
+
is determined as described in [[dcl.type.simple]], as though the
|
| 1047 |
+
initializer had been the operand of the `decltype`.
|
| 1048 |
+
|
| 1049 |
+
``` cpp
|
| 1050 |
+
int i;
|
| 1051 |
+
int&& f();
|
| 1052 |
+
auto x3a = i; // decltype(x3a) is int
|
| 1053 |
+
decltype(auto) x3d = i; // decltype(x3d) is int
|
| 1054 |
+
auto x4a = (i); // decltype(x4a) is int
|
| 1055 |
+
decltype(auto) x4d = (i); // decltype(x4d) is int&
|
| 1056 |
+
auto x5a = f(); // decltype(x5a) is int
|
| 1057 |
+
decltype(auto) x5d = f(); // decltype(x5d) is int&&
|
| 1058 |
+
auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
|
| 1059 |
+
decltype(auto) x6d = { 1, 2 }; // error, { 1, 2 } is not an expression
|
| 1060 |
+
auto *x7a = &i; // decltype(x7a) is int*
|
| 1061 |
+
decltype(auto)*x7d = &i; // error, declared type is not plain decltype(auto)
|
| 1062 |
+
```
|
| 1063 |
+
|
| 1064 |
+
If the *init-declarator-list* contains more than one *init-declarator*,
|
| 1065 |
+
they shall all form declarations of variables. The type of each declared
|
| 1066 |
+
variable is determined as described above, and if the type that replaces
|
| 1067 |
+
the placeholder type is not the same in each deduction, the program is
|
| 1068 |
+
ill-formed.
|
| 1069 |
+
|
| 1070 |
+
``` cpp
|
| 1071 |
+
auto x = 5, *y = &x; // OK: auto is int
|
| 1072 |
+
auto a = 5, b = { 1, 2 }; // error: different types for auto
|
| 1073 |
+
```
|
| 1074 |
+
|
| 1075 |
+
If a function with a declared return type that contains a placeholder
|
| 1076 |
+
type has multiple `return` statements, the return type is deduced for
|
| 1077 |
+
each `return` statement. If the type deduced is not the same in each
|
| 1078 |
+
deduction, the program is ill-formed.
|
| 1079 |
+
|
| 1080 |
+
If a function with a declared return type that uses a placeholder type
|
| 1081 |
+
has no `return` statements, the return type is deduced as though from a
|
| 1082 |
+
`return` statement with no operand at the closing brace of the function
|
| 1083 |
+
body.
|
| 1084 |
+
|
| 1085 |
+
``` cpp
|
| 1086 |
+
auto f() { } // OK, return type is void
|
| 1087 |
+
auto* g() { } // error, cannot deduce auto* from void()
|
| 1088 |
+
```
|
| 1089 |
+
|
| 1090 |
+
If the type of an entity with an undeduced placeholder type is needed to
|
| 1091 |
+
determine the type of an expression, the program is ill-formed. Once a
|
| 1092 |
+
`return` statement has been seen in a function, however, the return type
|
| 1093 |
+
deduced from that statement can be used in the rest of the function,
|
| 1094 |
+
including in other `return` statements.
|
| 1095 |
+
|
| 1096 |
+
``` cpp
|
| 1097 |
+
auto n = n; // error, n's type is unknown
|
| 1098 |
+
auto f();
|
| 1099 |
+
void g() { &f; } // error, f's return type is unknown
|
| 1100 |
+
auto sum(int i) {
|
| 1101 |
+
if (i == 1)
|
| 1102 |
+
return i; // sum's return type is int
|
| 1103 |
+
else
|
| 1104 |
+
return sum(i-1)+i; // OK, sum's return type has been deduced
|
| 1105 |
+
}
|
| 1106 |
+
```
|
| 1107 |
+
|
| 1108 |
+
Return type deduction for a function template with a placeholder in its
|
| 1109 |
+
declared type occurs when the definition is instantiated even if the
|
| 1110 |
+
function body contains a `return` statement with a non-type-dependent
|
| 1111 |
+
operand. Therefore, any use of a specialization of the function template
|
| 1112 |
+
will cause an implicit instantiation. Any errors that arise from this
|
| 1113 |
+
instantiation are not in the immediate context of the function type and
|
| 1114 |
+
can result in the program being ill-formed.
|
| 1115 |
+
|
| 1116 |
+
``` cpp
|
| 1117 |
+
template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
|
| 1118 |
+
typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type
|
| 1119 |
+
template<class T> auto f(T* t) { return *t; }
|
| 1120 |
+
void g() { int (*p)(int*) = &f; } // instantiates both fs to determine return types,
|
| 1121 |
+
// chooses second
|
| 1122 |
+
```
|
| 1123 |
+
|
| 1124 |
+
Redeclarations or specializations of a function or function template
|
| 1125 |
+
with a declared return type that uses a placeholder type shall also use
|
| 1126 |
+
that placeholder, not a deduced type.
|
| 1127 |
+
|
| 1128 |
+
``` cpp
|
| 1129 |
+
auto f();
|
| 1130 |
+
auto f() { return 42; } // return type is int
|
| 1131 |
+
auto f(); // OK
|
| 1132 |
+
int f(); // error, cannot be overloaded with auto f()
|
| 1133 |
+
decltype(auto) f(); // error, auto and decltype(auto) don't match
|
| 1134 |
+
|
| 1135 |
+
template <typename T> auto g(T t) { return t; } // #1
|
| 1136 |
+
template auto g(int); // OK, return type is int
|
| 1137 |
+
template char g(char); // error, no matching template
|
| 1138 |
+
template<> auto g(double); // OK, forward declaration with unknown return type
|
| 1139 |
+
|
| 1140 |
+
template <class T> T g(T t) { return t; } // OK, not functionally equivalent to #1
|
| 1141 |
+
template char g(char); // OK, now there is a matching template
|
| 1142 |
+
template auto g(float); // still matches #1
|
| 1143 |
+
|
| 1144 |
+
void h() { return g(42); } // error, ambiguous
|
| 1145 |
+
|
| 1146 |
+
template <typename T> struct A {
|
| 1147 |
+
friend T frf(T);
|
| 1148 |
+
};
|
| 1149 |
+
auto frf(int i) { return i; } // not a friend of A<int>
|
| 1150 |
+
```
|
| 1151 |
+
|
| 1152 |
+
A function declared with a return type that uses a placeholder type
|
| 1153 |
+
shall not be `virtual` ([[class.virtual]]).
|
| 1154 |
+
|
| 1155 |
+
An explicit instantiation declaration ([[temp.explicit]]) does not
|
| 1156 |
+
cause the instantiation of an entity declared using a placeholder type,
|
| 1157 |
+
but it also does not prevent that entity from being instantiated as
|
| 1158 |
+
needed to determine its type.
|
| 1159 |
+
|
| 1160 |
+
``` cpp
|
| 1161 |
+
template <typename T> auto f(T t) { return t; }
|
| 1162 |
+
extern template auto f(int); // does not instantiate f<int>
|
| 1163 |
+
int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit
|
| 1164 |
+
// instantiation definition is still required somewhere in the program
|
| 1165 |
+
```
|
| 1166 |
+
|