- tmp/tmpng4sxnb2/{from.md → to.md} +356 -276
tmp/tmpng4sxnb2/{from.md → to.md}
RENAMED
|
@@ -25,11 +25,11 @@ modify the type of the specifiers with operators such as `*` (pointer
|
|
| 25 |
to) and `()` (function returning). Initial values can also be specified
|
| 26 |
in a declarator; initializers are discussed in [[dcl.init]] and
|
| 27 |
[[class.init]].
|
| 28 |
|
| 29 |
Each *init-declarator* in a declaration is analyzed separately as if it
|
| 30 |
-
was in a declaration by itself.[^
|
| 31 |
|
| 32 |
Declarators have the syntax
|
| 33 |
|
| 34 |
``` bnf
|
| 35 |
declarator:
|
|
@@ -51,12 +51,12 @@ noptr-declarator:
|
|
| 51 |
'(' ptr-declarator ')'
|
| 52 |
```
|
| 53 |
|
| 54 |
``` bnf
|
| 55 |
parameters-and-qualifiers:
|
| 56 |
-
'(' parameter-declaration-clause ')'
|
| 57 |
-
ref-qualifierₒₚₜ exception-specificationₒₚₜ
|
| 58 |
```
|
| 59 |
|
| 60 |
``` bnf
|
| 61 |
trailing-return-type:
|
| 62 |
'->' trailing-type-specifier-seq abstract-declaratorₒₚₜ
|
|
@@ -88,17 +88,12 @@ ref-qualifier:
|
|
| 88 |
```
|
| 89 |
|
| 90 |
``` bnf
|
| 91 |
declarator-id:
|
| 92 |
'...'ₒₚₜ id-expression
|
| 93 |
-
nested-name-specifierₒₚₜ class-name
|
| 94 |
```
|
| 95 |
|
| 96 |
-
A *class-name* has special meaning in a declaration of the class of that
|
| 97 |
-
name and when qualified by that name using the scope resolution operator
|
| 98 |
-
`::` ([[expr.prim]], [[class.ctor]], [[class.dtor]]).
|
| 99 |
-
|
| 100 |
The optional *attribute-specifier-seq* in a *trailing-return-type*
|
| 101 |
appertains to the indicated return type. The *type-id* in a
|
| 102 |
*trailing-return-type* includes the longest possible sequence of
|
| 103 |
*abstract-declarator*s. This resolves the ambiguous binding of array and
|
| 104 |
function declarators.
|
|
@@ -283,32 +278,26 @@ void h(int *(C[10])); // void h(int *(*_fp)(C _parm[10]));
|
|
| 283 |
|
| 284 |
A list of declarators appears after an optional (Clause [[dcl.dcl]])
|
| 285 |
*decl-specifier-seq* ([[dcl.spec]]). Each declarator contains exactly
|
| 286 |
one *declarator-id*; it names the identifier that is declared. An
|
| 287 |
*unqualified-id* occurring in a *declarator-id* shall be a simple
|
| 288 |
-
*identifier* except for the declaration of some special functions
|
| 289 |
-
[[class.conv]], [[class.dtor]], [[over.oper]]) and for
|
| 290 |
-
of template specializations or partial specializations
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
*declarator-id*
|
| 299 |
-
|
| 300 |
-
qualifier
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
nominated by the *nested-name-specifier* of the *declarator-id*. The
|
| 305 |
-
*nested-name-specifier* of a qualified *declarator-id* shall not begin
|
| 306 |
-
with a *decltype-specifier*. If the qualifier is the global `::` scope
|
| 307 |
-
resolution operator, the *declarator-id* refers to a name declared in
|
| 308 |
-
the global namespace scope. The optional *attribute-specifier-seq*
|
| 309 |
-
following a *declarator-id* appertains to the entity that is declared.
|
| 310 |
|
| 311 |
A `static`, `thread_local`, `extern`, `register`, `mutable`, `friend`,
|
| 312 |
`inline`, `virtual`, or `typedef` specifier applies directly to each
|
| 313 |
*declarator-id* in an *init-declarator-list*; the type specified for
|
| 314 |
each *declarator-id* depends on both the *decl-specifier-seq* and its
|
|
@@ -420,13 +409,15 @@ cv-unqualified pointer later, for example:
|
|
| 420 |
*p = 5; // clobber ci
|
| 421 |
```
|
| 422 |
|
| 423 |
See also [[expr.ass]] and [[dcl.init]].
|
| 424 |
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
|
|
|
|
|
|
| 428 |
|
| 429 |
### References <a id="dcl.ref">[[dcl.ref]]</a>
|
| 430 |
|
| 431 |
In a declaration `T` `D` where `D` has either of the forms
|
| 432 |
|
|
@@ -437,13 +428,13 @@ In a declaration `T` `D` where `D` has either of the forms
|
|
| 437 |
|
| 438 |
and the type of the identifier in the declaration `T` `D1` is “ `T`,”
|
| 439 |
then the type of the identifier of `D` is “ reference to `T`.” The
|
| 440 |
optional *attribute-specifier-seq* appertains to the reference type.
|
| 441 |
Cv-qualified references are ill-formed except when the cv-qualifiers are
|
| 442 |
-
introduced through the use of a typedef
|
| 443 |
-
|
| 444 |
-
are ignored.
|
| 445 |
|
| 446 |
``` cpp
|
| 447 |
typedef int& A;
|
| 448 |
const A aref = 3; // ill-formed; lvalue reference to non-const initialized with rvalue
|
| 449 |
```
|
|
@@ -513,20 +504,20 @@ contains an explicit `extern` specifier ([[dcl.stc]]), is a class
|
|
| 513 |
member ([[class.mem]]) declaration within a class definition, or is the
|
| 514 |
declaration of a parameter or a return type ([[dcl.fct]]); see
|
| 515 |
[[basic.def]]. A reference shall be initialized to refer to a valid
|
| 516 |
object or function. in particular, a null reference cannot exist in a
|
| 517 |
well-defined program, because the only way to create such a reference
|
| 518 |
-
would be to bind it to the “object” obtained by
|
| 519 |
-
pointer, which causes undefined behavior. As described in
|
| 520 |
[[class.bit]], a reference cannot be bound directly to a bit-field.
|
| 521 |
|
| 522 |
-
If a typedef ([[dcl.typedef]]
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
|
| 529 |
``` cpp
|
| 530 |
int i;
|
| 531 |
typedef int& LRI;
|
| 532 |
typedef int&& RRI;
|
|
@@ -540,10 +531,13 @@ RRI&& r5 = 5; // r5 has the type int&&
|
|
| 540 |
|
| 541 |
decltype(r2)& r6 = i; // r6 has the type int&
|
| 542 |
decltype(r2)&& r7 = i; // r7 has the type int&
|
| 543 |
```
|
| 544 |
|
|
|
|
|
|
|
|
|
|
| 545 |
### Pointers to members <a id="dcl.mptr">[[dcl.mptr]]</a>
|
| 546 |
|
| 547 |
In a declaration `T` `D` where `D` has the form
|
| 548 |
|
| 549 |
``` bnf
|
|
@@ -607,24 +601,25 @@ and the type of the identifier in the declaration `T` `D1` is
|
|
| 607 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 608 |
`D` is an array type; if the type of the identifier of `D` contains the
|
| 609 |
`auto` , the program is ill-formed. `T` is called the array *element
|
| 610 |
type*; this type shall not be a reference type, the (possibly
|
| 611 |
cv-qualified) type `void`, a function type or an abstract class type. If
|
| 612 |
-
the *constant-expression* ([[expr.const]]) is present, it shall be
|
| 613 |
-
|
| 614 |
-
The constant expression specifies the *bound* of
|
| 615 |
-
the array. If the value of the constant
|
| 616 |
-
`N` elements numbered `0` to `N-1`, and
|
| 617 |
-
`D` is “ array of `N` `T`”. An object of
|
| 618 |
-
contiguously allocated non-empty set of `N`
|
| 619 |
-
Except as noted below, if the constant
|
| 620 |
-
of the identifier of `D` is “ array of
|
| 621 |
-
incomplete object type. The type “ array of
|
| 622 |
-
from the type “ array of unknown bound of
|
| 623 |
-
Any type of the form “ array of `N` `T`” is
|
| 624 |
-
`T`”, and similarly for “array of unknown
|
| 625 |
-
*attribute-specifier-seq* appertains to the
|
|
|
|
| 626 |
|
| 627 |
``` cpp
|
| 628 |
typedef int A[5], AA[2][3];
|
| 629 |
typedef const A CA; // type is ``array of 5 const int''
|
| 630 |
typedef const AA CAA; // type is ``array of 2 array of 3 const int''
|
|
@@ -753,11 +748,11 @@ and the type of the contained *declarator-id* in the declaration `T`
|
|
| 753 |
“*derived-declarator-type-list* function of
|
| 754 |
(*parameter-declaration-clause*) *cv-qualifier-seq*
|
| 755 |
*ref-qualifier*returning *trailing-return-type*”. The optional
|
| 756 |
*attribute-specifier-seq* appertains to the function type.
|
| 757 |
|
| 758 |
-
A type of either form is a *function type*.[^
|
| 759 |
|
| 760 |
``` bnf
|
| 761 |
parameter-declaration-clause:
|
| 762 |
parameter-declaration-listₒₚₜ ...ₒₚₜ
|
| 763 |
parameter-declaration-list ',' ...
|
|
@@ -783,18 +778,18 @@ appertains to the parameter.
|
|
| 783 |
The *parameter-declaration-clause* determines the arguments that can be
|
| 784 |
specified, and their processing, when the function is called. the
|
| 785 |
*parameter-declaration-clause* is used to convert the arguments
|
| 786 |
specified on the function call; see [[expr.call]]. If the
|
| 787 |
*parameter-declaration-clause* is empty, the function takes no
|
| 788 |
-
arguments.
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
If the *parameter-declaration-clause* terminates with an
|
| 792 |
-
function parameter pack ([[temp.variadic]]), the number
|
| 793 |
-
shall be equal to or greater than the number of parameters
|
| 794 |
-
have a default argument and are not function parameter
|
| 795 |
-
syntactically correct and where “” is not part of an
|
| 796 |
*abstract-declarator*, “” is synonymous with “”. the declaration
|
| 797 |
|
| 798 |
``` cpp
|
| 799 |
int printf(const char*, ...);
|
| 800 |
```
|
|
@@ -828,20 +823,31 @@ presence or absence of the ellipsis or a function parameter pack is the
|
|
| 828 |
function’s *parameter-type-list*. This transformation does not affect
|
| 829 |
the types of the parameters. For example,
|
| 830 |
`int(*)(const int p, decltype(p)*)` and `int(*)(int, const int*)` are
|
| 831 |
identical types.
|
| 832 |
|
| 833 |
-
A *cv-qualifier-seq* or a *ref-qualifier*
|
|
|
|
|
|
|
| 834 |
|
| 835 |
- the function type for a non-static member function,
|
| 836 |
- the function type to which a pointer to member refers,
|
| 837 |
- the top-level function type of a function typedef declaration or
|
| 838 |
*alias-declaration*,
|
| 839 |
- the *type-id* in the default argument of a *type-parameter* (
|
| 840 |
[[temp.param]]), or
|
| 841 |
- the *type-id* of a *template-argument* for a *type-parameter* (
|
| 842 |
-
[[temp.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 843 |
|
| 844 |
The effect of a *cv-qualifier-seq* in a function declarator is not the
|
| 845 |
same as adding cv-qualification on top of the function type. In the
|
| 846 |
latter case, the cv-qualifiers are ignored. a function type that has a
|
| 847 |
*cv-qualifier-seq* is not a cv-qualified type; there are no cv-qualified
|
|
@@ -870,20 +876,21 @@ int fseek(FILE*, long, int);
|
|
| 870 |
declares a function taking three arguments of the specified types, and
|
| 871 |
returning `int` ([[dcl.type]]).
|
| 872 |
|
| 873 |
If the type of a parameter includes a type of the form “pointer to array
|
| 874 |
of unknown bound of `T`” or “reference to array of unknown bound of
|
| 875 |
-
`T`,” the program is ill-formed.[^
|
| 876 |
type of type array or function, although they may have a return type of
|
| 877 |
type pointer or reference to such things. There shall be no arrays of
|
| 878 |
functions, although there can be arrays of pointers to functions.
|
| 879 |
|
| 880 |
Types shall not be defined in return or parameter types. The type of a
|
| 881 |
parameter or the return type for a function definition shall not be an
|
| 882 |
-
incomplete class type (possibly cv-qualified) unless the function
|
| 883 |
-
definition is nested within the
|
| 884 |
-
(including definitions in nested
|
|
|
|
| 885 |
|
| 886 |
A typedef of function type may be used to declare a function but shall
|
| 887 |
not be used to define a function ([[dcl.fct.def]]).
|
| 888 |
|
| 889 |
``` cpp
|
|
@@ -891,34 +898,18 @@ typedef void F();
|
|
| 891 |
F fv; // OK: equivalent to void fv();
|
| 892 |
F fv { } // ill-formed
|
| 893 |
void fv() { } // OK: definition of fv
|
| 894 |
```
|
| 895 |
|
| 896 |
-
A typedef of a function type whose declarator includes a
|
| 897 |
-
*cv-qualifier-seq* shall be used only to declare the function type for a
|
| 898 |
-
non-static member function, to declare the function type to which a
|
| 899 |
-
pointer to member refers, or to declare the top-level function type of
|
| 900 |
-
another function typedef declaration.
|
| 901 |
-
|
| 902 |
-
``` cpp
|
| 903 |
-
typedef int FIC(int) const;
|
| 904 |
-
FIC f; // ill-formed: does not declare a member function
|
| 905 |
-
struct S {
|
| 906 |
-
FIC f; // OK
|
| 907 |
-
};
|
| 908 |
-
FIC S::*pm = &S::f; // OK
|
| 909 |
-
```
|
| 910 |
-
|
| 911 |
An identifier can optionally be provided as a parameter name; if present
|
| 912 |
-
in a function definition ([[dcl.fct.def]]), it names a parameter
|
| 913 |
-
|
| 914 |
-
|
| 915 |
-
|
| 916 |
-
|
| 917 |
-
|
| 918 |
-
|
| 919 |
-
[[basic.scope.proto]]).
|
| 920 |
|
| 921 |
the declaration
|
| 922 |
|
| 923 |
``` cpp
|
| 924 |
int i,
|
|
@@ -952,11 +943,11 @@ IFUNC* fpif(int);
|
|
| 952 |
```
|
| 953 |
|
| 954 |
or
|
| 955 |
|
| 956 |
``` cpp
|
| 957 |
-
auto fpif(int)->int(*)(int)
|
| 958 |
```
|
| 959 |
|
| 960 |
A *trailing-return-type* is most useful for a type that would be more
|
| 961 |
complicated to specify before the *declarator-id*:
|
| 962 |
|
|
@@ -968,10 +959,13 @@ rather than
|
|
| 968 |
|
| 969 |
``` cpp
|
| 970 |
template <class T, class U> decltype((*(T*)0) + (*(U*)0)) add(T t, U u);
|
| 971 |
```
|
| 972 |
|
|
|
|
|
|
|
|
|
|
| 973 |
A *declarator-id* or *abstract-declarator* containing an ellipsis shall
|
| 974 |
only be used in a *parameter-declaration*. Such a
|
| 975 |
*parameter-declaration* is a parameter pack ([[temp.variadic]]). When
|
| 976 |
it is part of a *parameter-declaration-clause*, the parameter pack is a
|
| 977 |
function parameter pack ([[temp.variadic]]). Otherwise, the
|
|
@@ -991,13 +985,13 @@ void g() {
|
|
| 991 |
```
|
| 992 |
|
| 993 |
There is a syntactic ambiguity when an ellipsis occurs at the end of a
|
| 994 |
*parameter-declaration-clause* without a preceding comma. In this case,
|
| 995 |
the ellipsis is parsed as part of the *abstract-declarator* if the type
|
| 996 |
-
of the parameter names a template parameter pack that has not
|
| 997 |
-
expanded; otherwise, it is parsed as part of the
|
| 998 |
-
*parameter-declaration-clause*.[^
|
| 999 |
|
| 1000 |
### Default arguments <a id="dcl.fct.default">[[dcl.fct.default]]</a>
|
| 1001 |
|
| 1002 |
If an *initializer-clause* is specified in a *parameter-declaration*
|
| 1003 |
this *initializer-clause* is used as a default argument. Default
|
|
@@ -1023,11 +1017,11 @@ A default argument shall be specified only in the
|
|
| 1023 |
*parameter-declaration-clause* of a function declaration or in a
|
| 1024 |
*template-parameter* ([[temp.param]]); in the latter case, the
|
| 1025 |
*initializer-clause* shall be an *assignment-expression*. A default
|
| 1026 |
argument shall not be specified for a parameter pack. If it is specified
|
| 1027 |
in a *parameter-declaration-clause*, it shall not occur within a
|
| 1028 |
-
*declarator* or *abstract-declarator* of a *parameter-declaration*.[^
|
| 1029 |
|
| 1030 |
For non-template functions, default arguments can be added in later
|
| 1031 |
declarations of a function in the same scope. Declarations in different
|
| 1032 |
scopes have completely distinct sets of default arguments. That is,
|
| 1033 |
declarations in inner scopes do not acquire default arguments from
|
|
@@ -1066,20 +1060,19 @@ accumulated sets of default arguments at the end of the translation
|
|
| 1066 |
units shall be the same; see [[basic.def.odr]]. If a friend declaration
|
| 1067 |
specifies a default argument expression, that declaration shall be a
|
| 1068 |
definition and shall be the only declaration of the function or function
|
| 1069 |
template in the translation unit.
|
| 1070 |
|
| 1071 |
-
|
| 1072 |
-
|
| 1073 |
-
|
| 1074 |
-
|
| 1075 |
-
the
|
| 1076 |
-
|
| 1077 |
-
|
| 1078 |
-
|
| 1079 |
-
|
| 1080 |
-
with the value `f(2)`:
|
| 1081 |
|
| 1082 |
``` cpp
|
| 1083 |
int a = 1;
|
| 1084 |
int f(int);
|
| 1085 |
int g(int x = f(a)); // default argument: f(::a)
|
|
@@ -1098,13 +1091,16 @@ up as described in [[basic.lookup.unqual]]. Access checking applies to
|
|
| 1098 |
names in default arguments as described in Clause [[class.access]].
|
| 1099 |
|
| 1100 |
Except for member functions of class templates, the default arguments in
|
| 1101 |
a member function definition that appears outside of the class
|
| 1102 |
definition are added to the set of default arguments provided by the
|
| 1103 |
-
member function declaration in the class definition
|
| 1104 |
-
|
| 1105 |
-
|
|
|
|
|
|
|
|
|
|
| 1106 |
|
| 1107 |
``` cpp
|
| 1108 |
class C {
|
| 1109 |
void f(int i = 3);
|
| 1110 |
void g(int i, int j = 99);
|
|
@@ -1133,16 +1129,16 @@ function.
|
|
| 1133 |
class A {
|
| 1134 |
void f(A* p = this) { } // error
|
| 1135 |
};
|
| 1136 |
```
|
| 1137 |
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
names.
|
| 1144 |
|
| 1145 |
``` cpp
|
| 1146 |
int a;
|
| 1147 |
int f(int a, int b = a); // error: parameter a
|
| 1148 |
// used as default argument
|
|
@@ -1279,22 +1275,22 @@ void print(int a, int) {
|
|
| 1279 |
}
|
| 1280 |
```
|
| 1281 |
|
| 1282 |
In the *function-body*, a *function-local predefined variable* denotes a
|
| 1283 |
block-scope object of static storage duration that is implicitly defined
|
| 1284 |
-
(see [[basic.scope.
|
| 1285 |
|
| 1286 |
The function-local predefined variable `__func__` is defined as if a
|
| 1287 |
definition of the form
|
| 1288 |
|
| 1289 |
``` cpp
|
| 1290 |
static const char __func__[] = "function-name";
|
| 1291 |
```
|
| 1292 |
|
| 1293 |
had been provided, where *function-name* is an *implementation-defined*
|
| 1294 |
string. It is unspecified whether such a variable has an address
|
| 1295 |
-
distinct from that of any other object in the program.[^
|
| 1296 |
|
| 1297 |
``` cpp
|
| 1298 |
struct S {
|
| 1299 |
S() : s(__func__) { } // OK
|
| 1300 |
const char* s;
|
|
@@ -1305,11 +1301,11 @@ void f(const char * s = __func__); // error: __func__ is undeclared
|
|
| 1305 |
### Explicitly-defaulted functions <a id="dcl.fct.def.default">[[dcl.fct.def.default]]</a>
|
| 1306 |
|
| 1307 |
A function definition of the form:
|
| 1308 |
|
| 1309 |
``` bnf
|
| 1310 |
-
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator ' = default ;'
|
| 1311 |
```
|
| 1312 |
|
| 1313 |
is called an *explicitly-defaulted* definition. A function that is
|
| 1314 |
explicitly defaulted shall
|
| 1315 |
|
|
@@ -1320,50 +1316,51 @@ explicitly defaulted shall
|
|
| 1320 |
non-const `T`”, where `T` is the name of the member function’s class)
|
| 1321 |
as if it had been implicitly declared, and
|
| 1322 |
- not have default arguments.
|
| 1323 |
|
| 1324 |
An explicitly-defaulted function may be declared `constexpr` only if it
|
| 1325 |
-
would have been implicitly declared as `constexpr`
|
| 1326 |
-
|
| 1327 |
-
[[except.spec]]) with the *exception-specification* on the implicit
|
| 1328 |
-
declaration. If a function is explicitly defaulted on its first
|
| 1329 |
-
declaration,
|
| 1330 |
|
| 1331 |
- it is implicitly considered to be `constexpr` if the implicit
|
| 1332 |
-
declaration would be,
|
| 1333 |
- it is implicitly considered to have the same *exception-specification*
|
| 1334 |
-
as if it had been implicitly declared ([[except.spec]])
|
| 1335 |
-
|
| 1336 |
-
|
| 1337 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1338 |
|
| 1339 |
``` cpp
|
| 1340 |
struct S {
|
| 1341 |
constexpr S() = default; // ill-formed: implicit S() is not constexpr
|
| 1342 |
S(int a = 0) = default; // ill-formed: default argument
|
| 1343 |
void operator=(const S&) = default; // ill-formed: non-matching return type
|
| 1344 |
-
~S() throw(int) = default; //
|
| 1345 |
private:
|
| 1346 |
int i;
|
| 1347 |
S(S&); // OK: private copy constructor
|
| 1348 |
};
|
| 1349 |
S::S(S&) = default; // OK: defines copy constructor
|
| 1350 |
```
|
| 1351 |
|
| 1352 |
Explicitly-defaulted functions and implicitly-declared functions are
|
| 1353 |
collectively called *defaulted* functions, and the implementation shall
|
| 1354 |
provide implicit definitions for them ([[class.ctor]] [[class.dtor]],
|
| 1355 |
-
[[class.copy]]), which might mean defining them as deleted. A
|
| 1356 |
-
|
| 1357 |
-
|
| 1358 |
-
|
| 1359 |
-
|
| 1360 |
-
|
| 1361 |
-
|
| 1362 |
-
|
| 1363 |
-
|
| 1364 |
-
base.
|
| 1365 |
|
| 1366 |
``` cpp
|
| 1367 |
struct trivial {
|
| 1368 |
trivial() = default;
|
| 1369 |
trivial(const trivial&) = default;
|
|
@@ -1382,11 +1379,11 @@ nontrivial1::nontrivial1() = default; // not first declaration
|
|
| 1382 |
### Deleted definitions <a id="dcl.fct.def.delete">[[dcl.fct.def.delete]]</a>
|
| 1383 |
|
| 1384 |
A function definition of the form:
|
| 1385 |
|
| 1386 |
``` bnf
|
| 1387 |
-
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator ' = delete ;'
|
| 1388 |
```
|
| 1389 |
|
| 1390 |
is called a *deleted definition*. A function with a deleted definition
|
| 1391 |
is also called a *deleted function*.
|
| 1392 |
|
|
@@ -1508,15 +1505,18 @@ int c(b);
|
|
| 1508 |
Default arguments are more restricted; see [[dcl.fct.default]].
|
| 1509 |
|
| 1510 |
The order of initialization of variables with static storage duration is
|
| 1511 |
described in [[basic.start]] and [[stmt.dcl]].
|
| 1512 |
|
|
|
|
|
|
|
|
|
|
| 1513 |
To *zero-initialize* an object or reference of type `T` means:
|
| 1514 |
|
| 1515 |
-
- if `T` is a scalar type ([[basic.types]]), the object is
|
| 1516 |
-
value `0` (zero)
|
| 1517 |
-
|
| 1518 |
- if `T` is a (possibly cv-qualified) non-union class type, each
|
| 1519 |
non-static data member and each base-class subobject is
|
| 1520 |
zero-initialized and padding is initialized to zero bits;
|
| 1521 |
- if `T` is a (possibly cv-qualified) union type, the object’s first
|
| 1522 |
non-static named data member is zero-initialized and padding is
|
|
@@ -1525,29 +1525,33 @@ To *zero-initialize* an object or reference of type `T` means:
|
|
| 1525 |
- if `T` is a reference type, no initialization is performed.
|
| 1526 |
|
| 1527 |
To *default-initialize* an object of type `T` means:
|
| 1528 |
|
| 1529 |
- if `T` is a (possibly cv-qualified) class type (Clause [[class]]),
|
| 1530 |
-
the default constructor for `T` is called (and the
|
| 1531 |
-
ill-formed if `T` has no
|
|
|
|
|
|
|
|
|
|
| 1532 |
- if `T` is an array type, each element is default-initialized;
|
| 1533 |
- otherwise, no initialization is performed.
|
| 1534 |
|
| 1535 |
If a program calls for the default initialization of an object of a
|
| 1536 |
const-qualified type `T`, `T` shall be a class type with a user-provided
|
| 1537 |
default constructor.
|
| 1538 |
|
| 1539 |
To *value-initialize* an object of type `T` means:
|
| 1540 |
|
| 1541 |
- if `T` is a (possibly cv-qualified) class type (Clause [[class]])
|
| 1542 |
-
with
|
| 1543 |
-
constructor
|
| 1544 |
-
|
| 1545 |
-
- if `T` is a (possibly cv-qualified)
|
| 1546 |
-
|
| 1547 |
-
|
| 1548 |
-
constructor
|
|
|
|
| 1549 |
- if `T` is an array type, then each element is value-initialized;
|
| 1550 |
- otherwise, the object is zero-initialized.
|
| 1551 |
|
| 1552 |
An object that is value-initialized is deemed to be constructed and thus
|
| 1553 |
subject to provisions of this International Standard applying to
|
|
@@ -1575,14 +1579,48 @@ is not the declaration of an object of class `X`, but the declaration of
|
|
| 1575 |
a function taking no argument and returning an `X`. The form `()` is
|
| 1576 |
permitted in certain other initialization contexts ([[expr.new]],
|
| 1577 |
[[expr.type.conv]], [[class.base.init]]).
|
| 1578 |
|
| 1579 |
If no initializer is specified for an object, the object is
|
| 1580 |
-
default-initialized
|
| 1581 |
-
|
| 1582 |
-
|
| 1583 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1584 |
|
| 1585 |
An initializer for a static member is in the scope of the member’s
|
| 1586 |
class.
|
| 1587 |
|
| 1588 |
``` cpp
|
|
@@ -1696,15 +1734,14 @@ An *initializer-clause* followed by an ellipsis is a pack expansion (
|
|
| 1696 |
[[temp.variadic]]).
|
| 1697 |
|
| 1698 |
### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
|
| 1699 |
|
| 1700 |
An *aggregate* is an array or a class (Clause [[class]]) with no
|
| 1701 |
-
user-provided constructors ([[class.ctor]]), no
|
| 1702 |
-
|
| 1703 |
-
[[class.
|
| 1704 |
-
[[class.
|
| 1705 |
-
virtual functions ([[class.virtual]]).
|
| 1706 |
|
| 1707 |
When an aggregate is initialized by an initializer list, as specified
|
| 1708 |
in [[dcl.init.list]], the elements of the initializer list are taken as
|
| 1709 |
initializers for the members of the aggregate, in increasing subscript
|
| 1710 |
or member order. Each member is copy-initialized from the corresponding
|
|
@@ -1740,11 +1777,11 @@ int x[] = { 1, 3, 5 };
|
|
| 1740 |
```
|
| 1741 |
|
| 1742 |
declares and initializes `x` as a one-dimensional array that has three
|
| 1743 |
elements since no size was specified and there are three initializers.
|
| 1744 |
An empty initializer list `{}` shall not be used as the
|
| 1745 |
-
*initializer-clause * for an array of unknown bound.[^
|
| 1746 |
|
| 1747 |
Static data members and anonymous bit-fields are not considered members
|
| 1748 |
of the class for purposes of aggregate initialization.
|
| 1749 |
|
| 1750 |
``` cpp
|
|
@@ -1771,20 +1808,30 @@ char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
|
|
| 1771 |
|
| 1772 |
is ill-formed.
|
| 1773 |
|
| 1774 |
If there are fewer *initializer-clause*s in the list than there are
|
| 1775 |
members in the aggregate, then each member not explicitly initialized
|
| 1776 |
-
shall be initialized from
|
|
|
|
| 1777 |
[[dcl.init.list]]).
|
| 1778 |
|
| 1779 |
``` cpp
|
| 1780 |
-
struct S { int a; const char* b; int c; };
|
| 1781 |
S ss = { 1, "asdf" };
|
| 1782 |
```
|
| 1783 |
|
| 1784 |
-
initializes `ss.a` with 1, `ss.b` with `"asdf"`,
|
| 1785 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1786 |
|
| 1787 |
If an aggregate class `C` contains a subaggregate member `m` that has no
|
| 1788 |
members for purposes of aggregate initialization, the
|
| 1789 |
*initializer-clause* for `m` shall not be omitted from an
|
| 1790 |
*initializer-list* for an object of type `C` unless the
|
|
@@ -1829,17 +1876,11 @@ float y[4][3] = {
|
|
| 1829 |
```
|
| 1830 |
|
| 1831 |
initializes the first column of `y` (regarded as a two-dimensional
|
| 1832 |
array) and leaves the rest zero.
|
| 1833 |
|
| 1834 |
-
|
| 1835 |
-
|
| 1836 |
-
``` cpp
|
| 1837 |
-
T x = { a };
|
| 1838 |
-
```
|
| 1839 |
-
|
| 1840 |
-
braces can be elided in an *initializer-list* as follows.[^17] If the
|
| 1841 |
*initializer-list* begins with a left brace, then the succeeding
|
| 1842 |
comma-separated list of *initializer-clause*s initializes the members of
|
| 1843 |
a subaggregate; it is erroneous for there to be more
|
| 1844 |
*initializer-clause*s than members. If, however, the *initializer-list*
|
| 1845 |
for a subaggregate does not begin with a left brace, then only enough
|
|
@@ -1928,17 +1969,17 @@ As described above, the braces around the *initializer-clause* for a
|
|
| 1928 |
union member can be omitted if the union is a member of another
|
| 1929 |
aggregate.
|
| 1930 |
|
| 1931 |
### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
|
| 1932 |
|
| 1933 |
-
|
| 1934 |
-
|
| 1935 |
-
|
| 1936 |
-
|
| 1937 |
-
|
| 1938 |
-
characters of the value of the string literal initialize the
|
| 1939 |
-
the array.
|
| 1940 |
|
| 1941 |
``` cpp
|
| 1942 |
char msg[] = "Syntax error on line %s\n";
|
| 1943 |
```
|
| 1944 |
|
|
@@ -1996,33 +2037,30 @@ extern int& r2; // OK
|
|
| 1996 |
```
|
| 1997 |
|
| 1998 |
Given types “ `T1`” and “ `T2`,” “ `T1`” is to “ `T2`” if `T1` is the
|
| 1999 |
same type as `T2`, or `T1` is a base class of `T2`. “ `T1`” is with “
|
| 2000 |
`T2`” if `T1` is reference-related to `T2` and *cv1* is the same
|
| 2001 |
-
cv-qualification as, or greater cv-qualification than, *cv2*.
|
| 2002 |
-
|
| 2003 |
-
|
| 2004 |
-
|
| 2005 |
-
|
| 2006 |
-
|
| 2007 |
-
|
| 2008 |
-
program that necessitates such a binding is ill-formed if `T1` is an
|
| 2009 |
-
inaccessible (Clause [[class.access]]) or ambiguous (
|
| 2010 |
-
[[class.member.lookup]]) base class of `T2`.
|
| 2011 |
|
| 2012 |
A reference to type “*cv1* `T1`” is initialized by an expression of type
|
| 2013 |
“*cv2* `T2`” as follows:
|
| 2014 |
|
| 2015 |
- If the reference is an lvalue reference and the initializer expression
|
| 2016 |
- is an lvalue (but is not a bit-field), and “ `T1`” is
|
| 2017 |
reference-compatible with “ `T2`,” or
|
| 2018 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 2019 |
-
reference-related to `T2`, and can be
|
| 2020 |
-
|
| 2021 |
-
|
| 2022 |
-
|
| 2023 |
-
|
| 2024 |
|
| 2025 |
then the reference is bound to the initializer expression lvalue in
|
| 2026 |
the first case and to the lvalue result of the conversion in the
|
| 2027 |
second case (or, in either case, to the appropriate base class
|
| 2028 |
subobject of the object). The usual lvalue-to-rvalue ([[conv.lval]]),
|
|
@@ -2048,16 +2086,18 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
|
|
| 2048 |
int i = 2;
|
| 2049 |
double& rd3 = i; // error: type mismatch and reference not const
|
| 2050 |
```
|
| 2051 |
|
| 2052 |
- If the initializer expression
|
| 2053 |
-
- is an xvalue, class prvalue, array prvalue
|
| 2054 |
-
“*cv1* `T1`” is reference-compatible with
|
|
|
|
| 2055 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 2056 |
-
reference-related to `T2`, and can be
|
| 2057 |
-
|
| 2058 |
-
|
|
|
|
| 2059 |
|
| 2060 |
then the reference is bound to the value of the initializer
|
| 2061 |
expression in the first case and to the result of the conversion in
|
| 2062 |
the second case (or, in either case, to an appropriate base class
|
| 2063 |
subobject). In the second case, if the reference is an rvalue
|
|
@@ -2079,18 +2119,40 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
|
|
| 2079 |
int&& rri = static_cast<int&&>(i2); // bound directly to i2
|
| 2080 |
B&& rrb = x; // bound directly to the result of operator B
|
| 2081 |
int&& rri2 = X(); // error: lvalue-to-rvalue conversion applied to the
|
| 2082 |
// result of operator int&
|
| 2083 |
```
|
| 2084 |
-
- Otherwise
|
| 2085 |
-
|
| 2086 |
-
|
| 2087 |
-
|
| 2088 |
-
|
| 2089 |
-
|
| 2090 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2091 |
``` cpp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2092 |
const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
|
| 2093 |
double&& rrd = 2; // rrd refers to temporary with value 2.0
|
| 2094 |
const volatile int cvi = 1;
|
| 2095 |
const int& r2 = cvi; // error: type qualifiers dropped
|
| 2096 |
double d2 = 1.0;
|
|
@@ -2119,10 +2181,11 @@ list-initialization in a copy-initialization context is called
|
|
| 2119 |
*copy-list-initialization*. List-initialization can be used
|
| 2120 |
|
| 2121 |
- as the initializer in a variable definition ([[dcl.init]])
|
| 2122 |
- as the initializer in a new expression ([[expr.new]])
|
| 2123 |
- in a return statement ([[stmt.return]])
|
|
|
|
| 2124 |
- as a function argument ([[expr.call]])
|
| 2125 |
- as a subscript ([[expr.sub]])
|
| 2126 |
- as an argument to a constructor invocation ([[dcl.init]],
|
| 2127 |
[[expr.type.conv]])
|
| 2128 |
- as an initializer for a non-static data member ([[class.mem]])
|
|
@@ -2144,22 +2207,24 @@ A constructor is an *initializer-list constructor* if its first
|
|
| 2144 |
parameter is of type `std::initializer_list<E>` or reference to possibly
|
| 2145 |
cv-qualified `std::initializer_list<E>` for some type `E`, and either
|
| 2146 |
there are no other parameters or else all other parameters have default
|
| 2147 |
arguments ([[dcl.fct.default]]). Initializer-list constructors are
|
| 2148 |
favored over other constructors in list-initialization (
|
| 2149 |
-
[[over.match.list]]).
|
| 2150 |
-
|
| 2151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2152 |
type is not named ([[dcl.spec.auto]]) — the program is ill-formed.
|
| 2153 |
|
| 2154 |
List-initialization of an object or reference of type `T` is defined as
|
| 2155 |
follows:
|
| 2156 |
|
| 2157 |
-
- If
|
| 2158 |
-
|
| 2159 |
-
- Otherwise, if `T` is an aggregate, aggregate initialization is
|
| 2160 |
-
performed ([[dcl.init.aggr]]).
|
| 2161 |
``` cpp
|
| 2162 |
double ad[] = { 1, 2.0 }; // OK
|
| 2163 |
int ai[] = { 1, 2.0 }; // error: narrowing
|
| 2164 |
|
| 2165 |
struct S2 {
|
|
@@ -2168,13 +2233,15 @@ follows:
|
|
| 2168 |
};
|
| 2169 |
S2 s21 = { 1, 2, 3.0 }; // OK
|
| 2170 |
S2 s22 { 1.0, 2, 3 }; // error: narrowing
|
| 2171 |
S2 s23 { }; // OK: default to 0,0,0
|
| 2172 |
```
|
| 2173 |
-
- Otherwise, if `T` is a
|
| 2174 |
-
|
| 2175 |
-
|
|
|
|
|
|
|
| 2176 |
initialization of an object from a class of the same type (
|
| 2177 |
[[dcl.init]]).
|
| 2178 |
- Otherwise, if `T` is a class type, constructors are considered. The
|
| 2179 |
applicable constructors are enumerated and the best one is chosen
|
| 2180 |
through overload resolution ([[over.match]], [[over.match.list]]).
|
|
@@ -2208,15 +2275,25 @@ follows:
|
|
| 2208 |
};
|
| 2209 |
S s1 = { 1, 2, 3.0 }; // OK: invoke #1
|
| 2210 |
S s2 { 1.0, 2, 3 }; // error: narrowing
|
| 2211 |
S s3 { }; // OK: invoke #2
|
| 2212 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2213 |
- Otherwise, if `T` is a reference type, a prvalue temporary of the type
|
| 2214 |
-
referenced by `T` is list-initialized
|
| 2215 |
-
|
| 2216 |
-
|
| 2217 |
-
type
|
|
|
|
| 2218 |
``` cpp
|
| 2219 |
struct S {
|
| 2220 |
S(std::initializer_list<double>); // #1
|
| 2221 |
S(const std::string&); // #2
|
| 2222 |
// ...
|
|
@@ -2226,18 +2303,10 @@ follows:
|
|
| 2226 |
S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
|
| 2227 |
const int& i1 = { 1 }; // OK
|
| 2228 |
const int& i2 = { 1.1 }; // error: narrowing
|
| 2229 |
const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
|
| 2230 |
```
|
| 2231 |
-
- Otherwise, if the initializer list has a single element, the object or
|
| 2232 |
-
reference is initialized from that element; if a narrowing conversion
|
| 2233 |
-
(see below) is required to convert the element to `T`, the program is
|
| 2234 |
-
ill-formed.
|
| 2235 |
-
``` cpp
|
| 2236 |
-
int x1 {2}; // OK
|
| 2237 |
-
int x2 {2.0}; // error: narrowing
|
| 2238 |
-
```
|
| 2239 |
- Otherwise, if the initializer list has no elements, the object is
|
| 2240 |
value-initialized.
|
| 2241 |
``` cpp
|
| 2242 |
int** pp {}; // initialized to null pointer
|
| 2243 |
```
|
|
@@ -2272,17 +2341,19 @@ ordering holds regardless of the semantics of the initialization; for
|
|
| 2272 |
example, it applies when the elements of the *initializer-list* are
|
| 2273 |
interpreted as arguments of a constructor call, even though ordinarily
|
| 2274 |
there are no sequencing constraints on the arguments of a call.
|
| 2275 |
|
| 2276 |
An object of type `std::initializer_list<E>` is constructed from an
|
| 2277 |
-
initializer list as if the implementation allocated
|
| 2278 |
-
elements of type `E`, where N is the number of elements in the
|
| 2279 |
initializer list. Each element of that array is copy-initialized with
|
| 2280 |
the corresponding element of the initializer list, and the
|
| 2281 |
`std::initializer_list<E>` object is constructed to refer to that array.
|
| 2282 |
-
|
| 2283 |
-
the
|
|
|
|
|
|
|
| 2284 |
|
| 2285 |
``` cpp
|
| 2286 |
struct X {
|
| 2287 |
X(std::initializer_list<double> v);
|
| 2288 |
};
|
|
@@ -2291,35 +2362,47 @@ X x{ 1,2,3 };
|
|
| 2291 |
|
| 2292 |
The initialization will be implemented in a way roughly equivalent to
|
| 2293 |
this:
|
| 2294 |
|
| 2295 |
``` cpp
|
| 2296 |
-
double __a[3] = {double{1}, double{2}, double{3}};
|
| 2297 |
X x(std::initializer_list<double>(__a, __a+3));
|
| 2298 |
```
|
| 2299 |
|
| 2300 |
assuming that the implementation can construct an `initializer_list`
|
| 2301 |
object with a pair of pointers.
|
| 2302 |
|
| 2303 |
-
The
|
| 2304 |
-
|
|
|
|
|
|
|
| 2305 |
|
| 2306 |
``` cpp
|
| 2307 |
typedef std::complex<double> cmplx;
|
| 2308 |
std::vector<cmplx> v1 = { 1, 2, 3 };
|
| 2309 |
|
| 2310 |
void f() {
|
| 2311 |
std::vector<cmplx> v2{ 1, 2, 3 };
|
| 2312 |
std::initializer_list<int> i3 = { 1, 2, 3 };
|
| 2313 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2314 |
```
|
| 2315 |
|
| 2316 |
-
For `v1` and `v2`, the `initializer_list` object
|
| 2317 |
-
`{ 1, 2, 3 }`
|
| 2318 |
-
|
| 2319 |
-
|
| 2320 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2321 |
|
| 2322 |
A *narrowing conversion* is an implicit conversion
|
| 2323 |
|
| 2324 |
- from a floating-point type to an integer type, or
|
| 2325 |
- from `long double` to `double` or `float`, or from `double` to
|
|
@@ -2330,13 +2413,12 @@ A *narrowing conversion* is an implicit conversion
|
|
| 2330 |
type, except where the source is a constant expression and the actual
|
| 2331 |
value after conversion will fit into the target type and will produce
|
| 2332 |
the original value when converted back to the original type, or
|
| 2333 |
- from an integer type or unscoped enumeration type to an integer type
|
| 2334 |
that cannot represent all the values of the original type, except
|
| 2335 |
-
where the source is a constant expression
|
| 2336 |
-
|
| 2337 |
-
value when converted back to the original type.
|
| 2338 |
|
| 2339 |
As indicated above, such conversions are not allowed at the top level in
|
| 2340 |
list-initializations.
|
| 2341 |
|
| 2342 |
``` cpp
|
|
@@ -2374,12 +2456,13 @@ int a[] =
|
|
| 2374 |
[basic.lookup.udir]: basic.md#basic.lookup.udir
|
| 2375 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 2376 |
[basic.lval]: basic.md#basic.lval
|
| 2377 |
[basic.namespace]: #basic.namespace
|
| 2378 |
[basic.scope]: basic.md#basic.scope
|
| 2379 |
-
[basic.scope.
|
| 2380 |
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
|
|
|
| 2381 |
[basic.scope.proto]: basic.md#basic.scope.proto
|
| 2382 |
[basic.start]: basic.md#basic.start
|
| 2383 |
[basic.start.init]: basic.md#basic.start.init
|
| 2384 |
[basic.stc]: basic.md#basic.stc
|
| 2385 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
|
@@ -2403,11 +2486,10 @@ int a[] =
|
|
| 2403 |
[class.inhctor]: special.md#class.inhctor
|
| 2404 |
[class.init]: special.md#class.init
|
| 2405 |
[class.mem]: class.md#class.mem
|
| 2406 |
[class.member.lookup]: class.md#class.member.lookup
|
| 2407 |
[class.mfct]: class.md#class.mfct
|
| 2408 |
-
[class.mfct.non-static]: class.md#class.mfct.non-static
|
| 2409 |
[class.name]: class.md#class.name
|
| 2410 |
[class.qual]: basic.md#class.qual
|
| 2411 |
[class.static]: class.md#class.static
|
| 2412 |
[class.static.data]: class.md#class.static.data
|
| 2413 |
[class.temporary]: special.md#class.temporary
|
|
@@ -2415,19 +2497,21 @@ int a[] =
|
|
| 2415 |
[class.union]: class.md#class.union
|
| 2416 |
[class.virtual]: class.md#class.virtual
|
| 2417 |
[conv]: conv.md#conv
|
| 2418 |
[conv.array]: conv.md#conv.array
|
| 2419 |
[conv.func]: conv.md#conv.func
|
|
|
|
| 2420 |
[conv.lval]: conv.md#conv.lval
|
| 2421 |
[conv.prom]: conv.md#conv.prom
|
| 2422 |
[conv.ptr]: conv.md#conv.ptr
|
| 2423 |
[dcl.align]: #dcl.align
|
| 2424 |
[dcl.ambig.res]: #dcl.ambig.res
|
| 2425 |
[dcl.array]: #dcl.array
|
| 2426 |
[dcl.asm]: #dcl.asm
|
| 2427 |
[dcl.attr]: #dcl.attr
|
| 2428 |
[dcl.attr.depend]: #dcl.attr.depend
|
|
|
|
| 2429 |
[dcl.attr.grammar]: #dcl.attr.grammar
|
| 2430 |
[dcl.attr.noreturn]: #dcl.attr.noreturn
|
| 2431 |
[dcl.constexpr]: #dcl.constexpr
|
| 2432 |
[dcl.dcl]: #dcl.dcl
|
| 2433 |
[dcl.decl]: #dcl.decl
|
|
@@ -2465,15 +2549,18 @@ int a[] =
|
|
| 2465 |
[except.throw]: except.md#except.throw
|
| 2466 |
[expr]: expr.md#expr
|
| 2467 |
[expr.alignof]: expr.md#expr.alignof
|
| 2468 |
[expr.ass]: expr.md#expr.ass
|
| 2469 |
[expr.call]: expr.md#expr.call
|
|
|
|
|
|
|
|
|
|
| 2470 |
[expr.const]: expr.md#expr.const
|
| 2471 |
[expr.const.cast]: expr.md#expr.const.cast
|
| 2472 |
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 2473 |
[expr.new]: expr.md#expr.new
|
| 2474 |
-
[expr.prim]: expr.md#expr.prim
|
| 2475 |
[expr.ref]: expr.md#expr.ref
|
| 2476 |
[expr.static.cast]: expr.md#expr.static.cast
|
| 2477 |
[expr.sub]: expr.md#expr.sub
|
| 2478 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 2479 |
[expr.unary]: expr.md#expr.unary
|
|
@@ -2484,19 +2571,19 @@ int a[] =
|
|
| 2484 |
[intro.multithread]: intro.md#intro.multithread
|
| 2485 |
[lex.charset]: lex.md#lex.charset
|
| 2486 |
[lex.digraph]: lex.md#lex.digraph
|
| 2487 |
[lex.key]: lex.md#lex.key
|
| 2488 |
[lex.name]: lex.md#lex.name
|
|
|
|
| 2489 |
[namespace.alias]: #namespace.alias
|
| 2490 |
[namespace.def]: #namespace.def
|
| 2491 |
[namespace.memdef]: #namespace.memdef
|
| 2492 |
[namespace.qual]: basic.md#namespace.qual
|
| 2493 |
[namespace.udecl]: #namespace.udecl
|
| 2494 |
[namespace.udir]: #namespace.udir
|
| 2495 |
[namespace.unnamed]: #namespace.unnamed
|
| 2496 |
[over]: over.md#over
|
| 2497 |
-
[over.ics.rank]: over.md#over.ics.rank
|
| 2498 |
[over.match]: over.md#over.match
|
| 2499 |
[over.match.conv]: over.md#over.match.conv
|
| 2500 |
[over.match.copy]: over.md#over.match.copy
|
| 2501 |
[over.match.ctor]: over.md#over.match.ctor
|
| 2502 |
[over.match.list]: over.md#over.match.list
|
|
@@ -2512,11 +2599,10 @@ int a[] =
|
|
| 2512 |
[stmt.select]: stmt.md#stmt.select
|
| 2513 |
[stmt.stmt]: stmt.md#stmt.stmt
|
| 2514 |
[support.runtime]: language.md#support.runtime
|
| 2515 |
[tab:simple.type.specifiers]: #tab:simple.type.specifiers
|
| 2516 |
[temp]: temp.md#temp
|
| 2517 |
-
[temp.arg]: temp.md#temp.arg
|
| 2518 |
[temp.arg.type]: temp.md#temp.arg.type
|
| 2519 |
[temp.class.spec]: temp.md#temp.class.spec
|
| 2520 |
[temp.deduct.call]: temp.md#temp.deduct.call
|
| 2521 |
[temp.dep]: temp.md#temp.dep
|
| 2522 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
|
@@ -2531,39 +2617,35 @@ int a[] =
|
|
| 2531 |
|
| 2532 |
[^1]: The “implicit int” rule of C is no longer supported.
|
| 2533 |
|
| 2534 |
[^2]: The inline keyword has no effect on the linkage of a function.
|
| 2535 |
|
| 2536 |
-
[^3]:
|
| 2537 |
-
conversion ([[conv.lval]]) if the corresponding copy-initialization
|
| 2538 |
-
requires one.
|
| 2539 |
-
|
| 2540 |
-
[^4]: There is no special provision for a *decl-specifier-seq* that
|
| 2541 |
lacks a *type-specifier* or that has a *type-specifier* that only
|
| 2542 |
specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
|
| 2543 |
supported.
|
| 2544 |
|
| 2545 |
-
[^
|
| 2546 |
semantics for the enumeration type. It does not preclude an
|
| 2547 |
expression of enumeration type from having a value that falls
|
| 2548 |
outside this range.
|
| 2549 |
|
| 2550 |
-
[^
|
| 2551 |
linkage, they are effectively qualified by a name unique to their
|
| 2552 |
translation unit and therefore can never be seen from any other
|
| 2553 |
translation unit.
|
| 2554 |
|
| 2555 |
-
[^
|
| 2556 |
unqualified.
|
| 2557 |
|
| 2558 |
-
[^
|
| 2559 |
resolved by considering whether one member hides the other along
|
| 2560 |
some paths ([[class.member.lookup]]). There is no such
|
| 2561 |
disambiguation when considering the set of names found as a result
|
| 2562 |
of following *using-directive*s.
|
| 2563 |
|
| 2564 |
-
[^
|
| 2565 |
the corresponding sequence of declarations each with a single
|
| 2566 |
declarator. That is
|
| 2567 |
|
| 2568 |
`T D1, D2, ... Dn;`
|
| 2569 |
|
|
@@ -2593,42 +2675,40 @@ int a[] =
|
|
| 2593 |
`auto i = 1, j = 2.0; \textrm{// error: deduced types for \tcode{i} and \tcode{j} do not match}`
|
| 2594 |
as opposed to
|
| 2595 |
`auto i = 1; \textrm{// OK: \tcode{i} deduced to have type \tcode{int}}`
|
| 2596 |
`auto j = 2.0; \textrm{// OK: \tcode{j} deduced to have type \tcode{double}}`
|
| 2597 |
|
| 2598 |
-
[^
|
| 2599 |
in function return types.
|
| 2600 |
|
| 2601 |
-
[^
|
| 2602 |
to array of unknown bound of `T`” and where means any sequence of
|
| 2603 |
“pointer to” and “array of” derived declarator types. This exclusion
|
| 2604 |
applies to the parameters of the function, and if a parameter is a
|
| 2605 |
pointer to function or pointer to member function then to its
|
| 2606 |
parameters also, etc.
|
| 2607 |
|
| 2608 |
-
[^
|
| 2609 |
comma (so the ellipsis will be parsed as part of the
|
| 2610 |
*parameter-declaration-clause*) or by introducing a name for the
|
| 2611 |
parameter (so the ellipsis will be parsed as part of the
|
| 2612 |
*declarator-id*).
|
| 2613 |
|
| 2614 |
-
[^
|
| 2615 |
declarations of pointers to functions, references to functions, or
|
| 2616 |
`typedef` declarations.
|
| 2617 |
|
| 2618 |
-
[^
|
| 2619 |
variables with names that are reserved to the implementation (
|
| 2620 |
[[global.names]]). If a predefined variable is not odr-used (
|
| 2621 |
[[basic.def.odr]]), its string value need not be present in the
|
| 2622 |
program image.
|
| 2623 |
|
| 2624 |
-
[^
|
| 2625 |
-
|
| 2626 |
-
|
| 2627 |
|
| 2628 |
-
[^
|
| 2629 |
nonetheless C++does not have zero length arrays.
|
| 2630 |
|
| 2631 |
-
[^
|
| 2632 |
-
|
| 2633 |
-
[^18]: This requires a conversion function ([[class.conv.fct]])
|
| 2634 |
returning a reference type.
|
|
|
|
| 25 |
to) and `()` (function returning). Initial values can also be specified
|
| 26 |
in a declarator; initializers are discussed in [[dcl.init]] and
|
| 27 |
[[class.init]].
|
| 28 |
|
| 29 |
Each *init-declarator* in a declaration is analyzed separately as if it
|
| 30 |
+
was in a declaration by itself.[^8]
|
| 31 |
|
| 32 |
Declarators have the syntax
|
| 33 |
|
| 34 |
``` bnf
|
| 35 |
declarator:
|
|
|
|
| 51 |
'(' ptr-declarator ')'
|
| 52 |
```
|
| 53 |
|
| 54 |
``` bnf
|
| 55 |
parameters-and-qualifiers:
|
| 56 |
+
'(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 57 |
+
ref-qualifierₒₚₜ exception-specificationₒₚₜ attribute-specifier-seqₒₚₜ
|
| 58 |
```
|
| 59 |
|
| 60 |
``` bnf
|
| 61 |
trailing-return-type:
|
| 62 |
'->' trailing-type-specifier-seq abstract-declaratorₒₚₜ
|
|
|
|
| 88 |
```
|
| 89 |
|
| 90 |
``` bnf
|
| 91 |
declarator-id:
|
| 92 |
'...'ₒₚₜ id-expression
|
|
|
|
| 93 |
```
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
The optional *attribute-specifier-seq* in a *trailing-return-type*
|
| 96 |
appertains to the indicated return type. The *type-id* in a
|
| 97 |
*trailing-return-type* includes the longest possible sequence of
|
| 98 |
*abstract-declarator*s. This resolves the ambiguous binding of array and
|
| 99 |
function declarators.
|
|
|
|
| 278 |
|
| 279 |
A list of declarators appears after an optional (Clause [[dcl.dcl]])
|
| 280 |
*decl-specifier-seq* ([[dcl.spec]]). Each declarator contains exactly
|
| 281 |
one *declarator-id*; it names the identifier that is declared. An
|
| 282 |
*unqualified-id* occurring in a *declarator-id* shall be a simple
|
| 283 |
+
*identifier* except for the declaration of some special functions (
|
| 284 |
+
[[class.ctor]], [[class.conv]], [[class.dtor]], [[over.oper]]) and for
|
| 285 |
+
the declaration of template specializations or partial specializations (
|
| 286 |
+
[[temp.spec]]). When the *declarator-id* is qualified, the declaration
|
| 287 |
+
shall refer to a previously declared member of the class or namespace to
|
| 288 |
+
which the qualifier refers (or, in the case of a namespace, of an
|
| 289 |
+
element of the inline namespace set of that namespace (
|
| 290 |
+
[[namespace.def]])) or to a specialization thereof; the member shall not
|
| 291 |
+
merely have been introduced by a *using-declaration* in the scope of the
|
| 292 |
+
class or namespace nominated by the *nested-name-specifier* of the
|
| 293 |
+
*declarator-id*. The *nested-name-specifier* of a qualified
|
| 294 |
+
*declarator-id* shall not begin with a *decltype-specifier*. If the
|
| 295 |
+
qualifier is the global `::` scope resolution operator, the
|
| 296 |
+
*declarator-id* refers to a name declared in the global namespace scope.
|
| 297 |
+
The optional *attribute-specifier-seq* following a *declarator-id*
|
| 298 |
+
appertains to the entity that is declared.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
|
| 300 |
A `static`, `thread_local`, `extern`, `register`, `mutable`, `friend`,
|
| 301 |
`inline`, `virtual`, or `typedef` specifier applies directly to each
|
| 302 |
*declarator-id* in an *init-declarator-list*; the type specified for
|
| 303 |
each *declarator-id* depends on both the *decl-specifier-seq* and its
|
|
|
|
| 409 |
*p = 5; // clobber ci
|
| 410 |
```
|
| 411 |
|
| 412 |
See also [[expr.ass]] and [[dcl.init]].
|
| 413 |
|
| 414 |
+
Forming a pointer to reference type is ill-formed; see [[dcl.ref]].
|
| 415 |
+
Forming a pointer to function type is ill-formed if the function type
|
| 416 |
+
has *cv-qualifier*s or a *ref-qualifier*; see [[dcl.fct]]. Since the
|
| 417 |
+
address of a bit-field ([[class.bit]]) cannot be taken, a pointer can
|
| 418 |
+
never point to a bit-field.
|
| 419 |
|
| 420 |
### References <a id="dcl.ref">[[dcl.ref]]</a>
|
| 421 |
|
| 422 |
In a declaration `T` `D` where `D` has either of the forms
|
| 423 |
|
|
|
|
| 428 |
|
| 429 |
and the type of the identifier in the declaration `T` `D1` is “ `T`,”
|
| 430 |
then the type of the identifier of `D` is “ reference to `T`.” The
|
| 431 |
optional *attribute-specifier-seq* appertains to the reference type.
|
| 432 |
Cv-qualified references are ill-formed except when the cv-qualifiers are
|
| 433 |
+
introduced through the use of a *typedef-name* ([[dcl.typedef]],
|
| 434 |
+
[[temp.param]]) or *decltype-specifier* ([[dcl.type.simple]]), in which
|
| 435 |
+
case the cv-qualifiers are ignored.
|
| 436 |
|
| 437 |
``` cpp
|
| 438 |
typedef int& A;
|
| 439 |
const A aref = 3; // ill-formed; lvalue reference to non-const initialized with rvalue
|
| 440 |
```
|
|
|
|
| 504 |
member ([[class.mem]]) declaration within a class definition, or is the
|
| 505 |
declaration of a parameter or a return type ([[dcl.fct]]); see
|
| 506 |
[[basic.def]]. A reference shall be initialized to refer to a valid
|
| 507 |
object or function. in particular, a null reference cannot exist in a
|
| 508 |
well-defined program, because the only way to create such a reference
|
| 509 |
+
would be to bind it to the “object” obtained by indirection through a
|
| 510 |
+
null pointer, which causes undefined behavior. As described in
|
| 511 |
[[class.bit]], a reference cannot be bound directly to a bit-field.
|
| 512 |
|
| 513 |
+
If a *typedef-name* ([[dcl.typedef]], [[temp.param]]) or a
|
| 514 |
+
*decltype-specifier* ([[dcl.type.simple]]) denotes a type `TR` that is
|
| 515 |
+
a reference to a type `T`, an attempt to create the type “lvalue
|
| 516 |
+
reference to cv `TR`” creates the type “lvalue reference to `T`”, while
|
| 517 |
+
an attempt to create the type “rvalue reference to cv `TR`” creates the
|
| 518 |
+
type `TR`.
|
| 519 |
|
| 520 |
``` cpp
|
| 521 |
int i;
|
| 522 |
typedef int& LRI;
|
| 523 |
typedef int&& RRI;
|
|
|
|
| 531 |
|
| 532 |
decltype(r2)& r6 = i; // r6 has the type int&
|
| 533 |
decltype(r2)&& r7 = i; // r7 has the type int&
|
| 534 |
```
|
| 535 |
|
| 536 |
+
Forming a reference to function type is ill-formed if the function type
|
| 537 |
+
has *cv-qualifier*s or a *ref-qualifier*; see [[dcl.fct]].
|
| 538 |
+
|
| 539 |
### Pointers to members <a id="dcl.mptr">[[dcl.mptr]]</a>
|
| 540 |
|
| 541 |
In a declaration `T` `D` where `D` has the form
|
| 542 |
|
| 543 |
``` bnf
|
|
|
|
| 601 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 602 |
`D` is an array type; if the type of the identifier of `D` contains the
|
| 603 |
`auto` , the program is ill-formed. `T` is called the array *element
|
| 604 |
type*; this type shall not be a reference type, the (possibly
|
| 605 |
cv-qualified) type `void`, a function type or an abstract class type. If
|
| 606 |
+
the *constant-expression* ([[expr.const]]) is present, it shall be a
|
| 607 |
+
converted constant expression of type `std::size_t` and its value shall
|
| 608 |
+
be greater than zero. The constant expression specifies the *bound* of
|
| 609 |
+
(number of elements in) the array. If the value of the constant
|
| 610 |
+
expression is `N`, the array has `N` elements numbered `0` to `N-1`, and
|
| 611 |
+
the type of the identifier of `D` is “ array of `N` `T`”. An object of
|
| 612 |
+
array type contains a contiguously allocated non-empty set of `N`
|
| 613 |
+
subobjects of type `T`. Except as noted below, if the constant
|
| 614 |
+
expression is omitted, the type of the identifier of `D` is “ array of
|
| 615 |
+
unknown bound of `T`”, an incomplete object type. The type “ array of
|
| 616 |
+
`N` `T`” is a different type from the type “ array of unknown bound of
|
| 617 |
+
`T`”, see [[basic.types]]. Any type of the form “ array of `N` `T`” is
|
| 618 |
+
adjusted to “array of `N` `T`”, and similarly for “array of unknown
|
| 619 |
+
bound of `T`”. The optional *attribute-specifier-seq* appertains to the
|
| 620 |
+
array.
|
| 621 |
|
| 622 |
``` cpp
|
| 623 |
typedef int A[5], AA[2][3];
|
| 624 |
typedef const A CA; // type is ``array of 5 const int''
|
| 625 |
typedef const AA CAA; // type is ``array of 2 array of 3 const int''
|
|
|
|
| 748 |
“*derived-declarator-type-list* function of
|
| 749 |
(*parameter-declaration-clause*) *cv-qualifier-seq*
|
| 750 |
*ref-qualifier*returning *trailing-return-type*”. The optional
|
| 751 |
*attribute-specifier-seq* appertains to the function type.
|
| 752 |
|
| 753 |
+
A type of either form is a *function type*.[^9]
|
| 754 |
|
| 755 |
``` bnf
|
| 756 |
parameter-declaration-clause:
|
| 757 |
parameter-declaration-listₒₚₜ ...ₒₚₜ
|
| 758 |
parameter-declaration-list ',' ...
|
|
|
|
| 778 |
The *parameter-declaration-clause* determines the arguments that can be
|
| 779 |
specified, and their processing, when the function is called. the
|
| 780 |
*parameter-declaration-clause* is used to convert the arguments
|
| 781 |
specified on the function call; see [[expr.call]]. If the
|
| 782 |
*parameter-declaration-clause* is empty, the function takes no
|
| 783 |
+
arguments. A parameter list consisting of a single unnamed parameter of
|
| 784 |
+
non-dependent type `void` is equivalent to an empty parameter list.
|
| 785 |
+
Except for this special case, a parameter shall not have type *cv*
|
| 786 |
+
`void`. If the *parameter-declaration-clause* terminates with an
|
| 787 |
+
ellipsis or a function parameter pack ([[temp.variadic]]), the number
|
| 788 |
+
of arguments shall be equal to or greater than the number of parameters
|
| 789 |
+
that do not have a default argument and are not function parameter
|
| 790 |
+
packs. Where syntactically correct and where “” is not part of an
|
| 791 |
*abstract-declarator*, “” is synonymous with “”. the declaration
|
| 792 |
|
| 793 |
``` cpp
|
| 794 |
int printf(const char*, ...);
|
| 795 |
```
|
|
|
|
| 823 |
function’s *parameter-type-list*. This transformation does not affect
|
| 824 |
the types of the parameters. For example,
|
| 825 |
`int(*)(const int p, decltype(p)*)` and `int(*)(int, const int*)` are
|
| 826 |
identical types.
|
| 827 |
|
| 828 |
+
A function type with a *cv-qualifier-seq* or a *ref-qualifier*
|
| 829 |
+
(including a type named by *typedef-name* ([[dcl.typedef]],
|
| 830 |
+
[[temp.param]])) shall appear only as:
|
| 831 |
|
| 832 |
- the function type for a non-static member function,
|
| 833 |
- the function type to which a pointer to member refers,
|
| 834 |
- the top-level function type of a function typedef declaration or
|
| 835 |
*alias-declaration*,
|
| 836 |
- the *type-id* in the default argument of a *type-parameter* (
|
| 837 |
[[temp.param]]), or
|
| 838 |
- the *type-id* of a *template-argument* for a *type-parameter* (
|
| 839 |
+
[[temp.arg.type]]).
|
| 840 |
+
|
| 841 |
+
``` cpp
|
| 842 |
+
typedef int FIC(int) const;
|
| 843 |
+
FIC f; // ill-formed: does not declare a member function
|
| 844 |
+
struct S {
|
| 845 |
+
FIC f; // OK
|
| 846 |
+
};
|
| 847 |
+
FIC S::*pm = &S::f; // OK
|
| 848 |
+
```
|
| 849 |
|
| 850 |
The effect of a *cv-qualifier-seq* in a function declarator is not the
|
| 851 |
same as adding cv-qualification on top of the function type. In the
|
| 852 |
latter case, the cv-qualifiers are ignored. a function type that has a
|
| 853 |
*cv-qualifier-seq* is not a cv-qualified type; there are no cv-qualified
|
|
|
|
| 876 |
declares a function taking three arguments of the specified types, and
|
| 877 |
returning `int` ([[dcl.type]]).
|
| 878 |
|
| 879 |
If the type of a parameter includes a type of the form “pointer to array
|
| 880 |
of unknown bound of `T`” or “reference to array of unknown bound of
|
| 881 |
+
`T`,” the program is ill-formed.[^10] Functions shall not have a return
|
| 882 |
type of type array or function, although they may have a return type of
|
| 883 |
type pointer or reference to such things. There shall be no arrays of
|
| 884 |
functions, although there can be arrays of pointers to functions.
|
| 885 |
|
| 886 |
Types shall not be defined in return or parameter types. The type of a
|
| 887 |
parameter or the return type for a function definition shall not be an
|
| 888 |
+
incomplete class type (possibly cv-qualified) unless the function is
|
| 889 |
+
deleted ([[dcl.fct.def.delete]]) or the definition is nested within the
|
| 890 |
+
*member-specification* for that class (including definitions in nested
|
| 891 |
+
classes defined within the class).
|
| 892 |
|
| 893 |
A typedef of function type may be used to declare a function but shall
|
| 894 |
not be used to define a function ([[dcl.fct.def]]).
|
| 895 |
|
| 896 |
``` cpp
|
|
|
|
| 898 |
F fv; // OK: equivalent to void fv();
|
| 899 |
F fv { } // ill-formed
|
| 900 |
void fv() { } // OK: definition of fv
|
| 901 |
```
|
| 902 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 903 |
An identifier can optionally be provided as a parameter name; if present
|
| 904 |
+
in a function definition ([[dcl.fct.def]]), it names a parameter. In
|
| 905 |
+
particular, parameter names are also optional in function definitions
|
| 906 |
+
and names used for a parameter in different declarations and the
|
| 907 |
+
definition of a function need not be the same. If a parameter name is
|
| 908 |
+
present in a function declaration that is not a definition, it cannot be
|
| 909 |
+
used outside of its function declarator because that is the extent of
|
| 910 |
+
its potential scope ([[basic.scope.proto]]).
|
|
|
|
| 911 |
|
| 912 |
the declaration
|
| 913 |
|
| 914 |
``` cpp
|
| 915 |
int i,
|
|
|
|
| 943 |
```
|
| 944 |
|
| 945 |
or
|
| 946 |
|
| 947 |
``` cpp
|
| 948 |
+
auto fpif(int)->int(*)(int);
|
| 949 |
```
|
| 950 |
|
| 951 |
A *trailing-return-type* is most useful for a type that would be more
|
| 952 |
complicated to specify before the *declarator-id*:
|
| 953 |
|
|
|
|
| 959 |
|
| 960 |
``` cpp
|
| 961 |
template <class T, class U> decltype((*(T*)0) + (*(U*)0)) add(T t, U u);
|
| 962 |
```
|
| 963 |
|
| 964 |
+
A *non-template function* is a function that is not a function template
|
| 965 |
+
specialization. A function template is not a function.
|
| 966 |
+
|
| 967 |
A *declarator-id* or *abstract-declarator* containing an ellipsis shall
|
| 968 |
only be used in a *parameter-declaration*. Such a
|
| 969 |
*parameter-declaration* is a parameter pack ([[temp.variadic]]). When
|
| 970 |
it is part of a *parameter-declaration-clause*, the parameter pack is a
|
| 971 |
function parameter pack ([[temp.variadic]]). Otherwise, the
|
|
|
|
| 985 |
```
|
| 986 |
|
| 987 |
There is a syntactic ambiguity when an ellipsis occurs at the end of a
|
| 988 |
*parameter-declaration-clause* without a preceding comma. In this case,
|
| 989 |
the ellipsis is parsed as part of the *abstract-declarator* if the type
|
| 990 |
+
of the parameter either names a template parameter pack that has not
|
| 991 |
+
been expanded or contains `auto`; otherwise, it is parsed as part of the
|
| 992 |
+
*parameter-declaration-clause*.[^11]
|
| 993 |
|
| 994 |
### Default arguments <a id="dcl.fct.default">[[dcl.fct.default]]</a>
|
| 995 |
|
| 996 |
If an *initializer-clause* is specified in a *parameter-declaration*
|
| 997 |
this *initializer-clause* is used as a default argument. Default
|
|
|
|
| 1017 |
*parameter-declaration-clause* of a function declaration or in a
|
| 1018 |
*template-parameter* ([[temp.param]]); in the latter case, the
|
| 1019 |
*initializer-clause* shall be an *assignment-expression*. A default
|
| 1020 |
argument shall not be specified for a parameter pack. If it is specified
|
| 1021 |
in a *parameter-declaration-clause*, it shall not occur within a
|
| 1022 |
+
*declarator* or *abstract-declarator* of a *parameter-declaration*.[^12]
|
| 1023 |
|
| 1024 |
For non-template functions, default arguments can be added in later
|
| 1025 |
declarations of a function in the same scope. Declarations in different
|
| 1026 |
scopes have completely distinct sets of default arguments. That is,
|
| 1027 |
declarations in inner scopes do not acquire default arguments from
|
|
|
|
| 1060 |
units shall be the same; see [[basic.def.odr]]. If a friend declaration
|
| 1061 |
specifies a default argument expression, that declaration shall be a
|
| 1062 |
definition and shall be the only declaration of the function or function
|
| 1063 |
template in the translation unit.
|
| 1064 |
|
| 1065 |
+
The default argument has the same semantic constraints as the
|
| 1066 |
+
initializer in a declaration of a variable of the parameter type, using
|
| 1067 |
+
the copy-initialization semantics ([[dcl.init]]). The names in the
|
| 1068 |
+
default argument are bound, and the semantic constraints are checked, at
|
| 1069 |
+
the point where the default argument appears. Name lookup and checking
|
| 1070 |
+
of semantic constraints for default arguments in function templates and
|
| 1071 |
+
in member functions of class templates are performed as described in
|
| 1072 |
+
[[temp.inst]]. in the following code, `g` will be called with the value
|
| 1073 |
+
`f(2)`:
|
|
|
|
| 1074 |
|
| 1075 |
``` cpp
|
| 1076 |
int a = 1;
|
| 1077 |
int f(int);
|
| 1078 |
int g(int x = f(a)); // default argument: f(::a)
|
|
|
|
| 1091 |
names in default arguments as described in Clause [[class.access]].
|
| 1092 |
|
| 1093 |
Except for member functions of class templates, the default arguments in
|
| 1094 |
a member function definition that appears outside of the class
|
| 1095 |
definition are added to the set of default arguments provided by the
|
| 1096 |
+
member function declaration in the class definition; the program is
|
| 1097 |
+
ill-formed if a default constructor ([[class.ctor]]), copy or move
|
| 1098 |
+
constructor, or copy or move assignment operator ([[class.copy]]) is so
|
| 1099 |
+
declared. Default arguments for a member function of a class template
|
| 1100 |
+
shall be specified on the initial declaration of the member function
|
| 1101 |
+
within the class template.
|
| 1102 |
|
| 1103 |
``` cpp
|
| 1104 |
class C {
|
| 1105 |
void f(int i = 3);
|
| 1106 |
void g(int i, int j = 99);
|
|
|
|
| 1129 |
class A {
|
| 1130 |
void f(A* p = this) { } // error
|
| 1131 |
};
|
| 1132 |
```
|
| 1133 |
|
| 1134 |
+
A default argument is evaluated each time the function is called with no
|
| 1135 |
+
argument for the corresponding parameter. The order of evaluation of
|
| 1136 |
+
function arguments is unspecified. Consequently, parameters of a
|
| 1137 |
+
function shall not be used in a default argument, even if they are not
|
| 1138 |
+
evaluated. Parameters of a function declared before a default argument
|
| 1139 |
+
are in scope and can hide namespace and class member names.
|
| 1140 |
|
| 1141 |
``` cpp
|
| 1142 |
int a;
|
| 1143 |
int f(int a, int b = a); // error: parameter a
|
| 1144 |
// used as default argument
|
|
|
|
| 1275 |
}
|
| 1276 |
```
|
| 1277 |
|
| 1278 |
In the *function-body*, a *function-local predefined variable* denotes a
|
| 1279 |
block-scope object of static storage duration that is implicitly defined
|
| 1280 |
+
(see [[basic.scope.block]]).
|
| 1281 |
|
| 1282 |
The function-local predefined variable `__func__` is defined as if a
|
| 1283 |
definition of the form
|
| 1284 |
|
| 1285 |
``` cpp
|
| 1286 |
static const char __func__[] = "function-name";
|
| 1287 |
```
|
| 1288 |
|
| 1289 |
had been provided, where *function-name* is an *implementation-defined*
|
| 1290 |
string. It is unspecified whether such a variable has an address
|
| 1291 |
+
distinct from that of any other object in the program.[^13]
|
| 1292 |
|
| 1293 |
``` cpp
|
| 1294 |
struct S {
|
| 1295 |
S() : s(__func__) { } // OK
|
| 1296 |
const char* s;
|
|
|
|
| 1301 |
### Explicitly-defaulted functions <a id="dcl.fct.def.default">[[dcl.fct.def.default]]</a>
|
| 1302 |
|
| 1303 |
A function definition of the form:
|
| 1304 |
|
| 1305 |
``` bnf
|
| 1306 |
+
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator virt-specifier-seqₒₚₜ ' = default ;'
|
| 1307 |
```
|
| 1308 |
|
| 1309 |
is called an *explicitly-defaulted* definition. A function that is
|
| 1310 |
explicitly defaulted shall
|
| 1311 |
|
|
|
|
| 1316 |
non-const `T`”, where `T` is the name of the member function’s class)
|
| 1317 |
as if it had been implicitly declared, and
|
| 1318 |
- not have default arguments.
|
| 1319 |
|
| 1320 |
An explicitly-defaulted function may be declared `constexpr` only if it
|
| 1321 |
+
would have been implicitly declared as `constexpr`. If a function is
|
| 1322 |
+
explicitly defaulted on its first declaration,
|
|
|
|
|
|
|
|
|
|
| 1323 |
|
| 1324 |
- it is implicitly considered to be `constexpr` if the implicit
|
| 1325 |
+
declaration would be, and,
|
| 1326 |
- it is implicitly considered to have the same *exception-specification*
|
| 1327 |
+
as if it had been implicitly declared ([[except.spec]]).
|
| 1328 |
+
|
| 1329 |
+
If a function that is explicitly defaulted has an explicit
|
| 1330 |
+
*exception-specification* that is not compatible ([[except.spec]]) with
|
| 1331 |
+
the *exception-specification* on the implicit declaration, then
|
| 1332 |
+
|
| 1333 |
+
- if the function is explicitly defaulted on its first declaration, it
|
| 1334 |
+
is defined as deleted;
|
| 1335 |
+
- otherwise, the program is ill-formed.
|
| 1336 |
|
| 1337 |
``` cpp
|
| 1338 |
struct S {
|
| 1339 |
constexpr S() = default; // ill-formed: implicit S() is not constexpr
|
| 1340 |
S(int a = 0) = default; // ill-formed: default argument
|
| 1341 |
void operator=(const S&) = default; // ill-formed: non-matching return type
|
| 1342 |
+
~S() throw(int) = default; // deleted: exception specification does not match
|
| 1343 |
private:
|
| 1344 |
int i;
|
| 1345 |
S(S&); // OK: private copy constructor
|
| 1346 |
};
|
| 1347 |
S::S(S&) = default; // OK: defines copy constructor
|
| 1348 |
```
|
| 1349 |
|
| 1350 |
Explicitly-defaulted functions and implicitly-declared functions are
|
| 1351 |
collectively called *defaulted* functions, and the implementation shall
|
| 1352 |
provide implicit definitions for them ([[class.ctor]] [[class.dtor]],
|
| 1353 |
+
[[class.copy]]), which might mean defining them as deleted. A function
|
| 1354 |
+
is *user-provided* if it is user-declared and not explicitly defaulted
|
| 1355 |
+
or deleted on its first declaration. A user-provided
|
| 1356 |
+
explicitly-defaulted function (i.e., explicitly defaulted after its
|
| 1357 |
+
first declaration) is defined at the point where it is explicitly
|
| 1358 |
+
defaulted; if such a function is implicitly defined as deleted, the
|
| 1359 |
+
program is ill-formed. Declaring a function as defaulted after its first
|
| 1360 |
+
declaration can provide efficient execution and concise definition while
|
| 1361 |
+
enabling a stable binary interface to an evolving code base.
|
|
|
|
| 1362 |
|
| 1363 |
``` cpp
|
| 1364 |
struct trivial {
|
| 1365 |
trivial() = default;
|
| 1366 |
trivial(const trivial&) = default;
|
|
|
|
| 1379 |
### Deleted definitions <a id="dcl.fct.def.delete">[[dcl.fct.def.delete]]</a>
|
| 1380 |
|
| 1381 |
A function definition of the form:
|
| 1382 |
|
| 1383 |
``` bnf
|
| 1384 |
+
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator virt-specifier-seqₒₚₜ ' = delete ;'
|
| 1385 |
```
|
| 1386 |
|
| 1387 |
is called a *deleted definition*. A function with a deleted definition
|
| 1388 |
is also called a *deleted function*.
|
| 1389 |
|
|
|
|
| 1505 |
Default arguments are more restricted; see [[dcl.fct.default]].
|
| 1506 |
|
| 1507 |
The order of initialization of variables with static storage duration is
|
| 1508 |
described in [[basic.start]] and [[stmt.dcl]].
|
| 1509 |
|
| 1510 |
+
A declaration of a block-scope variable with external or internal
|
| 1511 |
+
linkage that has an *initializer* is ill-formed.
|
| 1512 |
+
|
| 1513 |
To *zero-initialize* an object or reference of type `T` means:
|
| 1514 |
|
| 1515 |
+
- if `T` is a scalar type ([[basic.types]]), the object is initialized
|
| 1516 |
+
to the value obtained by converting the integer literal `0` (zero) to
|
| 1517 |
+
`T`;[^14]
|
| 1518 |
- if `T` is a (possibly cv-qualified) non-union class type, each
|
| 1519 |
non-static data member and each base-class subobject is
|
| 1520 |
zero-initialized and padding is initialized to zero bits;
|
| 1521 |
- if `T` is a (possibly cv-qualified) union type, the object’s first
|
| 1522 |
non-static named data member is zero-initialized and padding is
|
|
|
|
| 1525 |
- if `T` is a reference type, no initialization is performed.
|
| 1526 |
|
| 1527 |
To *default-initialize* an object of type `T` means:
|
| 1528 |
|
| 1529 |
- if `T` is a (possibly cv-qualified) class type (Clause [[class]]),
|
| 1530 |
+
the default constructor ([[class.ctor]]) for `T` is called (and the
|
| 1531 |
+
initialization is ill-formed if `T` has no default constructor or
|
| 1532 |
+
overload resolution ([[over.match]]) results in an ambiguity or in a
|
| 1533 |
+
function that is deleted or inaccessible from the context of the
|
| 1534 |
+
initialization);
|
| 1535 |
- if `T` is an array type, each element is default-initialized;
|
| 1536 |
- otherwise, no initialization is performed.
|
| 1537 |
|
| 1538 |
If a program calls for the default initialization of an object of a
|
| 1539 |
const-qualified type `T`, `T` shall be a class type with a user-provided
|
| 1540 |
default constructor.
|
| 1541 |
|
| 1542 |
To *value-initialize* an object of type `T` means:
|
| 1543 |
|
| 1544 |
- if `T` is a (possibly cv-qualified) class type (Clause [[class]])
|
| 1545 |
+
with either no default constructor ([[class.ctor]]) or a default
|
| 1546 |
+
constructor that is user-provided or deleted, then the object is
|
| 1547 |
+
default-initialized;
|
| 1548 |
+
- if `T` is a (possibly cv-qualified) class type without a user-provided
|
| 1549 |
+
or deleted default constructor, then the object is zero-initialized
|
| 1550 |
+
and the semantic constraints for default-initialization are checked,
|
| 1551 |
+
and if `T` has a non-trivial default constructor, the object is
|
| 1552 |
+
default-initialized;
|
| 1553 |
- if `T` is an array type, then each element is value-initialized;
|
| 1554 |
- otherwise, the object is zero-initialized.
|
| 1555 |
|
| 1556 |
An object that is value-initialized is deemed to be constructed and thus
|
| 1557 |
subject to provisions of this International Standard applying to
|
|
|
|
| 1579 |
a function taking no argument and returning an `X`. The form `()` is
|
| 1580 |
permitted in certain other initialization contexts ([[expr.new]],
|
| 1581 |
[[expr.type.conv]], [[class.base.init]]).
|
| 1582 |
|
| 1583 |
If no initializer is specified for an object, the object is
|
| 1584 |
+
default-initialized. When storage for an object with automatic or
|
| 1585 |
+
dynamic storage duration is obtained, the object has an *indeterminate
|
| 1586 |
+
value*, and if no initialization is performed for the object, that
|
| 1587 |
+
object retains an indeterminate value until that value is replaced (
|
| 1588 |
+
[[expr.ass]]). Objects with static or thread storage duration are
|
| 1589 |
+
zero-initialized, see [[basic.start.init]]. If an indeterminate value
|
| 1590 |
+
is produced by an evaluation, the behavior is undefined except in the
|
| 1591 |
+
following cases:
|
| 1592 |
+
|
| 1593 |
+
- If an indeterminate value of unsigned narrow character type (
|
| 1594 |
+
[[basic.fundamental]]) is produced by the evaluation of:
|
| 1595 |
+
- the second or third operand of a conditional expression (
|
| 1596 |
+
[[expr.cond]]),
|
| 1597 |
+
- the right operand of a comma expression ([[expr.comma]]),
|
| 1598 |
+
- the operand of a cast or conversion to an unsigned narrow character
|
| 1599 |
+
type ([[conv.integral]], [[expr.type.conv]], [[expr.static.cast]],
|
| 1600 |
+
[[expr.cast]]), or
|
| 1601 |
+
- a discarded-value expression (Clause [[expr]]),
|
| 1602 |
+
|
| 1603 |
+
then the result of the operation is an indeterminate value.
|
| 1604 |
+
- If an indeterminate value of unsigned narrow character type is
|
| 1605 |
+
produced by the evaluation of the right operand of a simple assignment
|
| 1606 |
+
operator ([[expr.ass]]) whose first operand is an lvalue of unsigned
|
| 1607 |
+
narrow character type, an indeterminate value replaces the value of
|
| 1608 |
+
the object referred to by the left operand.
|
| 1609 |
+
- If an indeterminate value of unsigned narrow character type is
|
| 1610 |
+
produced by the evaluation of the initialization expression when
|
| 1611 |
+
initializing an object of unsigned narrow character type, that object
|
| 1612 |
+
is initialized to an indeterminate value.
|
| 1613 |
+
|
| 1614 |
+
``` cpp
|
| 1615 |
+
int f(bool b) {
|
| 1616 |
+
unsigned char c;
|
| 1617 |
+
unsigned char d = c; // OK, d has an indeterminate value
|
| 1618 |
+
int e = d; // undefined behavior
|
| 1619 |
+
return b ? d : 0; // undefined behavior if b is true
|
| 1620 |
+
}
|
| 1621 |
+
```
|
| 1622 |
|
| 1623 |
An initializer for a static member is in the scope of the member’s
|
| 1624 |
class.
|
| 1625 |
|
| 1626 |
``` cpp
|
|
|
|
| 1734 |
[[temp.variadic]]).
|
| 1735 |
|
| 1736 |
### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
|
| 1737 |
|
| 1738 |
An *aggregate* is an array or a class (Clause [[class]]) with no
|
| 1739 |
+
user-provided constructors ([[class.ctor]]), no private or protected
|
| 1740 |
+
non-static data members (Clause [[class.access]]), no base classes
|
| 1741 |
+
(Clause [[class.derived]]), and no virtual functions (
|
| 1742 |
+
[[class.virtual]]).
|
|
|
|
| 1743 |
|
| 1744 |
When an aggregate is initialized by an initializer list, as specified
|
| 1745 |
in [[dcl.init.list]], the elements of the initializer list are taken as
|
| 1746 |
initializers for the members of the aggregate, in increasing subscript
|
| 1747 |
or member order. Each member is copy-initialized from the corresponding
|
|
|
|
| 1777 |
```
|
| 1778 |
|
| 1779 |
declares and initializes `x` as a one-dimensional array that has three
|
| 1780 |
elements since no size was specified and there are three initializers.
|
| 1781 |
An empty initializer list `{}` shall not be used as the
|
| 1782 |
+
*initializer-clause * for an array of unknown bound.[^15]
|
| 1783 |
|
| 1784 |
Static data members and anonymous bit-fields are not considered members
|
| 1785 |
of the class for purposes of aggregate initialization.
|
| 1786 |
|
| 1787 |
``` cpp
|
|
|
|
| 1808 |
|
| 1809 |
is ill-formed.
|
| 1810 |
|
| 1811 |
If there are fewer *initializer-clause*s in the list than there are
|
| 1812 |
members in the aggregate, then each member not explicitly initialized
|
| 1813 |
+
shall be initialized from its *brace-or-equal-initializer* or, if there
|
| 1814 |
+
is no *brace-or-equal-initializer*, from an empty initializer list (
|
| 1815 |
[[dcl.init.list]]).
|
| 1816 |
|
| 1817 |
``` cpp
|
| 1818 |
+
struct S { int a; const char* b; int c; int d = b[a]; };
|
| 1819 |
S ss = { 1, "asdf" };
|
| 1820 |
```
|
| 1821 |
|
| 1822 |
+
initializes `ss.a` with 1, `ss.b` with `"asdf"`, `ss.c` with the value
|
| 1823 |
+
of an expression of the form `int{}` (that is, `0`), and `ss.d` with the
|
| 1824 |
+
value of `ss.b[ss.a]` (that is, `'s'`), and in
|
| 1825 |
+
|
| 1826 |
+
``` cpp
|
| 1827 |
+
struct X { int i, j, k = 42; };
|
| 1828 |
+
X a[] = { 1, 2, 3, 4, 5, 6 };
|
| 1829 |
+
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
|
| 1830 |
+
```
|
| 1831 |
+
|
| 1832 |
+
`a` and `b` have the same value
|
| 1833 |
|
| 1834 |
If an aggregate class `C` contains a subaggregate member `m` that has no
|
| 1835 |
members for purposes of aggregate initialization, the
|
| 1836 |
*initializer-clause* for `m` shall not be omitted from an
|
| 1837 |
*initializer-list* for an object of type `C` unless the
|
|
|
|
| 1876 |
```
|
| 1877 |
|
| 1878 |
initializes the first column of `y` (regarded as a two-dimensional
|
| 1879 |
array) and leaves the rest zero.
|
| 1880 |
|
| 1881 |
+
Braces can be elided in an *initializer-list* as follows. If the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1882 |
*initializer-list* begins with a left brace, then the succeeding
|
| 1883 |
comma-separated list of *initializer-clause*s initializes the members of
|
| 1884 |
a subaggregate; it is erroneous for there to be more
|
| 1885 |
*initializer-clause*s than members. If, however, the *initializer-list*
|
| 1886 |
for a subaggregate does not begin with a left brace, then only enough
|
|
|
|
| 1969 |
union member can be omitted if the union is a member of another
|
| 1970 |
aggregate.
|
| 1971 |
|
| 1972 |
### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
|
| 1973 |
|
| 1974 |
+
An array of narrow character type ([[basic.fundamental]]), `char16_t`
|
| 1975 |
+
array, `char32_t` array, or `wchar_t` array can be initialized by a
|
| 1976 |
+
narrow string literal, `char16_t` string literal, `char32_t` string
|
| 1977 |
+
literal, or wide string literal, respectively, or by an
|
| 1978 |
+
appropriately-typed string literal enclosed in braces ([[lex.string]]).
|
| 1979 |
+
Successive characters of the value of the string literal initialize the
|
| 1980 |
+
elements of the array.
|
| 1981 |
|
| 1982 |
``` cpp
|
| 1983 |
char msg[] = "Syntax error on line %s\n";
|
| 1984 |
```
|
| 1985 |
|
|
|
|
| 2037 |
```
|
| 2038 |
|
| 2039 |
Given types “ `T1`” and “ `T2`,” “ `T1`” is to “ `T2`” if `T1` is the
|
| 2040 |
same type as `T2`, or `T1` is a base class of `T2`. “ `T1`” is with “
|
| 2041 |
`T2`” if `T1` is reference-related to `T2` and *cv1* is the same
|
| 2042 |
+
cv-qualification as, or greater cv-qualification than, *cv2*. In all
|
| 2043 |
+
cases where the reference-related or reference-compatible relationship
|
| 2044 |
+
of two types is used to establish the validity of a reference binding,
|
| 2045 |
+
and `T1` is a base class of `T2`, a program that necessitates such a
|
| 2046 |
+
binding is ill-formed if `T1` is an inaccessible (Clause
|
| 2047 |
+
[[class.access]]) or ambiguous ([[class.member.lookup]]) base class of
|
| 2048 |
+
`T2`.
|
|
|
|
|
|
|
|
|
|
| 2049 |
|
| 2050 |
A reference to type “*cv1* `T1`” is initialized by an expression of type
|
| 2051 |
“*cv2* `T2`” as follows:
|
| 2052 |
|
| 2053 |
- If the reference is an lvalue reference and the initializer expression
|
| 2054 |
- is an lvalue (but is not a bit-field), and “ `T1`” is
|
| 2055 |
reference-compatible with “ `T2`,” or
|
| 2056 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 2057 |
+
reference-related to `T2`, and can be converted to an lvalue of type
|
| 2058 |
+
“ `T3`,” where “ `T1`” is reference-compatible with “ `T3`”[^16]
|
| 2059 |
+
(this conversion is selected by enumerating the applicable
|
| 2060 |
+
conversion functions ([[over.match.ref]]) and choosing the best one
|
| 2061 |
+
through overload resolution ([[over.match]])),
|
| 2062 |
|
| 2063 |
then the reference is bound to the initializer expression lvalue in
|
| 2064 |
the first case and to the lvalue result of the conversion in the
|
| 2065 |
second case (or, in either case, to the appropriate base class
|
| 2066 |
subobject of the object). The usual lvalue-to-rvalue ([[conv.lval]]),
|
|
|
|
| 2086 |
int i = 2;
|
| 2087 |
double& rd3 = i; // error: type mismatch and reference not const
|
| 2088 |
```
|
| 2089 |
|
| 2090 |
- If the initializer expression
|
| 2091 |
+
- is an xvalue (but not a bit-field), class prvalue, array prvalue
|
| 2092 |
+
or function lvalue and “*cv1* `T1`” is reference-compatible with
|
| 2093 |
+
“*cv2* `T2`”, or
|
| 2094 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 2095 |
+
reference-related to `T2`, and can be converted to an xvalue,
|
| 2096 |
+
class prvalue, or function lvalue of type “*cv3* `T3`”, where
|
| 2097 |
+
“*cv1* `T1`” is reference-compatible with “*cv3* `T3`” (see
|
| 2098 |
+
[[over.match.ref]]),
|
| 2099 |
|
| 2100 |
then the reference is bound to the value of the initializer
|
| 2101 |
expression in the first case and to the result of the conversion in
|
| 2102 |
the second case (or, in either case, to an appropriate base class
|
| 2103 |
subobject). In the second case, if the reference is an rvalue
|
|
|
|
| 2119 |
int&& rri = static_cast<int&&>(i2); // bound directly to i2
|
| 2120 |
B&& rrb = x; // bound directly to the result of operator B
|
| 2121 |
int&& rri2 = X(); // error: lvalue-to-rvalue conversion applied to the
|
| 2122 |
// result of operator int&
|
| 2123 |
```
|
| 2124 |
+
- Otherwise:
|
| 2125 |
+
- If `T1` is a class type, user-defined conversions are considered
|
| 2126 |
+
using the rules for copy-initialization of an object of type “
|
| 2127 |
+
`T1`” by user-defined conversion ([[dcl.init]],
|
| 2128 |
+
[[over.match.copy]]); the program is ill-formed if the
|
| 2129 |
+
corresponding non-reference copy-initialization would be
|
| 2130 |
+
ill-formed. The result of the call to the conversion function, as
|
| 2131 |
+
described for the non-reference copy-initialization, is then used
|
| 2132 |
+
to direct-initialize the reference. The program is ill-formed if
|
| 2133 |
+
the direct-initialization does not result in a direct binding or
|
| 2134 |
+
if it involves a user-defined conversion.
|
| 2135 |
+
- If `T1` is a non-class type, a temporary of type “ `T1`” is
|
| 2136 |
+
created and copy-initialized ([[dcl.init]]) from the initializer
|
| 2137 |
+
expression. The reference is then bound to the temporary.
|
| 2138 |
+
|
| 2139 |
+
If `T1` is reference-related to `T2`:
|
| 2140 |
+
- *cv1* shall be the same cv-qualification as, or greater
|
| 2141 |
+
cv-qualification than, *cv2*; and
|
| 2142 |
+
- if the reference is an rvalue reference, the initializer
|
| 2143 |
+
expression shall not be an lvalue.
|
| 2144 |
+
|
| 2145 |
``` cpp
|
| 2146 |
+
struct Banana { };
|
| 2147 |
+
struct Enigma { operator const Banana(); };
|
| 2148 |
+
void enigmatic() {
|
| 2149 |
+
typedef const Banana ConstBanana;
|
| 2150 |
+
Banana &&banana1 = ConstBanana(); // ill-formed
|
| 2151 |
+
Banana &&banana2 = Enigma(); // ill-formed
|
| 2152 |
+
}
|
| 2153 |
+
|
| 2154 |
const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
|
| 2155 |
double&& rrd = 2; // rrd refers to temporary with value 2.0
|
| 2156 |
const volatile int cvi = 1;
|
| 2157 |
const int& r2 = cvi; // error: type qualifiers dropped
|
| 2158 |
double d2 = 1.0;
|
|
|
|
| 2181 |
*copy-list-initialization*. List-initialization can be used
|
| 2182 |
|
| 2183 |
- as the initializer in a variable definition ([[dcl.init]])
|
| 2184 |
- as the initializer in a new expression ([[expr.new]])
|
| 2185 |
- in a return statement ([[stmt.return]])
|
| 2186 |
+
- as a *for-range-initializer* ([[stmt.iter]])
|
| 2187 |
- as a function argument ([[expr.call]])
|
| 2188 |
- as a subscript ([[expr.sub]])
|
| 2189 |
- as an argument to a constructor invocation ([[dcl.init]],
|
| 2190 |
[[expr.type.conv]])
|
| 2191 |
- as an initializer for a non-static data member ([[class.mem]])
|
|
|
|
| 2207 |
parameter is of type `std::initializer_list<E>` or reference to possibly
|
| 2208 |
cv-qualified `std::initializer_list<E>` for some type `E`, and either
|
| 2209 |
there are no other parameters or else all other parameters have default
|
| 2210 |
arguments ([[dcl.fct.default]]). Initializer-list constructors are
|
| 2211 |
favored over other constructors in list-initialization (
|
| 2212 |
+
[[over.match.list]]). Passing an initializer list as the argument to the
|
| 2213 |
+
constructor template `template<class T> C(T)` of a class `C` does not
|
| 2214 |
+
create an initializer-list constructor, because an initializer list
|
| 2215 |
+
argument causes the corresponding parameter to be a non-deduced
|
| 2216 |
+
context ([[temp.deduct.call]]). The template `std::initializer_list` is
|
| 2217 |
+
not predefined; if the header `<initializer_list>` is not included prior
|
| 2218 |
+
to a use of `std::initializer_list` — even an implicit use in which the
|
| 2219 |
type is not named ([[dcl.spec.auto]]) — the program is ill-formed.
|
| 2220 |
|
| 2221 |
List-initialization of an object or reference of type `T` is defined as
|
| 2222 |
follows:
|
| 2223 |
|
| 2224 |
+
- If `T` is an aggregate, aggregate initialization is performed (
|
| 2225 |
+
[[dcl.init.aggr]]).
|
|
|
|
|
|
|
| 2226 |
``` cpp
|
| 2227 |
double ad[] = { 1, 2.0 }; // OK
|
| 2228 |
int ai[] = { 1, 2.0 }; // error: narrowing
|
| 2229 |
|
| 2230 |
struct S2 {
|
|
|
|
| 2233 |
};
|
| 2234 |
S2 s21 = { 1, 2, 3.0 }; // OK
|
| 2235 |
S2 s22 { 1.0, 2, 3 }; // error: narrowing
|
| 2236 |
S2 s23 { }; // OK: default to 0,0,0
|
| 2237 |
```
|
| 2238 |
+
- Otherwise, if the initializer list has no elements and `T` is a class
|
| 2239 |
+
type with a default constructor, the object is value-initialized.
|
| 2240 |
+
- Otherwise, if `T` is a specialization of `std::initializer_list<E>`, a
|
| 2241 |
+
prvalue `initializer_list` object is constructed as described below
|
| 2242 |
+
and used to initialize the object according to the rules for
|
| 2243 |
initialization of an object from a class of the same type (
|
| 2244 |
[[dcl.init]]).
|
| 2245 |
- Otherwise, if `T` is a class type, constructors are considered. The
|
| 2246 |
applicable constructors are enumerated and the best one is chosen
|
| 2247 |
through overload resolution ([[over.match]], [[over.match.list]]).
|
|
|
|
| 2275 |
};
|
| 2276 |
S s1 = { 1, 2, 3.0 }; // OK: invoke #1
|
| 2277 |
S s2 { 1.0, 2, 3 }; // error: narrowing
|
| 2278 |
S s3 { }; // OK: invoke #2
|
| 2279 |
```
|
| 2280 |
+
- Otherwise, if the initializer list has a single element of type `E`
|
| 2281 |
+
and either `T` is not a reference type or its referenced type is
|
| 2282 |
+
reference-related to `E`, the object or reference is initialized from
|
| 2283 |
+
that element; if a narrowing conversion (see below) is required to
|
| 2284 |
+
convert the element to `T`, the program is ill-formed.
|
| 2285 |
+
``` cpp
|
| 2286 |
+
int x1 {2}; // OK
|
| 2287 |
+
int x2 {2.0}; // error: narrowing
|
| 2288 |
+
```
|
| 2289 |
- Otherwise, if `T` is a reference type, a prvalue temporary of the type
|
| 2290 |
+
referenced by `T` is copy-list-initialized or direct-list-initialized,
|
| 2291 |
+
depending on the kind of initialization for the reference, and the
|
| 2292 |
+
reference is bound to that temporary. As usual, the binding will fail
|
| 2293 |
+
and the program is ill-formed if the reference type is an lvalue
|
| 2294 |
+
reference to a non-const type.
|
| 2295 |
``` cpp
|
| 2296 |
struct S {
|
| 2297 |
S(std::initializer_list<double>); // #1
|
| 2298 |
S(const std::string&); // #2
|
| 2299 |
// ...
|
|
|
|
| 2303 |
S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
|
| 2304 |
const int& i1 = { 1 }; // OK
|
| 2305 |
const int& i2 = { 1.1 }; // error: narrowing
|
| 2306 |
const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
|
| 2307 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2308 |
- Otherwise, if the initializer list has no elements, the object is
|
| 2309 |
value-initialized.
|
| 2310 |
``` cpp
|
| 2311 |
int** pp {}; // initialized to null pointer
|
| 2312 |
```
|
|
|
|
| 2341 |
example, it applies when the elements of the *initializer-list* are
|
| 2342 |
interpreted as arguments of a constructor call, even though ordinarily
|
| 2343 |
there are no sequencing constraints on the arguments of a call.
|
| 2344 |
|
| 2345 |
An object of type `std::initializer_list<E>` is constructed from an
|
| 2346 |
+
initializer list as if the implementation allocated a temporary array of
|
| 2347 |
+
N elements of type `const E`, where N is the number of elements in the
|
| 2348 |
initializer list. Each element of that array is copy-initialized with
|
| 2349 |
the corresponding element of the initializer list, and the
|
| 2350 |
`std::initializer_list<E>` object is constructed to refer to that array.
|
| 2351 |
+
A constructor or conversion function selected for the copy shall be
|
| 2352 |
+
accessible (Clause [[class.access]]) in the context of the initializer
|
| 2353 |
+
list. If a narrowing conversion is required to initialize any of the
|
| 2354 |
+
elements, the program is ill-formed.
|
| 2355 |
|
| 2356 |
``` cpp
|
| 2357 |
struct X {
|
| 2358 |
X(std::initializer_list<double> v);
|
| 2359 |
};
|
|
|
|
| 2362 |
|
| 2363 |
The initialization will be implemented in a way roughly equivalent to
|
| 2364 |
this:
|
| 2365 |
|
| 2366 |
``` cpp
|
| 2367 |
+
const double __a[3] = {double{1}, double{2}, double{3}};
|
| 2368 |
X x(std::initializer_list<double>(__a, __a+3));
|
| 2369 |
```
|
| 2370 |
|
| 2371 |
assuming that the implementation can construct an `initializer_list`
|
| 2372 |
object with a pair of pointers.
|
| 2373 |
|
| 2374 |
+
The array has the same lifetime as any other temporary object (
|
| 2375 |
+
[[class.temporary]]), except that initializing an `initializer_list`
|
| 2376 |
+
object from the array extends the lifetime of the array exactly like
|
| 2377 |
+
binding a reference to a temporary.
|
| 2378 |
|
| 2379 |
``` cpp
|
| 2380 |
typedef std::complex<double> cmplx;
|
| 2381 |
std::vector<cmplx> v1 = { 1, 2, 3 };
|
| 2382 |
|
| 2383 |
void f() {
|
| 2384 |
std::vector<cmplx> v2{ 1, 2, 3 };
|
| 2385 |
std::initializer_list<int> i3 = { 1, 2, 3 };
|
| 2386 |
}
|
| 2387 |
+
|
| 2388 |
+
struct A {
|
| 2389 |
+
std::initializer_list<int> i4;
|
| 2390 |
+
A() : i4{ 1, 2, 3 } {} // creates an A with a dangling reference
|
| 2391 |
+
};
|
| 2392 |
```
|
| 2393 |
|
| 2394 |
+
For `v1` and `v2`, the `initializer_list` object is a parameter in a
|
| 2395 |
+
function call, so the array created for `{ 1, 2, 3 }` has
|
| 2396 |
+
full-expression lifetime. For `i3`, the `initializer_list` object is a
|
| 2397 |
+
variable, so the array persists for the lifetime of the variable. For
|
| 2398 |
+
`i4`, the `initializer_list` object is initialized in a constructor’s
|
| 2399 |
+
*ctor-initializer*, so the array persists only until the constructor
|
| 2400 |
+
exits, and so any use of the elements of `i4` after the constructor
|
| 2401 |
+
exits produces undefined behavior. The implementation is free to
|
| 2402 |
+
allocate the array in read-only memory if an explicit array with the
|
| 2403 |
+
same initializer could be so allocated.
|
| 2404 |
|
| 2405 |
A *narrowing conversion* is an implicit conversion
|
| 2406 |
|
| 2407 |
- from a floating-point type to an integer type, or
|
| 2408 |
- from `long double` to `double` or `float`, or from `double` to
|
|
|
|
| 2413 |
type, except where the source is a constant expression and the actual
|
| 2414 |
value after conversion will fit into the target type and will produce
|
| 2415 |
the original value when converted back to the original type, or
|
| 2416 |
- from an integer type or unscoped enumeration type to an integer type
|
| 2417 |
that cannot represent all the values of the original type, except
|
| 2418 |
+
where the source is a constant expression whose value after integral
|
| 2419 |
+
promotions will fit into the target type.
|
|
|
|
| 2420 |
|
| 2421 |
As indicated above, such conversions are not allowed at the top level in
|
| 2422 |
list-initializations.
|
| 2423 |
|
| 2424 |
``` cpp
|
|
|
|
| 2456 |
[basic.lookup.udir]: basic.md#basic.lookup.udir
|
| 2457 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 2458 |
[basic.lval]: basic.md#basic.lval
|
| 2459 |
[basic.namespace]: #basic.namespace
|
| 2460 |
[basic.scope]: basic.md#basic.scope
|
| 2461 |
+
[basic.scope.block]: basic.md#basic.scope.block
|
| 2462 |
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
| 2463 |
+
[basic.scope.pdecl]: basic.md#basic.scope.pdecl
|
| 2464 |
[basic.scope.proto]: basic.md#basic.scope.proto
|
| 2465 |
[basic.start]: basic.md#basic.start
|
| 2466 |
[basic.start.init]: basic.md#basic.start.init
|
| 2467 |
[basic.stc]: basic.md#basic.stc
|
| 2468 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
|
|
|
| 2486 |
[class.inhctor]: special.md#class.inhctor
|
| 2487 |
[class.init]: special.md#class.init
|
| 2488 |
[class.mem]: class.md#class.mem
|
| 2489 |
[class.member.lookup]: class.md#class.member.lookup
|
| 2490 |
[class.mfct]: class.md#class.mfct
|
|
|
|
| 2491 |
[class.name]: class.md#class.name
|
| 2492 |
[class.qual]: basic.md#class.qual
|
| 2493 |
[class.static]: class.md#class.static
|
| 2494 |
[class.static.data]: class.md#class.static.data
|
| 2495 |
[class.temporary]: special.md#class.temporary
|
|
|
|
| 2497 |
[class.union]: class.md#class.union
|
| 2498 |
[class.virtual]: class.md#class.virtual
|
| 2499 |
[conv]: conv.md#conv
|
| 2500 |
[conv.array]: conv.md#conv.array
|
| 2501 |
[conv.func]: conv.md#conv.func
|
| 2502 |
+
[conv.integral]: conv.md#conv.integral
|
| 2503 |
[conv.lval]: conv.md#conv.lval
|
| 2504 |
[conv.prom]: conv.md#conv.prom
|
| 2505 |
[conv.ptr]: conv.md#conv.ptr
|
| 2506 |
[dcl.align]: #dcl.align
|
| 2507 |
[dcl.ambig.res]: #dcl.ambig.res
|
| 2508 |
[dcl.array]: #dcl.array
|
| 2509 |
[dcl.asm]: #dcl.asm
|
| 2510 |
[dcl.attr]: #dcl.attr
|
| 2511 |
[dcl.attr.depend]: #dcl.attr.depend
|
| 2512 |
+
[dcl.attr.deprecated]: #dcl.attr.deprecated
|
| 2513 |
[dcl.attr.grammar]: #dcl.attr.grammar
|
| 2514 |
[dcl.attr.noreturn]: #dcl.attr.noreturn
|
| 2515 |
[dcl.constexpr]: #dcl.constexpr
|
| 2516 |
[dcl.dcl]: #dcl.dcl
|
| 2517 |
[dcl.decl]: #dcl.decl
|
|
|
|
| 2549 |
[except.throw]: except.md#except.throw
|
| 2550 |
[expr]: expr.md#expr
|
| 2551 |
[expr.alignof]: expr.md#expr.alignof
|
| 2552 |
[expr.ass]: expr.md#expr.ass
|
| 2553 |
[expr.call]: expr.md#expr.call
|
| 2554 |
+
[expr.cast]: expr.md#expr.cast
|
| 2555 |
+
[expr.comma]: expr.md#expr.comma
|
| 2556 |
+
[expr.cond]: expr.md#expr.cond
|
| 2557 |
[expr.const]: expr.md#expr.const
|
| 2558 |
[expr.const.cast]: expr.md#expr.const.cast
|
| 2559 |
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 2560 |
[expr.new]: expr.md#expr.new
|
| 2561 |
+
[expr.prim.lambda]: expr.md#expr.prim.lambda
|
| 2562 |
[expr.ref]: expr.md#expr.ref
|
| 2563 |
[expr.static.cast]: expr.md#expr.static.cast
|
| 2564 |
[expr.sub]: expr.md#expr.sub
|
| 2565 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 2566 |
[expr.unary]: expr.md#expr.unary
|
|
|
|
| 2571 |
[intro.multithread]: intro.md#intro.multithread
|
| 2572 |
[lex.charset]: lex.md#lex.charset
|
| 2573 |
[lex.digraph]: lex.md#lex.digraph
|
| 2574 |
[lex.key]: lex.md#lex.key
|
| 2575 |
[lex.name]: lex.md#lex.name
|
| 2576 |
+
[lex.string]: lex.md#lex.string
|
| 2577 |
[namespace.alias]: #namespace.alias
|
| 2578 |
[namespace.def]: #namespace.def
|
| 2579 |
[namespace.memdef]: #namespace.memdef
|
| 2580 |
[namespace.qual]: basic.md#namespace.qual
|
| 2581 |
[namespace.udecl]: #namespace.udecl
|
| 2582 |
[namespace.udir]: #namespace.udir
|
| 2583 |
[namespace.unnamed]: #namespace.unnamed
|
| 2584 |
[over]: over.md#over
|
|
|
|
| 2585 |
[over.match]: over.md#over.match
|
| 2586 |
[over.match.conv]: over.md#over.match.conv
|
| 2587 |
[over.match.copy]: over.md#over.match.copy
|
| 2588 |
[over.match.ctor]: over.md#over.match.ctor
|
| 2589 |
[over.match.list]: over.md#over.match.list
|
|
|
|
| 2599 |
[stmt.select]: stmt.md#stmt.select
|
| 2600 |
[stmt.stmt]: stmt.md#stmt.stmt
|
| 2601 |
[support.runtime]: language.md#support.runtime
|
| 2602 |
[tab:simple.type.specifiers]: #tab:simple.type.specifiers
|
| 2603 |
[temp]: temp.md#temp
|
|
|
|
| 2604 |
[temp.arg.type]: temp.md#temp.arg.type
|
| 2605 |
[temp.class.spec]: temp.md#temp.class.spec
|
| 2606 |
[temp.deduct.call]: temp.md#temp.deduct.call
|
| 2607 |
[temp.dep]: temp.md#temp.dep
|
| 2608 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
|
|
|
| 2617 |
|
| 2618 |
[^1]: The “implicit int” rule of C is no longer supported.
|
| 2619 |
|
| 2620 |
[^2]: The inline keyword has no effect on the linkage of a function.
|
| 2621 |
|
| 2622 |
+
[^3]: There is no special provision for a *decl-specifier-seq* that
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2623 |
lacks a *type-specifier* or that has a *type-specifier* that only
|
| 2624 |
specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
|
| 2625 |
supported.
|
| 2626 |
|
| 2627 |
+
[^4]: This set of values is used to define promotion and conversion
|
| 2628 |
semantics for the enumeration type. It does not preclude an
|
| 2629 |
expression of enumeration type from having a value that falls
|
| 2630 |
outside this range.
|
| 2631 |
|
| 2632 |
+
[^5]: Although entities in an unnamed namespace might have external
|
| 2633 |
linkage, they are effectively qualified by a name unique to their
|
| 2634 |
translation unit and therefore can never be seen from any other
|
| 2635 |
translation unit.
|
| 2636 |
|
| 2637 |
+
[^6]: this implies that the name of the class or function is
|
| 2638 |
unqualified.
|
| 2639 |
|
| 2640 |
+
[^7]: During name lookup in a class hierarchy, some ambiguities may be
|
| 2641 |
resolved by considering whether one member hides the other along
|
| 2642 |
some paths ([[class.member.lookup]]). There is no such
|
| 2643 |
disambiguation when considering the set of names found as a result
|
| 2644 |
of following *using-directive*s.
|
| 2645 |
|
| 2646 |
+
[^8]: A declaration with several declarators is usually equivalent to
|
| 2647 |
the corresponding sequence of declarations each with a single
|
| 2648 |
declarator. That is
|
| 2649 |
|
| 2650 |
`T D1, D2, ... Dn;`
|
| 2651 |
|
|
|
|
| 2675 |
`auto i = 1, j = 2.0; \textrm{// error: deduced types for \tcode{i} and \tcode{j} do not match}`
|
| 2676 |
as opposed to
|
| 2677 |
`auto i = 1; \textrm{// OK: \tcode{i} deduced to have type \tcode{int}}`
|
| 2678 |
`auto j = 2.0; \textrm{// OK: \tcode{j} deduced to have type \tcode{double}}`
|
| 2679 |
|
| 2680 |
+
[^9]: As indicated by syntax, cv-qualifiers are a significant component
|
| 2681 |
in function return types.
|
| 2682 |
|
| 2683 |
+
[^10]: This excludes parameters of type “ `T2`” where `T2` is “pointer
|
| 2684 |
to array of unknown bound of `T`” and where means any sequence of
|
| 2685 |
“pointer to” and “array of” derived declarator types. This exclusion
|
| 2686 |
applies to the parameters of the function, and if a parameter is a
|
| 2687 |
pointer to function or pointer to member function then to its
|
| 2688 |
parameters also, etc.
|
| 2689 |
|
| 2690 |
+
[^11]: One can explicitly disambiguate the parse either by introducing a
|
| 2691 |
comma (so the ellipsis will be parsed as part of the
|
| 2692 |
*parameter-declaration-clause*) or by introducing a name for the
|
| 2693 |
parameter (so the ellipsis will be parsed as part of the
|
| 2694 |
*declarator-id*).
|
| 2695 |
|
| 2696 |
+
[^12]: This means that default arguments cannot appear, for example, in
|
| 2697 |
declarations of pointers to functions, references to functions, or
|
| 2698 |
`typedef` declarations.
|
| 2699 |
|
| 2700 |
+
[^13]: Implementations are permitted to provide additional predefined
|
| 2701 |
variables with names that are reserved to the implementation (
|
| 2702 |
[[global.names]]). If a predefined variable is not odr-used (
|
| 2703 |
[[basic.def.odr]]), its string value need not be present in the
|
| 2704 |
program image.
|
| 2705 |
|
| 2706 |
+
[^14]: As specified in [[conv.ptr]], converting an integer literal
|
| 2707 |
+
whose value is `0` to a pointer type results in a null pointer
|
| 2708 |
+
value.
|
| 2709 |
|
| 2710 |
+
[^15]: The syntax provides for empty *initializer-list*s, but
|
| 2711 |
nonetheless C++does not have zero length arrays.
|
| 2712 |
|
| 2713 |
+
[^16]: This requires a conversion function ([[class.conv.fct]])
|
|
|
|
|
|
|
| 2714 |
returning a reference type.
|