- tmp/tmpunrgxckk/{from.md → to.md} +442 -268
tmp/tmpunrgxckk/{from.md → to.md}
RENAMED
|
@@ -34,11 +34,11 @@ block-declaration:
|
|
| 34 |
opaque-enum-declaration
|
| 35 |
```
|
| 36 |
|
| 37 |
``` bnf
|
| 38 |
alias-declaration:
|
| 39 |
-
'using' identifier attribute-specifier-seqₒₚₜ = type-id ';'
|
| 40 |
```
|
| 41 |
|
| 42 |
``` bnf
|
| 43 |
simple-declaration:
|
| 44 |
decl-specifier-seqₒₚₜ init-declarator-listₒₚₜ ';'
|
|
@@ -106,13 +106,12 @@ omitted only when declaring a class (Clause [[class]]) or enumeration (
|
|
| 106 |
[[dcl.enum]]), that is, when the *decl-specifier-seq* contains either a
|
| 107 |
*class-specifier*, an *elaborated-type-specifier* with a *class-key* (
|
| 108 |
[[class.name]]), or an *enum-specifier*. In these cases and whenever a
|
| 109 |
*class-specifier* or *enum-specifier* is present in the
|
| 110 |
*decl-specifier-seq*, the identifiers in these specifiers are among the
|
| 111 |
-
names being declared by the declaration (as *class-
|
| 112 |
-
or *
|
| 113 |
-
for the declaration of an unnamed bit-field ([[class.bit]]), the
|
| 114 |
*decl-specifier-seq* shall introduce one or more names into the program,
|
| 115 |
or shall redeclare a name introduced by a previous declaration.
|
| 116 |
|
| 117 |
``` cpp
|
| 118 |
enum { }; // ill-formed
|
|
@@ -261,12 +260,12 @@ taken. This use is deprecated (see [[depr.register]]).
|
|
| 261 |
|
| 262 |
The `thread_local` specifier indicates that the named entity has thread
|
| 263 |
storage duration ([[basic.stc.thread]]). It shall be applied only to
|
| 264 |
the names of variables of namespace or block scope and to the names of
|
| 265 |
static data members. When `thread_local` is applied to a variable of
|
| 266 |
-
block scope the *storage-class-specifier* `static` is implied if
|
| 267 |
-
|
| 268 |
|
| 269 |
The `static` specifier can be applied only to names of variables and
|
| 270 |
functions and to anonymous unions ([[class.union]]). There can be no
|
| 271 |
`static` function declarations within a block, nor any `static` function
|
| 272 |
parameters. A `static` specifier used in the declaration of a variable
|
|
@@ -282,16 +281,10 @@ The `extern` specifier can be applied only to the names of variables and
|
|
| 282 |
functions. The `extern` specifier cannot be used in the declaration of
|
| 283 |
class members or function parameters. For the linkage of a name declared
|
| 284 |
with an `extern` specifier, see [[basic.link]]. The `extern` keyword
|
| 285 |
can also be used in s and s, but it is not a in such contexts.
|
| 286 |
|
| 287 |
-
A name declared in a namespace scope without a *storage-class-specifier*
|
| 288 |
-
has external linkage unless it has internal linkage because of a
|
| 289 |
-
previous declaration and provided it is not declared `const`. Objects
|
| 290 |
-
declared `const` and not explicitly declared `extern` have internal
|
| 291 |
-
linkage.
|
| 292 |
-
|
| 293 |
The linkages implied by successive declarations for a given entity shall
|
| 294 |
agree. That is, within a given scope, each declaration declaring the
|
| 295 |
same variable name or the same overloading of a function name shall
|
| 296 |
imply the same linkage. Each function in a given set of overloaded
|
| 297 |
functions can have a different linkage, however.
|
|
@@ -555,34 +548,35 @@ The `friend` specifier is used to specify access to class members; see
|
|
| 555 |
[[class.friend]].
|
| 556 |
|
| 557 |
### The `constexpr` specifier <a id="dcl.constexpr">[[dcl.constexpr]]</a>
|
| 558 |
|
| 559 |
The `constexpr` specifier shall be applied only to the definition of a
|
| 560 |
-
variable, the declaration of a function or function
|
| 561 |
-
declaration of a static data member of a literal type (
|
| 562 |
-
[[basic.types]]). If any declaration of a function
|
| 563 |
-
has `constexpr` specifier, then all its
|
| 564 |
-
`constexpr` specifier. An explicit
|
| 565 |
-
template declaration with respect to
|
| 566 |
-
parameters cannot be declared
|
|
|
|
| 567 |
|
| 568 |
``` cpp
|
| 569 |
-
constexpr
|
| 570 |
constexpr int bufsz = 1024; // OK: definition
|
| 571 |
constexpr struct pixel { // error: pixel is a type
|
| 572 |
int x;
|
| 573 |
int y;
|
| 574 |
constexpr pixel(int); // OK: declaration
|
| 575 |
};
|
| 576 |
constexpr pixel::pixel(int a)
|
| 577 |
-
: x(
|
| 578 |
-
{ }
|
| 579 |
constexpr pixel small(2); // error: square not defined, so small(2)
|
| 580 |
// not constant~([expr.const]) so constexpr not satisfied
|
| 581 |
|
| 582 |
-
constexpr
|
| 583 |
-
|
| 584 |
}
|
| 585 |
constexpr pixel large(4); // OK: square defined
|
| 586 |
int next(constexpr int x) { // error: not for parameters
|
| 587 |
return x + 1;
|
| 588 |
}
|
|
@@ -601,122 +595,89 @@ constraints:
|
|
| 601 |
|
| 602 |
- it shall not be virtual ([[class.virtual]]);
|
| 603 |
- its return type shall be a literal type;
|
| 604 |
- each of its parameter types shall be a literal type;
|
| 605 |
- its *function-body* shall be `= delete`, `= default`, or a
|
| 606 |
-
*compound-statement* that
|
| 607 |
-
-
|
| 608 |
-
|
| 609 |
-
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
classes or enumerations,
|
| 613 |
-
|
| 614 |
-
- *using-declaration*s,
|
| 615 |
-
|
| 616 |
-
- *using-directive*s,
|
| 617 |
-
|
| 618 |
-
- and exactly one return statement;
|
| 619 |
-
- every constructor call and implicit conversion used in initializing
|
| 620 |
-
the return value ([[stmt.return]], [[dcl.init]]) shall be one of
|
| 621 |
-
those allowed in a constant expression ([[expr.const]]).
|
| 622 |
|
| 623 |
``` cpp
|
| 624 |
constexpr int square(int x)
|
| 625 |
{ return x * x; } // OK
|
| 626 |
constexpr long long_max()
|
| 627 |
{ return 2147483647; } // OK
|
| 628 |
-
constexpr int abs(int x)
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 632 |
constexpr int prev(int x)
|
| 633 |
-
{ return --x; } //
|
| 634 |
-
constexpr int g(int x, int n) { //
|
| 635 |
int r = 1;
|
| 636 |
while (--n > 0) r *= x;
|
| 637 |
return r;
|
| 638 |
}
|
| 639 |
```
|
| 640 |
|
| 641 |
-
|
| 642 |
-
types shall be a literal type. In addition, either its *function-body*
|
| 643 |
-
shall be `= delete` or `= default` or it shall satisfy the following
|
| 644 |
constraints:
|
| 645 |
|
| 646 |
- the class shall not have any virtual base classes;
|
|
|
|
| 647 |
- its *function-body* shall not be a *function-try-block*;
|
| 648 |
-
- the *compound-statement* of its *function-body* shall contain only
|
| 649 |
-
- null statements,
|
| 650 |
|
| 651 |
-
|
|
|
|
| 652 |
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
the corresponding parameter type and converting a full-expression to
|
| 669 |
-
the corresponding member type shall be one of those allowed in a
|
| 670 |
-
constant expression.
|
| 671 |
|
| 672 |
``` cpp
|
| 673 |
struct Length {
|
| 674 |
-
|
| 675 |
private:
|
| 676 |
int val;
|
| 677 |
};
|
| 678 |
```
|
| 679 |
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
argument
|
| 683 |
-
|
| 684 |
-
use of the corresponding parameter in the *function-body*, and, for
|
| 685 |
-
`constexpr` functions, implicitly converting the resulting returned
|
| 686 |
-
expression or *braced-init-list* to the return type of the function as
|
| 687 |
-
if by copy-initialization. Such substitution does not change the
|
| 688 |
-
meaning.
|
| 689 |
-
|
| 690 |
-
``` cpp
|
| 691 |
-
constexpr int f(void *) { return 0; }
|
| 692 |
-
constexpr int f(...) { return 1; }
|
| 693 |
-
constexpr int g1() { return f(0); } // calls f(void *)
|
| 694 |
-
constexpr int g2(int n) { return f(n); } // calls f(...) even for n == 0
|
| 695 |
-
constexpr int g3(int n) { return f(n*0); } // calls f(...)
|
| 696 |
-
|
| 697 |
-
namespace N {
|
| 698 |
-
constexpr int c = 5;
|
| 699 |
-
constexpr int h() { return c; }
|
| 700 |
-
}
|
| 701 |
-
constexpr int c = 0;
|
| 702 |
-
constexpr int g4() { return N::h(); } // value is 5, c is not looked up again after the substitution
|
| 703 |
-
```
|
| 704 |
-
|
| 705 |
-
For a `constexpr` function, if no function argument values exist such
|
| 706 |
-
that the function invocation substitution would produce a constant
|
| 707 |
expression ([[expr.const]]), the program is ill-formed; no diagnostic
|
| 708 |
-
required.
|
| 709 |
-
such that after function invocation substitution, every constructor call
|
| 710 |
-
and full-expression in the *mem-initializer*s would be a constant
|
| 711 |
-
expression (including conversions), the program is ill-formed; no
|
| 712 |
-
diagnostic required.
|
| 713 |
|
| 714 |
``` cpp
|
| 715 |
constexpr int f(bool b)
|
| 716 |
{ return b ? throw 0 : 0; } // OK
|
| 717 |
-
constexpr int f() {
|
| 718 |
|
| 719 |
struct B {
|
| 720 |
constexpr B(int x) : i(0) { } // x is unused
|
| 721 |
int i;
|
| 722 |
};
|
|
@@ -730,37 +691,25 @@ struct D : B {
|
|
| 730 |
```
|
| 731 |
|
| 732 |
If the instantiated template specialization of a `constexpr` function
|
| 733 |
template or member function of a class template would fail to satisfy
|
| 734 |
the requirements for a `constexpr` function or `constexpr` constructor,
|
| 735 |
-
that specialization is
|
| 736 |
-
constructor
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
|
|
|
| 740 |
|
| 741 |
A call to a `constexpr` function produces the same result as a call to
|
| 742 |
an equivalent non-`constexpr` function in all respects except that a
|
| 743 |
call to a `constexpr` function can appear in a constant expression.
|
| 744 |
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
[[class.mfct.non-static]]). The `constexpr` specifier has no other
|
| 748 |
-
effect on the function type. The keyword `const` is ignored if it
|
| 749 |
-
appears in the *cv-qualifier-seq* of the function declarator of the
|
| 750 |
-
declaration of such a member function. The class of which that function
|
| 751 |
-
is a member shall be a literal type ([[basic.types]]).
|
| 752 |
|
| 753 |
``` cpp
|
| 754 |
-
class debug_flag {
|
| 755 |
-
public:
|
| 756 |
-
explicit debug_flag(bool);
|
| 757 |
-
constexpr bool is_on(); // error: debug_flag not
|
| 758 |
-
// literal type
|
| 759 |
-
private:
|
| 760 |
-
bool flag;
|
| 761 |
-
};
|
| 762 |
constexpr int bar(int x, int y) // OK
|
| 763 |
{ return x + y + x*y; }
|
| 764 |
// ...
|
| 765 |
int bar(int x, int y) // error: redefinition of bar
|
| 766 |
{ return x * 2 + 3 * y; }
|
|
@@ -771,12 +720,12 @@ object as `const`. Such an object shall have literal type and shall be
|
|
| 771 |
initialized. If it is initialized by a constructor call, that call shall
|
| 772 |
be a constant expression ([[expr.const]]). Otherwise, or if a
|
| 773 |
`constexpr` specifier is used in a reference declaration, every
|
| 774 |
full-expression that appears in its initializer shall be a constant
|
| 775 |
expression. Each implicit conversion used in converting the initializer
|
| 776 |
-
expressions and each constructor call used for the initialization
|
| 777 |
-
|
| 778 |
|
| 779 |
``` cpp
|
| 780 |
struct pixel {
|
| 781 |
int x, y;
|
| 782 |
};
|
|
@@ -832,25 +781,27 @@ exceptions to this rule are the following:
|
|
| 832 |
or `int`.
|
| 833 |
- `short` or `long` can be combined with `int`.
|
| 834 |
- `long` can be combined with `double`.
|
| 835 |
- `long` can be combined with `long`.
|
| 836 |
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
|
|
|
| 841 |
*alias-declaration* ([[dcl.typedef]]) that is not the *declaration* of
|
| 842 |
a *template-declaration*.
|
| 843 |
|
| 844 |
*enum-specifier*s, *class-specifier*s, and *typename-specifier*s are
|
| 845 |
-
discussed in [[dcl.enum]], [[class]], and [[temp.res]],
|
| 846 |
-
The remaining *type-specifier*s are discussed in the rest
|
| 847 |
-
section.
|
| 848 |
|
| 849 |
#### The *cv-qualifiers* <a id="dcl.type.cv">[[dcl.type.cv]]</a>
|
| 850 |
|
| 851 |
-
There are two *cv-qualifiers*, `const` and `volatile`.
|
|
|
|
| 852 |
*cv-qualifier* appears in a *decl-specifier-seq*, the
|
| 853 |
*init-declarator-list* of the declaration shall not be empty.
|
| 854 |
[[basic.type.qualifier]] and [[dcl.fct]] describe how cv-qualifiers
|
| 855 |
affect object and function types. Redundant cv-qualifications are
|
| 856 |
ignored. For example, these could be introduced by typedefs.
|
|
@@ -908,19 +859,22 @@ y.x.j++; // ill-formed: const-qualified member modified
|
|
| 908 |
Y* p = const_cast<Y*>(&y); // cast away const-ness of y
|
| 909 |
p->x.i = 99; // well-formed: mutable member can be modified
|
| 910 |
p->x.j = 99; // undefined: modifies a const member
|
| 911 |
```
|
| 912 |
|
| 913 |
-
|
| 914 |
-
|
| 915 |
-
|
|
|
|
| 916 |
|
| 917 |
`volatile` is a hint to the implementation to avoid aggressive
|
| 918 |
optimization involving the object because the value of the object might
|
| 919 |
-
be changed by means undetectable by an implementation.
|
| 920 |
-
|
| 921 |
-
|
|
|
|
|
|
|
| 922 |
|
| 923 |
#### Simple type specifiers <a id="dcl.type.simple">[[dcl.type.simple]]</a>
|
| 924 |
|
| 925 |
The simple type specifiers are
|
| 926 |
|
|
@@ -954,18 +908,19 @@ type-name:
|
|
| 954 |
```
|
| 955 |
|
| 956 |
``` bnf
|
| 957 |
decltype-specifier:
|
| 958 |
'decltype' '(' expression ')'
|
|
|
|
| 959 |
```
|
| 960 |
|
| 961 |
The `auto` specifier is a placeholder for a type to be deduced (
|
| 962 |
[[dcl.spec.auto]]). The other *simple-type-specifier*s specify either a
|
| 963 |
-
previously-declared
|
| 964 |
-
[[basic.fundamental]]). Table
|
| 965 |
-
|
| 966 |
-
specify.
|
| 967 |
|
| 968 |
**Table: *simple-type-specifier*{s} and the types they specify** <a id="tab:simple.type.specifiers">[tab:simple.type.specifiers]</a>
|
| 969 |
|
| 970 |
| | |
|
| 971 |
| ---------------------- | -------------------------------------- |
|
|
@@ -1009,16 +964,16 @@ specify.
|
|
| 1009 |
| decltype(*expression*) | the type as defined below |
|
| 1010 |
|
| 1011 |
|
| 1012 |
When multiple *simple-type-specifiers* are allowed, they can be freely
|
| 1013 |
intermixed with other *decl-specifiers* in any order. It is
|
| 1014 |
-
implementation-defined whether objects of `char` type
|
| 1015 |
-
|
| 1016 |
-
|
| 1017 |
-
to be signed; it is redundant in other contexts.
|
| 1018 |
|
| 1019 |
-
|
|
|
|
| 1020 |
|
| 1021 |
- if `e` is an unparenthesized *id-expression* or an unparenthesized
|
| 1022 |
class member access ([[expr.ref]]), `decltype(e)` is the type of the
|
| 1023 |
entity named by `e`. If there is no such entity, or if `e` names a set
|
| 1024 |
of overloaded functions, the program is ill-formed;
|
|
@@ -1034,16 +989,19 @@ The operand of the `decltype` specifier is an unevaluated operand
|
|
| 1034 |
``` cpp
|
| 1035 |
const int&& foo();
|
| 1036 |
int i;
|
| 1037 |
struct A { double x; };
|
| 1038 |
const A* a = new A();
|
| 1039 |
-
decltype(foo()) x1 =
|
| 1040 |
decltype(i) x2; // type is int
|
| 1041 |
decltype(a->x) x3; // type is double
|
| 1042 |
decltype((a->x)) x4 = x3; // type is const double&
|
| 1043 |
```
|
| 1044 |
|
|
|
|
|
|
|
|
|
|
| 1045 |
in the case where the operand of a *decltype-specifier* is a function
|
| 1046 |
call and the return type of the function is a class type, a special
|
| 1047 |
rule ([[expr.call]]) ensures that the return type is not required to be
|
| 1048 |
complete (as it would be if the call appeared in a sub-expression or
|
| 1049 |
outside of a *decltype-specifier*). In this context, the common purpose
|
|
@@ -1086,11 +1044,12 @@ void r() {
|
|
| 1086 |
#### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
|
| 1087 |
|
| 1088 |
``` bnf
|
| 1089 |
elaborated-type-specifier:
|
| 1090 |
class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
|
| 1091 |
-
class-key
|
|
|
|
| 1092 |
'enum' nested-name-specifierₒₚₜ identifier
|
| 1093 |
```
|
| 1094 |
|
| 1095 |
An *attribute-specifier-seq* shall not appear in an
|
| 1096 |
*elaborated-type-specifier* unless the latter is the sole constituent of
|
|
@@ -1146,68 +1105,94 @@ enum class E { a, b };
|
|
| 1146 |
enum E x = E::a; // OK
|
| 1147 |
```
|
| 1148 |
|
| 1149 |
#### `auto` specifier <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
|
| 1150 |
|
| 1151 |
-
The `auto` *type-specifier*
|
| 1152 |
-
|
| 1153 |
-
|
|
|
|
|
|
|
| 1154 |
|
| 1155 |
-
The
|
| 1156 |
-
*
|
| 1157 |
-
declarator is valid.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1158 |
|
| 1159 |
-
|
| 1160 |
-
|
| 1161 |
-
expression
|
| 1162 |
-
|
| 1163 |
-
|
| 1164 |
-
|
| 1165 |
-
|
| 1166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1167 |
|
| 1168 |
``` cpp
|
| 1169 |
auto x = 5; // OK: x has type int
|
| 1170 |
const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
|
| 1171 |
static auto y = 0.0; // OK: y has type double
|
| 1172 |
auto int r; // error: auto is not a storage-class-specifier
|
|
|
|
|
|
|
|
|
|
| 1173 |
```
|
| 1174 |
|
| 1175 |
-
|
| 1176 |
selection statement ([[stmt.select]]) or an iteration statement (
|
| 1177 |
[[stmt.iter]]), in the in the or of a ([[expr.new]]), in a
|
| 1178 |
*for-range-declaration*, and in declaring a static data member with a
|
| 1179 |
*brace-or-equal-initializer* that appears within the of a class
|
| 1180 |
definition ([[class.static.data]]).
|
| 1181 |
|
| 1182 |
-
A program that uses `auto` in a context not
|
| 1183 |
-
section is ill-formed.
|
| 1184 |
|
| 1185 |
-
|
| 1186 |
-
|
| 1187 |
-
|
| 1188 |
-
|
| 1189 |
-
|
| 1190 |
-
|
| 1191 |
-
*
|
| 1192 |
-
|
| 1193 |
-
|
| 1194 |
-
|
| 1195 |
-
|
| 1196 |
-
|
| 1197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1198 |
|
| 1199 |
``` cpp
|
| 1200 |
auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
|
| 1201 |
auto x2 = { 1, 2.0 }; // error: cannot deduce element type
|
| 1202 |
```
|
| 1203 |
|
| 1204 |
-
If the list of declarators contains more than one declarator, the type
|
| 1205 |
-
of each declared variable is determined as described above. If the type
|
| 1206 |
-
deduced for the template parameter `U` is not the same in each
|
| 1207 |
-
deduction, the program is ill-formed.
|
| 1208 |
-
|
| 1209 |
``` cpp
|
| 1210 |
const auto &i = expr;
|
| 1211 |
```
|
| 1212 |
|
| 1213 |
The type of `i` is the deduced type of the parameter `u` in the call
|
|
@@ -1215,10 +1200,134 @@ The type of `i` is the deduced type of the parameter `u` in the call
|
|
| 1215 |
|
| 1216 |
``` cpp
|
| 1217 |
template <class U> void f(const U& u);
|
| 1218 |
```
|
| 1219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1220 |
## Enumeration declarations <a id="dcl.enum">[[dcl.enum]]</a>
|
| 1221 |
|
| 1222 |
An enumeration is a distinct type ([[basic.compound]]) with named
|
| 1223 |
constants. Its name becomes an *enum-name*, within its scope.
|
| 1224 |
|
|
@@ -1275,11 +1384,22 @@ enumerator:
|
|
| 1275 |
```
|
| 1276 |
|
| 1277 |
The optional *attribute-specifier-seq* in the *enum-head* and the
|
| 1278 |
*opaque-enum-declaration* appertains to the enumeration; the attributes
|
| 1279 |
in that *attribute-specifier-seq* are thereafter considered attributes
|
| 1280 |
-
of the enumeration whenever it is named.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1281 |
|
| 1282 |
The enumeration type declared with an *enum-key* of only `enum` is an
|
| 1283 |
unscoped enumeration, and its *enumerator*s are *unscoped enumerators*.
|
| 1284 |
The *enum-key*s `enum class` and `enum struct` are semantically
|
| 1285 |
equivalent; an enumeration type declared with one of these is a *scoped
|
|
@@ -1322,33 +1442,40 @@ declared directly in the class or namespace to which the
|
|
| 1322 |
by a *using-declaration*), and the *enum-specifier* shall appear in a
|
| 1323 |
namespace enclosing the previous declaration.
|
| 1324 |
|
| 1325 |
Each enumeration defines a type that is different from all other types.
|
| 1326 |
Each enumeration also has an underlying type. The underlying type can be
|
| 1327 |
-
explicitly specified using *enum-base*
|
| 1328 |
-
|
| 1329 |
-
the underlying type is said to be *fixed*.
|
| 1330 |
-
of an *enum-specifier*, each enumerator has
|
| 1331 |
-
If the underlying type is fixed, the type
|
| 1332 |
-
the closing brace is the underlying type and
|
| 1333 |
-
in the *enumerator-definition* shall be a
|
| 1334 |
-
of the underlying type ([[expr.const]])
|
| 1335 |
-
|
| 1336 |
-
|
| 1337 |
-
each enumerator is the type of its initializing value:
|
| 1338 |
|
| 1339 |
-
- If an initializer is specified for an enumerator, the
|
| 1340 |
-
value has the same type as the expression and the
|
| 1341 |
*constant-expression* shall be an integral constant expression (
|
| 1342 |
-
[[expr.const]]).
|
| 1343 |
-
|
| 1344 |
-
|
| 1345 |
-
-
|
| 1346 |
-
|
| 1347 |
-
|
| 1348 |
-
|
| 1349 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1350 |
|
| 1351 |
For an enumeration whose underlying type is not fixed, the underlying
|
| 1352 |
type is an integral type that can represent all the enumerator values
|
| 1353 |
defined in the enumeration. If no integral type can represent all the
|
| 1354 |
enumerator values, the enumeration is ill-formed. It is
|
|
@@ -1370,13 +1497,13 @@ integer. bₘᵢₙ is zero if eₘᵢₙ is non-negative and -(bₘₐₓ+K) ot
|
|
| 1370 |
The size of the smallest bit-field large enough to hold all the values
|
| 1371 |
of the enumeration type is max(M,1) if bₘᵢₙ is zero and M+1 otherwise.
|
| 1372 |
It is possible to define an enumeration that has values not defined by
|
| 1373 |
any of its enumerators. If the *enumerator-list* is empty, the values of
|
| 1374 |
the enumeration are as if the enumeration had a single enumerator with
|
| 1375 |
-
value 0.[^
|
| 1376 |
|
| 1377 |
-
Two enumeration types are layout-compatible if they have the same
|
| 1378 |
*underlying type*.
|
| 1379 |
|
| 1380 |
The value of an enumerator or an object of an unscoped enumeration type
|
| 1381 |
is converted to an integer by integral promotion ([[conv.prom]]).
|
| 1382 |
|
|
@@ -1605,11 +1732,11 @@ An *unnamed-namespace-definition* behaves as if it were replaced by
|
|
| 1605 |
```
|
| 1606 |
|
| 1607 |
where `inline` appears if and only if it appears in the
|
| 1608 |
*unnamed-namespace-definition*, all occurrences of *unique* in a
|
| 1609 |
translation unit are replaced by the same identifier, and this
|
| 1610 |
-
identifier differs from all other identifiers in the entire program.[^
|
| 1611 |
|
| 1612 |
``` cpp
|
| 1613 |
namespace { int i; } // unique ::i
|
| 1614 |
void f() { i++; } // unique ::i++
|
| 1615 |
|
|
@@ -1663,29 +1790,31 @@ namespace R {
|
|
| 1663 |
void Q::V::g() { /* ... */ } // error: R doesn't enclose Q
|
| 1664 |
}
|
| 1665 |
```
|
| 1666 |
|
| 1667 |
Every name first declared in a namespace is a member of that namespace.
|
| 1668 |
-
If a `friend` declaration in a non-local class first declares a class
|
| 1669 |
-
function[^
|
| 1670 |
-
enclosing namespace. The
|
| 1671 |
-
|
| 1672 |
-
[[basic.lookup.
|
| 1673 |
-
|
| 1674 |
-
|
| 1675 |
-
|
| 1676 |
-
|
| 1677 |
-
|
| 1678 |
-
|
| 1679 |
-
|
| 1680 |
-
|
| 1681 |
-
|
| 1682 |
-
|
| 1683 |
-
|
|
|
|
|
|
|
| 1684 |
|
| 1685 |
``` cpp
|
| 1686 |
-
// Assume f and g have not yet been
|
| 1687 |
void h(int);
|
| 1688 |
template <class T> void f2(T);
|
| 1689 |
namespace A {
|
| 1690 |
class X {
|
| 1691 |
friend void f(X); // A::f(X) is a friend
|
|
@@ -1776,12 +1905,12 @@ specified name is so declared; specifying an enumeration name in a
|
|
| 1776 |
*using-declaration* does not declare its enumerators in the
|
| 1777 |
*using-declaration*’s declarative region. If a *using-declaration* names
|
| 1778 |
a constructor ([[class.qual]]), it implicitly declares a set of
|
| 1779 |
constructors in the class in which the *using-declaration* appears (
|
| 1780 |
[[class.inhctor]]); otherwise the name specified in a
|
| 1781 |
-
*using-declaration* is a synonym for
|
| 1782 |
-
|
| 1783 |
|
| 1784 |
Every *using-declaration* is a *declaration* and a *member-declaration*
|
| 1785 |
and so can be used in a class definition.
|
| 1786 |
|
| 1787 |
``` cpp
|
|
@@ -1915,14 +2044,16 @@ struct X : B {
|
|
| 1915 |
using B::i;
|
| 1916 |
using B::i; // error: double member declaration
|
| 1917 |
};
|
| 1918 |
```
|
| 1919 |
|
| 1920 |
-
|
| 1921 |
-
|
| 1922 |
-
*using-declaration*
|
| 1923 |
-
|
|
|
|
|
|
|
| 1924 |
|
| 1925 |
``` cpp
|
| 1926 |
namespace A {
|
| 1927 |
void f(int);
|
| 1928 |
}
|
|
@@ -1984,17 +2115,20 @@ void func() {
|
|
| 1984 |
struct x x1; // x1 has class type B::x
|
| 1985 |
}
|
| 1986 |
```
|
| 1987 |
|
| 1988 |
If a function declaration in namespace scope or block scope has the same
|
| 1989 |
-
name and the same parameter
|
| 1990 |
-
*using-declaration*, and the declarations do not declare
|
| 1991 |
-
function, the program is ill-formed.
|
| 1992 |
-
|
| 1993 |
-
|
| 1994 |
-
|
| 1995 |
-
|
|
|
|
|
|
|
|
|
|
| 1996 |
|
| 1997 |
``` cpp
|
| 1998 |
namespace B {
|
| 1999 |
void f(int);
|
| 2000 |
void f(double);
|
|
@@ -2017,11 +2151,11 @@ void h() {
|
|
| 2017 |
When a *using-declaration* brings names from a base class into a derived
|
| 2018 |
class scope, member functions and member function templates in the
|
| 2019 |
derived class override and/or hide member functions and member function
|
| 2020 |
templates with the same name, parameter-type-list ([[dcl.fct]]),
|
| 2021 |
cv-qualification, and *ref-qualifier* (if any) in a base class (rather
|
| 2022 |
-
than conflicting). For *using-
|
| 2023 |
see [[class.inhctor]].
|
| 2024 |
|
| 2025 |
``` cpp
|
| 2026 |
struct B {
|
| 2027 |
virtual void f(int);
|
|
@@ -2252,11 +2386,11 @@ transitive search is unordered. In particular, the order in which
|
|
| 2252 |
namespaces were considered and the relationships among the namespaces
|
| 2253 |
implied by the *using-directive*s do not cause preference to be given to
|
| 2254 |
any of the declarations found by the search. An ambiguity exists if the
|
| 2255 |
best match finds two functions with the same signature, even if one is
|
| 2256 |
in a namespace reachable through *using-directive*s in the namespace of
|
| 2257 |
-
the other.[^
|
| 2258 |
|
| 2259 |
``` cpp
|
| 2260 |
namespace D {
|
| 2261 |
int d1;
|
| 2262 |
void f(char);
|
|
@@ -2353,16 +2487,16 @@ with external linkage declared within the *linkage-specification*.
|
|
| 2353 |
``` cpp
|
| 2354 |
extern "C" void f1(void(*pf)(int));
|
| 2355 |
// the name f1 and its function type have C language
|
| 2356 |
// linkage; pf is a pointer to a C function
|
| 2357 |
extern "C" typedef void FUNC();
|
| 2358 |
-
FUNC f2; // the name f2 has
|
| 2359 |
// function's type has C language linkage
|
| 2360 |
extern "C" FUNC f3; // the name of function f3 and the function's type
|
| 2361 |
// have C language linkage
|
| 2362 |
-
void (*pf2)(FUNC*); // the name of the variable pf2 has
|
| 2363 |
-
// the type of pf2 is pointer to
|
| 2364 |
// takes one parameter of type pointer to C function
|
| 2365 |
extern "C" {
|
| 2366 |
static void f4(); // the name of the function f4 has
|
| 2367 |
// internal linkage (not C language
|
| 2368 |
// linkage) and the function's type
|
|
@@ -2370,24 +2504,23 @@ extern "C" {
|
|
| 2370 |
}
|
| 2371 |
|
| 2372 |
extern "C" void f5() {
|
| 2373 |
extern void f4(); // OK: Name linkage (internal)
|
| 2374 |
// and function type linkage (C
|
| 2375 |
-
// language linkage)
|
| 2376 |
// previous declaration.
|
| 2377 |
}
|
| 2378 |
|
| 2379 |
extern void f4(); // OK: Name linkage (internal)
|
| 2380 |
// and function type linkage (C
|
| 2381 |
-
// language linkage)
|
| 2382 |
// previous declaration.
|
| 2383 |
-
}
|
| 2384 |
|
| 2385 |
void f6() {
|
| 2386 |
extern void f4(); // OK: Name linkage (internal)
|
| 2387 |
// and function type linkage (C
|
| 2388 |
-
// language linkage)
|
| 2389 |
// previous declaration.
|
| 2390 |
}
|
| 2391 |
```
|
| 2392 |
|
| 2393 |
A C language linkage is ignored in determining the language linkage of
|
|
@@ -2482,13 +2615,13 @@ extern "C" {
|
|
| 2482 |
int i; // definition
|
| 2483 |
}
|
| 2484 |
extern "C" static void g(); // error
|
| 2485 |
```
|
| 2486 |
|
| 2487 |
-
Because the language linkage is part of a function type, when
|
| 2488 |
-
|
| 2489 |
-
refers is considered a C function.
|
| 2490 |
|
| 2491 |
Linkage from C++to objects defined in other languages and to objects
|
| 2492 |
defined in C++from other languages is implementation-defined and
|
| 2493 |
language-dependent. Only where the object layout strategies of two
|
| 2494 |
language implementations are similar enough can such linkage be
|
|
@@ -2513,11 +2646,11 @@ attribute-specifier:
|
|
| 2513 |
```
|
| 2514 |
|
| 2515 |
``` bnf
|
| 2516 |
alignment-specifier:
|
| 2517 |
'alignas (' type-id '...'ₒₚₜ ')'
|
| 2518 |
-
'alignas ('
|
| 2519 |
```
|
| 2520 |
|
| 2521 |
``` bnf
|
| 2522 |
attribute-list:
|
| 2523 |
attributeₒₚₜ
|
|
@@ -2600,18 +2733,18 @@ For an *attribute-token* not specified in this International Standard,
|
|
| 2600 |
the behavior is *implementation-defined*.
|
| 2601 |
|
| 2602 |
Two consecutive left square bracket tokens shall appear only when
|
| 2603 |
introducing an *attribute-specifier*. If two consecutive left square
|
| 2604 |
brackets appear where an *attribute-specifier* is not allowed, the
|
| 2605 |
-
program is ill
|
| 2606 |
production.
|
| 2607 |
|
| 2608 |
``` cpp
|
| 2609 |
int p[10];
|
| 2610 |
void f() {
|
| 2611 |
int x = 42, y[5];
|
| 2612 |
-
int(p[[x] { return x; }()]); // error:
|
| 2613 |
// declarator-id and not a function-style cast of
|
| 2614 |
// an element of p.
|
| 2615 |
y[[] { return 2; }()] = 2; // error even though attributes are not allowed
|
| 2616 |
// in this context.
|
| 2617 |
}
|
|
@@ -2619,20 +2752,24 @@ void f() {
|
|
| 2619 |
|
| 2620 |
### Alignment specifier <a id="dcl.align">[[dcl.align]]</a>
|
| 2621 |
|
| 2622 |
An *alignment-specifier* may be applied to a variable or to a class data
|
| 2623 |
member, but it shall not be applied to a bit-field, a function
|
| 2624 |
-
parameter,
|
| 2625 |
-
|
| 2626 |
-
*alignment-specifier* may also be applied to the declaration
|
| 2627 |
-
|
| 2628 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2629 |
|
| 2630 |
When the *alignment-specifier* is of the form `alignas(`
|
| 2631 |
-
*
|
| 2632 |
|
| 2633 |
-
- the *
|
| 2634 |
- if the constant expression evaluates to a fundamental alignment, the
|
| 2635 |
alignment requirement of the declared entity shall be the specified
|
| 2636 |
fundamental alignment
|
| 2637 |
- if the constant expression evaluates to an extended alignment and the
|
| 2638 |
implementation supports that alignment in the context of the
|
|
@@ -2764,18 +2901,18 @@ int foo_array[10][10];
|
|
| 2764 |
|
| 2765 |
[[carries_dependency]] struct foo* f(int i) {
|
| 2766 |
return foo_head[i].load(memory_order_consume);
|
| 2767 |
}
|
| 2768 |
|
| 2769 |
-
|
| 2770 |
return kill_dependency(foo_array[*x][*y]);
|
| 2771 |
}
|
| 2772 |
|
| 2773 |
/* Translation unit B. */
|
| 2774 |
|
| 2775 |
[[carries_dependency]] struct foo* f(int i);
|
| 2776 |
-
|
| 2777 |
|
| 2778 |
int c = 3;
|
| 2779 |
|
| 2780 |
void h(int i) {
|
| 2781 |
struct foo* p;
|
|
@@ -2790,11 +2927,48 @@ The `carries_dependency` attribute on function `f` means that the return
|
|
| 2790 |
value carries a dependency out of `f`, so that the implementation need
|
| 2791 |
not constrain ordering upon return from `f`. Implementations of `f` and
|
| 2792 |
its caller may choose to preserve dependencies instead of emitting
|
| 2793 |
hardware memory ordering instructions (a.k.a. fences).
|
| 2794 |
|
| 2795 |
-
Function `g`’s second
|
| 2796 |
-
its first
|
| 2797 |
-
carries a dependency into `g`, but its second call does not. The
|
| 2798 |
implementation might need to insert a fence prior to the second call to
|
| 2799 |
`g`.
|
| 2800 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
opaque-enum-declaration
|
| 35 |
```
|
| 36 |
|
| 37 |
``` bnf
|
| 38 |
alias-declaration:
|
| 39 |
+
'using' identifier attribute-specifier-seqₒₚₜ '=' type-id ';'
|
| 40 |
```
|
| 41 |
|
| 42 |
``` bnf
|
| 43 |
simple-declaration:
|
| 44 |
decl-specifier-seqₒₚₜ init-declarator-listₒₚₜ ';'
|
|
|
|
| 106 |
[[dcl.enum]]), that is, when the *decl-specifier-seq* contains either a
|
| 107 |
*class-specifier*, an *elaborated-type-specifier* with a *class-key* (
|
| 108 |
[[class.name]]), or an *enum-specifier*. In these cases and whenever a
|
| 109 |
*class-specifier* or *enum-specifier* is present in the
|
| 110 |
*decl-specifier-seq*, the identifiers in these specifiers are among the
|
| 111 |
+
names being declared by the declaration (as *class-name*s, *enum-name*s,
|
| 112 |
+
or *enumerator*s, depending on the syntax). In such cases, the
|
|
|
|
| 113 |
*decl-specifier-seq* shall introduce one or more names into the program,
|
| 114 |
or shall redeclare a name introduced by a previous declaration.
|
| 115 |
|
| 116 |
``` cpp
|
| 117 |
enum { }; // ill-formed
|
|
|
|
| 260 |
|
| 261 |
The `thread_local` specifier indicates that the named entity has thread
|
| 262 |
storage duration ([[basic.stc.thread]]). It shall be applied only to
|
| 263 |
the names of variables of namespace or block scope and to the names of
|
| 264 |
static data members. When `thread_local` is applied to a variable of
|
| 265 |
+
block scope the *storage-class-specifier* `static` is implied if no
|
| 266 |
+
other *storage-class-specifier* appears in the *decl-specifier-seq*.
|
| 267 |
|
| 268 |
The `static` specifier can be applied only to names of variables and
|
| 269 |
functions and to anonymous unions ([[class.union]]). There can be no
|
| 270 |
`static` function declarations within a block, nor any `static` function
|
| 271 |
parameters. A `static` specifier used in the declaration of a variable
|
|
|
|
| 281 |
functions. The `extern` specifier cannot be used in the declaration of
|
| 282 |
class members or function parameters. For the linkage of a name declared
|
| 283 |
with an `extern` specifier, see [[basic.link]]. The `extern` keyword
|
| 284 |
can also be used in s and s, but it is not a in such contexts.
|
| 285 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
The linkages implied by successive declarations for a given entity shall
|
| 287 |
agree. That is, within a given scope, each declaration declaring the
|
| 288 |
same variable name or the same overloading of a function name shall
|
| 289 |
imply the same linkage. Each function in a given set of overloaded
|
| 290 |
functions can have a different linkage, however.
|
|
|
|
| 548 |
[[class.friend]].
|
| 549 |
|
| 550 |
### The `constexpr` specifier <a id="dcl.constexpr">[[dcl.constexpr]]</a>
|
| 551 |
|
| 552 |
The `constexpr` specifier shall be applied only to the definition of a
|
| 553 |
+
variable or variable template, the declaration of a function or function
|
| 554 |
+
template, or the declaration of a static data member of a literal type (
|
| 555 |
+
[[basic.types]]). If any declaration of a function, function template,
|
| 556 |
+
or variable template has a `constexpr` specifier, then all its
|
| 557 |
+
declarations shall contain the `constexpr` specifier. An explicit
|
| 558 |
+
specialization can differ from the template declaration with respect to
|
| 559 |
+
the `constexpr` specifier. Function parameters cannot be declared
|
| 560 |
+
`constexpr`.
|
| 561 |
|
| 562 |
``` cpp
|
| 563 |
+
constexpr void square(int &x); // OK: declaration
|
| 564 |
constexpr int bufsz = 1024; // OK: definition
|
| 565 |
constexpr struct pixel { // error: pixel is a type
|
| 566 |
int x;
|
| 567 |
int y;
|
| 568 |
constexpr pixel(int); // OK: declaration
|
| 569 |
};
|
| 570 |
constexpr pixel::pixel(int a)
|
| 571 |
+
: x(a), y(x) // OK: definition
|
| 572 |
+
{ square(x); }
|
| 573 |
constexpr pixel small(2); // error: square not defined, so small(2)
|
| 574 |
// not constant~([expr.const]) so constexpr not satisfied
|
| 575 |
|
| 576 |
+
constexpr void square(int &x) { // OK: definition
|
| 577 |
+
x *= x;
|
| 578 |
}
|
| 579 |
constexpr pixel large(4); // OK: square defined
|
| 580 |
int next(constexpr int x) { // error: not for parameters
|
| 581 |
return x + 1;
|
| 582 |
}
|
|
|
|
| 595 |
|
| 596 |
- it shall not be virtual ([[class.virtual]]);
|
| 597 |
- its return type shall be a literal type;
|
| 598 |
- each of its parameter types shall be a literal type;
|
| 599 |
- its *function-body* shall be `= delete`, `= default`, or a
|
| 600 |
+
*compound-statement* that does not contain
|
| 601 |
+
- an *asm-definition*,
|
| 602 |
+
- a `goto` statement,
|
| 603 |
+
- a *try-block*, or
|
| 604 |
+
- a definition of a variable of non-literal type or of static or
|
| 605 |
+
thread storage duration or for which no initialization is performed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 606 |
|
| 607 |
``` cpp
|
| 608 |
constexpr int square(int x)
|
| 609 |
{ return x * x; } // OK
|
| 610 |
constexpr long long_max()
|
| 611 |
{ return 2147483647; } // OK
|
| 612 |
+
constexpr int abs(int x) {
|
| 613 |
+
if (x < 0)
|
| 614 |
+
x = -x;
|
| 615 |
+
return x; // OK
|
| 616 |
+
}
|
| 617 |
+
constexpr int first(int n) {
|
| 618 |
+
static int value = n; // error: variable has static storage duration
|
| 619 |
+
return value;
|
| 620 |
+
}
|
| 621 |
+
constexpr int uninit() {
|
| 622 |
+
int a; // error: variable is uninitialized
|
| 623 |
+
return a;
|
| 624 |
+
}
|
| 625 |
constexpr int prev(int x)
|
| 626 |
+
{ return --x; } // OK
|
| 627 |
+
constexpr int g(int x, int n) { // OK
|
| 628 |
int r = 1;
|
| 629 |
while (--n > 0) r *= x;
|
| 630 |
return r;
|
| 631 |
}
|
| 632 |
```
|
| 633 |
|
| 634 |
+
The definition of a `constexpr` constructor shall satisfy the following
|
|
|
|
|
|
|
| 635 |
constraints:
|
| 636 |
|
| 637 |
- the class shall not have any virtual base classes;
|
| 638 |
+
- each of the parameter types shall be a literal type;
|
| 639 |
- its *function-body* shall not be a *function-try-block*;
|
|
|
|
|
|
|
| 640 |
|
| 641 |
+
In addition, either its *function-body* shall be `= delete`, or it shall
|
| 642 |
+
satisfy the following constraints:
|
| 643 |
|
| 644 |
+
- either its *function-body* shall be `= default`, or the
|
| 645 |
+
*compound-statement* of its *function-body* shall satisfy the
|
| 646 |
+
constraints for a *function-body* of a `constexpr` function;
|
| 647 |
+
- every non-variant non-static data member and base class sub-object
|
| 648 |
+
shall be initialized ([[class.base.init]]);
|
| 649 |
+
- if the class is a union having variant members ([[class.union]]),
|
| 650 |
+
exactly one of them shall be initialized;
|
| 651 |
+
- if the class is a union-like class, but is not a union, for each of
|
| 652 |
+
its anonymous union members having variant members, exactly one of
|
| 653 |
+
them shall be initialized;
|
| 654 |
+
- for a non-delegating constructor, every constructor selected to
|
| 655 |
+
initialize non-static data members and base class sub-objects shall be
|
| 656 |
+
a `constexpr` constructor;
|
| 657 |
+
- for a delegating constructor, the target constructor shall be a
|
| 658 |
+
`constexpr` constructor.
|
|
|
|
|
|
|
|
|
|
| 659 |
|
| 660 |
``` cpp
|
| 661 |
struct Length {
|
| 662 |
+
constexpr explicit Length(int i = 0) : val(i) { }
|
| 663 |
private:
|
| 664 |
int val;
|
| 665 |
};
|
| 666 |
```
|
| 667 |
|
| 668 |
+
For a non-template, non-defaulted `constexpr` function or a
|
| 669 |
+
non-template, non-defaulted, non-inheriting `constexpr` constructor, if
|
| 670 |
+
no argument values exist such that an invocation of the function or
|
| 671 |
+
constructor could be an evaluated subexpression of a core constant
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
expression ([[expr.const]]), the program is ill-formed; no diagnostic
|
| 673 |
+
required.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 674 |
|
| 675 |
``` cpp
|
| 676 |
constexpr int f(bool b)
|
| 677 |
{ return b ? throw 0 : 0; } // OK
|
| 678 |
+
constexpr int f() { return f(true); } // ill-formed, no diagnostic required
|
| 679 |
|
| 680 |
struct B {
|
| 681 |
constexpr B(int x) : i(0) { } // x is unused
|
| 682 |
int i;
|
| 683 |
};
|
|
|
|
| 691 |
```
|
| 692 |
|
| 693 |
If the instantiated template specialization of a `constexpr` function
|
| 694 |
template or member function of a class template would fail to satisfy
|
| 695 |
the requirements for a `constexpr` function or `constexpr` constructor,
|
| 696 |
+
that specialization is still a `constexpr` function or `constexpr`
|
| 697 |
+
constructor, even though a call to such a function cannot appear in a
|
| 698 |
+
constant expression. If no specialization of the template would satisfy
|
| 699 |
+
the requirements for a `constexpr` function or `constexpr` constructor
|
| 700 |
+
when considered as a non-template function or constructor, the template
|
| 701 |
+
is ill-formed; no diagnostic required.
|
| 702 |
|
| 703 |
A call to a `constexpr` function produces the same result as a call to
|
| 704 |
an equivalent non-`constexpr` function in all respects except that a
|
| 705 |
call to a `constexpr` function can appear in a constant expression.
|
| 706 |
|
| 707 |
+
The `constexpr` specifier has no effect on the type of a `constexpr`
|
| 708 |
+
function or a `constexpr` constructor.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 709 |
|
| 710 |
``` cpp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 711 |
constexpr int bar(int x, int y) // OK
|
| 712 |
{ return x + y + x*y; }
|
| 713 |
// ...
|
| 714 |
int bar(int x, int y) // error: redefinition of bar
|
| 715 |
{ return x * 2 + 3 * y; }
|
|
|
|
| 720 |
initialized. If it is initialized by a constructor call, that call shall
|
| 721 |
be a constant expression ([[expr.const]]). Otherwise, or if a
|
| 722 |
`constexpr` specifier is used in a reference declaration, every
|
| 723 |
full-expression that appears in its initializer shall be a constant
|
| 724 |
expression. Each implicit conversion used in converting the initializer
|
| 725 |
+
expressions and each constructor call used for the initialization is
|
| 726 |
+
part of such a full-expression.
|
| 727 |
|
| 728 |
``` cpp
|
| 729 |
struct pixel {
|
| 730 |
int x, y;
|
| 731 |
};
|
|
|
|
| 781 |
or `int`.
|
| 782 |
- `short` or `long` can be combined with `int`.
|
| 783 |
- `long` can be combined with `double`.
|
| 784 |
- `long` can be combined with `long`.
|
| 785 |
|
| 786 |
+
Except in a declaration of a constructor, destructor, or conversion
|
| 787 |
+
function, at least one *type-specifier* that is not a *cv-qualifier*
|
| 788 |
+
shall appear in a complete *type-specifier-seq* or a complete
|
| 789 |
+
*decl-specifier-seq*.[^3] A *type-specifier-seq* shall not define a
|
| 790 |
+
class or enumeration unless it appears in the *type-id* of an
|
| 791 |
*alias-declaration* ([[dcl.typedef]]) that is not the *declaration* of
|
| 792 |
a *template-declaration*.
|
| 793 |
|
| 794 |
*enum-specifier*s, *class-specifier*s, and *typename-specifier*s are
|
| 795 |
+
discussed in [[dcl.enum]], Clause [[class]], and [[temp.res]],
|
| 796 |
+
respectively. The remaining *type-specifier*s are discussed in the rest
|
| 797 |
+
of this section.
|
| 798 |
|
| 799 |
#### The *cv-qualifiers* <a id="dcl.type.cv">[[dcl.type.cv]]</a>
|
| 800 |
|
| 801 |
+
There are two *cv-qualifiers*, `const` and `volatile`. Each
|
| 802 |
+
*cv-qualifier* shall appear at most once in a *cv-qualifier-seq*. If a
|
| 803 |
*cv-qualifier* appears in a *decl-specifier-seq*, the
|
| 804 |
*init-declarator-list* of the declaration shall not be empty.
|
| 805 |
[[basic.type.qualifier]] and [[dcl.fct]] describe how cv-qualifiers
|
| 806 |
affect object and function types. Redundant cv-qualifications are
|
| 807 |
ignored. For example, these could be introduced by typedefs.
|
|
|
|
| 859 |
Y* p = const_cast<Y*>(&y); // cast away const-ness of y
|
| 860 |
p->x.i = 99; // well-formed: mutable member can be modified
|
| 861 |
p->x.j = 99; // undefined: modifies a const member
|
| 862 |
```
|
| 863 |
|
| 864 |
+
What constitutes an access to an object that has volatile-qualified type
|
| 865 |
+
is implementation-defined. If an attempt is made to refer to an object
|
| 866 |
+
defined with a volatile-qualified type through the use of a glvalue with
|
| 867 |
+
a non-volatile-qualified type, the program behavior is undefined.
|
| 868 |
|
| 869 |
`volatile` is a hint to the implementation to avoid aggressive
|
| 870 |
optimization involving the object because the value of the object might
|
| 871 |
+
be changed by means undetectable by an implementation. Furthermore, for
|
| 872 |
+
some implementations, `volatile` might indicate that special hardware
|
| 873 |
+
instructions are required to access the object. See [[intro.execution]]
|
| 874 |
+
for detailed semantics. In general, the semantics of `volatile` are
|
| 875 |
+
intended to be the same in C++as they are in C.
|
| 876 |
|
| 877 |
#### Simple type specifiers <a id="dcl.type.simple">[[dcl.type.simple]]</a>
|
| 878 |
|
| 879 |
The simple type specifiers are
|
| 880 |
|
|
|
|
| 908 |
```
|
| 909 |
|
| 910 |
``` bnf
|
| 911 |
decltype-specifier:
|
| 912 |
'decltype' '(' expression ')'
|
| 913 |
+
'decltype' '(' 'auto' ')'
|
| 914 |
```
|
| 915 |
|
| 916 |
The `auto` specifier is a placeholder for a type to be deduced (
|
| 917 |
[[dcl.spec.auto]]). The other *simple-type-specifier*s specify either a
|
| 918 |
+
previously-declared type, a type determined from an expression, or one
|
| 919 |
+
of the fundamental types ([[basic.fundamental]]). Table
|
| 920 |
+
[[tab:simple.type.specifiers]] summarizes the valid combinations of
|
| 921 |
+
*simple-type-specifier*s and the types they specify.
|
| 922 |
|
| 923 |
**Table: *simple-type-specifier*{s} and the types they specify** <a id="tab:simple.type.specifiers">[tab:simple.type.specifiers]</a>
|
| 924 |
|
| 925 |
| | |
|
| 926 |
| ---------------------- | -------------------------------------- |
|
|
|
|
| 964 |
| decltype(*expression*) | the type as defined below |
|
| 965 |
|
| 966 |
|
| 967 |
When multiple *simple-type-specifiers* are allowed, they can be freely
|
| 968 |
intermixed with other *decl-specifiers* in any order. It is
|
| 969 |
+
implementation-defined whether objects of `char` type are represented as
|
| 970 |
+
signed or unsigned quantities. The `signed` specifier forces `char`
|
| 971 |
+
objects to be signed; it is redundant in other contexts.
|
|
|
|
| 972 |
|
| 973 |
+
For an expression `e`, the type denoted by `decltype(e)` is defined as
|
| 974 |
+
follows:
|
| 975 |
|
| 976 |
- if `e` is an unparenthesized *id-expression* or an unparenthesized
|
| 977 |
class member access ([[expr.ref]]), `decltype(e)` is the type of the
|
| 978 |
entity named by `e`. If there is no such entity, or if `e` names a set
|
| 979 |
of overloaded functions, the program is ill-formed;
|
|
|
|
| 989 |
``` cpp
|
| 990 |
const int&& foo();
|
| 991 |
int i;
|
| 992 |
struct A { double x; };
|
| 993 |
const A* a = new A();
|
| 994 |
+
decltype(foo()) x1 = 0; // type is const int&&
|
| 995 |
decltype(i) x2; // type is int
|
| 996 |
decltype(a->x) x3; // type is double
|
| 997 |
decltype((a->x)) x4 = x3; // type is const double&
|
| 998 |
```
|
| 999 |
|
| 1000 |
+
The rules for determining types involving `decltype(auto)` are specified
|
| 1001 |
+
in [[dcl.spec.auto]].
|
| 1002 |
+
|
| 1003 |
in the case where the operand of a *decltype-specifier* is a function
|
| 1004 |
call and the return type of the function is a class type, a special
|
| 1005 |
rule ([[expr.call]]) ensures that the return type is not required to be
|
| 1006 |
complete (as it would be if the call appeared in a sub-expression or
|
| 1007 |
outside of a *decltype-specifier*). In this context, the common purpose
|
|
|
|
| 1044 |
#### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
|
| 1045 |
|
| 1046 |
``` bnf
|
| 1047 |
elaborated-type-specifier:
|
| 1048 |
class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
|
| 1049 |
+
class-key simple-template-id
|
| 1050 |
+
class-key nested-name-specifier 'template'ₒₚₜ simple-template-id
|
| 1051 |
'enum' nested-name-specifierₒₚₜ identifier
|
| 1052 |
```
|
| 1053 |
|
| 1054 |
An *attribute-specifier-seq* shall not appear in an
|
| 1055 |
*elaborated-type-specifier* unless the latter is the sole constituent of
|
|
|
|
| 1105 |
enum E x = E::a; // OK
|
| 1106 |
```
|
| 1107 |
|
| 1108 |
#### `auto` specifier <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
|
| 1109 |
|
| 1110 |
+
The `auto` and `decltype(auto)` *type-specifier*s designate a
|
| 1111 |
+
placeholder type that will be replaced later, either by deduction from
|
| 1112 |
+
an initializer or by explicit specification with a
|
| 1113 |
+
*trailing-return-type*. The `auto` *type-specifier* is also used to
|
| 1114 |
+
signify that a lambda is a generic lambda.
|
| 1115 |
|
| 1116 |
+
The placeholder type can appear with a function declarator in the
|
| 1117 |
+
*decl-specifier-seq*, *type-specifier-seq*, *conversion-function-id*, or
|
| 1118 |
+
*trailing-return-type*, in any context where such a declarator is valid.
|
| 1119 |
+
If the function declarator includes a *trailing-return-type* (
|
| 1120 |
+
[[dcl.fct]]), that specifies the declared return type of the function.
|
| 1121 |
+
If the declared return type of the function contains a placeholder type,
|
| 1122 |
+
the return type of the function is deduced from `return` statements in
|
| 1123 |
+
the body of the function, if any.
|
| 1124 |
|
| 1125 |
+
If the `auto` *type-specifier* appears as one of the *decl-specifier*s
|
| 1126 |
+
in the *decl-specifier-seq* of a *parameter-declaration* of a
|
| 1127 |
+
*lambda-expression*, the lambda is a *generic lambda* (
|
| 1128 |
+
[[expr.prim.lambda]]).
|
| 1129 |
+
|
| 1130 |
+
``` cpp
|
| 1131 |
+
auto glambda = [](int i, auto a) { return i; }; // OK: a generic lambda
|
| 1132 |
+
```
|
| 1133 |
+
|
| 1134 |
+
The type of a variable declared using `auto` or `decltype(auto)` is
|
| 1135 |
+
deduced from its initializer. This use is allowed when declaring
|
| 1136 |
+
variables in a block ([[stmt.block]]), in namespace scope (
|
| 1137 |
+
[[basic.scope.namespace]]), and in a ([[stmt.for]]). `auto` or
|
| 1138 |
+
`decltype(auto)` shall appear as one of the *decl-specifier*s in the
|
| 1139 |
+
*decl-specifier-seq* and the *decl-specifier-seq* shall be followed by
|
| 1140 |
+
one or more *init-declarator*s, each of which shall have a non-empty
|
| 1141 |
+
*initializer*. In an *initializer* of the form
|
| 1142 |
+
|
| 1143 |
+
``` cpp
|
| 1144 |
+
( expression-list )
|
| 1145 |
+
```
|
| 1146 |
+
|
| 1147 |
+
the *expression-list* shall be a single *assignment-expression*.
|
| 1148 |
|
| 1149 |
``` cpp
|
| 1150 |
auto x = 5; // OK: x has type int
|
| 1151 |
const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
|
| 1152 |
static auto y = 0.0; // OK: y has type double
|
| 1153 |
auto int r; // error: auto is not a storage-class-specifier
|
| 1154 |
+
auto f() -> int; // OK: f returns int
|
| 1155 |
+
auto g() { return 0.0; } // OK: g returns double
|
| 1156 |
+
auto h(); // OK: h's return type will be deduced when it is defined
|
| 1157 |
```
|
| 1158 |
|
| 1159 |
+
A placeholder type can also be used in declaring a variable in the of a
|
| 1160 |
selection statement ([[stmt.select]]) or an iteration statement (
|
| 1161 |
[[stmt.iter]]), in the in the or of a ([[expr.new]]), in a
|
| 1162 |
*for-range-declaration*, and in declaring a static data member with a
|
| 1163 |
*brace-or-equal-initializer* that appears within the of a class
|
| 1164 |
definition ([[class.static.data]]).
|
| 1165 |
|
| 1166 |
+
A program that uses `auto` or `decltype(auto)` in a context not
|
| 1167 |
+
explicitly allowed in this section is ill-formed.
|
| 1168 |
|
| 1169 |
+
When a variable declared using a placeholder type is initialized, or a
|
| 1170 |
+
`return` statement occurs in a function declared with a return type that
|
| 1171 |
+
contains a placeholder type, the deduced return type or variable type is
|
| 1172 |
+
determined from the type of its initializer. In the case of a `return`
|
| 1173 |
+
with no operand, the initializer is considered to be `void()`. Let `T`
|
| 1174 |
+
be the declared type of the variable or return type of the function. If
|
| 1175 |
+
the placeholder is the `auto` *type-specifier*, the deduced type is
|
| 1176 |
+
determined using the rules for template argument deduction. If the
|
| 1177 |
+
deduction is for a `return` statement and the initializer is a
|
| 1178 |
+
*braced-init-list* ([[dcl.init.list]]), the program is ill-formed.
|
| 1179 |
+
Otherwise, obtain `P` from `T` by replacing the occurrences of `auto`
|
| 1180 |
+
with either a new invented type template parameter `U` or, if the
|
| 1181 |
+
initializer is a *braced-init-list*, with `std::initializer_list<U>`.
|
| 1182 |
+
Deduce a value for `U` using the rules of template argument deduction
|
| 1183 |
+
from a function call ([[temp.deduct.call]]), where `P` is a function
|
| 1184 |
+
template parameter type and the initializer is the corresponding
|
| 1185 |
+
argument. If the deduction fails, the declaration is ill-formed.
|
| 1186 |
+
Otherwise, the type deduced for the variable or return type is obtained
|
| 1187 |
+
by substituting the deduced `U` into `P`.
|
| 1188 |
|
| 1189 |
``` cpp
|
| 1190 |
auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
|
| 1191 |
auto x2 = { 1, 2.0 }; // error: cannot deduce element type
|
| 1192 |
```
|
| 1193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1194 |
``` cpp
|
| 1195 |
const auto &i = expr;
|
| 1196 |
```
|
| 1197 |
|
| 1198 |
The type of `i` is the deduced type of the parameter `u` in the call
|
|
|
|
| 1200 |
|
| 1201 |
``` cpp
|
| 1202 |
template <class U> void f(const U& u);
|
| 1203 |
```
|
| 1204 |
|
| 1205 |
+
If the placeholder is the `decltype(auto)` *type-specifier*, the
|
| 1206 |
+
declared type of the variable or return type of the function shall be
|
| 1207 |
+
the placeholder alone. The type deduced for the variable or return type
|
| 1208 |
+
is determined as described in [[dcl.type.simple]], as though the
|
| 1209 |
+
initializer had been the operand of the `decltype`.
|
| 1210 |
+
|
| 1211 |
+
``` cpp
|
| 1212 |
+
int i;
|
| 1213 |
+
int&& f();
|
| 1214 |
+
auto x3a = i; // decltype(x3a) is int
|
| 1215 |
+
decltype(auto) x3d = i; // decltype(x3d) is int
|
| 1216 |
+
auto x4a = (i); // decltype(x4a) is int
|
| 1217 |
+
decltype(auto) x4d = (i); // decltype(x4d) is int&
|
| 1218 |
+
auto x5a = f(); // decltype(x5a) is int
|
| 1219 |
+
decltype(auto) x5d = f(); // decltype(x5d) is int&&
|
| 1220 |
+
auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
|
| 1221 |
+
decltype(auto) x6d = { 1, 2 }; // error, { 1, 2 } is not an expression
|
| 1222 |
+
auto *x7a = &i; // decltype(x7a) is int*
|
| 1223 |
+
decltype(auto)*x7d = &i; // error, declared type is not plain decltype(auto)
|
| 1224 |
+
```
|
| 1225 |
+
|
| 1226 |
+
If the *init-declarator-list* contains more than one *init-declarator*,
|
| 1227 |
+
they shall all form declarations of variables. The type of each declared
|
| 1228 |
+
variable is determined as described above, and if the type that replaces
|
| 1229 |
+
the placeholder type is not the same in each deduction, the program is
|
| 1230 |
+
ill-formed.
|
| 1231 |
+
|
| 1232 |
+
``` cpp
|
| 1233 |
+
auto x = 5, *y = &x; // OK: auto is int
|
| 1234 |
+
auto a = 5, b = { 1, 2 }; // error: different types for auto
|
| 1235 |
+
```
|
| 1236 |
+
|
| 1237 |
+
If a function with a declared return type that contains a placeholder
|
| 1238 |
+
type has multiple `return` statements, the return type is deduced for
|
| 1239 |
+
each `return` statement. If the type deduced is not the same in each
|
| 1240 |
+
deduction, the program is ill-formed.
|
| 1241 |
+
|
| 1242 |
+
If a function with a declared return type that uses a placeholder type
|
| 1243 |
+
has no `return` statements, the return type is deduced as though from a
|
| 1244 |
+
`return` statement with no operand at the closing brace of the function
|
| 1245 |
+
body.
|
| 1246 |
+
|
| 1247 |
+
``` cpp
|
| 1248 |
+
auto f() { } // OK, return type is void
|
| 1249 |
+
auto* g() { } // error, cannot deduce auto* from void()
|
| 1250 |
+
```
|
| 1251 |
+
|
| 1252 |
+
If the type of an entity with an undeduced placeholder type is needed to
|
| 1253 |
+
determine the type of an expression, the program is ill-formed. Once a
|
| 1254 |
+
`return` statement has been seen in a function, however, the return type
|
| 1255 |
+
deduced from that statement can be used in the rest of the function,
|
| 1256 |
+
including in other `return` statements.
|
| 1257 |
+
|
| 1258 |
+
``` cpp
|
| 1259 |
+
auto n = n; // error, n's type is unknown
|
| 1260 |
+
auto f();
|
| 1261 |
+
void g() { &f; } // error, f's return type is unknown
|
| 1262 |
+
auto sum(int i) {
|
| 1263 |
+
if (i == 1)
|
| 1264 |
+
return i; // sum's return type is int
|
| 1265 |
+
else
|
| 1266 |
+
return sum(i-1)+i; // OK, sum's return type has been deduced
|
| 1267 |
+
}
|
| 1268 |
+
```
|
| 1269 |
+
|
| 1270 |
+
Return type deduction for a function template with a placeholder in its
|
| 1271 |
+
declared type occurs when the definition is instantiated even if the
|
| 1272 |
+
function body contains a `return` statement with a non-type-dependent
|
| 1273 |
+
operand. Therefore, any use of a specialization of the function template
|
| 1274 |
+
will cause an implicit instantiation. Any errors that arise from this
|
| 1275 |
+
instantiation are not in the immediate context of the function type and
|
| 1276 |
+
can result in the program being ill-formed.
|
| 1277 |
+
|
| 1278 |
+
``` cpp
|
| 1279 |
+
template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
|
| 1280 |
+
typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type
|
| 1281 |
+
template<class T> auto f(T* t) { return *t; }
|
| 1282 |
+
void g() { int (*p)(int*) = &f; } // instantiates both fs to determine return types,
|
| 1283 |
+
// chooses second
|
| 1284 |
+
```
|
| 1285 |
+
|
| 1286 |
+
Redeclarations or specializations of a function or function template
|
| 1287 |
+
with a declared return type that uses a placeholder type shall also use
|
| 1288 |
+
that placeholder, not a deduced type.
|
| 1289 |
+
|
| 1290 |
+
``` cpp
|
| 1291 |
+
auto f();
|
| 1292 |
+
auto f() { return 42; } // return type is int
|
| 1293 |
+
auto f(); // OK
|
| 1294 |
+
int f(); // error, cannot be overloaded with auto f()
|
| 1295 |
+
decltype(auto) f(); // error, auto and decltype(auto) don't match
|
| 1296 |
+
|
| 1297 |
+
template <typename T> auto g(T t) { return t; } // #1
|
| 1298 |
+
template auto g(int); // OK, return type is int
|
| 1299 |
+
template char g(char); // error, no matching template
|
| 1300 |
+
template<> auto g(double); // OK, forward declaration with unknown return type
|
| 1301 |
+
|
| 1302 |
+
template <class T> T g(T t) { return t; } // OK, not functionally equivalent to #1
|
| 1303 |
+
template char g(char); // OK, now there is a matching template
|
| 1304 |
+
template auto g(float); // still matches #1
|
| 1305 |
+
|
| 1306 |
+
void h() { return g(42); } // error, ambiguous
|
| 1307 |
+
|
| 1308 |
+
template <typename T> struct A {
|
| 1309 |
+
friend T frf(T);
|
| 1310 |
+
};
|
| 1311 |
+
auto frf(int i) { return i; } // not a friend of A<int>
|
| 1312 |
+
```
|
| 1313 |
+
|
| 1314 |
+
A function declared with a return type that uses a placeholder type
|
| 1315 |
+
shall not be `virtual` ([[class.virtual]]).
|
| 1316 |
+
|
| 1317 |
+
An explicit instantiation declaration ([[temp.explicit]]) does not
|
| 1318 |
+
cause the instantiation of an entity declared using a placeholder type,
|
| 1319 |
+
but it also does not prevent that entity from being instantiated as
|
| 1320 |
+
needed to determine its type.
|
| 1321 |
+
|
| 1322 |
+
``` cpp
|
| 1323 |
+
template <typename T> auto f(T t) { return t; }
|
| 1324 |
+
extern template auto f(int); // does not instantiate f<int>
|
| 1325 |
+
int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit
|
| 1326 |
+
// instantiation definition is still required somewhere in the program
|
| 1327 |
+
```
|
| 1328 |
+
|
| 1329 |
## Enumeration declarations <a id="dcl.enum">[[dcl.enum]]</a>
|
| 1330 |
|
| 1331 |
An enumeration is a distinct type ([[basic.compound]]) with named
|
| 1332 |
constants. Its name becomes an *enum-name*, within its scope.
|
| 1333 |
|
|
|
|
| 1384 |
```
|
| 1385 |
|
| 1386 |
The optional *attribute-specifier-seq* in the *enum-head* and the
|
| 1387 |
*opaque-enum-declaration* appertains to the enumeration; the attributes
|
| 1388 |
in that *attribute-specifier-seq* are thereafter considered attributes
|
| 1389 |
+
of the enumeration whenever it is named. A `:` following “`enum`
|
| 1390 |
+
*identifier*” is parsed as part of an *enum-base*. This resolves a
|
| 1391 |
+
potential ambiguity between the declaration of an enumeration with an
|
| 1392 |
+
*enum-base* and the declaration of an unnamed bit-field of enumeration
|
| 1393 |
+
type.
|
| 1394 |
+
|
| 1395 |
+
``` cpp
|
| 1396 |
+
struct S {
|
| 1397 |
+
enum E : int {};
|
| 1398 |
+
enum E : int {}; // error: redeclaration of enumeration
|
| 1399 |
+
};
|
| 1400 |
+
```
|
| 1401 |
|
| 1402 |
The enumeration type declared with an *enum-key* of only `enum` is an
|
| 1403 |
unscoped enumeration, and its *enumerator*s are *unscoped enumerators*.
|
| 1404 |
The *enum-key*s `enum class` and `enum struct` are semantically
|
| 1405 |
equivalent; an enumeration type declared with one of these is a *scoped
|
|
|
|
| 1442 |
by a *using-declaration*), and the *enum-specifier* shall appear in a
|
| 1443 |
namespace enclosing the previous declaration.
|
| 1444 |
|
| 1445 |
Each enumeration defines a type that is different from all other types.
|
| 1446 |
Each enumeration also has an underlying type. The underlying type can be
|
| 1447 |
+
explicitly specified using an *enum-base*. For a scoped enumeration
|
| 1448 |
+
type, the underlying type is `int` if it is not explicitly specified. In
|
| 1449 |
+
both of these cases, the underlying type is said to be *fixed*.
|
| 1450 |
+
Following the closing brace of an *enum-specifier*, each enumerator has
|
| 1451 |
+
the type of its enumeration. If the underlying type is fixed, the type
|
| 1452 |
+
of each enumerator prior to the closing brace is the underlying type and
|
| 1453 |
+
the *constant-expression* in the *enumerator-definition* shall be a
|
| 1454 |
+
converted constant expression of the underlying type ([[expr.const]]).
|
| 1455 |
+
If the underlying type is not fixed, the type of each enumerator prior
|
| 1456 |
+
to the closing brace is determined as follows:
|
|
|
|
| 1457 |
|
| 1458 |
+
- If an initializer is specified for an enumerator, the
|
|
|
|
| 1459 |
*constant-expression* shall be an integral constant expression (
|
| 1460 |
+
[[expr.const]]). If the expression has unscoped enumeration type, the
|
| 1461 |
+
enumerator has the underlying type of that enumeration type, otherwise
|
| 1462 |
+
it has the same type as the expression.
|
| 1463 |
+
- If no initializer is specified for the first enumerator, its type is
|
| 1464 |
+
an unspecified signed integral type.
|
| 1465 |
+
- Otherwise the type of the enumerator is the same as that of the
|
| 1466 |
+
preceding enumerator unless the incremented value is not representable
|
| 1467 |
+
in that type, in which case the type is an unspecified integral type
|
| 1468 |
+
sufficient to contain the incremented value. If no such type exists,
|
| 1469 |
+
the program is ill-formed.
|
| 1470 |
+
|
| 1471 |
+
An enumeration whose underlying type is fixed is an incomplete type from
|
| 1472 |
+
its point of declaration ([[basic.scope.pdecl]]) to immediately after
|
| 1473 |
+
its *enum-base* (if any), at which point it becomes a complete type. An
|
| 1474 |
+
enumeration whose underlying type is not fixed is an incomplete type
|
| 1475 |
+
from its point of declaration to immediately after the closing `}` of
|
| 1476 |
+
its *enum-specifier*, at which point it becomes a complete type.
|
| 1477 |
|
| 1478 |
For an enumeration whose underlying type is not fixed, the underlying
|
| 1479 |
type is an integral type that can represent all the enumerator values
|
| 1480 |
defined in the enumeration. If no integral type can represent all the
|
| 1481 |
enumerator values, the enumeration is ill-formed. It is
|
|
|
|
| 1497 |
The size of the smallest bit-field large enough to hold all the values
|
| 1498 |
of the enumeration type is max(M,1) if bₘᵢₙ is zero and M+1 otherwise.
|
| 1499 |
It is possible to define an enumeration that has values not defined by
|
| 1500 |
any of its enumerators. If the *enumerator-list* is empty, the values of
|
| 1501 |
the enumeration are as if the enumeration had a single enumerator with
|
| 1502 |
+
value 0.[^4]
|
| 1503 |
|
| 1504 |
+
Two enumeration types are *layout-compatible* if they have the same
|
| 1505 |
*underlying type*.
|
| 1506 |
|
| 1507 |
The value of an enumerator or an object of an unscoped enumeration type
|
| 1508 |
is converted to an integer by integral promotion ([[conv.prom]]).
|
| 1509 |
|
|
|
|
| 1732 |
```
|
| 1733 |
|
| 1734 |
where `inline` appears if and only if it appears in the
|
| 1735 |
*unnamed-namespace-definition*, all occurrences of *unique* in a
|
| 1736 |
translation unit are replaced by the same identifier, and this
|
| 1737 |
+
identifier differs from all other identifiers in the entire program.[^5]
|
| 1738 |
|
| 1739 |
``` cpp
|
| 1740 |
namespace { int i; } // unique ::i
|
| 1741 |
void f() { i++; } // unique ::i++
|
| 1742 |
|
|
|
|
| 1790 |
void Q::V::g() { /* ... */ } // error: R doesn't enclose Q
|
| 1791 |
}
|
| 1792 |
```
|
| 1793 |
|
| 1794 |
Every name first declared in a namespace is a member of that namespace.
|
| 1795 |
+
If a `friend` declaration in a non-local class first declares a class,
|
| 1796 |
+
function, class template or function template[^6] the friend is a member
|
| 1797 |
+
of the innermost enclosing namespace. The `friend` declaration does not
|
| 1798 |
+
by itself make the name visible to unqualified lookup (
|
| 1799 |
+
[[basic.lookup.unqual]]) or qualified lookup ([[basic.lookup.qual]]).
|
| 1800 |
+
The name of the friend will be visible in its namespace if a matching
|
| 1801 |
+
declaration is provided at namespace scope (either before or after the
|
| 1802 |
+
class definition granting friendship). If a friend function or function
|
| 1803 |
+
template is called, its name may be found by the name lookup that
|
| 1804 |
+
considers functions from namespaces and classes associated with the
|
| 1805 |
+
types of the function arguments ([[basic.lookup.argdep]]). If the name
|
| 1806 |
+
in a `friend` declaration is neither qualified nor a *template-id* and
|
| 1807 |
+
the declaration is a function or an *elaborated-type-specifier*, the
|
| 1808 |
+
lookup to determine whether the entity has been previously declared
|
| 1809 |
+
shall not consider any scopes outside the innermost enclosing namespace.
|
| 1810 |
+
The other forms of `friend` declarations cannot declare a new member of
|
| 1811 |
+
the innermost enclosing namespace and thus follow the usual lookup
|
| 1812 |
+
rules.
|
| 1813 |
|
| 1814 |
``` cpp
|
| 1815 |
+
// Assume f and g have not yet been declared.
|
| 1816 |
void h(int);
|
| 1817 |
template <class T> void f2(T);
|
| 1818 |
namespace A {
|
| 1819 |
class X {
|
| 1820 |
friend void f(X); // A::f(X) is a friend
|
|
|
|
| 1905 |
*using-declaration* does not declare its enumerators in the
|
| 1906 |
*using-declaration*’s declarative region. If a *using-declaration* names
|
| 1907 |
a constructor ([[class.qual]]), it implicitly declares a set of
|
| 1908 |
constructors in the class in which the *using-declaration* appears (
|
| 1909 |
[[class.inhctor]]); otherwise the name specified in a
|
| 1910 |
+
*using-declaration* is a synonym for a set of declarations in another
|
| 1911 |
+
namespace or class.
|
| 1912 |
|
| 1913 |
Every *using-declaration* is a *declaration* and a *member-declaration*
|
| 1914 |
and so can be used in a class definition.
|
| 1915 |
|
| 1916 |
``` cpp
|
|
|
|
| 2044 |
using B::i;
|
| 2045 |
using B::i; // error: double member declaration
|
| 2046 |
};
|
| 2047 |
```
|
| 2048 |
|
| 2049 |
+
Members added to the namespace after the *using-declaration* are not
|
| 2050 |
+
considered when a use of the name is made. Thus, additional overloads
|
| 2051 |
+
added after the *using-declaration* are ignored, but default function
|
| 2052 |
+
arguments ([[dcl.fct.default]]), default template arguments (
|
| 2053 |
+
[[temp.param]]), and template specializations ([[temp.class.spec]],
|
| 2054 |
+
[[temp.expl.spec]]) are considered.
|
| 2055 |
|
| 2056 |
``` cpp
|
| 2057 |
namespace A {
|
| 2058 |
void f(int);
|
| 2059 |
}
|
|
|
|
| 2115 |
struct x x1; // x1 has class type B::x
|
| 2116 |
}
|
| 2117 |
```
|
| 2118 |
|
| 2119 |
If a function declaration in namespace scope or block scope has the same
|
| 2120 |
+
name and the same parameter-type-list ([[dcl.fct]]) as a function
|
| 2121 |
+
introduced by a *using-declaration*, and the declarations do not declare
|
| 2122 |
+
the same function, the program is ill-formed. If a function template
|
| 2123 |
+
declaration in namespace scope has the same name, parameter-type-list,
|
| 2124 |
+
return type, and template parameter list as a function template
|
| 2125 |
+
introduced by a *using-declaration*, the program is ill-formed. Two
|
| 2126 |
+
*using-declaration*s may introduce functions with the same name and the
|
| 2127 |
+
same parameter-type-list. If, for a call to an unqualified function
|
| 2128 |
+
name, function overload resolution selects the functions introduced by
|
| 2129 |
+
such *using-declaration*s, the function call is ill-formed.
|
| 2130 |
|
| 2131 |
``` cpp
|
| 2132 |
namespace B {
|
| 2133 |
void f(int);
|
| 2134 |
void f(double);
|
|
|
|
| 2151 |
When a *using-declaration* brings names from a base class into a derived
|
| 2152 |
class scope, member functions and member function templates in the
|
| 2153 |
derived class override and/or hide member functions and member function
|
| 2154 |
templates with the same name, parameter-type-list ([[dcl.fct]]),
|
| 2155 |
cv-qualification, and *ref-qualifier* (if any) in a base class (rather
|
| 2156 |
+
than conflicting). For *using-declaration*s that name a constructor,
|
| 2157 |
see [[class.inhctor]].
|
| 2158 |
|
| 2159 |
``` cpp
|
| 2160 |
struct B {
|
| 2161 |
virtual void f(int);
|
|
|
|
| 2386 |
namespaces were considered and the relationships among the namespaces
|
| 2387 |
implied by the *using-directive*s do not cause preference to be given to
|
| 2388 |
any of the declarations found by the search. An ambiguity exists if the
|
| 2389 |
best match finds two functions with the same signature, even if one is
|
| 2390 |
in a namespace reachable through *using-directive*s in the namespace of
|
| 2391 |
+
the other.[^7]
|
| 2392 |
|
| 2393 |
``` cpp
|
| 2394 |
namespace D {
|
| 2395 |
int d1;
|
| 2396 |
void f(char);
|
|
|
|
| 2487 |
``` cpp
|
| 2488 |
extern "C" void f1(void(*pf)(int));
|
| 2489 |
// the name f1 and its function type have C language
|
| 2490 |
// linkage; pf is a pointer to a C function
|
| 2491 |
extern "C" typedef void FUNC();
|
| 2492 |
+
FUNC f2; // the name f2 has C++language linkage and the
|
| 2493 |
// function's type has C language linkage
|
| 2494 |
extern "C" FUNC f3; // the name of function f3 and the function's type
|
| 2495 |
// have C language linkage
|
| 2496 |
+
void (*pf2)(FUNC*); // the name of the variable pf2 has C++linkage and
|
| 2497 |
+
// the type of pf2 is pointer to C++function that
|
| 2498 |
// takes one parameter of type pointer to C function
|
| 2499 |
extern "C" {
|
| 2500 |
static void f4(); // the name of the function f4 has
|
| 2501 |
// internal linkage (not C language
|
| 2502 |
// linkage) and the function's type
|
|
|
|
| 2504 |
}
|
| 2505 |
|
| 2506 |
extern "C" void f5() {
|
| 2507 |
extern void f4(); // OK: Name linkage (internal)
|
| 2508 |
// and function type linkage (C
|
| 2509 |
+
// language linkage) obtained from
|
| 2510 |
// previous declaration.
|
| 2511 |
}
|
| 2512 |
|
| 2513 |
extern void f4(); // OK: Name linkage (internal)
|
| 2514 |
// and function type linkage (C
|
| 2515 |
+
// language linkage) obtained from
|
| 2516 |
// previous declaration.
|
|
|
|
| 2517 |
|
| 2518 |
void f6() {
|
| 2519 |
extern void f4(); // OK: Name linkage (internal)
|
| 2520 |
// and function type linkage (C
|
| 2521 |
+
// language linkage) obtained from
|
| 2522 |
// previous declaration.
|
| 2523 |
}
|
| 2524 |
```
|
| 2525 |
|
| 2526 |
A C language linkage is ignored in determining the language linkage of
|
|
|
|
| 2615 |
int i; // definition
|
| 2616 |
}
|
| 2617 |
extern "C" static void g(); // error
|
| 2618 |
```
|
| 2619 |
|
| 2620 |
+
Because the language linkage is part of a function type, when
|
| 2621 |
+
indirecting through a pointer to C function, the function to which the
|
| 2622 |
+
resulting lvalue refers is considered a C function.
|
| 2623 |
|
| 2624 |
Linkage from C++to objects defined in other languages and to objects
|
| 2625 |
defined in C++from other languages is implementation-defined and
|
| 2626 |
language-dependent. Only where the object layout strategies of two
|
| 2627 |
language implementations are similar enough can such linkage be
|
|
|
|
| 2646 |
```
|
| 2647 |
|
| 2648 |
``` bnf
|
| 2649 |
alignment-specifier:
|
| 2650 |
'alignas (' type-id '...'ₒₚₜ ')'
|
| 2651 |
+
'alignas (' constant-expression '...'ₒₚₜ ')'
|
| 2652 |
```
|
| 2653 |
|
| 2654 |
``` bnf
|
| 2655 |
attribute-list:
|
| 2656 |
attributeₒₚₜ
|
|
|
|
| 2733 |
the behavior is *implementation-defined*.
|
| 2734 |
|
| 2735 |
Two consecutive left square bracket tokens shall appear only when
|
| 2736 |
introducing an *attribute-specifier*. If two consecutive left square
|
| 2737 |
brackets appear where an *attribute-specifier* is not allowed, the
|
| 2738 |
+
program is ill-formed even if the brackets match an alternative grammar
|
| 2739 |
production.
|
| 2740 |
|
| 2741 |
``` cpp
|
| 2742 |
int p[10];
|
| 2743 |
void f() {
|
| 2744 |
int x = 42, y[5];
|
| 2745 |
+
int(p[[x] { return x; }()]); // error: invalid attribute on a nested
|
| 2746 |
// declarator-id and not a function-style cast of
|
| 2747 |
// an element of p.
|
| 2748 |
y[[] { return 2; }()] = 2; // error even though attributes are not allowed
|
| 2749 |
// in this context.
|
| 2750 |
}
|
|
|
|
| 2752 |
|
| 2753 |
### Alignment specifier <a id="dcl.align">[[dcl.align]]</a>
|
| 2754 |
|
| 2755 |
An *alignment-specifier* may be applied to a variable or to a class data
|
| 2756 |
member, but it shall not be applied to a bit-field, a function
|
| 2757 |
+
parameter, an *exception-declaration* ([[except.handle]]), or a
|
| 2758 |
+
variable declared with the `register` storage class specifier. An
|
| 2759 |
+
*alignment-specifier* may also be applied to the declaration or
|
| 2760 |
+
definition of a class (in an *elaborated-type-specifier* (
|
| 2761 |
+
[[dcl.type.elab]]) or *class-head* (Clause [[class]]), respectively)
|
| 2762 |
+
and to the declaration or definition of an enumeration (in an
|
| 2763 |
+
*opaque-enum-declaration* or *enum-head*, respectively ([[dcl.enum]])).
|
| 2764 |
+
An *alignment-specifier* with an ellipsis is a pack expansion (
|
| 2765 |
+
[[temp.variadic]]).
|
| 2766 |
|
| 2767 |
When the *alignment-specifier* is of the form `alignas(`
|
| 2768 |
+
*constant-expression* `)`:
|
| 2769 |
|
| 2770 |
+
- the *constant-expression* shall be an integral constant expression
|
| 2771 |
- if the constant expression evaluates to a fundamental alignment, the
|
| 2772 |
alignment requirement of the declared entity shall be the specified
|
| 2773 |
fundamental alignment
|
| 2774 |
- if the constant expression evaluates to an extended alignment and the
|
| 2775 |
implementation supports that alignment in the context of the
|
|
|
|
| 2901 |
|
| 2902 |
[[carries_dependency]] struct foo* f(int i) {
|
| 2903 |
return foo_head[i].load(memory_order_consume);
|
| 2904 |
}
|
| 2905 |
|
| 2906 |
+
int g(int* x, int* y [[carries_dependency]]) {
|
| 2907 |
return kill_dependency(foo_array[*x][*y]);
|
| 2908 |
}
|
| 2909 |
|
| 2910 |
/* Translation unit B. */
|
| 2911 |
|
| 2912 |
[[carries_dependency]] struct foo* f(int i);
|
| 2913 |
+
int g(int* x, int* y [[carries_dependency]]);
|
| 2914 |
|
| 2915 |
int c = 3;
|
| 2916 |
|
| 2917 |
void h(int i) {
|
| 2918 |
struct foo* p;
|
|
|
|
| 2927 |
value carries a dependency out of `f`, so that the implementation need
|
| 2928 |
not constrain ordering upon return from `f`. Implementations of `f` and
|
| 2929 |
its caller may choose to preserve dependencies instead of emitting
|
| 2930 |
hardware memory ordering instructions (a.k.a. fences).
|
| 2931 |
|
| 2932 |
+
Function `g`’s second parameter has a `carries_dependency` attribute,
|
| 2933 |
+
but its first parameter does not. Therefore, function `h`’s first call
|
| 2934 |
+
to `g` carries a dependency into `g`, but its second call does not. The
|
| 2935 |
implementation might need to insert a fence prior to the second call to
|
| 2936 |
`g`.
|
| 2937 |
|
| 2938 |
+
### Deprecated attribute <a id="dcl.attr.deprecated">[[dcl.attr.deprecated]]</a>
|
| 2939 |
+
|
| 2940 |
+
The *attribute-token* `deprecated` can be used to mark names and
|
| 2941 |
+
entities whose use is still allowed, but is discouraged for some reason.
|
| 2942 |
+
in particular, `deprecated` is appropriate for names and entities that
|
| 2943 |
+
are deemed obsolescent or unsafe. It shall appear at most once in each
|
| 2944 |
+
*attribute-list*. An *attribute-argument-clause* may be present and, if
|
| 2945 |
+
present, it shall have the form:
|
| 2946 |
+
|
| 2947 |
+
``` cpp
|
| 2948 |
+
( string-literal )
|
| 2949 |
+
```
|
| 2950 |
+
|
| 2951 |
+
the *string-literal* in the *attribute-argument-clause* could be used to
|
| 2952 |
+
explain the rationale for deprecation and/or to suggest a replacing
|
| 2953 |
+
entity.
|
| 2954 |
+
|
| 2955 |
+
The attribute may be applied to the declaration of a class, a
|
| 2956 |
+
*typedef-name*, a variable, a non-static data member, a function, an
|
| 2957 |
+
enumeration, or a template specialization.
|
| 2958 |
+
|
| 2959 |
+
A name or entity declared without the `deprecated` attribute can later
|
| 2960 |
+
be re-declared with the attribute and vice-versa. Thus, an entity
|
| 2961 |
+
initially declared without the attribute can be marked as deprecated by
|
| 2962 |
+
a subsequent redeclaration. However, after an entity is marked as
|
| 2963 |
+
deprecated, later redeclarations do not un-deprecate the entity.
|
| 2964 |
+
Redeclarations using different forms of the attribute (with or without
|
| 2965 |
+
the *attribute-argument-clause* or with different
|
| 2966 |
+
*attribute-argument-clause*s) are allowed.
|
| 2967 |
+
|
| 2968 |
+
Implementations may use the `deprecated `attribute to produce a
|
| 2969 |
+
diagnostic message in case the program refers to a name or entity other
|
| 2970 |
+
than to declare it, after a declaration that specifies the attribute.
|
| 2971 |
+
The diagnostic message may include the text provided within the
|
| 2972 |
+
*attribute-argument-clause* of any `deprecated` attribute applied to the
|
| 2973 |
+
name or entity.
|
| 2974 |
+
|