- tmp/tmprq23l_rr/{from.md → to.md} +370 -2174
tmp/tmprq23l_rr/{from.md → to.md}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
# Declarators <a id="dcl.decl">[[dcl.decl]]</a>
|
| 2 |
|
| 3 |
A declarator declares a single variable, function, or type, within a
|
| 4 |
declaration. The *init-declarator-list* appearing in a declaration is a
|
| 5 |
comma-separated sequence of declarators, each of which can have an
|
| 6 |
initializer.
|
|
@@ -12,14 +12,15 @@ init-declarator-list:
|
|
| 12 |
```
|
| 13 |
|
| 14 |
``` bnf
|
| 15 |
init-declarator:
|
| 16 |
declarator initializerₒₚₜ
|
|
|
|
| 17 |
```
|
| 18 |
|
| 19 |
-
The three components of a *simple-declaration* are the attributes
|
| 20 |
-
[[dcl.attr]]
|
| 21 |
the declarators (*init-declarator-list*). The specifiers indicate the
|
| 22 |
type, storage class or other properties of the entities being declared.
|
| 23 |
The declarators specify the names of these entities and (optionally)
|
| 24 |
modify the type of the specifiers with operators such as `*` (pointer
|
| 25 |
to) and `()` (function returning). Initial values can also be specified
|
|
@@ -62,12 +63,11 @@ which is not equivalent to
|
|
| 62 |
struct S { ... };
|
| 63 |
S S;
|
| 64 |
S T; // error
|
| 65 |
```
|
| 66 |
|
| 67 |
-
Another exception is when `T` is `auto`
|
| 68 |
-
example:
|
| 69 |
|
| 70 |
``` cpp
|
| 71 |
auto i = 1, j = 2.0; // error: deduced types for i and j do not match
|
| 72 |
```
|
| 73 |
|
|
@@ -78,10 +78,34 @@ auto i = 1; // OK: i deduced to have type int
|
|
| 78 |
auto j = 2.0; // OK: j deduced to have type double
|
| 79 |
```
|
| 80 |
|
| 81 |
— *end note*]
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
Declarators have the syntax
|
| 84 |
|
| 85 |
``` bnf
|
| 86 |
declarator:
|
| 87 |
ptr-declarator
|
|
@@ -126,12 +150,12 @@ cv-qualifier-seq:
|
|
| 126 |
cv-qualifier cv-qualifier-seqₒₚₜ
|
| 127 |
```
|
| 128 |
|
| 129 |
``` bnf
|
| 130 |
cv-qualifier:
|
| 131 |
-
|
| 132 |
-
|
| 133 |
```
|
| 134 |
|
| 135 |
``` bnf
|
| 136 |
ref-qualifier:
|
| 137 |
'&'
|
|
@@ -141,11 +165,11 @@ ref-qualifier:
|
|
| 141 |
``` bnf
|
| 142 |
declarator-id:
|
| 143 |
'...'ₒₚₜ id-expression
|
| 144 |
```
|
| 145 |
|
| 146 |
-
## Type names <a id="dcl.name">[[dcl.name]]</a>
|
| 147 |
|
| 148 |
To specify type conversions explicitly, and as an argument of `sizeof`,
|
| 149 |
`alignof`, `new`, or `typeid`, the name of a type shall be specified.
|
| 150 |
This can be done with a *type-id*, which is syntactically a declaration
|
| 151 |
for a variable or function of that type that omits the name of the
|
|
@@ -215,14 +239,14 @@ pointers to `int`”, “pointer to array of 3 `int`”, “function of (no
|
|
| 215 |
parameters) returning pointer to `int`”, and “pointer to a function of
|
| 216 |
(`double`) returning `int`”.
|
| 217 |
|
| 218 |
— *end example*]
|
| 219 |
|
| 220 |
-
A type can also be named (often more easily) by using a `typedef`
|
| 221 |
-
[[dcl.typedef]]
|
| 222 |
|
| 223 |
-
## Ambiguity resolution <a id="dcl.ambig.res">[[dcl.ambig.res]]</a>
|
| 224 |
|
| 225 |
The ambiguity arising from the similarity between a function-style cast
|
| 226 |
and a declaration mentioned in [[stmt.ambig]] can also occur in the
|
| 227 |
context of a declaration. In that context, the choice is between a
|
| 228 |
function declaration with a redundant set of parentheses around a
|
|
@@ -312,22 +336,22 @@ void h(int *(C[10])); // void h(int *(*_fp)(C _parm[10]));
|
|
| 312 |
// not: void h(int *C[10]);
|
| 313 |
```
|
| 314 |
|
| 315 |
— *end example*]
|
| 316 |
|
| 317 |
-
## Meaning of declarators <a id="dcl.meaning">[[dcl.meaning]]</a>
|
| 318 |
|
| 319 |
A declarator contains exactly one *declarator-id*; it names the
|
| 320 |
identifier that is declared. An *unqualified-id* occurring in a
|
| 321 |
*declarator-id* shall be a simple *identifier* except for the
|
| 322 |
declaration of some special functions ([[class.ctor]], [[class.conv]],
|
| 323 |
[[class.dtor]], [[over.oper]]) and for the declaration of template
|
| 324 |
-
specializations or partial specializations
|
| 325 |
*declarator-id* is qualified, the declaration shall refer to a
|
| 326 |
previously declared member of the class or namespace to which the
|
| 327 |
qualifier refers (or, in the case of a namespace, of an element of the
|
| 328 |
-
inline namespace set of that namespace
|
| 329 |
specialization thereof; the member shall not merely have been introduced
|
| 330 |
by a *using-declaration* in the scope of the class or namespace
|
| 331 |
nominated by the *nested-name-specifier* of the *declarator-id*. The
|
| 332 |
*nested-name-specifier* of a qualified *declarator-id* shall not begin
|
| 333 |
with a *decltype-specifier*.
|
|
@@ -338,14 +362,15 @@ namespace scope. — *end note*]
|
|
| 338 |
|
| 339 |
The optional *attribute-specifier-seq* following a *declarator-id*
|
| 340 |
appertains to the entity that is declared.
|
| 341 |
|
| 342 |
A `static`, `thread_local`, `extern`, `mutable`, `friend`, `inline`,
|
| 343 |
-
`virtual`, `constexpr`,
|
| 344 |
-
directly to each *declarator-id* in an
|
| 345 |
-
*member-declarator-list*; the type specified
|
| 346 |
-
depends on both the *decl-specifier-seq* and
|
|
|
|
| 347 |
|
| 348 |
Thus, a declaration of a particular identifier has the form
|
| 349 |
|
| 350 |
``` cpp
|
| 351 |
T D
|
|
@@ -371,11 +396,11 @@ In the declaration
|
|
| 371 |
``` cpp
|
| 372 |
int unsigned i;
|
| 373 |
```
|
| 374 |
|
| 375 |
the type specifiers `int` `unsigned` determine the type “`unsigned int`”
|
| 376 |
-
|
| 377 |
|
| 378 |
— *end example*]
|
| 379 |
|
| 380 |
In a declaration *attribute-specifier-seq*ₒₚₜ `T` `D` where `D` is an
|
| 381 |
unadorned identifier the type of this identifier is “`T`”.
|
|
@@ -394,11 +419,11 @@ T D1
|
|
| 394 |
```
|
| 395 |
|
| 396 |
Parentheses do not alter the type of the embedded *declarator-id*, but
|
| 397 |
they can alter the binding of complex declarators.
|
| 398 |
|
| 399 |
-
### Pointers <a id="dcl.ptr">[[dcl.ptr]]</a>
|
| 400 |
|
| 401 |
In a declaration `T` `D` where `D` has the form
|
| 402 |
|
| 403 |
``` bnf
|
| 404 |
'*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
|
@@ -406,12 +431,12 @@ In a declaration `T` `D` where `D` has the form
|
|
| 406 |
|
| 407 |
and the type of the identifier in the declaration `T` `D1` is
|
| 408 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 409 |
`D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
|
| 410 |
`T`”. The *cv-qualifier*s apply to the pointer and not to the object
|
| 411 |
-
pointed to. Similarly, the optional *attribute-specifier-seq*
|
| 412 |
-
[[dcl.attr.grammar]]
|
| 413 |
pointed to.
|
| 414 |
|
| 415 |
[*Example 1*:
|
| 416 |
|
| 417 |
The declarations
|
|
@@ -464,14 +489,14 @@ cv-unqualified pointer later, for example:
|
|
| 464 |
See also [[expr.ass]] and [[dcl.init]].
|
| 465 |
|
| 466 |
[*Note 1*: Forming a pointer to reference type is ill-formed; see
|
| 467 |
[[dcl.ref]]. Forming a function pointer type is ill-formed if the
|
| 468 |
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 469 |
-
[[dcl.fct]]. Since the address of a bit-field
|
| 470 |
taken, a pointer can never point to a bit-field. — *end note*]
|
| 471 |
|
| 472 |
-
### References <a id="dcl.ref">[[dcl.ref]]</a>
|
| 473 |
|
| 474 |
In a declaration `T` `D` where `D` has either of the forms
|
| 475 |
|
| 476 |
``` bnf
|
| 477 |
'&' attribute-specifier-seqₒₚₜ 'D1'
|
|
@@ -482,18 +507,18 @@ and the type of the identifier in the declaration `T` `D1` is
|
|
| 482 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 483 |
`D` is “*derived-declarator-type-list* reference to `T`”. The optional
|
| 484 |
*attribute-specifier-seq* appertains to the reference type. Cv-qualified
|
| 485 |
references are ill-formed except when the cv-qualifiers are introduced
|
| 486 |
through the use of a *typedef-name* ([[dcl.typedef]], [[temp.param]])
|
| 487 |
-
or *decltype-specifier*
|
| 488 |
cv-qualifiers are ignored.
|
| 489 |
|
| 490 |
[*Example 1*:
|
| 491 |
|
| 492 |
``` cpp
|
| 493 |
typedef int& A;
|
| 494 |
-
const A aref = 3; //
|
| 495 |
```
|
| 496 |
|
| 497 |
The type of `aref` is “lvalue reference to `int`”, not “lvalue reference
|
| 498 |
to `const int`”.
|
| 499 |
|
|
@@ -557,19 +582,19 @@ void k() {
|
|
| 557 |
declares `p` to be a reference to a pointer to `link` so `h(q)` will
|
| 558 |
leave `q` with the value zero. See also [[dcl.init.ref]].
|
| 559 |
|
| 560 |
— *end example*]
|
| 561 |
|
| 562 |
-
It is unspecified whether or not a reference requires storage
|
| 563 |
-
[[basic.stc]]
|
| 564 |
|
| 565 |
There shall be no references to references, no arrays of references, and
|
| 566 |
no pointers to references. The declaration of a reference shall contain
|
| 567 |
-
an *initializer*
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
declaration of a parameter or a return type
|
| 571 |
[[basic.def]]. A reference shall be initialized to refer to a valid
|
| 572 |
object or function.
|
| 573 |
|
| 574 |
[*Note 2*: In particular, a null reference cannot exist in a
|
| 575 |
well-defined program, because the only way to create such a reference
|
|
@@ -577,15 +602,14 @@ would be to bind it to the “object” obtained by indirection through a
|
|
| 577 |
null pointer, which causes undefined behavior. As described in
|
| 578 |
[[class.bit]], a reference cannot be bound directly to a
|
| 579 |
bit-field. — *end note*]
|
| 580 |
|
| 581 |
If a *typedef-name* ([[dcl.typedef]], [[temp.param]]) or a
|
| 582 |
-
*decltype-specifier*
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
type `TR`.
|
| 587 |
|
| 588 |
[*Note 3*: This rule is known as reference collapsing. — *end note*]
|
| 589 |
|
| 590 |
[*Example 3*:
|
| 591 |
|
|
@@ -609,11 +633,11 @@ decltype(r2)&& r7 = i; // r7 has the type int&
|
|
| 609 |
|
| 610 |
[*Note 4*: Forming a reference to function type is ill-formed if the
|
| 611 |
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 612 |
[[dcl.fct]]. — *end note*]
|
| 613 |
|
| 614 |
-
### Pointers to members <a id="dcl.mptr">[[dcl.mptr]]</a>
|
| 615 |
|
| 616 |
In a declaration `T` `D` where `D` has the form
|
| 617 |
|
| 618 |
``` bnf
|
| 619 |
nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
|
@@ -622,11 +646,11 @@ nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒ
|
|
| 622 |
and the *nested-name-specifier* denotes a class, and the type of the
|
| 623 |
identifier in the declaration `T` `D1` is
|
| 624 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 625 |
`D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
|
| 626 |
member of class *nested-name-specifier* of type `T`”. The optional
|
| 627 |
-
*attribute-specifier-seq*
|
| 628 |
pointer-to-member.
|
| 629 |
|
| 630 |
[*Example 1*:
|
| 631 |
|
| 632 |
``` cpp
|
|
@@ -657,102 +681,107 @@ obj.*pmi = 7; // assign 7 to an integer member of obj
|
|
| 657 |
(obj.*pmf)(7); // call a function member of obj with the argument 7
|
| 658 |
```
|
| 659 |
|
| 660 |
— *end example*]
|
| 661 |
|
| 662 |
-
A pointer to member shall not point to a static member of a class
|
| 663 |
-
[[class.static]]
|
| 664 |
|
| 665 |
[*Note 1*: See also [[expr.unary]] and [[expr.mptr.oper]]. The type
|
| 666 |
“pointer to member” is distinct from the type “pointer”, that is, a
|
| 667 |
-
pointer to member is declared only by the pointer
|
| 668 |
syntax, and never by the pointer declarator syntax. There is no
|
| 669 |
“reference-to-member” type in C++. — *end note*]
|
| 670 |
|
| 671 |
-
### Arrays <a id="dcl.array">[[dcl.array]]</a>
|
| 672 |
|
| 673 |
In a declaration `T` `D` where `D` has the form
|
| 674 |
|
| 675 |
``` bnf
|
| 676 |
-
'D1 [' constant-expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
|
| 677 |
```
|
| 678 |
|
| 679 |
-
and the type of the
|
| 680 |
-
“*derived-declarator-type-list* `T`”,
|
| 681 |
-
`D` is
|
| 682 |
-
`
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
`
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 702 |
|
| 703 |
[*Example 1*:
|
| 704 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 705 |
``` cpp
|
| 706 |
typedef int A[5], AA[2][3];
|
| 707 |
typedef const A CA; // type is ``array of 5 const int''
|
| 708 |
typedef const AA CAA; // type is ``array of 2 array of 3 const int''
|
| 709 |
```
|
| 710 |
|
| 711 |
— *end example*]
|
| 712 |
|
| 713 |
-
[*Note
|
| 714 |
type; see [[basic.type.qualifier]]. — *end note*]
|
| 715 |
|
| 716 |
-
An
|
| 717 |
-
|
| 718 |
-
|
| 719 |
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
is calculated from the number of initial elements (say, `N`) supplied (
|
| 730 |
-
[[dcl.init.aggr]]), and the type of the identifier of `D` is “array of
|
| 731 |
-
`N` `T`”. Furthermore, if there is a preceding declaration of the entity
|
| 732 |
-
in the same scope in which the bound was specified, an omitted array
|
| 733 |
-
bound is taken to be the same as in that earlier declaration, and
|
| 734 |
-
similarly for the definition of a static data member of a class.
|
| 735 |
|
| 736 |
-
|
|
|
|
|
|
|
|
|
|
| 737 |
|
| 738 |
-
|
| 739 |
-
float fa[17], *afp[17];
|
| 740 |
-
```
|
| 741 |
-
|
| 742 |
-
declares an array of `float` numbers and an array of pointers to `float`
|
| 743 |
-
numbers. For another example,
|
| 744 |
-
|
| 745 |
-
``` cpp
|
| 746 |
-
static int x3d[3][5][7];
|
| 747 |
-
```
|
| 748 |
-
|
| 749 |
-
declares a static three-dimensional array of integers, with rank
|
| 750 |
-
3 × 5 × 7. In complete detail, `x3d` is an array of three items; each
|
| 751 |
-
item is an array of five arrays; each of the latter arrays is an array
|
| 752 |
-
of seven integers. Any of the expressions `x3d`, `x3d[i]`, `x3d[i][j]`,
|
| 753 |
-
`x3d[i][j][k]` can reasonably appear in an expression. Finally,
|
| 754 |
|
| 755 |
``` cpp
|
| 756 |
extern int x[10];
|
| 757 |
struct S {
|
| 758 |
static int y[10];
|
|
@@ -767,102 +796,106 @@ void f() {
|
|
| 767 |
}
|
| 768 |
```
|
| 769 |
|
| 770 |
— *end example*]
|
| 771 |
|
| 772 |
-
[*Note
|
| 773 |
-
described in [[conv.array]]. Objects of array types cannot be modified,
|
| 774 |
-
see [[basic.lval]]. — *end note*]
|
| 775 |
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
the conversion rules that apply to `+`, if `E1` is an array and `E2` an
|
| 780 |
-
integer, then `E1[E2]` refers to the `E2`-th member of `E1`. Therefore,
|
| 781 |
-
despite its asymmetric appearance, subscripting is a commutative
|
| 782 |
-
operation. — *end note*]
|
| 783 |
|
| 784 |
-
[*
|
| 785 |
-
|
| 786 |
-
A consistent rule is followed for multidimensional arrays. If `E` is an
|
| 787 |
-
*n*-dimensional array of rank i × j × … × k, then `E` appearing in an
|
| 788 |
-
expression that is subject to the array-to-pointer conversion (
|
| 789 |
-
[[conv.array]]) is converted to a pointer to an (n-1)-dimensional array
|
| 790 |
-
with rank j × … × k. If the `*` operator, either explicitly or
|
| 791 |
-
implicitly as a result of subscripting, is applied to this pointer, the
|
| 792 |
-
result is the pointed-to (n-1)-dimensional array, which itself is
|
| 793 |
-
immediately converted into a pointer.
|
| 794 |
-
|
| 795 |
-
[*Example 3*:
|
| 796 |
-
|
| 797 |
-
Consider
|
| 798 |
|
| 799 |
``` cpp
|
| 800 |
-
int
|
| 801 |
```
|
| 802 |
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 813 |
|
| 814 |
— *end example*]
|
| 815 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 816 |
— *end note*]
|
| 817 |
|
| 818 |
-
[*Note
|
| 819 |
-
|
| 820 |
-
the declaration helps determine the amount of storage consumed by an
|
| 821 |
-
array but plays no other part in subscript calculations. — *end note*]
|
| 822 |
|
| 823 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 824 |
|
| 825 |
In a declaration `T` `D` where `D` has the form
|
| 826 |
|
| 827 |
``` bnf
|
| 828 |
-
'D1 (' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 829 |
ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 830 |
```
|
| 831 |
|
| 832 |
and the type of the contained *declarator-id* in the declaration `T`
|
| 833 |
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 834 |
-
*declarator-id* in `D` is “*derived-declarator-type-list* `noexcept`
|
| 835 |
-
function of
|
| 836 |
-
*ref-qualifier*ₒₚₜ returning `T`”, where
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 840 |
|
| 841 |
In a declaration `T` `D` where `D` has the form
|
| 842 |
|
| 843 |
``` bnf
|
| 844 |
-
'D1 (' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 845 |
ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-type
|
| 846 |
```
|
| 847 |
|
| 848 |
and the type of the contained *declarator-id* in the declaration `T`
|
| 849 |
`D1` is “*derived-declarator-type-list* `T`”, `T` shall be the single
|
| 850 |
*type-specifier* `auto`. The type of the *declarator-id* in `D` is
|
| 851 |
-
“*derived-declarator-type-list* `noexcept`
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
*trailing-return-type*, and where the optional `noexcept` is present if
|
| 855 |
-
and only if the exception specification is non-throwing. The optional
|
| 856 |
-
*attribute-specifier-seq* appertains to the function type.
|
| 857 |
|
| 858 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 859 |
|
| 860 |
``` bnf
|
| 861 |
parameter-declaration-clause:
|
| 862 |
parameter-declaration-listₒₚₜ '...'ₒₚₜ
|
| 863 |
-
parameter-declaration-list ', ...'
|
| 864 |
```
|
| 865 |
|
| 866 |
``` bnf
|
| 867 |
parameter-declaration-list:
|
| 868 |
parameter-declaration
|
|
@@ -888,17 +921,18 @@ arguments specified on the function call; see
|
|
| 888 |
[[expr.call]]. — *end note*]
|
| 889 |
|
| 890 |
If the *parameter-declaration-clause* is empty, the function takes no
|
| 891 |
arguments. A parameter list consisting of a single unnamed parameter of
|
| 892 |
non-dependent type `void` is equivalent to an empty parameter list.
|
| 893 |
-
Except for this special case, a parameter shall not have type
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
|
|
|
|
| 900 |
|
| 901 |
[*Example 1*:
|
| 902 |
|
| 903 |
The declaration
|
| 904 |
|
|
@@ -913,31 +947,28 @@ arguments.
|
|
| 913 |
printf("hello world");
|
| 914 |
printf("a=%d b=%d", a, b);
|
| 915 |
```
|
| 916 |
|
| 917 |
However, the first argument must be of a type that can be converted to a
|
| 918 |
-
`const` `char*`
|
| 919 |
|
| 920 |
— *end example*]
|
| 921 |
|
| 922 |
[*Note 2*: The standard header `<cstdarg>` contains a mechanism for
|
| 923 |
accessing arguments passed using the ellipsis (see [[expr.call]] and
|
| 924 |
[[support.runtime]]). — *end note*]
|
| 925 |
|
| 926 |
-
|
| 927 |
-
|
| 928 |
-
|
| 929 |
-
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
forming the function type. The resulting list of transformed parameter
|
| 937 |
-
types and the presence or absence of the ellipsis or a function
|
| 938 |
-
parameter pack is the function’s *parameter-type-list*.
|
| 939 |
|
| 940 |
[*Note 3*: This transformation does not affect the types of the
|
| 941 |
parameters. For example, `int(*)(const int p, decltype(p)*)` and
|
| 942 |
`int(*)(int, const int*)` are identical types. — *end note*]
|
| 943 |
|
|
@@ -947,20 +978,20 @@ A function type with a *cv-qualifier-seq* or a *ref-qualifier*
|
|
| 947 |
|
| 948 |
- the function type for a non-static member function,
|
| 949 |
- the function type to which a pointer to member refers,
|
| 950 |
- the top-level function type of a function typedef declaration or
|
| 951 |
*alias-declaration*,
|
| 952 |
-
- the *type-id* in the default argument of a *type-parameter*
|
| 953 |
-
[[temp.param]]
|
| 954 |
-
- the *type-id* of a *template-argument* for a *type-parameter*
|
| 955 |
-
[[temp.arg.type]]
|
| 956 |
|
| 957 |
[*Example 2*:
|
| 958 |
|
| 959 |
``` cpp
|
| 960 |
typedef int FIC(int) const;
|
| 961 |
-
FIC f; //
|
| 962 |
struct S {
|
| 963 |
FIC f; // OK
|
| 964 |
};
|
| 965 |
FIC S::*pm = &S::f; // OK
|
| 966 |
```
|
|
@@ -986,11 +1017,12 @@ struct S {
|
|
| 986 |
|
| 987 |
— *end example*]
|
| 988 |
|
| 989 |
The return type, the parameter-type-list, the *ref-qualifier*, the
|
| 990 |
*cv-qualifier-seq*, and the exception specification, but not the default
|
| 991 |
-
arguments
|
|
|
|
| 992 |
|
| 993 |
[*Note 5*: Function types are checked during the assignments and
|
| 994 |
initializations of pointers to functions, references to functions, and
|
| 995 |
pointers to member functions. — *end note*]
|
| 996 |
|
|
@@ -1001,48 +1033,52 @@ The declaration
|
|
| 1001 |
``` cpp
|
| 1002 |
int fseek(FILE*, long, int);
|
| 1003 |
```
|
| 1004 |
|
| 1005 |
declares a function taking three arguments of the specified types, and
|
| 1006 |
-
returning `int`
|
| 1007 |
|
| 1008 |
— *end example*]
|
| 1009 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1010 |
Functions shall not have a return type of type array or function,
|
| 1011 |
although they may have a return type of type pointer or reference to
|
| 1012 |
such things. There shall be no arrays of functions, although there can
|
| 1013 |
be arrays of pointers to functions.
|
| 1014 |
|
| 1015 |
-
|
| 1016 |
-
|
| 1017 |
-
|
| 1018 |
-
|
| 1019 |
-
[[dcl.fct.def.delete]]).
|
| 1020 |
|
| 1021 |
A typedef of function type may be used to declare a function but shall
|
| 1022 |
-
not be used to define a function
|
| 1023 |
|
| 1024 |
[*Example 5*:
|
| 1025 |
|
| 1026 |
``` cpp
|
| 1027 |
typedef void F();
|
| 1028 |
F fv; // OK: equivalent to void fv();
|
| 1029 |
-
F fv { } //
|
| 1030 |
void fv() { } // OK: definition of fv
|
| 1031 |
```
|
| 1032 |
|
| 1033 |
— *end example*]
|
| 1034 |
|
| 1035 |
An identifier can optionally be provided as a parameter name; if present
|
| 1036 |
-
in a function definition
|
| 1037 |
|
| 1038 |
[*Note 6*: In particular, parameter names are also optional in function
|
| 1039 |
definitions and names used for a parameter in different declarations and
|
| 1040 |
the definition of a function need not be the same. If a parameter name
|
| 1041 |
is present in a function declaration that is not a definition, it cannot
|
| 1042 |
be used outside of its function declarator because that is the extent of
|
| 1043 |
-
its potential scope
|
| 1044 |
|
| 1045 |
[*Example 6*:
|
| 1046 |
|
| 1047 |
The declaration
|
| 1048 |
|
|
@@ -1107,23 +1143,100 @@ template <class T, class U> decltype((*(T*)0) + (*(U*)0)) add(T t, U u);
|
|
| 1107 |
A *non-template function* is a function that is not a function template
|
| 1108 |
specialization.
|
| 1109 |
|
| 1110 |
[*Note 8*: A function template is not a function. — *end note*]
|
| 1111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1112 |
A *declarator-id* or *abstract-declarator* containing an ellipsis shall
|
| 1113 |
-
only be used in a *parameter-declaration*.
|
| 1114 |
-
*parameter-declaration*
|
| 1115 |
-
|
| 1116 |
-
|
|
|
|
|
|
|
| 1117 |
|
| 1118 |
-
[*
|
| 1119 |
-
*template-parameter-list* and the parameter pack is a template parameter
|
| 1120 |
-
pack; see [[temp.param]]. — *end note*]
|
| 1121 |
-
|
| 1122 |
-
A function parameter pack is a pack expansion ([[temp.variadic]]).
|
| 1123 |
-
|
| 1124 |
-
[*Example 7*:
|
| 1125 |
|
| 1126 |
``` cpp
|
| 1127 |
template<typename... T> void f(T (* ...t)(int, int));
|
| 1128 |
|
| 1129 |
int add(int, int);
|
|
@@ -1139,17 +1252,19 @@ void g() {
|
|
| 1139 |
There is a syntactic ambiguity when an ellipsis occurs at the end of a
|
| 1140 |
*parameter-declaration-clause* without a preceding comma. In this case,
|
| 1141 |
the ellipsis is parsed as part of the *abstract-declarator* if the type
|
| 1142 |
of the parameter either names a template parameter pack that has not
|
| 1143 |
been expanded or contains `auto`; otherwise, it is parsed as part of the
|
| 1144 |
-
*parameter-declaration-clause*.[^
|
| 1145 |
|
| 1146 |
-
### Default arguments <a id="dcl.fct.default">[[dcl.fct.default]]</a>
|
| 1147 |
|
| 1148 |
If an *initializer-clause* is specified in a *parameter-declaration*
|
| 1149 |
-
this *initializer-clause* is used as a default argument.
|
| 1150 |
-
|
|
|
|
|
|
|
| 1151 |
|
| 1152 |
[*Example 1*:
|
| 1153 |
|
| 1154 |
The declaration
|
| 1155 |
|
|
@@ -1169,27 +1284,30 @@ respectively.
|
|
| 1169 |
|
| 1170 |
— *end example*]
|
| 1171 |
|
| 1172 |
A default argument shall be specified only in the
|
| 1173 |
*parameter-declaration-clause* of a function declaration or
|
| 1174 |
-
*lambda-declarator* or in a *template-parameter*
|
| 1175 |
-
|
| 1176 |
*assignment-expression*. A default argument shall not be specified for a
|
| 1177 |
-
parameter pack. If it is specified
|
| 1178 |
-
it shall not occur within a
|
| 1179 |
-
*parameter-declaration*.[^
|
| 1180 |
|
| 1181 |
For non-template functions, default arguments can be added in later
|
| 1182 |
declarations of a function in the same scope. Declarations in different
|
| 1183 |
scopes have completely distinct sets of default arguments. That is,
|
| 1184 |
declarations in inner scopes do not acquire default arguments from
|
| 1185 |
declarations in outer scopes, and vice versa. In a given function
|
| 1186 |
declaration, each parameter subsequent to a parameter with a default
|
| 1187 |
argument shall have a default argument supplied in this or a previous
|
| 1188 |
-
declaration
|
| 1189 |
-
shall
|
| 1190 |
-
|
|
|
|
|
|
|
|
|
|
| 1191 |
|
| 1192 |
[*Example 2*:
|
| 1193 |
|
| 1194 |
``` cpp
|
| 1195 |
void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow
|
|
@@ -1208,28 +1326,32 @@ void m() {
|
|
| 1208 |
void f(int, int = 5); // error: cannot redefine, even to same value
|
| 1209 |
}
|
| 1210 |
void n() {
|
| 1211 |
f(6); // OK, calls f(6, 7)
|
| 1212 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1213 |
```
|
| 1214 |
|
| 1215 |
— *end example*]
|
| 1216 |
|
| 1217 |
For a given inline function defined in different translation units, the
|
| 1218 |
accumulated sets of default arguments at the end of the translation
|
| 1219 |
-
units shall be the same;
|
| 1220 |
-
specifies a default argument expression, that declaration
|
| 1221 |
-
definition and shall be the only declaration of the function
|
| 1222 |
-
template in the translation unit.
|
| 1223 |
|
| 1224 |
The default argument has the same semantic constraints as the
|
| 1225 |
initializer in a declaration of a variable of the parameter type, using
|
| 1226 |
-
the copy-initialization semantics
|
| 1227 |
-
|
| 1228 |
-
|
| 1229 |
-
|
| 1230 |
-
|
| 1231 |
[[temp.inst]].
|
| 1232 |
|
| 1233 |
[*Example 3*:
|
| 1234 |
|
| 1235 |
In the following code, `g` will be called with the value `f(2)`:
|
|
@@ -1248,24 +1370,24 @@ void h() {
|
|
| 1248 |
}
|
| 1249 |
```
|
| 1250 |
|
| 1251 |
— *end example*]
|
| 1252 |
|
| 1253 |
-
[*Note
|
| 1254 |
are looked up as described in [[basic.lookup.unqual]]. Access checking
|
| 1255 |
-
applies to names in default arguments as described in
|
| 1256 |
[[class.access]]. — *end note*]
|
| 1257 |
|
| 1258 |
Except for member functions of class templates, the default arguments in
|
| 1259 |
a member function definition that appears outside of the class
|
| 1260 |
definition are added to the set of default arguments provided by the
|
| 1261 |
member function declaration in the class definition; the program is
|
| 1262 |
-
ill-formed if a default constructor
|
| 1263 |
-
constructor, or copy or move assignment operator
|
| 1264 |
-
declared. Default arguments for a member
|
| 1265 |
-
shall be specified on the initial
|
| 1266 |
-
within the class template.
|
| 1267 |
|
| 1268 |
[*Example 4*:
|
| 1269 |
|
| 1270 |
``` cpp
|
| 1271 |
class C {
|
|
@@ -1277,12 +1399,12 @@ void C::f(int i = 3) {} // error: default argument already specified in
|
|
| 1277 |
void C::g(int i = 88, int j) {} // in this translation unit, C::g can be called with no argument
|
| 1278 |
```
|
| 1279 |
|
| 1280 |
— *end example*]
|
| 1281 |
|
| 1282 |
-
A local variable
|
| 1283 |
-
|
| 1284 |
|
| 1285 |
[*Example 5*:
|
| 1286 |
|
| 1287 |
``` cpp
|
| 1288 |
void f() {
|
|
@@ -1293,11 +1415,11 @@ void f() {
|
|
| 1293 |
}
|
| 1294 |
```
|
| 1295 |
|
| 1296 |
— *end example*]
|
| 1297 |
|
| 1298 |
-
[*Note
|
| 1299 |
|
| 1300 |
The keyword `this` may not appear in a default argument of a member
|
| 1301 |
function; see [[expr.prim.this]].
|
| 1302 |
|
| 1303 |
[*Example 6*:
|
|
@@ -1329,13 +1451,13 @@ int h(int a, int b = sizeof(a)); // OK, unevaluated operand
|
|
| 1329 |
```
|
| 1330 |
|
| 1331 |
— *end example*]
|
| 1332 |
|
| 1333 |
A non-static member shall not appear in a default argument unless it
|
| 1334 |
-
appears as the *id-expression* of a class member access expression
|
| 1335 |
-
[[expr.ref]]
|
| 1336 |
-
[[expr.unary.op]]
|
| 1337 |
|
| 1338 |
[*Example 8*:
|
| 1339 |
|
| 1340 |
The declaration of `X::mem1()` in the following example is ill-formed
|
| 1341 |
because no object is supplied for the non-static member `X::a` used as
|
|
@@ -1351,11 +1473,11 @@ class X {
|
|
| 1351 |
};
|
| 1352 |
```
|
| 1353 |
|
| 1354 |
The declaration of `X::mem2()` is meaningful, however, since no object
|
| 1355 |
is needed to access the static member `X::b`. Classes, objects, and
|
| 1356 |
-
members are described in
|
| 1357 |
|
| 1358 |
— *end example*]
|
| 1359 |
|
| 1360 |
A default argument is not part of the type of a function.
|
| 1361 |
|
|
@@ -1374,21 +1496,21 @@ int (*p2)() = &f; // error: type mismatch
|
|
| 1374 |
```
|
| 1375 |
|
| 1376 |
— *end example*]
|
| 1377 |
|
| 1378 |
When a declaration of a function is introduced by way of a
|
| 1379 |
-
*using-declaration*
|
| 1380 |
information associated with the declaration is made known as well. If
|
| 1381 |
the function is redeclared thereafter in the namespace with additional
|
| 1382 |
default arguments, the additional arguments are also known at any point
|
| 1383 |
following the redeclaration where the *using-declaration* is in scope.
|
| 1384 |
|
| 1385 |
-
A virtual function call
|
| 1386 |
-
|
| 1387 |
-
|
| 1388 |
-
|
| 1389 |
-
|
| 1390 |
|
| 1391 |
[*Example 10*:
|
| 1392 |
|
| 1393 |
``` cpp
|
| 1394 |
struct A {
|
|
@@ -1405,1931 +1527,5 @@ void m() {
|
|
| 1405 |
}
|
| 1406 |
```
|
| 1407 |
|
| 1408 |
— *end example*]
|
| 1409 |
|
| 1410 |
-
## Function definitions <a id="dcl.fct.def">[[dcl.fct.def]]</a>
|
| 1411 |
-
|
| 1412 |
-
### In general <a id="dcl.fct.def.general">[[dcl.fct.def.general]]</a>
|
| 1413 |
-
|
| 1414 |
-
Function definitions have the form
|
| 1415 |
-
|
| 1416 |
-
``` bnf
|
| 1417 |
-
function-definition:
|
| 1418 |
-
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator virt-specifier-seqₒₚₜ function-body
|
| 1419 |
-
```
|
| 1420 |
-
|
| 1421 |
-
``` bnf
|
| 1422 |
-
function-body:
|
| 1423 |
-
ctor-initializerₒₚₜ compound-statement
|
| 1424 |
-
function-try-block
|
| 1425 |
-
'= default ;'
|
| 1426 |
-
'= delete ;'
|
| 1427 |
-
```
|
| 1428 |
-
|
| 1429 |
-
Any informal reference to the body of a function should be interpreted
|
| 1430 |
-
as a reference to the non-terminal *function-body*. The optional
|
| 1431 |
-
*attribute-specifier-seq* in a *function-definition* appertains to the
|
| 1432 |
-
function. A *virt-specifier-seq* can be part of a *function-definition*
|
| 1433 |
-
only if it is a *member-declaration* ([[class.mem]]).
|
| 1434 |
-
|
| 1435 |
-
In a *function-definition*, either `void` *declarator* `;` or
|
| 1436 |
-
*declarator* `;` shall be a well-formed function declaration as
|
| 1437 |
-
described in [[dcl.fct]]. A function shall be defined only in namespace
|
| 1438 |
-
or class scope.
|
| 1439 |
-
|
| 1440 |
-
[*Example 1*:
|
| 1441 |
-
|
| 1442 |
-
A simple example of a complete function definition is
|
| 1443 |
-
|
| 1444 |
-
``` cpp
|
| 1445 |
-
int max(int a, int b, int c) {
|
| 1446 |
-
int m = (a > b) ? a : b;
|
| 1447 |
-
return (m > c) ? m : c;
|
| 1448 |
-
}
|
| 1449 |
-
```
|
| 1450 |
-
|
| 1451 |
-
Here `int` is the *decl-specifier-seq*; `max(int` `a,` `int` `b,` `int`
|
| 1452 |
-
`c)` is the *declarator*; `{ /* ... */ }` is the *function-body*.
|
| 1453 |
-
|
| 1454 |
-
— *end example*]
|
| 1455 |
-
|
| 1456 |
-
A *ctor-initializer* is used only in a constructor; see [[class.ctor]]
|
| 1457 |
-
and [[class.init]].
|
| 1458 |
-
|
| 1459 |
-
[*Note 1*: A *cv-qualifier-seq* affects the type of `this` in the body
|
| 1460 |
-
of a member function; see [[dcl.ref]]. — *end note*]
|
| 1461 |
-
|
| 1462 |
-
[*Note 2*:
|
| 1463 |
-
|
| 1464 |
-
Unused parameters need not be named. For example,
|
| 1465 |
-
|
| 1466 |
-
``` cpp
|
| 1467 |
-
void print(int a, int) {
|
| 1468 |
-
std::printf("a = %d\n",a);
|
| 1469 |
-
}
|
| 1470 |
-
```
|
| 1471 |
-
|
| 1472 |
-
— *end note*]
|
| 1473 |
-
|
| 1474 |
-
In the *function-body*, a *function-local predefined variable* denotes a
|
| 1475 |
-
block-scope object of static storage duration that is implicitly defined
|
| 1476 |
-
(see [[basic.scope.block]]).
|
| 1477 |
-
|
| 1478 |
-
The function-local predefined variable `__func__` is defined as if a
|
| 1479 |
-
definition of the form
|
| 1480 |
-
|
| 1481 |
-
``` cpp
|
| 1482 |
-
static const char __func__[] = "function-name";
|
| 1483 |
-
```
|
| 1484 |
-
|
| 1485 |
-
had been provided, where *function-name* is an *implementation-defined*
|
| 1486 |
-
string. It is unspecified whether such a variable has an address
|
| 1487 |
-
distinct from that of any other object in the program.[^11]
|
| 1488 |
-
|
| 1489 |
-
[*Example 2*:
|
| 1490 |
-
|
| 1491 |
-
``` cpp
|
| 1492 |
-
struct S {
|
| 1493 |
-
S() : s(__func__) { } // OK
|
| 1494 |
-
const char* s;
|
| 1495 |
-
};
|
| 1496 |
-
void f(const char* s = __func__); // error: __func__ is undeclared
|
| 1497 |
-
```
|
| 1498 |
-
|
| 1499 |
-
— *end example*]
|
| 1500 |
-
|
| 1501 |
-
### Explicitly-defaulted functions <a id="dcl.fct.def.default">[[dcl.fct.def.default]]</a>
|
| 1502 |
-
|
| 1503 |
-
A function definition of the form:
|
| 1504 |
-
|
| 1505 |
-
``` bnf
|
| 1506 |
-
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator virt-specifier-seqₒₚₜ ' = default ;'
|
| 1507 |
-
```
|
| 1508 |
-
|
| 1509 |
-
is called an *explicitly-defaulted* definition. A function that is
|
| 1510 |
-
explicitly defaulted shall
|
| 1511 |
-
|
| 1512 |
-
- be a special member function,
|
| 1513 |
-
- have the same declared function type (except for possibly differing
|
| 1514 |
-
*ref-qualifier*s and except that in the case of a copy constructor or
|
| 1515 |
-
copy assignment operator, the parameter type may be “reference to
|
| 1516 |
-
non-const `T`”, where `T` is the name of the member function’s class)
|
| 1517 |
-
as if it had been implicitly declared, and
|
| 1518 |
-
- not have default arguments.
|
| 1519 |
-
|
| 1520 |
-
An explicitly-defaulted function that is not defined as deleted may be
|
| 1521 |
-
declared `constexpr` only if it would have been implicitly declared as
|
| 1522 |
-
`constexpr`. If a function is explicitly defaulted on its first
|
| 1523 |
-
declaration, it is implicitly considered to be `constexpr` if the
|
| 1524 |
-
implicit declaration would be.
|
| 1525 |
-
|
| 1526 |
-
If a function that is explicitly defaulted is declared with a
|
| 1527 |
-
*noexcept-specifier* that does not produce the same exception
|
| 1528 |
-
specification as the implicit declaration ([[except.spec]]), then
|
| 1529 |
-
|
| 1530 |
-
- if the function is explicitly defaulted on its first declaration, it
|
| 1531 |
-
is defined as deleted;
|
| 1532 |
-
- otherwise, the program is ill-formed.
|
| 1533 |
-
|
| 1534 |
-
[*Example 1*:
|
| 1535 |
-
|
| 1536 |
-
``` cpp
|
| 1537 |
-
struct S {
|
| 1538 |
-
constexpr S() = default; // ill-formed: implicit S() is not constexpr
|
| 1539 |
-
S(int a = 0) = default; // ill-formed: default argument
|
| 1540 |
-
void operator=(const S&) = default; // ill-formed: non-matching return type
|
| 1541 |
-
~S() noexcept(false) = default; // deleted: exception specification does not match
|
| 1542 |
-
private:
|
| 1543 |
-
int i;
|
| 1544 |
-
S(S&); // OK: private copy constructor
|
| 1545 |
-
};
|
| 1546 |
-
S::S(S&) = default; // OK: defines copy constructor
|
| 1547 |
-
```
|
| 1548 |
-
|
| 1549 |
-
— *end example*]
|
| 1550 |
-
|
| 1551 |
-
Explicitly-defaulted functions and implicitly-declared functions are
|
| 1552 |
-
collectively called *defaulted* functions, and the implementation shall
|
| 1553 |
-
provide implicit definitions for them ([[class.ctor]] [[class.dtor]],
|
| 1554 |
-
[[class.copy]]), which might mean defining them as deleted. A function
|
| 1555 |
-
is *user-provided* if it is user-declared and not explicitly defaulted
|
| 1556 |
-
or deleted on its first declaration. A user-provided
|
| 1557 |
-
explicitly-defaulted function (i.e., explicitly defaulted after its
|
| 1558 |
-
first declaration) is defined at the point where it is explicitly
|
| 1559 |
-
defaulted; if such a function is implicitly defined as deleted, the
|
| 1560 |
-
program is ill-formed.
|
| 1561 |
-
|
| 1562 |
-
[*Note 1*: Declaring a function as defaulted after its first
|
| 1563 |
-
declaration can provide efficient execution and concise definition while
|
| 1564 |
-
enabling a stable binary interface to an evolving code
|
| 1565 |
-
base. — *end note*]
|
| 1566 |
-
|
| 1567 |
-
[*Example 2*:
|
| 1568 |
-
|
| 1569 |
-
``` cpp
|
| 1570 |
-
struct trivial {
|
| 1571 |
-
trivial() = default;
|
| 1572 |
-
trivial(const trivial&) = default;
|
| 1573 |
-
trivial(trivial&&) = default;
|
| 1574 |
-
trivial& operator=(const trivial&) = default;
|
| 1575 |
-
trivial& operator=(trivial&&) = default;
|
| 1576 |
-
~trivial() = default;
|
| 1577 |
-
};
|
| 1578 |
-
|
| 1579 |
-
struct nontrivial1 {
|
| 1580 |
-
nontrivial1();
|
| 1581 |
-
};
|
| 1582 |
-
nontrivial1::nontrivial1() = default; // not first declaration
|
| 1583 |
-
```
|
| 1584 |
-
|
| 1585 |
-
— *end example*]
|
| 1586 |
-
|
| 1587 |
-
### Deleted definitions <a id="dcl.fct.def.delete">[[dcl.fct.def.delete]]</a>
|
| 1588 |
-
|
| 1589 |
-
A function definition of the form:
|
| 1590 |
-
|
| 1591 |
-
``` bnf
|
| 1592 |
-
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator virt-specifier-seqₒₚₜ ' = delete ;'
|
| 1593 |
-
```
|
| 1594 |
-
|
| 1595 |
-
is called a *deleted definition*. A function with a deleted definition
|
| 1596 |
-
is also called a *deleted function*.
|
| 1597 |
-
|
| 1598 |
-
A program that refers to a deleted function implicitly or explicitly,
|
| 1599 |
-
other than to declare it, is ill-formed.
|
| 1600 |
-
|
| 1601 |
-
[*Note 1*: This includes calling the function implicitly or explicitly
|
| 1602 |
-
and forming a pointer or pointer-to-member to the function. It applies
|
| 1603 |
-
even for references in expressions that are not potentially-evaluated.
|
| 1604 |
-
If a function is overloaded, it is referenced only if the function is
|
| 1605 |
-
selected by overload resolution. The implicit odr-use (
|
| 1606 |
-
[[basic.def.odr]]) of a virtual function does not, by itself, constitute
|
| 1607 |
-
a reference. — *end note*]
|
| 1608 |
-
|
| 1609 |
-
[*Example 1*:
|
| 1610 |
-
|
| 1611 |
-
One can enforce non-default-initialization and non-integral
|
| 1612 |
-
initialization with
|
| 1613 |
-
|
| 1614 |
-
``` cpp
|
| 1615 |
-
struct onlydouble {
|
| 1616 |
-
onlydouble() = delete; // OK, but redundant
|
| 1617 |
-
onlydouble(std::intmax_t) = delete;
|
| 1618 |
-
onlydouble(double);
|
| 1619 |
-
};
|
| 1620 |
-
```
|
| 1621 |
-
|
| 1622 |
-
— *end example*]
|
| 1623 |
-
|
| 1624 |
-
[*Example 2*:
|
| 1625 |
-
|
| 1626 |
-
One can prevent use of a class in certain *new-expression*s by using
|
| 1627 |
-
deleted definitions of a user-declared `operator new` for that class.
|
| 1628 |
-
|
| 1629 |
-
``` cpp
|
| 1630 |
-
struct sometype {
|
| 1631 |
-
void* operator new(std::size_t) = delete;
|
| 1632 |
-
void* operator new[](std::size_t) = delete;
|
| 1633 |
-
};
|
| 1634 |
-
sometype* p = new sometype; // error, deleted class operator new
|
| 1635 |
-
sometype* q = new sometype[3]; // error, deleted class operator new[]
|
| 1636 |
-
```
|
| 1637 |
-
|
| 1638 |
-
— *end example*]
|
| 1639 |
-
|
| 1640 |
-
[*Example 3*:
|
| 1641 |
-
|
| 1642 |
-
One can make a class uncopyable, i.e. move-only, by using deleted
|
| 1643 |
-
definitions of the copy constructor and copy assignment operator, and
|
| 1644 |
-
then providing defaulted definitions of the move constructor and move
|
| 1645 |
-
assignment operator.
|
| 1646 |
-
|
| 1647 |
-
``` cpp
|
| 1648 |
-
struct moveonly {
|
| 1649 |
-
moveonly() = default;
|
| 1650 |
-
moveonly(const moveonly&) = delete;
|
| 1651 |
-
moveonly(moveonly&&) = default;
|
| 1652 |
-
moveonly& operator=(const moveonly&) = delete;
|
| 1653 |
-
moveonly& operator=(moveonly&&) = default;
|
| 1654 |
-
~moveonly() = default;
|
| 1655 |
-
};
|
| 1656 |
-
moveonly* p;
|
| 1657 |
-
moveonly q(*p); // error, deleted copy constructor
|
| 1658 |
-
```
|
| 1659 |
-
|
| 1660 |
-
— *end example*]
|
| 1661 |
-
|
| 1662 |
-
A deleted function is implicitly an inline function ([[dcl.inline]]).
|
| 1663 |
-
|
| 1664 |
-
[*Note 2*: The one-definition rule ([[basic.def.odr]]) applies to
|
| 1665 |
-
deleted definitions. — *end note*]
|
| 1666 |
-
|
| 1667 |
-
A deleted definition of a function shall be the first declaration of the
|
| 1668 |
-
function or, for an explicit specialization of a function template, the
|
| 1669 |
-
first declaration of that specialization. An implicitly declared
|
| 1670 |
-
allocation or deallocation function ([[basic.stc.dynamic]]) shall not
|
| 1671 |
-
be defined as deleted.
|
| 1672 |
-
|
| 1673 |
-
[*Example 4*:
|
| 1674 |
-
|
| 1675 |
-
``` cpp
|
| 1676 |
-
struct sometype {
|
| 1677 |
-
sometype();
|
| 1678 |
-
};
|
| 1679 |
-
sometype::sometype() = delete; // ill-formed; not first declaration
|
| 1680 |
-
```
|
| 1681 |
-
|
| 1682 |
-
— *end example*]
|
| 1683 |
-
|
| 1684 |
-
## Structured binding declarations <a id="dcl.struct.bind">[[dcl.struct.bind]]</a>
|
| 1685 |
-
|
| 1686 |
-
A structured binding declaration introduces the *identifier*s `v`₀,
|
| 1687 |
-
`v`₁, `v`₂, ... of the *identifier-list* as names (
|
| 1688 |
-
[[basic.scope.declarative]]), called *structured binding*s. Let cv
|
| 1689 |
-
denote the *cv-qualifier*s in the *decl-specifier-seq*. First, a
|
| 1690 |
-
variable with a unique name `e` is introduced. If the
|
| 1691 |
-
*assignment-expression* in the *initializer* has array type `A` and no
|
| 1692 |
-
*ref-qualifier* is present, `e` has type cv `A` and each element is
|
| 1693 |
-
copy-initialized or direct-initialized from the corresponding element of
|
| 1694 |
-
the *assignment-expression* as specified by the form of the
|
| 1695 |
-
*initializer*. Otherwise, `e` is defined as-if by
|
| 1696 |
-
|
| 1697 |
-
``` bnf
|
| 1698 |
-
attribute-specifier-seqₒₚₜ decl-specifier-seq ref-qualifierₒₚₜ 'e' initializer ';'
|
| 1699 |
-
```
|
| 1700 |
-
|
| 1701 |
-
where the declaration is never interpreted as a function declaration and
|
| 1702 |
-
the parts of the declaration other than the *declarator-id* are taken
|
| 1703 |
-
from the corresponding structured binding declaration. The type of the
|
| 1704 |
-
*id-expression* `e` is called `E`.
|
| 1705 |
-
|
| 1706 |
-
[*Note 1*: `E` is never a reference type (Clause
|
| 1707 |
-
[[expr]]). — *end note*]
|
| 1708 |
-
|
| 1709 |
-
If `E` is an array type with element type `T`, the number of elements in
|
| 1710 |
-
the *identifier-list* shall be equal to the number of elements of `E`.
|
| 1711 |
-
Each `v`ᵢ is the name of an lvalue that refers to the element i of the
|
| 1712 |
-
array and whose type is `T`; the referenced type is `T`.
|
| 1713 |
-
|
| 1714 |
-
[*Note 2*: The top-level cv-qualifiers of `T` are cv. — *end note*]
|
| 1715 |
-
|
| 1716 |
-
[*Example 1*:
|
| 1717 |
-
|
| 1718 |
-
``` cpp
|
| 1719 |
-
auto f() -> int(&)[2];
|
| 1720 |
-
auto [ x, y ] = f(); // x and y refer to elements in a copy of the array return value
|
| 1721 |
-
auto& [ xr, yr ] = f(); // xr and yr refer to elements in the array referred to by f's return value
|
| 1722 |
-
```
|
| 1723 |
-
|
| 1724 |
-
— *end example*]
|
| 1725 |
-
|
| 1726 |
-
Otherwise, if the *qualified-id* `std::tuple_size<E>` names a complete
|
| 1727 |
-
type, the expression `std::tuple_size<E>::value` shall be a well-formed
|
| 1728 |
-
integral constant expression and the number of elements in the
|
| 1729 |
-
*identifier-list* shall be equal to the value of that expression. The
|
| 1730 |
-
*unqualified-id* `get` is looked up in the scope of `E` by class member
|
| 1731 |
-
access lookup ([[basic.lookup.classref]]), and if that finds at least
|
| 1732 |
-
one declaration, the initializer is `e.get<i>()`. Otherwise, the
|
| 1733 |
-
initializer is `get<i>(e)`, where `get` is looked up in the associated
|
| 1734 |
-
namespaces ([[basic.lookup.argdep]]). In either case, `get<i>` is
|
| 1735 |
-
interpreted as a *template-id*.
|
| 1736 |
-
|
| 1737 |
-
[*Note 3*: Ordinary unqualified lookup ([[basic.lookup.unqual]]) is
|
| 1738 |
-
not performed. — *end note*]
|
| 1739 |
-
|
| 1740 |
-
In either case, `e` is an lvalue if the type of the entity `e` is an
|
| 1741 |
-
lvalue reference and an xvalue otherwise. Given the type `Tᵢ` designated
|
| 1742 |
-
by `std::tuple_element<i, E>::type`, each `v`ᵢ is a variable of type
|
| 1743 |
-
“reference to `Tᵢ`” initialized with the initializer, where the
|
| 1744 |
-
reference is an lvalue reference if the initializer is an lvalue and an
|
| 1745 |
-
rvalue reference otherwise; the referenced type is `Tᵢ`.
|
| 1746 |
-
|
| 1747 |
-
Otherwise, all of `E`’s non-static data members shall be public direct
|
| 1748 |
-
members of `E` or of the same unambiguous public base class of `E`, `E`
|
| 1749 |
-
shall not have an anonymous union member, and the number of elements in
|
| 1750 |
-
the *identifier-list* shall be equal to the number of non-static data
|
| 1751 |
-
members of `E`. Designating the non-static data members of `E` as `m`₀,
|
| 1752 |
-
`m`₁, `m`₂, ... (in declaration order), each `v`ᵢ is the name of an
|
| 1753 |
-
lvalue that refers to the member `m`ᵢ of `e` and whose type is cv `Tᵢ`,
|
| 1754 |
-
where `Tᵢ` is the declared type of that member; the referenced type is
|
| 1755 |
-
cv `Tᵢ`. The lvalue is a bit-field if that member is a bit-field.
|
| 1756 |
-
|
| 1757 |
-
[*Example 2*:
|
| 1758 |
-
|
| 1759 |
-
``` cpp
|
| 1760 |
-
struct S { int x1 : 2; volatile double y1; };
|
| 1761 |
-
S f();
|
| 1762 |
-
const auto [ x, y ] = f();
|
| 1763 |
-
```
|
| 1764 |
-
|
| 1765 |
-
The type of the *id-expression* `x` is “`const int`”, the type of the
|
| 1766 |
-
*id-expression* `y` is “`const volatile double`”.
|
| 1767 |
-
|
| 1768 |
-
— *end example*]
|
| 1769 |
-
|
| 1770 |
-
## Initializers <a id="dcl.init">[[dcl.init]]</a>
|
| 1771 |
-
|
| 1772 |
-
A declarator can specify an initial value for the identifier being
|
| 1773 |
-
declared. The identifier designates a variable being initialized. The
|
| 1774 |
-
process of initialization described in the remainder of [[dcl.init]]
|
| 1775 |
-
applies also to initializations specified by other syntactic contexts,
|
| 1776 |
-
such as the initialization of function parameters ([[expr.call]]) or
|
| 1777 |
-
the initialization of return values ([[stmt.return]]).
|
| 1778 |
-
|
| 1779 |
-
``` bnf
|
| 1780 |
-
initializer:
|
| 1781 |
-
brace-or-equal-initializer
|
| 1782 |
-
'(' expression-list ')'
|
| 1783 |
-
```
|
| 1784 |
-
|
| 1785 |
-
``` bnf
|
| 1786 |
-
brace-or-equal-initializer:
|
| 1787 |
-
'=' initializer-clause
|
| 1788 |
-
braced-init-list
|
| 1789 |
-
```
|
| 1790 |
-
|
| 1791 |
-
``` bnf
|
| 1792 |
-
initializer-clause:
|
| 1793 |
-
assignment-expression
|
| 1794 |
-
braced-init-list
|
| 1795 |
-
```
|
| 1796 |
-
|
| 1797 |
-
``` bnf
|
| 1798 |
-
initializer-list:
|
| 1799 |
-
initializer-clause '...'ₒₚₜ
|
| 1800 |
-
initializer-list ',' initializer-clause '...'ₒₚₜ
|
| 1801 |
-
```
|
| 1802 |
-
|
| 1803 |
-
``` bnf
|
| 1804 |
-
braced-init-list:
|
| 1805 |
-
'{' initializer-list ','ₒₚₜ '}'
|
| 1806 |
-
'{' '}'
|
| 1807 |
-
```
|
| 1808 |
-
|
| 1809 |
-
``` bnf
|
| 1810 |
-
expr-or-braced-init-list:
|
| 1811 |
-
expression
|
| 1812 |
-
braced-init-list
|
| 1813 |
-
```
|
| 1814 |
-
|
| 1815 |
-
Except for objects declared with the `constexpr` specifier, for which
|
| 1816 |
-
see [[dcl.constexpr]], an *initializer* in the definition of a variable
|
| 1817 |
-
can consist of arbitrary expressions involving literals and previously
|
| 1818 |
-
declared variables and functions, regardless of the variable’s storage
|
| 1819 |
-
duration.
|
| 1820 |
-
|
| 1821 |
-
[*Example 1*:
|
| 1822 |
-
|
| 1823 |
-
``` cpp
|
| 1824 |
-
int f(int);
|
| 1825 |
-
int a = 2;
|
| 1826 |
-
int b = f(a);
|
| 1827 |
-
int c(b);
|
| 1828 |
-
```
|
| 1829 |
-
|
| 1830 |
-
— *end example*]
|
| 1831 |
-
|
| 1832 |
-
[*Note 1*: Default arguments are more restricted; see
|
| 1833 |
-
[[dcl.fct.default]]. — *end note*]
|
| 1834 |
-
|
| 1835 |
-
[*Note 2*: The order of initialization of variables with static storage
|
| 1836 |
-
duration is described in [[basic.start]] and
|
| 1837 |
-
[[stmt.dcl]]. — *end note*]
|
| 1838 |
-
|
| 1839 |
-
A declaration of a block-scope variable with external or internal
|
| 1840 |
-
linkage that has an *initializer* is ill-formed.
|
| 1841 |
-
|
| 1842 |
-
To *zero-initialize* an object or reference of type `T` means:
|
| 1843 |
-
|
| 1844 |
-
- if `T` is a scalar type ([[basic.types]]), the object is initialized
|
| 1845 |
-
to the value obtained by converting the integer literal `0` (zero) to
|
| 1846 |
-
`T`;[^12]
|
| 1847 |
-
- if `T` is a (possibly cv-qualified) non-union class type, each
|
| 1848 |
-
non-static data member, each non-virtual base class subobject, and, if
|
| 1849 |
-
the object is not a base class subobject, each virtual base class
|
| 1850 |
-
subobject is zero-initialized and padding is initialized to zero bits;
|
| 1851 |
-
- if `T` is a (possibly cv-qualified) union type, the object’s first
|
| 1852 |
-
non-static named data member is zero-initialized and padding is
|
| 1853 |
-
initialized to zero bits;
|
| 1854 |
-
- if `T` is an array type, each element is zero-initialized;
|
| 1855 |
-
- if `T` is a reference type, no initialization is performed.
|
| 1856 |
-
|
| 1857 |
-
To *default-initialize* an object of type `T` means:
|
| 1858 |
-
|
| 1859 |
-
- If `T` is a (possibly cv-qualified) class type (Clause [[class]]),
|
| 1860 |
-
constructors are considered. The applicable constructors are
|
| 1861 |
-
enumerated ([[over.match.ctor]]), and the best one for the
|
| 1862 |
-
*initializer* `()` is chosen through overload resolution (
|
| 1863 |
-
[[over.match]]). The constructor thus selected is called, with an
|
| 1864 |
-
empty argument list, to initialize the object.
|
| 1865 |
-
- If `T` is an array type, each element is default-initialized.
|
| 1866 |
-
- Otherwise, no initialization is performed.
|
| 1867 |
-
|
| 1868 |
-
A class type `T` is *const-default-constructible* if
|
| 1869 |
-
default-initialization of `T` would invoke a user-provided constructor
|
| 1870 |
-
of `T` (not inherited from a base class) or if
|
| 1871 |
-
|
| 1872 |
-
- each direct non-variant non-static data member `M` of `T` has a
|
| 1873 |
-
default member initializer or, if `M` is of class type `X` (or array
|
| 1874 |
-
thereof), `X` is const-default-constructible,
|
| 1875 |
-
- if `T` is a union with at least one non-static data member, exactly
|
| 1876 |
-
one variant member has a default member initializer,
|
| 1877 |
-
- if `T` is not a union, for each anonymous union member with at least
|
| 1878 |
-
one non-static data member (if any), exactly one non-static data
|
| 1879 |
-
member has a default member initializer, and
|
| 1880 |
-
- each potentially constructed base class of `T` is
|
| 1881 |
-
const-default-constructible.
|
| 1882 |
-
|
| 1883 |
-
If a program calls for the default-initialization of an object of a
|
| 1884 |
-
const-qualified type `T`, `T` shall be a const-default-constructible
|
| 1885 |
-
class type or array thereof.
|
| 1886 |
-
|
| 1887 |
-
To *value-initialize* an object of type `T` means:
|
| 1888 |
-
|
| 1889 |
-
- if `T` is a (possibly cv-qualified) class type (Clause [[class]])
|
| 1890 |
-
with either no default constructor ([[class.ctor]]) or a default
|
| 1891 |
-
constructor that is user-provided or deleted, then the object is
|
| 1892 |
-
default-initialized;
|
| 1893 |
-
- if `T` is a (possibly cv-qualified) class type without a user-provided
|
| 1894 |
-
or deleted default constructor, then the object is zero-initialized
|
| 1895 |
-
and the semantic constraints for default-initialization are checked,
|
| 1896 |
-
and if `T` has a non-trivial default constructor, the object is
|
| 1897 |
-
default-initialized;
|
| 1898 |
-
- if `T` is an array type, then each element is value-initialized;
|
| 1899 |
-
- otherwise, the object is zero-initialized.
|
| 1900 |
-
|
| 1901 |
-
A program that calls for default-initialization or value-initialization
|
| 1902 |
-
of an entity of reference type is ill-formed.
|
| 1903 |
-
|
| 1904 |
-
[*Note 3*: Every object of static storage duration is zero-initialized
|
| 1905 |
-
at program startup before any other initialization takes place. In some
|
| 1906 |
-
cases, additional initialization is done later. — *end note*]
|
| 1907 |
-
|
| 1908 |
-
An object whose initializer is an empty set of parentheses, i.e., `()`,
|
| 1909 |
-
shall be value-initialized.
|
| 1910 |
-
|
| 1911 |
-
[*Note 4*:
|
| 1912 |
-
|
| 1913 |
-
Since `()` is not permitted by the syntax for *initializer*,
|
| 1914 |
-
|
| 1915 |
-
``` cpp
|
| 1916 |
-
X a();
|
| 1917 |
-
```
|
| 1918 |
-
|
| 1919 |
-
is not the declaration of an object of class `X`, but the declaration of
|
| 1920 |
-
a function taking no argument and returning an `X`. The form `()` is
|
| 1921 |
-
permitted in certain other initialization contexts ([[expr.new]],
|
| 1922 |
-
[[expr.type.conv]], [[class.base.init]]).
|
| 1923 |
-
|
| 1924 |
-
— *end note*]
|
| 1925 |
-
|
| 1926 |
-
If no initializer is specified for an object, the object is
|
| 1927 |
-
default-initialized. When storage for an object with automatic or
|
| 1928 |
-
dynamic storage duration is obtained, the object has an *indeterminate
|
| 1929 |
-
value*, and if no initialization is performed for the object, that
|
| 1930 |
-
object retains an indeterminate value until that value is replaced (
|
| 1931 |
-
[[expr.ass]]).
|
| 1932 |
-
|
| 1933 |
-
[*Note 5*: Objects with static or thread storage duration are
|
| 1934 |
-
zero-initialized, see [[basic.start.static]]. — *end note*]
|
| 1935 |
-
|
| 1936 |
-
If an indeterminate value is produced by an evaluation, the behavior is
|
| 1937 |
-
undefined except in the following cases:
|
| 1938 |
-
|
| 1939 |
-
- If an indeterminate value of unsigned narrow character type (
|
| 1940 |
-
[[basic.fundamental]]) or `std::byte` type ([[cstddef.syn]]) is
|
| 1941 |
-
produced by the evaluation of:
|
| 1942 |
-
- the second or third operand of a conditional expression (
|
| 1943 |
-
[[expr.cond]]),
|
| 1944 |
-
- the right operand of a comma expression ([[expr.comma]]),
|
| 1945 |
-
- the operand of a cast or conversion ([[conv.integral]],
|
| 1946 |
-
[[expr.type.conv]], [[expr.static.cast]], [[expr.cast]]) to an
|
| 1947 |
-
unsigned narrow character type or `std::byte` type (
|
| 1948 |
-
[[cstddef.syn]]), or
|
| 1949 |
-
- a discarded-value expression (Clause [[expr]]),
|
| 1950 |
-
|
| 1951 |
-
then the result of the operation is an indeterminate value.
|
| 1952 |
-
- If an indeterminate value of unsigned narrow character type or
|
| 1953 |
-
`std::byte` type is produced by the evaluation of the right operand of
|
| 1954 |
-
a simple assignment operator ([[expr.ass]]) whose first operand is an
|
| 1955 |
-
lvalue of unsigned narrow character type or `std::byte` type, an
|
| 1956 |
-
indeterminate value replaces the value of the object referred to by
|
| 1957 |
-
the left operand.
|
| 1958 |
-
- If an indeterminate value of unsigned narrow character type is
|
| 1959 |
-
produced by the evaluation of the initialization expression when
|
| 1960 |
-
initializing an object of unsigned narrow character type, that object
|
| 1961 |
-
is initialized to an indeterminate value.
|
| 1962 |
-
- If an indeterminate value of unsigned narrow character type or
|
| 1963 |
-
`std::byte` type is produced by the evaluation of the initialization
|
| 1964 |
-
expression when initializing an object of `std::byte` type, that
|
| 1965 |
-
object is initialized to an indeterminate value.
|
| 1966 |
-
|
| 1967 |
-
[*Example 2*:
|
| 1968 |
-
|
| 1969 |
-
``` cpp
|
| 1970 |
-
int f(bool b) {
|
| 1971 |
-
unsigned char c;
|
| 1972 |
-
unsigned char d = c; // OK, d has an indeterminate value
|
| 1973 |
-
int e = d; // undefined behavior
|
| 1974 |
-
return b ? d : 0; // undefined behavior if b is true
|
| 1975 |
-
}
|
| 1976 |
-
```
|
| 1977 |
-
|
| 1978 |
-
— *end example*]
|
| 1979 |
-
|
| 1980 |
-
An initializer for a static member is in the scope of the member’s
|
| 1981 |
-
class.
|
| 1982 |
-
|
| 1983 |
-
[*Example 3*:
|
| 1984 |
-
|
| 1985 |
-
``` cpp
|
| 1986 |
-
int a;
|
| 1987 |
-
|
| 1988 |
-
struct X {
|
| 1989 |
-
static int a;
|
| 1990 |
-
static int b;
|
| 1991 |
-
};
|
| 1992 |
-
|
| 1993 |
-
int X::a = 1;
|
| 1994 |
-
int X::b = a; // X::b = X::a
|
| 1995 |
-
```
|
| 1996 |
-
|
| 1997 |
-
— *end example*]
|
| 1998 |
-
|
| 1999 |
-
If the entity being initialized does not have class type, the
|
| 2000 |
-
*expression-list* in a parenthesized initializer shall be a single
|
| 2001 |
-
expression.
|
| 2002 |
-
|
| 2003 |
-
The initialization that occurs in the `=` form of a
|
| 2004 |
-
*brace-or-equal-initializer* or *condition* ([[stmt.select]]), as well
|
| 2005 |
-
as in argument passing, function return, throwing an exception (
|
| 2006 |
-
[[except.throw]]), handling an exception ([[except.handle]]), and
|
| 2007 |
-
aggregate member initialization ([[dcl.init.aggr]]), is called
|
| 2008 |
-
*copy-initialization*.
|
| 2009 |
-
|
| 2010 |
-
[*Note 6*: Copy-initialization may invoke a move (
|
| 2011 |
-
[[class.copy]]). — *end note*]
|
| 2012 |
-
|
| 2013 |
-
The initialization that occurs in the forms
|
| 2014 |
-
|
| 2015 |
-
``` cpp
|
| 2016 |
-
T x(a);
|
| 2017 |
-
T x{a};
|
| 2018 |
-
```
|
| 2019 |
-
|
| 2020 |
-
as well as in `new` expressions ([[expr.new]]), `static_cast`
|
| 2021 |
-
expressions ([[expr.static.cast]]), functional notation type
|
| 2022 |
-
conversions ([[expr.type.conv]]), *mem-initializer*s (
|
| 2023 |
-
[[class.base.init]]), and the *braced-init-list* form of a *condition*
|
| 2024 |
-
is called *direct-initialization*.
|
| 2025 |
-
|
| 2026 |
-
The semantics of initializers are as follows. The *destination type* is
|
| 2027 |
-
the type of the object or reference being initialized and the *source
|
| 2028 |
-
type* is the type of the initializer expression. If the initializer is
|
| 2029 |
-
not a single (possibly parenthesized) expression, the source type is not
|
| 2030 |
-
defined.
|
| 2031 |
-
|
| 2032 |
-
- If the initializer is a (non-parenthesized) *braced-init-list* or is
|
| 2033 |
-
`=` *braced-init-list*, the object or reference is list-initialized (
|
| 2034 |
-
[[dcl.init.list]]).
|
| 2035 |
-
- If the destination type is a reference type, see [[dcl.init.ref]].
|
| 2036 |
-
- If the destination type is an array of characters, an array of
|
| 2037 |
-
`char16_t`, an array of `char32_t`, or an array of `wchar_t`, and the
|
| 2038 |
-
initializer is a string literal, see [[dcl.init.string]].
|
| 2039 |
-
- If the initializer is `()`, the object is value-initialized.
|
| 2040 |
-
- Otherwise, if the destination type is an array, the program is
|
| 2041 |
-
ill-formed.
|
| 2042 |
-
- If the destination type is a (possibly cv-qualified) class type:
|
| 2043 |
-
- If the initializer expression is a prvalue and the cv-unqualified
|
| 2044 |
-
version of the source type is the same class as the class of the
|
| 2045 |
-
destination, the initializer expression is used to initialize the
|
| 2046 |
-
destination object. \[*Example 4*: `T x = T(T(T()));` calls the `T`
|
| 2047 |
-
default constructor to initialize `x`. — *end example*]
|
| 2048 |
-
- Otherwise, if the initialization is direct-initialization, or if it
|
| 2049 |
-
is copy-initialization where the cv-unqualified version of the
|
| 2050 |
-
source type is the same class as, or a derived class of, the class
|
| 2051 |
-
of the destination, constructors are considered. The applicable
|
| 2052 |
-
constructors are enumerated ([[over.match.ctor]]), and the best one
|
| 2053 |
-
is chosen through overload resolution ([[over.match]]). The
|
| 2054 |
-
constructor so selected is called to initialize the object, with the
|
| 2055 |
-
initializer expression or *expression-list* as its argument(s). If
|
| 2056 |
-
no constructor applies, or the overload resolution is ambiguous, the
|
| 2057 |
-
initialization is ill-formed.
|
| 2058 |
-
- Otherwise (i.e., for the remaining copy-initialization cases),
|
| 2059 |
-
user-defined conversion sequences that can convert from the source
|
| 2060 |
-
type to the destination type or (when a conversion function is used)
|
| 2061 |
-
to a derived class thereof are enumerated as described in
|
| 2062 |
-
[[over.match.copy]], and the best one is chosen through overload
|
| 2063 |
-
resolution ([[over.match]]). If the conversion cannot be done or is
|
| 2064 |
-
ambiguous, the initialization is ill-formed. The function selected
|
| 2065 |
-
is called with the initializer expression as its argument; if the
|
| 2066 |
-
function is a constructor, the call is a prvalue of the
|
| 2067 |
-
cv-unqualified version of the destination type whose result object
|
| 2068 |
-
is initialized by the constructor. The call is used to
|
| 2069 |
-
direct-initialize, according to the rules above, the object that is
|
| 2070 |
-
the destination of the copy-initialization.
|
| 2071 |
-
- Otherwise, if the source type is a (possibly cv-qualified) class type,
|
| 2072 |
-
conversion functions are considered. The applicable conversion
|
| 2073 |
-
functions are enumerated ([[over.match.conv]]), and the best one is
|
| 2074 |
-
chosen through overload resolution ([[over.match]]). The user-defined
|
| 2075 |
-
conversion so selected is called to convert the initializer expression
|
| 2076 |
-
into the object being initialized. If the conversion cannot be done or
|
| 2077 |
-
is ambiguous, the initialization is ill-formed.
|
| 2078 |
-
- Otherwise, the initial value of the object being initialized is the
|
| 2079 |
-
(possibly converted) value of the initializer expression. Standard
|
| 2080 |
-
conversions (Clause [[conv]]) will be used, if necessary, to convert
|
| 2081 |
-
the initializer expression to the cv-unqualified version of the
|
| 2082 |
-
destination type; no user-defined conversions are considered. If the
|
| 2083 |
-
conversion cannot be done, the initialization is ill-formed. When
|
| 2084 |
-
initializing a bit-field with a value that it cannot represent, the
|
| 2085 |
-
resulting value of the bit-field is *implementation-defined*.
|
| 2086 |
-
\[*Note 7*:
|
| 2087 |
-
An expression of type “*cv1* `T`” can initialize an object of type
|
| 2088 |
-
“*cv2* `T`” independently of the cv-qualifiers *cv1* and *cv2*.
|
| 2089 |
-
``` cpp
|
| 2090 |
-
int a;
|
| 2091 |
-
const int b = a;
|
| 2092 |
-
int c = b;
|
| 2093 |
-
```
|
| 2094 |
-
|
| 2095 |
-
— *end note*]
|
| 2096 |
-
|
| 2097 |
-
An *initializer-clause* followed by an ellipsis is a pack expansion (
|
| 2098 |
-
[[temp.variadic]]).
|
| 2099 |
-
|
| 2100 |
-
If the initializer is a parenthesized *expression-list*, the expressions
|
| 2101 |
-
are evaluated in the order specified for function calls (
|
| 2102 |
-
[[expr.call]]).
|
| 2103 |
-
|
| 2104 |
-
An object whose initialization has completed is deemed to be
|
| 2105 |
-
constructed, even if no constructor of the object’s class is invoked for
|
| 2106 |
-
the initialization.
|
| 2107 |
-
|
| 2108 |
-
[*Note 8*: Such an object might have been value-initialized or
|
| 2109 |
-
initialized by aggregate initialization ([[dcl.init.aggr]]) or by an
|
| 2110 |
-
inherited constructor ([[class.inhctor.init]]). — *end note*]
|
| 2111 |
-
|
| 2112 |
-
A declaration that specifies the initialization of a variable, whether
|
| 2113 |
-
from an explicit initializer or by default-initialization, is called the
|
| 2114 |
-
*initializing declaration* of that variable.
|
| 2115 |
-
|
| 2116 |
-
[*Note 9*: In most cases this is the defining declaration (
|
| 2117 |
-
[[basic.def]]) of the variable, but the initializing declaration of a
|
| 2118 |
-
non-inline static data member ([[class.static.data]]) might be the
|
| 2119 |
-
declaration within the class definition and not the definition at
|
| 2120 |
-
namespace scope. — *end note*]
|
| 2121 |
-
|
| 2122 |
-
### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
|
| 2123 |
-
|
| 2124 |
-
An *aggregate* is an array or a class (Clause [[class]]) with
|
| 2125 |
-
|
| 2126 |
-
- no user-provided, `explicit`, or inherited constructors (
|
| 2127 |
-
[[class.ctor]]),
|
| 2128 |
-
- no private or protected non-static data members (Clause
|
| 2129 |
-
[[class.access]]),
|
| 2130 |
-
- no virtual functions ([[class.virtual]]), and
|
| 2131 |
-
- no virtual, private, or protected base classes ([[class.mi]]).
|
| 2132 |
-
|
| 2133 |
-
[*Note 1*: Aggregate initialization does not allow accessing protected
|
| 2134 |
-
and private base class’ members or constructors. — *end note*]
|
| 2135 |
-
|
| 2136 |
-
The *elements* of an aggregate are:
|
| 2137 |
-
|
| 2138 |
-
- for an array, the array elements in increasing subscript order, or
|
| 2139 |
-
- for a class, the direct base classes in declaration order, followed by
|
| 2140 |
-
the direct non-static data members ([[class.mem]]) that are not
|
| 2141 |
-
members of an anonymous union, in declaration order.
|
| 2142 |
-
|
| 2143 |
-
When an aggregate is initialized by an initializer list as specified in
|
| 2144 |
-
[[dcl.init.list]], the elements of the initializer list are taken as
|
| 2145 |
-
initializers for the elements of the aggregate, in order. Each element
|
| 2146 |
-
is copy-initialized from the corresponding *initializer-clause*. If the
|
| 2147 |
-
*initializer-clause* is an expression and a narrowing conversion (
|
| 2148 |
-
[[dcl.init.list]]) is required to convert the expression, the program is
|
| 2149 |
-
ill-formed.
|
| 2150 |
-
|
| 2151 |
-
[*Note 2*: If an *initializer-clause* is itself an initializer list,
|
| 2152 |
-
the element is list-initialized, which will result in a recursive
|
| 2153 |
-
application of the rules in this section if the element is an
|
| 2154 |
-
aggregate. — *end note*]
|
| 2155 |
-
|
| 2156 |
-
[*Example 1*:
|
| 2157 |
-
|
| 2158 |
-
``` cpp
|
| 2159 |
-
struct A {
|
| 2160 |
-
int x;
|
| 2161 |
-
struct B {
|
| 2162 |
-
int i;
|
| 2163 |
-
int j;
|
| 2164 |
-
} b;
|
| 2165 |
-
} a = { 1, { 2, 3 } };
|
| 2166 |
-
```
|
| 2167 |
-
|
| 2168 |
-
initializes `a.x` with 1, `a.b.i` with 2, `a.b.j` with 3.
|
| 2169 |
-
|
| 2170 |
-
``` cpp
|
| 2171 |
-
struct base1 { int b1, b2 = 42; };
|
| 2172 |
-
struct base2 {
|
| 2173 |
-
base2() {
|
| 2174 |
-
b3 = 42;
|
| 2175 |
-
}
|
| 2176 |
-
int b3;
|
| 2177 |
-
};
|
| 2178 |
-
struct derived : base1, base2 {
|
| 2179 |
-
int d;
|
| 2180 |
-
};
|
| 2181 |
-
|
| 2182 |
-
derived d1{{1, 2}, {}, 4};
|
| 2183 |
-
derived d2{{}, {}, 4};
|
| 2184 |
-
```
|
| 2185 |
-
|
| 2186 |
-
initializes `d1.b1` with 1, `d1.b2` with 2, `d1.b3` with 42, `d1.d` with
|
| 2187 |
-
4, and `d2.b1` with 0, `d2.b2` with 42, `d2.b3` with 42, `d2.d` with 4.
|
| 2188 |
-
|
| 2189 |
-
— *end example*]
|
| 2190 |
-
|
| 2191 |
-
An aggregate that is a class can also be initialized with a single
|
| 2192 |
-
expression not enclosed in braces, as described in [[dcl.init]].
|
| 2193 |
-
|
| 2194 |
-
An array of unknown bound initialized with a brace-enclosed
|
| 2195 |
-
*initializer-list* containing `n` *initializer-clause*s, where `n` shall
|
| 2196 |
-
be greater than zero, is defined as having `n` elements (
|
| 2197 |
-
[[dcl.array]]).
|
| 2198 |
-
|
| 2199 |
-
[*Example 2*:
|
| 2200 |
-
|
| 2201 |
-
``` cpp
|
| 2202 |
-
int x[] = { 1, 3, 5 };
|
| 2203 |
-
```
|
| 2204 |
-
|
| 2205 |
-
declares and initializes `x` as a one-dimensional array that has three
|
| 2206 |
-
elements since no size was specified and there are three initializers.
|
| 2207 |
-
|
| 2208 |
-
— *end example*]
|
| 2209 |
-
|
| 2210 |
-
An empty initializer list `{}` shall not be used as the
|
| 2211 |
-
*initializer-clause* for an array of unknown bound.[^13]
|
| 2212 |
-
|
| 2213 |
-
[*Note 3*:
|
| 2214 |
-
|
| 2215 |
-
A default member initializer does not determine the bound for a member
|
| 2216 |
-
array of unknown bound. Since the default member initializer is ignored
|
| 2217 |
-
if a suitable *mem-initializer* is present ([[class.base.init]]), the
|
| 2218 |
-
default member initializer is not considered to initialize the array of
|
| 2219 |
-
unknown bound.
|
| 2220 |
-
|
| 2221 |
-
[*Example 3*:
|
| 2222 |
-
|
| 2223 |
-
``` cpp
|
| 2224 |
-
struct S {
|
| 2225 |
-
int y[] = { 0 }; // error: non-static data member of incomplete type
|
| 2226 |
-
};
|
| 2227 |
-
```
|
| 2228 |
-
|
| 2229 |
-
— *end example*]
|
| 2230 |
-
|
| 2231 |
-
— *end note*]
|
| 2232 |
-
|
| 2233 |
-
[*Note 4*:
|
| 2234 |
-
|
| 2235 |
-
Static data members and unnamed bit-fields are not considered elements
|
| 2236 |
-
of the aggregate.
|
| 2237 |
-
|
| 2238 |
-
[*Example 4*:
|
| 2239 |
-
|
| 2240 |
-
``` cpp
|
| 2241 |
-
struct A {
|
| 2242 |
-
int i;
|
| 2243 |
-
static int s;
|
| 2244 |
-
int j;
|
| 2245 |
-
int :17;
|
| 2246 |
-
int k;
|
| 2247 |
-
} a = { 1, 2, 3 };
|
| 2248 |
-
```
|
| 2249 |
-
|
| 2250 |
-
Here, the second initializer 2 initializes `a.j` and not the static data
|
| 2251 |
-
member `A::s`, and the third initializer 3 initializes `a.k` and not the
|
| 2252 |
-
unnamed bit-field before it.
|
| 2253 |
-
|
| 2254 |
-
— *end example*]
|
| 2255 |
-
|
| 2256 |
-
— *end note*]
|
| 2257 |
-
|
| 2258 |
-
An *initializer-list* is ill-formed if the number of
|
| 2259 |
-
*initializer-clause*s exceeds the number of elements to initialize.
|
| 2260 |
-
|
| 2261 |
-
[*Example 5*:
|
| 2262 |
-
|
| 2263 |
-
``` cpp
|
| 2264 |
-
char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
|
| 2265 |
-
```
|
| 2266 |
-
|
| 2267 |
-
is ill-formed.
|
| 2268 |
-
|
| 2269 |
-
— *end example*]
|
| 2270 |
-
|
| 2271 |
-
If there are fewer *initializer-clause*s in the list than there are
|
| 2272 |
-
elements in a non-union aggregate, then each element not explicitly
|
| 2273 |
-
initialized is initialized as follows:
|
| 2274 |
-
|
| 2275 |
-
- If the element has a default member initializer ([[class.mem]]), the
|
| 2276 |
-
element is initialized from that initializer.
|
| 2277 |
-
- Otherwise, if the element is not a reference, the element is
|
| 2278 |
-
copy-initialized from an empty initializer list ([[dcl.init.list]]).
|
| 2279 |
-
- Otherwise, the program is ill-formed.
|
| 2280 |
-
|
| 2281 |
-
If the aggregate is a union and the initializer list is empty, then
|
| 2282 |
-
|
| 2283 |
-
- if any variant member has a default member initializer, that member is
|
| 2284 |
-
initialized from its default member initializer;
|
| 2285 |
-
- otherwise, the first member of the union (if any) is copy-initialized
|
| 2286 |
-
from an empty initializer list.
|
| 2287 |
-
|
| 2288 |
-
[*Example 6*:
|
| 2289 |
-
|
| 2290 |
-
``` cpp
|
| 2291 |
-
struct S { int a; const char* b; int c; int d = b[a]; };
|
| 2292 |
-
S ss = { 1, "asdf" };
|
| 2293 |
-
```
|
| 2294 |
-
|
| 2295 |
-
initializes `ss.a` with 1, `ss.b` with `"asdf"`, `ss.c` with the value
|
| 2296 |
-
of an expression of the form `int{}` (that is, `0`), and `ss.d` with the
|
| 2297 |
-
value of `ss.b[ss.a]` (that is, `'s'`), and in
|
| 2298 |
-
|
| 2299 |
-
``` cpp
|
| 2300 |
-
struct X { int i, j, k = 42; };
|
| 2301 |
-
X a[] = { 1, 2, 3, 4, 5, 6 };
|
| 2302 |
-
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
|
| 2303 |
-
```
|
| 2304 |
-
|
| 2305 |
-
`a` and `b` have the same value
|
| 2306 |
-
|
| 2307 |
-
— *end example*]
|
| 2308 |
-
|
| 2309 |
-
If a reference member is initialized from its default member initializer
|
| 2310 |
-
and a potentially-evaluated subexpression thereof is an aggregate
|
| 2311 |
-
initialization that would use that default member initializer, the
|
| 2312 |
-
program is ill-formed.
|
| 2313 |
-
|
| 2314 |
-
[*Example 7*:
|
| 2315 |
-
|
| 2316 |
-
``` cpp
|
| 2317 |
-
struct A;
|
| 2318 |
-
extern A a;
|
| 2319 |
-
struct A {
|
| 2320 |
-
const A& a1 { A{a,a} }; // OK
|
| 2321 |
-
const A& a2 { A{} }; // error
|
| 2322 |
-
};
|
| 2323 |
-
A a{a,a}; // OK
|
| 2324 |
-
```
|
| 2325 |
-
|
| 2326 |
-
— *end example*]
|
| 2327 |
-
|
| 2328 |
-
If an aggregate class `C` contains a subaggregate element `e` with no
|
| 2329 |
-
elements, the *initializer-clause* for `e` shall not be omitted from an
|
| 2330 |
-
*initializer-list* for an object of type `C` unless the
|
| 2331 |
-
*initializer-clause*s for all elements of `C` following `e` are also
|
| 2332 |
-
omitted.
|
| 2333 |
-
|
| 2334 |
-
[*Example 8*:
|
| 2335 |
-
|
| 2336 |
-
``` cpp
|
| 2337 |
-
struct S { } s;
|
| 2338 |
-
struct A {
|
| 2339 |
-
S s1;
|
| 2340 |
-
int i1;
|
| 2341 |
-
S s2;
|
| 2342 |
-
int i2;
|
| 2343 |
-
S s3;
|
| 2344 |
-
int i3;
|
| 2345 |
-
} a = {
|
| 2346 |
-
{ }, // Required initialization
|
| 2347 |
-
0,
|
| 2348 |
-
s, // Required initialization
|
| 2349 |
-
0
|
| 2350 |
-
}; // Initialization not required for A::s3 because A::i3 is also not initialized
|
| 2351 |
-
```
|
| 2352 |
-
|
| 2353 |
-
— *end example*]
|
| 2354 |
-
|
| 2355 |
-
When initializing a multi-dimensional array, the *initializer-clause*s
|
| 2356 |
-
initialize the elements with the last (rightmost) index of the array
|
| 2357 |
-
varying the fastest ([[dcl.array]]).
|
| 2358 |
-
|
| 2359 |
-
[*Example 9*:
|
| 2360 |
-
|
| 2361 |
-
``` cpp
|
| 2362 |
-
int x[2][2] = { 3, 1, 4, 2 };
|
| 2363 |
-
```
|
| 2364 |
-
|
| 2365 |
-
initializes `x[0][0]` to `3`, `x[0][1]` to `1`, `x[1][0]` to `4`, and
|
| 2366 |
-
`x[1][1]` to `2`. On the other hand,
|
| 2367 |
-
|
| 2368 |
-
``` cpp
|
| 2369 |
-
float y[4][3] = {
|
| 2370 |
-
{ 1 }, { 2 }, { 3 }, { 4 }
|
| 2371 |
-
};
|
| 2372 |
-
```
|
| 2373 |
-
|
| 2374 |
-
initializes the first column of `y` (regarded as a two-dimensional
|
| 2375 |
-
array) and leaves the rest zero.
|
| 2376 |
-
|
| 2377 |
-
— *end example*]
|
| 2378 |
-
|
| 2379 |
-
Braces can be elided in an *initializer-list* as follows. If the
|
| 2380 |
-
*initializer-list* begins with a left brace, then the succeeding
|
| 2381 |
-
comma-separated list of *initializer-clause*s initializes the elements
|
| 2382 |
-
of a subaggregate; it is erroneous for there to be more
|
| 2383 |
-
*initializer-clause*s than elements. If, however, the *initializer-list*
|
| 2384 |
-
for a subaggregate does not begin with a left brace, then only enough
|
| 2385 |
-
*initializer-clause*s from the list are taken to initialize the elements
|
| 2386 |
-
of the subaggregate; any remaining *initializer-clause*s are left to
|
| 2387 |
-
initialize the next element of the aggregate of which the current
|
| 2388 |
-
subaggregate is an element.
|
| 2389 |
-
|
| 2390 |
-
[*Example 10*:
|
| 2391 |
-
|
| 2392 |
-
``` cpp
|
| 2393 |
-
float y[4][3] = {
|
| 2394 |
-
{ 1, 3, 5 },
|
| 2395 |
-
{ 2, 4, 6 },
|
| 2396 |
-
{ 3, 5, 7 },
|
| 2397 |
-
};
|
| 2398 |
-
```
|
| 2399 |
-
|
| 2400 |
-
is a completely-braced initialization: 1, 3, and 5 initialize the first
|
| 2401 |
-
row of the array `y[0]`, namely `y[0][0]`, `y[0][1]`, and `y[0][2]`.
|
| 2402 |
-
Likewise the next two lines initialize `y[1]` and `y[2]`. The
|
| 2403 |
-
initializer ends early and therefore `y[3]`s elements are initialized as
|
| 2404 |
-
if explicitly initialized with an expression of the form `float()`, that
|
| 2405 |
-
is, are initialized with `0.0`. In the following example, braces in the
|
| 2406 |
-
*initializer-list* are elided; however the *initializer-list* has the
|
| 2407 |
-
same effect as the completely-braced *initializer-list* of the above
|
| 2408 |
-
example,
|
| 2409 |
-
|
| 2410 |
-
``` cpp
|
| 2411 |
-
float y[4][3] = {
|
| 2412 |
-
1, 3, 5, 2, 4, 6, 3, 5, 7
|
| 2413 |
-
};
|
| 2414 |
-
```
|
| 2415 |
-
|
| 2416 |
-
The initializer for `y` begins with a left brace, but the one for `y[0]`
|
| 2417 |
-
does not, therefore three elements from the list are used. Likewise the
|
| 2418 |
-
next three are taken successively for `y[1]` and `y[2]`.
|
| 2419 |
-
|
| 2420 |
-
— *end example*]
|
| 2421 |
-
|
| 2422 |
-
All implicit type conversions (Clause [[conv]]) are considered when
|
| 2423 |
-
initializing the element with an *assignment-expression*. If the
|
| 2424 |
-
*assignment-expression* can initialize an element, the element is
|
| 2425 |
-
initialized. Otherwise, if the element is itself a subaggregate, brace
|
| 2426 |
-
elision is assumed and the *assignment-expression* is considered for the
|
| 2427 |
-
initialization of the first element of the subaggregate.
|
| 2428 |
-
|
| 2429 |
-
[*Note 5*: As specified above, brace elision cannot apply to
|
| 2430 |
-
subaggregates with no elements; an *initializer-clause* for the entire
|
| 2431 |
-
subobject is required. — *end note*]
|
| 2432 |
-
|
| 2433 |
-
[*Example 11*:
|
| 2434 |
-
|
| 2435 |
-
``` cpp
|
| 2436 |
-
struct A {
|
| 2437 |
-
int i;
|
| 2438 |
-
operator int();
|
| 2439 |
-
};
|
| 2440 |
-
struct B {
|
| 2441 |
-
A a1, a2;
|
| 2442 |
-
int z;
|
| 2443 |
-
};
|
| 2444 |
-
A a;
|
| 2445 |
-
B b = { 4, a, a };
|
| 2446 |
-
```
|
| 2447 |
-
|
| 2448 |
-
Braces are elided around the *initializer-clause* for `b.a1.i`. `b.a1.i`
|
| 2449 |
-
is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
|
| 2450 |
-
initialized with whatever `a.operator int()` returns.
|
| 2451 |
-
|
| 2452 |
-
— *end example*]
|
| 2453 |
-
|
| 2454 |
-
[*Note 6*: An aggregate array or an aggregate class may contain
|
| 2455 |
-
elements of a class type with a user-provided constructor (
|
| 2456 |
-
[[class.ctor]]). Initialization of these aggregate objects is described
|
| 2457 |
-
in [[class.expl.init]]. — *end note*]
|
| 2458 |
-
|
| 2459 |
-
[*Note 7*: Whether the initialization of aggregates with static storage
|
| 2460 |
-
duration is static or dynamic is specified in [[basic.start.static]],
|
| 2461 |
-
[[basic.start.dynamic]], and [[stmt.dcl]]. — *end note*]
|
| 2462 |
-
|
| 2463 |
-
When a union is initialized with a brace-enclosed initializer, the
|
| 2464 |
-
braces shall only contain an *initializer-clause* for the first
|
| 2465 |
-
non-static data member of the union.
|
| 2466 |
-
|
| 2467 |
-
[*Example 12*:
|
| 2468 |
-
|
| 2469 |
-
``` cpp
|
| 2470 |
-
union u { int a; const char* b; };
|
| 2471 |
-
u a = { 1 };
|
| 2472 |
-
u b = a;
|
| 2473 |
-
u c = 1; // error
|
| 2474 |
-
u d = { 0, "asdf" }; // error
|
| 2475 |
-
u e = { "asdf" }; // error
|
| 2476 |
-
```
|
| 2477 |
-
|
| 2478 |
-
— *end example*]
|
| 2479 |
-
|
| 2480 |
-
[*Note 8*: As described above, the braces around the
|
| 2481 |
-
*initializer-clause* for a union member can be omitted if the union is a
|
| 2482 |
-
member of another aggregate. — *end note*]
|
| 2483 |
-
|
| 2484 |
-
### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
|
| 2485 |
-
|
| 2486 |
-
An array of narrow character type ([[basic.fundamental]]), `char16_t`
|
| 2487 |
-
array, `char32_t` array, or `wchar_t` array can be initialized by a
|
| 2488 |
-
narrow string literal, `char16_t` string literal, `char32_t` string
|
| 2489 |
-
literal, or wide string literal, respectively, or by an
|
| 2490 |
-
appropriately-typed string literal enclosed in braces ([[lex.string]]).
|
| 2491 |
-
Successive characters of the value of the string literal initialize the
|
| 2492 |
-
elements of the array.
|
| 2493 |
-
|
| 2494 |
-
[*Example 1*:
|
| 2495 |
-
|
| 2496 |
-
``` cpp
|
| 2497 |
-
char msg[] = "Syntax error on line %s\n";
|
| 2498 |
-
```
|
| 2499 |
-
|
| 2500 |
-
shows a character array whose members are initialized with a
|
| 2501 |
-
*string-literal*. Note that because `'\n'` is a single character and
|
| 2502 |
-
because a trailing `'\0'` is appended, `sizeof(msg)` is `25`.
|
| 2503 |
-
|
| 2504 |
-
— *end example*]
|
| 2505 |
-
|
| 2506 |
-
There shall not be more initializers than there are array elements.
|
| 2507 |
-
|
| 2508 |
-
[*Example 2*:
|
| 2509 |
-
|
| 2510 |
-
``` cpp
|
| 2511 |
-
char cv[4] = "asdf"; // error
|
| 2512 |
-
```
|
| 2513 |
-
|
| 2514 |
-
is ill-formed since there is no space for the implied trailing `'\0'`.
|
| 2515 |
-
|
| 2516 |
-
— *end example*]
|
| 2517 |
-
|
| 2518 |
-
If there are fewer initializers than there are array elements, each
|
| 2519 |
-
element not explicitly initialized shall be zero-initialized (
|
| 2520 |
-
[[dcl.init]]).
|
| 2521 |
-
|
| 2522 |
-
### References <a id="dcl.init.ref">[[dcl.init.ref]]</a>
|
| 2523 |
-
|
| 2524 |
-
A variable whose declared type is “reference to type `T`” ([[dcl.ref]])
|
| 2525 |
-
shall be initialized.
|
| 2526 |
-
|
| 2527 |
-
[*Example 1*:
|
| 2528 |
-
|
| 2529 |
-
``` cpp
|
| 2530 |
-
int g(int) noexcept;
|
| 2531 |
-
void f() {
|
| 2532 |
-
int i;
|
| 2533 |
-
int& r = i; // r refers to i
|
| 2534 |
-
r = 1; // the value of i becomes 1
|
| 2535 |
-
int* p = &r; // p points to i
|
| 2536 |
-
int& rr = r; // rr refers to what r refers to, that is, to i
|
| 2537 |
-
int (&rg)(int) = g; // rg refers to the function g
|
| 2538 |
-
rg(i); // calls function g
|
| 2539 |
-
int a[3];
|
| 2540 |
-
int (&ra)[3] = a; // ra refers to the array a
|
| 2541 |
-
ra[1] = i; // modifies a[1]
|
| 2542 |
-
}
|
| 2543 |
-
```
|
| 2544 |
-
|
| 2545 |
-
— *end example*]
|
| 2546 |
-
|
| 2547 |
-
A reference cannot be changed to refer to another object after
|
| 2548 |
-
initialization.
|
| 2549 |
-
|
| 2550 |
-
[*Note 1*: Assignment to a reference assigns to the object referred to
|
| 2551 |
-
by the reference ([[expr.ass]]). — *end note*]
|
| 2552 |
-
|
| 2553 |
-
Argument passing ([[expr.call]]) and function value return (
|
| 2554 |
-
[[stmt.return]]) are initializations.
|
| 2555 |
-
|
| 2556 |
-
The initializer can be omitted for a reference only in a parameter
|
| 2557 |
-
declaration ([[dcl.fct]]), in the declaration of a function return
|
| 2558 |
-
type, in the declaration of a class member within its class definition (
|
| 2559 |
-
[[class.mem]]), and where the `extern` specifier is explicitly used.
|
| 2560 |
-
|
| 2561 |
-
[*Example 2*:
|
| 2562 |
-
|
| 2563 |
-
``` cpp
|
| 2564 |
-
int& r1; // error: initializer missing
|
| 2565 |
-
extern int& r2; // OK
|
| 2566 |
-
```
|
| 2567 |
-
|
| 2568 |
-
— *end example*]
|
| 2569 |
-
|
| 2570 |
-
Given types “*cv1* `T1`” and “*cv2* `T2`”, “*cv1* `T1`” is
|
| 2571 |
-
*reference-related* to “*cv2* `T2`” if `T1` is the same type as `T2`, or
|
| 2572 |
-
`T1` is a base class of `T2`. “*cv1* `T1`” is *reference-compatible*
|
| 2573 |
-
with “*cv2* `T2`” if
|
| 2574 |
-
|
| 2575 |
-
- `T1` is reference-related to `T2`, or
|
| 2576 |
-
- `T2` is “`noexcept` function” and `T1` is “function”, where the
|
| 2577 |
-
function types are otherwise the same,
|
| 2578 |
-
|
| 2579 |
-
and *cv1* is the same cv-qualification as, or greater cv-qualification
|
| 2580 |
-
than, *cv2*. In all cases where the reference-related or
|
| 2581 |
-
reference-compatible relationship of two types is used to establish the
|
| 2582 |
-
validity of a reference binding, and `T1` is a base class of `T2`, a
|
| 2583 |
-
program that necessitates such a binding is ill-formed if `T1` is an
|
| 2584 |
-
inaccessible (Clause [[class.access]]) or ambiguous (
|
| 2585 |
-
[[class.member.lookup]]) base class of `T2`.
|
| 2586 |
-
|
| 2587 |
-
A reference to type “*cv1* `T1`” is initialized by an expression of type
|
| 2588 |
-
“*cv2* `T2`” as follows:
|
| 2589 |
-
|
| 2590 |
-
- If the reference is an lvalue reference and the initializer expression
|
| 2591 |
-
- is an lvalue (but is not a bit-field), and “*cv1* `T1`” is
|
| 2592 |
-
reference-compatible with “*cv2* `T2`”, or
|
| 2593 |
-
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 2594 |
-
reference-related to `T2`, and can be converted to an lvalue of type
|
| 2595 |
-
“*cv3* `T3`”, where “*cv1* `T1`” is reference-compatible with “*cv3*
|
| 2596 |
-
`T3`”[^14] (this conversion is selected by enumerating the
|
| 2597 |
-
applicable conversion functions ([[over.match.ref]]) and choosing
|
| 2598 |
-
the best one through overload resolution ([[over.match]])),
|
| 2599 |
-
|
| 2600 |
-
then the reference is bound to the initializer expression lvalue in
|
| 2601 |
-
the first case and to the lvalue result of the conversion in the
|
| 2602 |
-
second case (or, in either case, to the appropriate base class
|
| 2603 |
-
subobject of the object).
|
| 2604 |
-
\[*Note 2*: The usual lvalue-to-rvalue ([[conv.lval]]),
|
| 2605 |
-
array-to-pointer ([[conv.array]]), and function-to-pointer (
|
| 2606 |
-
[[conv.func]]) standard conversions are not needed, and therefore are
|
| 2607 |
-
suppressed, when such direct bindings to lvalues are
|
| 2608 |
-
done. — *end note*]
|
| 2609 |
-
\[*Example 3*:
|
| 2610 |
-
``` cpp
|
| 2611 |
-
double d = 2.0;
|
| 2612 |
-
double& rd = d; // rd refers to d
|
| 2613 |
-
const double& rcd = d; // rcd refers to d
|
| 2614 |
-
|
| 2615 |
-
struct A { };
|
| 2616 |
-
struct B : A { operator int&(); } b;
|
| 2617 |
-
A& ra = b; // ra refers to A subobject in b
|
| 2618 |
-
const A& rca = b; // rca refers to A subobject in b
|
| 2619 |
-
int& ir = B(); // ir refers to the result of B::operator int&
|
| 2620 |
-
```
|
| 2621 |
-
|
| 2622 |
-
— *end example*]
|
| 2623 |
-
- Otherwise, the reference shall be an lvalue reference to a
|
| 2624 |
-
non-volatile const type (i.e., *cv1* shall be `const`), or the
|
| 2625 |
-
reference shall be an rvalue reference.
|
| 2626 |
-
\[*Example 4*:
|
| 2627 |
-
``` cpp
|
| 2628 |
-
double& rd2 = 2.0; // error: not an lvalue and reference not const
|
| 2629 |
-
int i = 2;
|
| 2630 |
-
double& rd3 = i; // error: type mismatch and reference not const
|
| 2631 |
-
```
|
| 2632 |
-
|
| 2633 |
-
— *end example*]
|
| 2634 |
-
- If the initializer expression
|
| 2635 |
-
- is an rvalue (but not a bit-field) or function lvalue and “*cv1*
|
| 2636 |
-
`T1`” is reference-compatible with “*cv2* `T2`”, or
|
| 2637 |
-
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 2638 |
-
reference-related to `T2`, and can be converted to an rvalue or
|
| 2639 |
-
function lvalue of type “*cv3* `T3`”, where “*cv1* `T1`” is
|
| 2640 |
-
reference-compatible with “*cv3* `T3`” (see [[over.match.ref]]),
|
| 2641 |
-
|
| 2642 |
-
then the value of the initializer expression in the first case and
|
| 2643 |
-
the result of the conversion in the second case is called the
|
| 2644 |
-
converted initializer. If the converted initializer is a prvalue,
|
| 2645 |
-
its type `T4` is adjusted to type “*cv1* `T4`” ([[conv.qual]]) and
|
| 2646 |
-
the temporary materialization conversion ([[conv.rval]]) is
|
| 2647 |
-
applied. In any case, the reference is bound to the resulting
|
| 2648 |
-
glvalue (or to an appropriate base class subobject).
|
| 2649 |
-
\[*Example 5*:
|
| 2650 |
-
``` cpp
|
| 2651 |
-
struct A { };
|
| 2652 |
-
struct B : A { } b;
|
| 2653 |
-
extern B f();
|
| 2654 |
-
const A& rca2 = f(); // bound to the A subobject of the B rvalue.
|
| 2655 |
-
A&& rra = f(); // same as above
|
| 2656 |
-
struct X {
|
| 2657 |
-
operator B();
|
| 2658 |
-
operator int&();
|
| 2659 |
-
} x;
|
| 2660 |
-
const A& r = x; // bound to the A subobject of the result of the conversion
|
| 2661 |
-
int i2 = 42;
|
| 2662 |
-
int&& rri = static_cast<int&&>(i2); // bound directly to i2
|
| 2663 |
-
B&& rrb = x; // bound directly to the result of operator B
|
| 2664 |
-
```
|
| 2665 |
-
|
| 2666 |
-
— *end example*]
|
| 2667 |
-
- Otherwise:
|
| 2668 |
-
- If `T1` or `T2` is a class type and `T1` is not reference-related
|
| 2669 |
-
to `T2`, user-defined conversions are considered using the rules
|
| 2670 |
-
for copy-initialization of an object of type “*cv1* `T1`” by
|
| 2671 |
-
user-defined conversion ([[dcl.init]], [[over.match.copy]],
|
| 2672 |
-
[[over.match.conv]]); the program is ill-formed if the
|
| 2673 |
-
corresponding non-reference copy-initialization would be
|
| 2674 |
-
ill-formed. The result of the call to the conversion function, as
|
| 2675 |
-
described for the non-reference copy-initialization, is then used
|
| 2676 |
-
to direct-initialize the reference. For this
|
| 2677 |
-
direct-initialization, user-defined conversions are not
|
| 2678 |
-
considered.
|
| 2679 |
-
- Otherwise, the initializer expression is implicitly converted to a
|
| 2680 |
-
prvalue of type “*cv1* `T1`”. The temporary materialization
|
| 2681 |
-
conversion is applied and the reference is bound to the result.
|
| 2682 |
-
|
| 2683 |
-
If `T1` is reference-related to `T2`:
|
| 2684 |
-
- *cv1* shall be the same cv-qualification as, or greater
|
| 2685 |
-
cv-qualification than, *cv2*; and
|
| 2686 |
-
- if the reference is an rvalue reference, the initializer
|
| 2687 |
-
expression shall not be an lvalue.
|
| 2688 |
-
|
| 2689 |
-
\[*Example 6*:
|
| 2690 |
-
``` cpp
|
| 2691 |
-
struct Banana { };
|
| 2692 |
-
struct Enigma { operator const Banana(); };
|
| 2693 |
-
struct Alaska { operator Banana&(); };
|
| 2694 |
-
void enigmatic() {
|
| 2695 |
-
typedef const Banana ConstBanana;
|
| 2696 |
-
Banana &&banana1 = ConstBanana(); // ill-formed
|
| 2697 |
-
Banana &&banana2 = Enigma(); // ill-formed
|
| 2698 |
-
Banana &&banana3 = Alaska(); // ill-formed
|
| 2699 |
-
}
|
| 2700 |
-
|
| 2701 |
-
const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
|
| 2702 |
-
double&& rrd = 2; // rrd refers to temporary with value 2.0
|
| 2703 |
-
const volatile int cvi = 1;
|
| 2704 |
-
const int& r2 = cvi; // error: cv-qualifier dropped
|
| 2705 |
-
struct A { operator volatile int&(); } a;
|
| 2706 |
-
const int& r3 = a; // error: cv-qualifier dropped
|
| 2707 |
-
// from result of conversion function
|
| 2708 |
-
double d2 = 1.0;
|
| 2709 |
-
double&& rrd2 = d2; // error: initializer is lvalue of related type
|
| 2710 |
-
struct X { operator int&(); };
|
| 2711 |
-
int&& rri2 = X(); // error: result of conversion function is lvalue of related type
|
| 2712 |
-
int i3 = 2;
|
| 2713 |
-
double&& rrd3 = i3; // rrd3 refers to temporary with value 2.0
|
| 2714 |
-
```
|
| 2715 |
-
|
| 2716 |
-
— *end example*]
|
| 2717 |
-
|
| 2718 |
-
In all cases except the last (i.e., implicitly converting the
|
| 2719 |
-
initializer expression to the underlying type of the reference), the
|
| 2720 |
-
reference is said to *bind directly* to the initializer expression.
|
| 2721 |
-
|
| 2722 |
-
[*Note 3*: [[class.temporary]] describes the lifetime of temporaries
|
| 2723 |
-
bound to references. — *end note*]
|
| 2724 |
-
|
| 2725 |
-
### List-initialization <a id="dcl.init.list">[[dcl.init.list]]</a>
|
| 2726 |
-
|
| 2727 |
-
*List-initialization* is initialization of an object or reference from a
|
| 2728 |
-
*braced-init-list*. Such an initializer is called an *initializer list*,
|
| 2729 |
-
and the comma-separated *initializer-clause*s of the list are called the
|
| 2730 |
-
*elements* of the initializer list. An initializer list may be empty.
|
| 2731 |
-
List-initialization can occur in direct-initialization or
|
| 2732 |
-
copy-initialization contexts; list-initialization in a
|
| 2733 |
-
direct-initialization context is called *direct-list-initialization* and
|
| 2734 |
-
list-initialization in a copy-initialization context is called
|
| 2735 |
-
*copy-list-initialization*.
|
| 2736 |
-
|
| 2737 |
-
[*Note 1*:
|
| 2738 |
-
|
| 2739 |
-
List-initialization can be used
|
| 2740 |
-
|
| 2741 |
-
- as the initializer in a variable definition ([[dcl.init]])
|
| 2742 |
-
- as the initializer in a *new-expression* ([[expr.new]])
|
| 2743 |
-
- in a return statement ([[stmt.return]])
|
| 2744 |
-
- as a *for-range-initializer* ([[stmt.iter]])
|
| 2745 |
-
- as a function argument ([[expr.call]])
|
| 2746 |
-
- as a subscript ([[expr.sub]])
|
| 2747 |
-
- as an argument to a constructor invocation ([[dcl.init]],
|
| 2748 |
-
[[expr.type.conv]])
|
| 2749 |
-
- as an initializer for a non-static data member ([[class.mem]])
|
| 2750 |
-
- in a *mem-initializer* ([[class.base.init]])
|
| 2751 |
-
- on the right-hand side of an assignment ([[expr.ass]])
|
| 2752 |
-
|
| 2753 |
-
[*Example 1*:
|
| 2754 |
-
|
| 2755 |
-
``` cpp
|
| 2756 |
-
int a = {1};
|
| 2757 |
-
std::complex<double> z{1,2};
|
| 2758 |
-
new std::vector<std::string>{"once", "upon", "a", "time"}; // 4 string elements
|
| 2759 |
-
f( {"Nicholas","Annemarie"} ); // pass list of two elements
|
| 2760 |
-
return { "Norah" }; // return list of one element
|
| 2761 |
-
int* e {}; // initialization to zero / null pointer
|
| 2762 |
-
x = double{1}; // explicitly construct a double
|
| 2763 |
-
std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
|
| 2764 |
-
```
|
| 2765 |
-
|
| 2766 |
-
— *end example*]
|
| 2767 |
-
|
| 2768 |
-
— *end note*]
|
| 2769 |
-
|
| 2770 |
-
A constructor is an *initializer-list constructor* if its first
|
| 2771 |
-
parameter is of type `std::initializer_list<E>` or reference to possibly
|
| 2772 |
-
cv-qualified `std::initializer_list<E>` for some type `E`, and either
|
| 2773 |
-
there are no other parameters or else all other parameters have default
|
| 2774 |
-
arguments ([[dcl.fct.default]]).
|
| 2775 |
-
|
| 2776 |
-
[*Note 2*: Initializer-list constructors are favored over other
|
| 2777 |
-
constructors in list-initialization ([[over.match.list]]). Passing an
|
| 2778 |
-
initializer list as the argument to the constructor template
|
| 2779 |
-
`template<class T> C(T)` of a class `C` does not create an
|
| 2780 |
-
initializer-list constructor, because an initializer list argument
|
| 2781 |
-
causes the corresponding parameter to be a non-deduced context (
|
| 2782 |
-
[[temp.deduct.call]]). — *end note*]
|
| 2783 |
-
|
| 2784 |
-
The template `std::initializer_list` is not predefined; if the header
|
| 2785 |
-
`<initializer_list>` is not included prior to a use of
|
| 2786 |
-
`std::initializer_list` — even an implicit use in which the type is not
|
| 2787 |
-
named ([[dcl.spec.auto]]) — the program is ill-formed.
|
| 2788 |
-
|
| 2789 |
-
List-initialization of an object or reference of type `T` is defined as
|
| 2790 |
-
follows:
|
| 2791 |
-
|
| 2792 |
-
- If `T` is an aggregate class and the initializer list has a single
|
| 2793 |
-
element of type *cv* `U`, where `U` is `T` or a class derived from
|
| 2794 |
-
`T`, the object is initialized from that element (by
|
| 2795 |
-
copy-initialization for copy-list-initialization, or by
|
| 2796 |
-
direct-initialization for direct-list-initialization).
|
| 2797 |
-
- Otherwise, if `T` is a character array and the initializer list has a
|
| 2798 |
-
single element that is an appropriately-typed string literal (
|
| 2799 |
-
[[dcl.init.string]]), initialization is performed as described in that
|
| 2800 |
-
section.
|
| 2801 |
-
- Otherwise, if `T` is an aggregate, aggregate initialization is
|
| 2802 |
-
performed ([[dcl.init.aggr]]).
|
| 2803 |
-
\[*Example 2*:
|
| 2804 |
-
``` cpp
|
| 2805 |
-
double ad[] = { 1, 2.0 }; // OK
|
| 2806 |
-
int ai[] = { 1, 2.0 }; // error: narrowing
|
| 2807 |
-
|
| 2808 |
-
struct S2 {
|
| 2809 |
-
int m1;
|
| 2810 |
-
double m2, m3;
|
| 2811 |
-
};
|
| 2812 |
-
S2 s21 = { 1, 2, 3.0 }; // OK
|
| 2813 |
-
S2 s22 { 1.0, 2, 3 }; // error: narrowing
|
| 2814 |
-
S2 s23 { }; // OK: default to 0,0,0
|
| 2815 |
-
```
|
| 2816 |
-
|
| 2817 |
-
— *end example*]
|
| 2818 |
-
- Otherwise, if the initializer list has no elements and `T` is a class
|
| 2819 |
-
type with a default constructor, the object is value-initialized.
|
| 2820 |
-
- Otherwise, if `T` is a specialization of `std::initializer_list<E>`,
|
| 2821 |
-
the object is constructed as described below.
|
| 2822 |
-
- Otherwise, if `T` is a class type, constructors are considered. The
|
| 2823 |
-
applicable constructors are enumerated and the best one is chosen
|
| 2824 |
-
through overload resolution ([[over.match]], [[over.match.list]]).
|
| 2825 |
-
If a narrowing conversion (see below) is required to convert any of
|
| 2826 |
-
the arguments, the program is ill-formed.
|
| 2827 |
-
\[*Example 3*:
|
| 2828 |
-
``` cpp
|
| 2829 |
-
struct S {
|
| 2830 |
-
S(std::initializer_list<double>); // #1
|
| 2831 |
-
S(std::initializer_list<int>); // #2
|
| 2832 |
-
S(); // #3
|
| 2833 |
-
// ...
|
| 2834 |
-
};
|
| 2835 |
-
S s1 = { 1.0, 2.0, 3.0 }; // invoke #1
|
| 2836 |
-
S s2 = { 1, 2, 3 }; // invoke #2
|
| 2837 |
-
S s3 = { }; // invoke #3
|
| 2838 |
-
```
|
| 2839 |
-
|
| 2840 |
-
— *end example*]
|
| 2841 |
-
\[*Example 4*:
|
| 2842 |
-
``` cpp
|
| 2843 |
-
struct Map {
|
| 2844 |
-
Map(std::initializer_list<std::pair<std::string,int>>);
|
| 2845 |
-
};
|
| 2846 |
-
Map ship = {{"Sophie",14}, {"Surprise",28}};
|
| 2847 |
-
```
|
| 2848 |
-
|
| 2849 |
-
— *end example*]
|
| 2850 |
-
\[*Example 5*:
|
| 2851 |
-
``` cpp
|
| 2852 |
-
struct S {
|
| 2853 |
-
// no initializer-list constructors
|
| 2854 |
-
S(int, double, double); // #1
|
| 2855 |
-
S(); // #2
|
| 2856 |
-
// ...
|
| 2857 |
-
};
|
| 2858 |
-
S s1 = { 1, 2, 3.0 }; // OK: invoke #1
|
| 2859 |
-
S s2 { 1.0, 2, 3 }; // error: narrowing
|
| 2860 |
-
S s3 { }; // OK: invoke #2
|
| 2861 |
-
```
|
| 2862 |
-
|
| 2863 |
-
— *end example*]
|
| 2864 |
-
- Otherwise, if `T` is an enumeration with a fixed underlying type (
|
| 2865 |
-
[[dcl.enum]]), the *initializer-list* has a single element `v`, and
|
| 2866 |
-
the initialization is direct-list-initialization, the object is
|
| 2867 |
-
initialized with the value `T(v)` ([[expr.type.conv]]); if a
|
| 2868 |
-
narrowing conversion is required to convert `v` to the underlying type
|
| 2869 |
-
of `T`, the program is ill-formed.
|
| 2870 |
-
\[*Example 6*:
|
| 2871 |
-
``` cpp
|
| 2872 |
-
enum byte : unsigned char { };
|
| 2873 |
-
byte b { 42 }; // OK
|
| 2874 |
-
byte c = { 42 }; // error
|
| 2875 |
-
byte d = byte{ 42 }; // OK; same value as b
|
| 2876 |
-
byte e { -1 }; // error
|
| 2877 |
-
|
| 2878 |
-
struct A { byte b; };
|
| 2879 |
-
A a1 = { { 42 } }; // error
|
| 2880 |
-
A a2 = { byte{ 42 } }; // OK
|
| 2881 |
-
|
| 2882 |
-
void f(byte);
|
| 2883 |
-
f({ 42 }); // error
|
| 2884 |
-
|
| 2885 |
-
enum class Handle : uint32_t { Invalid = 0 };
|
| 2886 |
-
Handle h { 42 }; // OK
|
| 2887 |
-
```
|
| 2888 |
-
|
| 2889 |
-
— *end example*]
|
| 2890 |
-
- Otherwise, if the initializer list has a single element of type `E`
|
| 2891 |
-
and either `T` is not a reference type or its referenced type is
|
| 2892 |
-
reference-related to `E`, the object or reference is initialized from
|
| 2893 |
-
that element (by copy-initialization for copy-list-initialization, or
|
| 2894 |
-
by direct-initialization for direct-list-initialization); if a
|
| 2895 |
-
narrowing conversion (see below) is required to convert the element to
|
| 2896 |
-
`T`, the program is ill-formed.
|
| 2897 |
-
\[*Example 7*:
|
| 2898 |
-
``` cpp
|
| 2899 |
-
int x1 {2}; // OK
|
| 2900 |
-
int x2 {2.0}; // error: narrowing
|
| 2901 |
-
```
|
| 2902 |
-
|
| 2903 |
-
— *end example*]
|
| 2904 |
-
- Otherwise, if `T` is a reference type, a prvalue of the type
|
| 2905 |
-
referenced by `T` is generated. The prvalue initializes its result
|
| 2906 |
-
object by copy-list-initialization or direct-list-initialization,
|
| 2907 |
-
depending on the kind of initialization for the reference. The prvalue
|
| 2908 |
-
is then used to direct-initialize the reference.
|
| 2909 |
-
\[*Note 3*: As usual, the binding will fail and the program is
|
| 2910 |
-
ill-formed if the reference type is an lvalue reference to a non-const
|
| 2911 |
-
type. — *end note*]
|
| 2912 |
-
\[*Example 8*:
|
| 2913 |
-
``` cpp
|
| 2914 |
-
struct S {
|
| 2915 |
-
S(std::initializer_list<double>); // #1
|
| 2916 |
-
S(const std::string&); // #2
|
| 2917 |
-
// ...
|
| 2918 |
-
};
|
| 2919 |
-
const S& r1 = { 1, 2, 3.0 }; // OK: invoke #1
|
| 2920 |
-
const S& r2 { "Spinach" }; // OK: invoke #2
|
| 2921 |
-
S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
|
| 2922 |
-
const int& i1 = { 1 }; // OK
|
| 2923 |
-
const int& i2 = { 1.1 }; // error: narrowing
|
| 2924 |
-
const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
|
| 2925 |
-
```
|
| 2926 |
-
|
| 2927 |
-
— *end example*]
|
| 2928 |
-
- Otherwise, if the initializer list has no elements, the object is
|
| 2929 |
-
value-initialized.
|
| 2930 |
-
\[*Example 9*:
|
| 2931 |
-
``` cpp
|
| 2932 |
-
int** pp {}; // initialized to null pointer
|
| 2933 |
-
```
|
| 2934 |
-
|
| 2935 |
-
— *end example*]
|
| 2936 |
-
- Otherwise, the program is ill-formed.
|
| 2937 |
-
\[*Example 10*:
|
| 2938 |
-
``` cpp
|
| 2939 |
-
struct A { int i; int j; };
|
| 2940 |
-
A a1 { 1, 2 }; // aggregate initialization
|
| 2941 |
-
A a2 { 1.2 }; // error: narrowing
|
| 2942 |
-
struct B {
|
| 2943 |
-
B(std::initializer_list<int>);
|
| 2944 |
-
};
|
| 2945 |
-
B b1 { 1, 2 }; // creates initializer_list<int> and calls constructor
|
| 2946 |
-
B b2 { 1, 2.0 }; // error: narrowing
|
| 2947 |
-
struct C {
|
| 2948 |
-
C(int i, double j);
|
| 2949 |
-
};
|
| 2950 |
-
C c1 = { 1, 2.2 }; // calls constructor with arguments (1, 2.2)
|
| 2951 |
-
C c2 = { 1.1, 2 }; // error: narrowing
|
| 2952 |
-
|
| 2953 |
-
int j { 1 }; // initialize to 1
|
| 2954 |
-
int k { }; // initialize to 0
|
| 2955 |
-
```
|
| 2956 |
-
|
| 2957 |
-
— *end example*]
|
| 2958 |
-
|
| 2959 |
-
Within the *initializer-list* of a *braced-init-list*, the
|
| 2960 |
-
*initializer-clause*s, including any that result from pack expansions (
|
| 2961 |
-
[[temp.variadic]]), are evaluated in the order in which they appear.
|
| 2962 |
-
That is, every value computation and side effect associated with a given
|
| 2963 |
-
*initializer-clause* is sequenced before every value computation and
|
| 2964 |
-
side effect associated with any *initializer-clause* that follows it in
|
| 2965 |
-
the comma-separated list of the *initializer-list*.
|
| 2966 |
-
|
| 2967 |
-
[*Note 4*: This evaluation ordering holds regardless of the semantics
|
| 2968 |
-
of the initialization; for example, it applies when the elements of the
|
| 2969 |
-
*initializer-list* are interpreted as arguments of a constructor call,
|
| 2970 |
-
even though ordinarily there are no sequencing constraints on the
|
| 2971 |
-
arguments of a call. — *end note*]
|
| 2972 |
-
|
| 2973 |
-
An object of type `std::initializer_list<E>` is constructed from an
|
| 2974 |
-
initializer list as if the implementation generated and materialized (
|
| 2975 |
-
[[conv.rval]]) a prvalue of type “array of N `const E`”, where N is the
|
| 2976 |
-
number of elements in the initializer list. Each element of that array
|
| 2977 |
-
is copy-initialized with the corresponding element of the initializer
|
| 2978 |
-
list, and the `std::initializer_list<E>` object is constructed to refer
|
| 2979 |
-
to that array.
|
| 2980 |
-
|
| 2981 |
-
[*Note 5*: A constructor or conversion function selected for the copy
|
| 2982 |
-
shall be accessible (Clause [[class.access]]) in the context of the
|
| 2983 |
-
initializer list. — *end note*]
|
| 2984 |
-
|
| 2985 |
-
If a narrowing conversion is required to initialize any of the elements,
|
| 2986 |
-
the program is ill-formed.
|
| 2987 |
-
|
| 2988 |
-
[*Example 11*:
|
| 2989 |
-
|
| 2990 |
-
``` cpp
|
| 2991 |
-
struct X {
|
| 2992 |
-
X(std::initializer_list<double> v);
|
| 2993 |
-
};
|
| 2994 |
-
X x{ 1,2,3 };
|
| 2995 |
-
```
|
| 2996 |
-
|
| 2997 |
-
The initialization will be implemented in a way roughly equivalent to
|
| 2998 |
-
this:
|
| 2999 |
-
|
| 3000 |
-
``` cpp
|
| 3001 |
-
const double __a[3] = {double{1}, double{2}, double{3}};
|
| 3002 |
-
X x(std::initializer_list<double>(__a, __a+3));
|
| 3003 |
-
```
|
| 3004 |
-
|
| 3005 |
-
assuming that the implementation can construct an `initializer_list`
|
| 3006 |
-
object with a pair of pointers.
|
| 3007 |
-
|
| 3008 |
-
— *end example*]
|
| 3009 |
-
|
| 3010 |
-
The array has the same lifetime as any other temporary object (
|
| 3011 |
-
[[class.temporary]]), except that initializing an `initializer_list`
|
| 3012 |
-
object from the array extends the lifetime of the array exactly like
|
| 3013 |
-
binding a reference to a temporary.
|
| 3014 |
-
|
| 3015 |
-
[*Example 12*:
|
| 3016 |
-
|
| 3017 |
-
``` cpp
|
| 3018 |
-
typedef std::complex<double> cmplx;
|
| 3019 |
-
std::vector<cmplx> v1 = { 1, 2, 3 };
|
| 3020 |
-
|
| 3021 |
-
void f() {
|
| 3022 |
-
std::vector<cmplx> v2{ 1, 2, 3 };
|
| 3023 |
-
std::initializer_list<int> i3 = { 1, 2, 3 };
|
| 3024 |
-
}
|
| 3025 |
-
|
| 3026 |
-
struct A {
|
| 3027 |
-
std::initializer_list<int> i4;
|
| 3028 |
-
A() : i4{ 1, 2, 3 } {} // ill-formed, would create a dangling reference
|
| 3029 |
-
};
|
| 3030 |
-
```
|
| 3031 |
-
|
| 3032 |
-
For `v1` and `v2`, the `initializer_list` object is a parameter in a
|
| 3033 |
-
function call, so the array created for `{ 1, 2, 3 }` has
|
| 3034 |
-
full-expression lifetime. For `i3`, the `initializer_list` object is a
|
| 3035 |
-
variable, so the array persists for the lifetime of the variable. For
|
| 3036 |
-
`i4`, the `initializer_list` object is initialized in the constructor’s
|
| 3037 |
-
*ctor-initializer* as if by binding a temporary array to a reference
|
| 3038 |
-
member, so the program is ill-formed ([[class.base.init]]).
|
| 3039 |
-
|
| 3040 |
-
— *end example*]
|
| 3041 |
-
|
| 3042 |
-
[*Note 6*: The implementation is free to allocate the array in
|
| 3043 |
-
read-only memory if an explicit array with the same initializer could be
|
| 3044 |
-
so allocated. — *end note*]
|
| 3045 |
-
|
| 3046 |
-
A *narrowing conversion* is an implicit conversion
|
| 3047 |
-
|
| 3048 |
-
- from a floating-point type to an integer type, or
|
| 3049 |
-
- from `long double` to `double` or `float`, or from `double` to
|
| 3050 |
-
`float`, except where the source is a constant expression and the
|
| 3051 |
-
actual value after conversion is within the range of values that can
|
| 3052 |
-
be represented (even if it cannot be represented exactly), or
|
| 3053 |
-
- from an integer type or unscoped enumeration type to a floating-point
|
| 3054 |
-
type, except where the source is a constant expression and the actual
|
| 3055 |
-
value after conversion will fit into the target type and will produce
|
| 3056 |
-
the original value when converted back to the original type, or
|
| 3057 |
-
- from an integer type or unscoped enumeration type to an integer type
|
| 3058 |
-
that cannot represent all the values of the original type, except
|
| 3059 |
-
where the source is a constant expression whose value after integral
|
| 3060 |
-
promotions will fit into the target type.
|
| 3061 |
-
|
| 3062 |
-
[*Note 7*: As indicated above, such conversions are not allowed at the
|
| 3063 |
-
top level in list-initializations. — *end note*]
|
| 3064 |
-
|
| 3065 |
-
[*Example 13*:
|
| 3066 |
-
|
| 3067 |
-
``` cpp
|
| 3068 |
-
int x = 999; // x is not a constant expression
|
| 3069 |
-
const int y = 999;
|
| 3070 |
-
const int z = 99;
|
| 3071 |
-
char c1 = x; // OK, though it might narrow (in this case, it does narrow)
|
| 3072 |
-
char c2{x}; // error: might narrow
|
| 3073 |
-
char c3{y}; // error: narrows (assuming char is 8 bits)
|
| 3074 |
-
char c4{z}; // OK: no narrowing needed
|
| 3075 |
-
unsigned char uc1 = {5}; // OK: no narrowing needed
|
| 3076 |
-
unsigned char uc2 = {-1}; // error: narrows
|
| 3077 |
-
unsigned int ui1 = {-1}; // error: narrows
|
| 3078 |
-
signed int si1 =
|
| 3079 |
-
{ (unsigned int)-1 }; // error: narrows
|
| 3080 |
-
int ii = {2.0}; // error: narrows
|
| 3081 |
-
float f1 { x }; // error: might narrow
|
| 3082 |
-
float f2 { 7 }; // OK: 7 can be exactly represented as a float
|
| 3083 |
-
int f(int);
|
| 3084 |
-
int a[] =
|
| 3085 |
-
{ 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level
|
| 3086 |
-
```
|
| 3087 |
-
|
| 3088 |
-
— *end example*]
|
| 3089 |
-
|
| 3090 |
-
<!-- Link reference definitions -->
|
| 3091 |
-
[basic.align]: basic.md#basic.align
|
| 3092 |
-
[basic.compound]: basic.md#basic.compound
|
| 3093 |
-
[basic.def]: basic.md#basic.def
|
| 3094 |
-
[basic.def.odr]: basic.md#basic.def.odr
|
| 3095 |
-
[basic.fundamental]: basic.md#basic.fundamental
|
| 3096 |
-
[basic.life]: basic.md#basic.life
|
| 3097 |
-
[basic.link]: basic.md#basic.link
|
| 3098 |
-
[basic.lookup]: basic.md#basic.lookup
|
| 3099 |
-
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 3100 |
-
[basic.lookup.classref]: basic.md#basic.lookup.classref
|
| 3101 |
-
[basic.lookup.elab]: basic.md#basic.lookup.elab
|
| 3102 |
-
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
| 3103 |
-
[basic.lookup.udir]: basic.md#basic.lookup.udir
|
| 3104 |
-
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 3105 |
-
[basic.lval]: basic.md#basic.lval
|
| 3106 |
-
[basic.namespace]: #basic.namespace
|
| 3107 |
-
[basic.scope]: basic.md#basic.scope
|
| 3108 |
-
[basic.scope.block]: basic.md#basic.scope.block
|
| 3109 |
-
[basic.scope.declarative]: basic.md#basic.scope.declarative
|
| 3110 |
-
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
| 3111 |
-
[basic.scope.pdecl]: basic.md#basic.scope.pdecl
|
| 3112 |
-
[basic.scope.proto]: basic.md#basic.scope.proto
|
| 3113 |
-
[basic.start]: basic.md#basic.start
|
| 3114 |
-
[basic.start.dynamic]: basic.md#basic.start.dynamic
|
| 3115 |
-
[basic.start.static]: basic.md#basic.start.static
|
| 3116 |
-
[basic.stc]: basic.md#basic.stc
|
| 3117 |
-
[basic.stc.auto]: basic.md#basic.stc.auto
|
| 3118 |
-
[basic.stc.dynamic]: basic.md#basic.stc.dynamic
|
| 3119 |
-
[basic.stc.static]: basic.md#basic.stc.static
|
| 3120 |
-
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 3121 |
-
[basic.type.qualifier]: basic.md#basic.type.qualifier
|
| 3122 |
-
[basic.types]: basic.md#basic.types
|
| 3123 |
-
[class]: class.md#class
|
| 3124 |
-
[class.access]: class.md#class.access
|
| 3125 |
-
[class.base.init]: special.md#class.base.init
|
| 3126 |
-
[class.bit]: class.md#class.bit
|
| 3127 |
-
[class.conv]: special.md#class.conv
|
| 3128 |
-
[class.conv.ctor]: special.md#class.conv.ctor
|
| 3129 |
-
[class.conv.fct]: special.md#class.conv.fct
|
| 3130 |
-
[class.copy]: special.md#class.copy
|
| 3131 |
-
[class.ctor]: special.md#class.ctor
|
| 3132 |
-
[class.dtor]: special.md#class.dtor
|
| 3133 |
-
[class.expl.init]: special.md#class.expl.init
|
| 3134 |
-
[class.friend]: class.md#class.friend
|
| 3135 |
-
[class.inhctor.init]: special.md#class.inhctor.init
|
| 3136 |
-
[class.init]: special.md#class.init
|
| 3137 |
-
[class.mem]: class.md#class.mem
|
| 3138 |
-
[class.member.lookup]: class.md#class.member.lookup
|
| 3139 |
-
[class.mfct]: class.md#class.mfct
|
| 3140 |
-
[class.mi]: class.md#class.mi
|
| 3141 |
-
[class.name]: class.md#class.name
|
| 3142 |
-
[class.qual]: basic.md#class.qual
|
| 3143 |
-
[class.static]: class.md#class.static
|
| 3144 |
-
[class.static.data]: class.md#class.static.data
|
| 3145 |
-
[class.temporary]: special.md#class.temporary
|
| 3146 |
-
[class.union]: class.md#class.union
|
| 3147 |
-
[class.union.anon]: class.md#class.union.anon
|
| 3148 |
-
[class.virtual]: class.md#class.virtual
|
| 3149 |
-
[conv]: conv.md#conv
|
| 3150 |
-
[conv.array]: conv.md#conv.array
|
| 3151 |
-
[conv.func]: conv.md#conv.func
|
| 3152 |
-
[conv.integral]: conv.md#conv.integral
|
| 3153 |
-
[conv.lval]: conv.md#conv.lval
|
| 3154 |
-
[conv.prom]: conv.md#conv.prom
|
| 3155 |
-
[conv.ptr]: conv.md#conv.ptr
|
| 3156 |
-
[conv.qual]: conv.md#conv.qual
|
| 3157 |
-
[conv.rval]: conv.md#conv.rval
|
| 3158 |
-
[cstddef.syn]: language.md#cstddef.syn
|
| 3159 |
-
[dcl.align]: #dcl.align
|
| 3160 |
-
[dcl.ambig.res]: #dcl.ambig.res
|
| 3161 |
-
[dcl.array]: #dcl.array
|
| 3162 |
-
[dcl.asm]: #dcl.asm
|
| 3163 |
-
[dcl.attr]: #dcl.attr
|
| 3164 |
-
[dcl.attr.depend]: #dcl.attr.depend
|
| 3165 |
-
[dcl.attr.deprecated]: #dcl.attr.deprecated
|
| 3166 |
-
[dcl.attr.fallthrough]: #dcl.attr.fallthrough
|
| 3167 |
-
[dcl.attr.grammar]: #dcl.attr.grammar
|
| 3168 |
-
[dcl.attr.nodiscard]: #dcl.attr.nodiscard
|
| 3169 |
-
[dcl.attr.noreturn]: #dcl.attr.noreturn
|
| 3170 |
-
[dcl.attr.unused]: #dcl.attr.unused
|
| 3171 |
-
[dcl.constexpr]: #dcl.constexpr
|
| 3172 |
-
[dcl.dcl]: #dcl.dcl
|
| 3173 |
-
[dcl.decl]: #dcl.decl
|
| 3174 |
-
[dcl.enum]: #dcl.enum
|
| 3175 |
-
[dcl.fct]: #dcl.fct
|
| 3176 |
-
[dcl.fct.def]: #dcl.fct.def
|
| 3177 |
-
[dcl.fct.def.default]: #dcl.fct.def.default
|
| 3178 |
-
[dcl.fct.def.delete]: #dcl.fct.def.delete
|
| 3179 |
-
[dcl.fct.def.general]: #dcl.fct.def.general
|
| 3180 |
-
[dcl.fct.default]: #dcl.fct.default
|
| 3181 |
-
[dcl.fct.spec]: #dcl.fct.spec
|
| 3182 |
-
[dcl.friend]: #dcl.friend
|
| 3183 |
-
[dcl.init]: #dcl.init
|
| 3184 |
-
[dcl.init.aggr]: #dcl.init.aggr
|
| 3185 |
-
[dcl.init.list]: #dcl.init.list
|
| 3186 |
-
[dcl.init.ref]: #dcl.init.ref
|
| 3187 |
-
[dcl.init.string]: #dcl.init.string
|
| 3188 |
-
[dcl.inline]: #dcl.inline
|
| 3189 |
-
[dcl.link]: #dcl.link
|
| 3190 |
-
[dcl.meaning]: #dcl.meaning
|
| 3191 |
-
[dcl.mptr]: #dcl.mptr
|
| 3192 |
-
[dcl.name]: #dcl.name
|
| 3193 |
-
[dcl.ptr]: #dcl.ptr
|
| 3194 |
-
[dcl.ref]: #dcl.ref
|
| 3195 |
-
[dcl.spec]: #dcl.spec
|
| 3196 |
-
[dcl.spec.auto]: #dcl.spec.auto
|
| 3197 |
-
[dcl.stc]: #dcl.stc
|
| 3198 |
-
[dcl.struct.bind]: #dcl.struct.bind
|
| 3199 |
-
[dcl.type]: #dcl.type
|
| 3200 |
-
[dcl.type.auto.deduct]: #dcl.type.auto.deduct
|
| 3201 |
-
[dcl.type.class.deduct]: #dcl.type.class.deduct
|
| 3202 |
-
[dcl.type.cv]: #dcl.type.cv
|
| 3203 |
-
[dcl.type.elab]: #dcl.type.elab
|
| 3204 |
-
[dcl.type.simple]: #dcl.type.simple
|
| 3205 |
-
[dcl.typedef]: #dcl.typedef
|
| 3206 |
-
[except.handle]: except.md#except.handle
|
| 3207 |
-
[except.spec]: except.md#except.spec
|
| 3208 |
-
[except.throw]: except.md#except.throw
|
| 3209 |
-
[expr]: expr.md#expr
|
| 3210 |
-
[expr.alignof]: expr.md#expr.alignof
|
| 3211 |
-
[expr.ass]: expr.md#expr.ass
|
| 3212 |
-
[expr.call]: expr.md#expr.call
|
| 3213 |
-
[expr.cast]: expr.md#expr.cast
|
| 3214 |
-
[expr.comma]: expr.md#expr.comma
|
| 3215 |
-
[expr.cond]: expr.md#expr.cond
|
| 3216 |
-
[expr.const]: expr.md#expr.const
|
| 3217 |
-
[expr.const.cast]: expr.md#expr.const.cast
|
| 3218 |
-
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 3219 |
-
[expr.new]: expr.md#expr.new
|
| 3220 |
-
[expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
|
| 3221 |
-
[expr.prim.this]: expr.md#expr.prim.this
|
| 3222 |
-
[expr.ref]: expr.md#expr.ref
|
| 3223 |
-
[expr.static.cast]: expr.md#expr.static.cast
|
| 3224 |
-
[expr.sub]: expr.md#expr.sub
|
| 3225 |
-
[expr.type.conv]: expr.md#expr.type.conv
|
| 3226 |
-
[expr.unary]: expr.md#expr.unary
|
| 3227 |
-
[expr.unary.op]: expr.md#expr.unary.op
|
| 3228 |
-
[intro.compliance]: intro.md#intro.compliance
|
| 3229 |
-
[intro.execution]: intro.md#intro.execution
|
| 3230 |
-
[intro.multithread]: intro.md#intro.multithread
|
| 3231 |
-
[lex.charset]: lex.md#lex.charset
|
| 3232 |
-
[lex.digraph]: lex.md#lex.digraph
|
| 3233 |
-
[lex.key]: lex.md#lex.key
|
| 3234 |
-
[lex.name]: lex.md#lex.name
|
| 3235 |
-
[lex.string]: lex.md#lex.string
|
| 3236 |
-
[namespace.alias]: #namespace.alias
|
| 3237 |
-
[namespace.def]: #namespace.def
|
| 3238 |
-
[namespace.memdef]: #namespace.memdef
|
| 3239 |
-
[namespace.qual]: basic.md#namespace.qual
|
| 3240 |
-
[namespace.udecl]: #namespace.udecl
|
| 3241 |
-
[namespace.udir]: #namespace.udir
|
| 3242 |
-
[namespace.unnamed]: #namespace.unnamed
|
| 3243 |
-
[over]: over.md#over
|
| 3244 |
-
[over.match]: over.md#over.match
|
| 3245 |
-
[over.match.class.deduct]: over.md#over.match.class.deduct
|
| 3246 |
-
[over.match.conv]: over.md#over.match.conv
|
| 3247 |
-
[over.match.copy]: over.md#over.match.copy
|
| 3248 |
-
[over.match.ctor]: over.md#over.match.ctor
|
| 3249 |
-
[over.match.list]: over.md#over.match.list
|
| 3250 |
-
[over.match.ref]: over.md#over.match.ref
|
| 3251 |
-
[over.oper]: over.md#over.oper
|
| 3252 |
-
[over.sub]: over.md#over.sub
|
| 3253 |
-
[stmt.ambig]: stmt.md#stmt.ambig
|
| 3254 |
-
[stmt.dcl]: stmt.md#stmt.dcl
|
| 3255 |
-
[stmt.expr]: stmt.md#stmt.expr
|
| 3256 |
-
[stmt.if]: stmt.md#stmt.if
|
| 3257 |
-
[stmt.iter]: stmt.md#stmt.iter
|
| 3258 |
-
[stmt.label]: stmt.md#stmt.label
|
| 3259 |
-
[stmt.return]: stmt.md#stmt.return
|
| 3260 |
-
[stmt.select]: stmt.md#stmt.select
|
| 3261 |
-
[stmt.stmt]: stmt.md#stmt.stmt
|
| 3262 |
-
[stmt.switch]: stmt.md#stmt.switch
|
| 3263 |
-
[support.runtime]: language.md#support.runtime
|
| 3264 |
-
[tab:simple.type.specifiers]: #tab:simple.type.specifiers
|
| 3265 |
-
[temp]: temp.md#temp
|
| 3266 |
-
[temp.arg.type]: temp.md#temp.arg.type
|
| 3267 |
-
[temp.class.spec]: temp.md#temp.class.spec
|
| 3268 |
-
[temp.deduct]: temp.md#temp.deduct
|
| 3269 |
-
[temp.deduct.call]: temp.md#temp.deduct.call
|
| 3270 |
-
[temp.dep]: temp.md#temp.dep
|
| 3271 |
-
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 3272 |
-
[temp.explicit]: temp.md#temp.explicit
|
| 3273 |
-
[temp.inst]: temp.md#temp.inst
|
| 3274 |
-
[temp.mem]: temp.md#temp.mem
|
| 3275 |
-
[temp.names]: temp.md#temp.names
|
| 3276 |
-
[temp.param]: temp.md#temp.param
|
| 3277 |
-
[temp.res]: temp.md#temp.res
|
| 3278 |
-
[temp.spec]: temp.md#temp.spec
|
| 3279 |
-
[temp.variadic]: temp.md#temp.variadic
|
| 3280 |
-
|
| 3281 |
-
[^1]: The “implicit int” rule of C is no longer supported.
|
| 3282 |
-
|
| 3283 |
-
[^2]: The `inline` keyword has no effect on the linkage of a function.
|
| 3284 |
-
|
| 3285 |
-
[^3]: There is no special provision for a *decl-specifier-seq* that
|
| 3286 |
-
lacks a *type-specifier* or that has a *type-specifier* that only
|
| 3287 |
-
specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
|
| 3288 |
-
supported.
|
| 3289 |
-
|
| 3290 |
-
[^4]: This set of values is used to define promotion and conversion
|
| 3291 |
-
semantics for the enumeration type. It does not preclude an
|
| 3292 |
-
expression of enumeration type from having a value that falls
|
| 3293 |
-
outside this range.
|
| 3294 |
-
|
| 3295 |
-
[^5]: this implies that the name of the class or function is
|
| 3296 |
-
unqualified.
|
| 3297 |
-
|
| 3298 |
-
[^6]: A *using-declaration* with more than one *using-declarator* is
|
| 3299 |
-
equivalent to a corresponding sequence of *using-declaration*s with
|
| 3300 |
-
one *using-declarator* each.
|
| 3301 |
-
|
| 3302 |
-
[^7]: During name lookup in a class hierarchy, some ambiguities may be
|
| 3303 |
-
resolved by considering whether one member hides the other along
|
| 3304 |
-
some paths ([[class.member.lookup]]). There is no such
|
| 3305 |
-
disambiguation when considering the set of names found as a result
|
| 3306 |
-
of following *using-directive*s.
|
| 3307 |
-
|
| 3308 |
-
[^8]: As indicated by syntax, cv-qualifiers are a significant component
|
| 3309 |
-
in function return types.
|
| 3310 |
-
|
| 3311 |
-
[^9]: One can explicitly disambiguate the parse either by introducing a
|
| 3312 |
-
comma (so the ellipsis will be parsed as part of the
|
| 3313 |
-
*parameter-declaration-clause*) or by introducing a name for the
|
| 3314 |
-
parameter (so the ellipsis will be parsed as part of the
|
| 3315 |
-
*declarator-id*).
|
| 3316 |
-
|
| 3317 |
-
[^10]: This means that default arguments cannot appear, for example, in
|
| 3318 |
-
declarations of pointers to functions, references to functions, or
|
| 3319 |
-
`typedef` declarations.
|
| 3320 |
-
|
| 3321 |
-
[^11]: Implementations are permitted to provide additional predefined
|
| 3322 |
-
variables with names that are reserved to the implementation (
|
| 3323 |
-
[[lex.name]]). If a predefined variable is not odr-used (
|
| 3324 |
-
[[basic.def.odr]]), its string value need not be present in the
|
| 3325 |
-
program image.
|
| 3326 |
-
|
| 3327 |
-
[^12]: As specified in [[conv.ptr]], converting an integer literal
|
| 3328 |
-
whose value is `0` to a pointer type results in a null pointer
|
| 3329 |
-
value.
|
| 3330 |
-
|
| 3331 |
-
[^13]: The syntax provides for empty *initializer-list*s, but
|
| 3332 |
-
nonetheless C++does not have zero length arrays.
|
| 3333 |
-
|
| 3334 |
-
[^14]: This requires a conversion function ([[class.conv.fct]])
|
| 3335 |
-
returning a reference type.
|
|
|
|
| 1 |
+
## Declarators <a id="dcl.decl">[[dcl.decl]]</a>
|
| 2 |
|
| 3 |
A declarator declares a single variable, function, or type, within a
|
| 4 |
declaration. The *init-declarator-list* appearing in a declaration is a
|
| 5 |
comma-separated sequence of declarators, each of which can have an
|
| 6 |
initializer.
|
|
|
|
| 12 |
```
|
| 13 |
|
| 14 |
``` bnf
|
| 15 |
init-declarator:
|
| 16 |
declarator initializerₒₚₜ
|
| 17 |
+
declarator requires-clause
|
| 18 |
```
|
| 19 |
|
| 20 |
+
The three components of a *simple-declaration* are the attributes
|
| 21 |
+
[[dcl.attr]], the specifiers (*decl-specifier-seq*; [[dcl.spec]]) and
|
| 22 |
the declarators (*init-declarator-list*). The specifiers indicate the
|
| 23 |
type, storage class or other properties of the entities being declared.
|
| 24 |
The declarators specify the names of these entities and (optionally)
|
| 25 |
modify the type of the specifiers with operators such as `*` (pointer
|
| 26 |
to) and `()` (function returning). Initial values can also be specified
|
|
|
|
| 63 |
struct S { ... };
|
| 64 |
S S;
|
| 65 |
S T; // error
|
| 66 |
```
|
| 67 |
|
| 68 |
+
Another exception is when `T` is `auto` [[dcl.spec.auto]], for example:
|
|
|
|
| 69 |
|
| 70 |
``` cpp
|
| 71 |
auto i = 1, j = 2.0; // error: deduced types for i and j do not match
|
| 72 |
```
|
| 73 |
|
|
|
|
| 78 |
auto j = 2.0; // OK: j deduced to have type double
|
| 79 |
```
|
| 80 |
|
| 81 |
— *end note*]
|
| 82 |
|
| 83 |
+
The optional *requires-clause* [[temp.pre]] in an *init-declarator* or
|
| 84 |
+
*member-declarator* shall be present only if the declarator declares a
|
| 85 |
+
templated function [[dcl.fct]]. When present after a declarator, the
|
| 86 |
+
*requires-clause* is called the *trailing *requires-clause**. The
|
| 87 |
+
trailing *requires-clause* introduces the *constraint-expression* that
|
| 88 |
+
results from interpreting its *constraint-logical-or-expression* as a
|
| 89 |
+
*constraint-expression*.
|
| 90 |
+
|
| 91 |
+
[*Example 1*:
|
| 92 |
+
|
| 93 |
+
``` cpp
|
| 94 |
+
void f1(int a) requires true; // error: non-templated function
|
| 95 |
+
template<typename T>
|
| 96 |
+
auto f2(T a) -> bool requires true; // OK
|
| 97 |
+
template<typename T>
|
| 98 |
+
auto f3(T a) requires true -> bool; // error: requires-clause precedes trailing-return-type
|
| 99 |
+
void (*pf)() requires true; // error: constraint on a variable
|
| 100 |
+
void g(int (*)() requires true); // error: constraint on a parameter-declaration
|
| 101 |
+
|
| 102 |
+
auto* p = new void(*)(char) requires true; // error: not a function declaration
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
— *end example*]
|
| 106 |
+
|
| 107 |
Declarators have the syntax
|
| 108 |
|
| 109 |
``` bnf
|
| 110 |
declarator:
|
| 111 |
ptr-declarator
|
|
|
|
| 150 |
cv-qualifier cv-qualifier-seqₒₚₜ
|
| 151 |
```
|
| 152 |
|
| 153 |
``` bnf
|
| 154 |
cv-qualifier:
|
| 155 |
+
const
|
| 156 |
+
volatile
|
| 157 |
```
|
| 158 |
|
| 159 |
``` bnf
|
| 160 |
ref-qualifier:
|
| 161 |
'&'
|
|
|
|
| 165 |
``` bnf
|
| 166 |
declarator-id:
|
| 167 |
'...'ₒₚₜ id-expression
|
| 168 |
```
|
| 169 |
|
| 170 |
+
### Type names <a id="dcl.name">[[dcl.name]]</a>
|
| 171 |
|
| 172 |
To specify type conversions explicitly, and as an argument of `sizeof`,
|
| 173 |
`alignof`, `new`, or `typeid`, the name of a type shall be specified.
|
| 174 |
This can be done with a *type-id*, which is syntactically a declaration
|
| 175 |
for a variable or function of that type that omits the name of the
|
|
|
|
| 239 |
parameters) returning pointer to `int`”, and “pointer to a function of
|
| 240 |
(`double`) returning `int`”.
|
| 241 |
|
| 242 |
— *end example*]
|
| 243 |
|
| 244 |
+
A type can also be named (often more easily) by using a `typedef`
|
| 245 |
+
[[dcl.typedef]].
|
| 246 |
|
| 247 |
+
### Ambiguity resolution <a id="dcl.ambig.res">[[dcl.ambig.res]]</a>
|
| 248 |
|
| 249 |
The ambiguity arising from the similarity between a function-style cast
|
| 250 |
and a declaration mentioned in [[stmt.ambig]] can also occur in the
|
| 251 |
context of a declaration. In that context, the choice is between a
|
| 252 |
function declaration with a redundant set of parentheses around a
|
|
|
|
| 336 |
// not: void h(int *C[10]);
|
| 337 |
```
|
| 338 |
|
| 339 |
— *end example*]
|
| 340 |
|
| 341 |
+
### Meaning of declarators <a id="dcl.meaning">[[dcl.meaning]]</a>
|
| 342 |
|
| 343 |
A declarator contains exactly one *declarator-id*; it names the
|
| 344 |
identifier that is declared. An *unqualified-id* occurring in a
|
| 345 |
*declarator-id* shall be a simple *identifier* except for the
|
| 346 |
declaration of some special functions ([[class.ctor]], [[class.conv]],
|
| 347 |
[[class.dtor]], [[over.oper]]) and for the declaration of template
|
| 348 |
+
specializations or partial specializations [[temp.spec]]. When the
|
| 349 |
*declarator-id* is qualified, the declaration shall refer to a
|
| 350 |
previously declared member of the class or namespace to which the
|
| 351 |
qualifier refers (or, in the case of a namespace, of an element of the
|
| 352 |
+
inline namespace set of that namespace [[namespace.def]]) or to a
|
| 353 |
specialization thereof; the member shall not merely have been introduced
|
| 354 |
by a *using-declaration* in the scope of the class or namespace
|
| 355 |
nominated by the *nested-name-specifier* of the *declarator-id*. The
|
| 356 |
*nested-name-specifier* of a qualified *declarator-id* shall not begin
|
| 357 |
with a *decltype-specifier*.
|
|
|
|
| 362 |
|
| 363 |
The optional *attribute-specifier-seq* following a *declarator-id*
|
| 364 |
appertains to the entity that is declared.
|
| 365 |
|
| 366 |
A `static`, `thread_local`, `extern`, `mutable`, `friend`, `inline`,
|
| 367 |
+
`virtual`, `constexpr`, or `typedef` specifier or an
|
| 368 |
+
*explicit-specifier* applies directly to each *declarator-id* in an
|
| 369 |
+
*init-declarator-list* or *member-declarator-list*; the type specified
|
| 370 |
+
for each *declarator-id* depends on both the *decl-specifier-seq* and
|
| 371 |
+
its *declarator*.
|
| 372 |
|
| 373 |
Thus, a declaration of a particular identifier has the form
|
| 374 |
|
| 375 |
``` cpp
|
| 376 |
T D
|
|
|
|
| 396 |
``` cpp
|
| 397 |
int unsigned i;
|
| 398 |
```
|
| 399 |
|
| 400 |
the type specifiers `int` `unsigned` determine the type “`unsigned int`”
|
| 401 |
+
[[dcl.type.simple]].
|
| 402 |
|
| 403 |
— *end example*]
|
| 404 |
|
| 405 |
In a declaration *attribute-specifier-seq*ₒₚₜ `T` `D` where `D` is an
|
| 406 |
unadorned identifier the type of this identifier is “`T`”.
|
|
|
|
| 419 |
```
|
| 420 |
|
| 421 |
Parentheses do not alter the type of the embedded *declarator-id*, but
|
| 422 |
they can alter the binding of complex declarators.
|
| 423 |
|
| 424 |
+
#### Pointers <a id="dcl.ptr">[[dcl.ptr]]</a>
|
| 425 |
|
| 426 |
In a declaration `T` `D` where `D` has the form
|
| 427 |
|
| 428 |
``` bnf
|
| 429 |
'*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
|
|
|
| 431 |
|
| 432 |
and the type of the identifier in the declaration `T` `D1` is
|
| 433 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 434 |
`D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
|
| 435 |
`T`”. The *cv-qualifier*s apply to the pointer and not to the object
|
| 436 |
+
pointed to. Similarly, the optional *attribute-specifier-seq*
|
| 437 |
+
[[dcl.attr.grammar]] appertains to the pointer and not to the object
|
| 438 |
pointed to.
|
| 439 |
|
| 440 |
[*Example 1*:
|
| 441 |
|
| 442 |
The declarations
|
|
|
|
| 489 |
See also [[expr.ass]] and [[dcl.init]].
|
| 490 |
|
| 491 |
[*Note 1*: Forming a pointer to reference type is ill-formed; see
|
| 492 |
[[dcl.ref]]. Forming a function pointer type is ill-formed if the
|
| 493 |
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 494 |
+
[[dcl.fct]]. Since the address of a bit-field [[class.bit]] cannot be
|
| 495 |
taken, a pointer can never point to a bit-field. — *end note*]
|
| 496 |
|
| 497 |
+
#### References <a id="dcl.ref">[[dcl.ref]]</a>
|
| 498 |
|
| 499 |
In a declaration `T` `D` where `D` has either of the forms
|
| 500 |
|
| 501 |
``` bnf
|
| 502 |
'&' attribute-specifier-seqₒₚₜ 'D1'
|
|
|
|
| 507 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 508 |
`D` is “*derived-declarator-type-list* reference to `T`”. The optional
|
| 509 |
*attribute-specifier-seq* appertains to the reference type. Cv-qualified
|
| 510 |
references are ill-formed except when the cv-qualifiers are introduced
|
| 511 |
through the use of a *typedef-name* ([[dcl.typedef]], [[temp.param]])
|
| 512 |
+
or *decltype-specifier* [[dcl.type.simple]], in which case the
|
| 513 |
cv-qualifiers are ignored.
|
| 514 |
|
| 515 |
[*Example 1*:
|
| 516 |
|
| 517 |
``` cpp
|
| 518 |
typedef int& A;
|
| 519 |
+
const A aref = 3; // error: lvalue reference to non-const initialized with rvalue
|
| 520 |
```
|
| 521 |
|
| 522 |
The type of `aref` is “lvalue reference to `int`”, not “lvalue reference
|
| 523 |
to `const int`”.
|
| 524 |
|
|
|
|
| 582 |
declares `p` to be a reference to a pointer to `link` so `h(q)` will
|
| 583 |
leave `q` with the value zero. See also [[dcl.init.ref]].
|
| 584 |
|
| 585 |
— *end example*]
|
| 586 |
|
| 587 |
+
It is unspecified whether or not a reference requires storage
|
| 588 |
+
[[basic.stc]].
|
| 589 |
|
| 590 |
There shall be no references to references, no arrays of references, and
|
| 591 |
no pointers to references. The declaration of a reference shall contain
|
| 592 |
+
an *initializer* [[dcl.init.ref]] except when the declaration contains
|
| 593 |
+
an explicit `extern` specifier [[dcl.stc]], is a class member
|
| 594 |
+
[[class.mem]] declaration within a class definition, or is the
|
| 595 |
+
declaration of a parameter or a return type [[dcl.fct]]; see
|
| 596 |
[[basic.def]]. A reference shall be initialized to refer to a valid
|
| 597 |
object or function.
|
| 598 |
|
| 599 |
[*Note 2*: In particular, a null reference cannot exist in a
|
| 600 |
well-defined program, because the only way to create such a reference
|
|
|
|
| 602 |
null pointer, which causes undefined behavior. As described in
|
| 603 |
[[class.bit]], a reference cannot be bound directly to a
|
| 604 |
bit-field. — *end note*]
|
| 605 |
|
| 606 |
If a *typedef-name* ([[dcl.typedef]], [[temp.param]]) or a
|
| 607 |
+
*decltype-specifier* [[dcl.type.simple]] denotes a type `TR` that is a
|
| 608 |
+
reference to a type `T`, an attempt to create the type “lvalue reference
|
| 609 |
+
to cv `TR`” creates the type “lvalue reference to `T`”, while an attempt
|
| 610 |
+
to create the type “rvalue reference to cv `TR`” creates the type `TR`.
|
|
|
|
| 611 |
|
| 612 |
[*Note 3*: This rule is known as reference collapsing. — *end note*]
|
| 613 |
|
| 614 |
[*Example 3*:
|
| 615 |
|
|
|
|
| 633 |
|
| 634 |
[*Note 4*: Forming a reference to function type is ill-formed if the
|
| 635 |
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 636 |
[[dcl.fct]]. — *end note*]
|
| 637 |
|
| 638 |
+
#### Pointers to members <a id="dcl.mptr">[[dcl.mptr]]</a>
|
| 639 |
|
| 640 |
In a declaration `T` `D` where `D` has the form
|
| 641 |
|
| 642 |
``` bnf
|
| 643 |
nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
|
|
|
|
| 646 |
and the *nested-name-specifier* denotes a class, and the type of the
|
| 647 |
identifier in the declaration `T` `D1` is
|
| 648 |
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 649 |
`D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
|
| 650 |
member of class *nested-name-specifier* of type `T`”. The optional
|
| 651 |
+
*attribute-specifier-seq* [[dcl.attr.grammar]] appertains to the
|
| 652 |
pointer-to-member.
|
| 653 |
|
| 654 |
[*Example 1*:
|
| 655 |
|
| 656 |
``` cpp
|
|
|
|
| 681 |
(obj.*pmf)(7); // call a function member of obj with the argument 7
|
| 682 |
```
|
| 683 |
|
| 684 |
— *end example*]
|
| 685 |
|
| 686 |
+
A pointer to member shall not point to a static member of a class
|
| 687 |
+
[[class.static]], a member with reference type, or “cv `void`”.
|
| 688 |
|
| 689 |
[*Note 1*: See also [[expr.unary]] and [[expr.mptr.oper]]. The type
|
| 690 |
“pointer to member” is distinct from the type “pointer”, that is, a
|
| 691 |
+
pointer to member is declared only by the pointer-to-member declarator
|
| 692 |
syntax, and never by the pointer declarator syntax. There is no
|
| 693 |
“reference-to-member” type in C++. — *end note*]
|
| 694 |
|
| 695 |
+
#### Arrays <a id="dcl.array">[[dcl.array]]</a>
|
| 696 |
|
| 697 |
In a declaration `T` `D` where `D` has the form
|
| 698 |
|
| 699 |
``` bnf
|
| 700 |
+
'D1' '[' constant-expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
|
| 701 |
```
|
| 702 |
|
| 703 |
+
and the type of the contained *declarator-id* in the declaration `T`
|
| 704 |
+
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 705 |
+
*declarator-id* in `D` is “*derived-declarator-type-list* array of `N`
|
| 706 |
+
`T`”. The *constant-expression* shall be a converted constant expression
|
| 707 |
+
of type `std::size_t` [[expr.const]]. Its value `N` specifies the *array
|
| 708 |
+
bound*, i.e., the number of elements in the array; `N` shall be greater
|
| 709 |
+
than zero.
|
| 710 |
+
|
| 711 |
+
In a declaration `T` `D` where `D` has the form
|
| 712 |
+
|
| 713 |
+
``` bnf
|
| 714 |
+
'D1 [ ]' attribute-specifier-seqₒₚₜ
|
| 715 |
+
```
|
| 716 |
+
|
| 717 |
+
and the type of the contained *declarator-id* in the declaration `T`
|
| 718 |
+
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 719 |
+
*declarator-id* in `D` is “*derived-declarator-type-list* array of
|
| 720 |
+
unknown bound of `T`”, except as specified below.
|
| 721 |
+
|
| 722 |
+
A type of the form “array of `N` `U`” or “array of unknown bound of `U`”
|
| 723 |
+
is an *array type*. The optional *attribute-specifier-seq* appertains to
|
| 724 |
+
the array type.
|
| 725 |
+
|
| 726 |
+
`U` is called the array *element type*; this type shall not be a
|
| 727 |
+
placeholder type [[dcl.spec.auto]], a reference type, a function type,
|
| 728 |
+
an array of unknown bound, or cv `void`.
|
| 729 |
+
|
| 730 |
+
[*Note 1*: An array can be constructed from one of the fundamental
|
| 731 |
+
types (except `void`), from a pointer, from a pointer to member, from a
|
| 732 |
+
class, from an enumeration type, or from an array of known
|
| 733 |
+
bound. — *end note*]
|
| 734 |
|
| 735 |
[*Example 1*:
|
| 736 |
|
| 737 |
+
``` cpp
|
| 738 |
+
float fa[17], *afp[17];
|
| 739 |
+
```
|
| 740 |
+
|
| 741 |
+
declares an array of `float` numbers and an array of pointers to `float`
|
| 742 |
+
numbers.
|
| 743 |
+
|
| 744 |
+
— *end example*]
|
| 745 |
+
|
| 746 |
+
Any type of the form “*cv-qualifier-seq* array of `N` `U`” is adjusted
|
| 747 |
+
to “array of `N` *cv-qualifier-seq* `U`”, and similarly for “array of
|
| 748 |
+
unknown bound of `U`”.
|
| 749 |
+
|
| 750 |
+
[*Example 2*:
|
| 751 |
+
|
| 752 |
``` cpp
|
| 753 |
typedef int A[5], AA[2][3];
|
| 754 |
typedef const A CA; // type is ``array of 5 const int''
|
| 755 |
typedef const AA CAA; // type is ``array of 2 array of 3 const int''
|
| 756 |
```
|
| 757 |
|
| 758 |
— *end example*]
|
| 759 |
|
| 760 |
+
[*Note 2*: An “array of `N` *cv-qualifier-seq* `U`” has cv-qualified
|
| 761 |
type; see [[basic.type.qualifier]]. — *end note*]
|
| 762 |
|
| 763 |
+
An object of type “array of `N` `U`” contains a contiguously allocated
|
| 764 |
+
non-empty set of `N` subobjects of type `U`, known as the *elements* of
|
| 765 |
+
the array, and numbered `0` to `N-1`.
|
| 766 |
|
| 767 |
+
In addition to declarations in which an incomplete object type is
|
| 768 |
+
allowed, an array bound may be omitted in some cases in the declaration
|
| 769 |
+
of a function parameter [[dcl.fct]]. An array bound may also be omitted
|
| 770 |
+
when an object (but not a non-static data member) of array type is
|
| 771 |
+
initialized and the declarator is followed by an initializer (
|
| 772 |
+
[[dcl.init]], [[class.mem]], [[expr.type.conv]], [[expr.new]]). In these
|
| 773 |
+
cases, the array bound is calculated from the number of initial elements
|
| 774 |
+
(say, `N`) supplied [[dcl.init.aggr]], and the type of the array is
|
| 775 |
+
“array of `N` `U`”.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 776 |
|
| 777 |
+
Furthermore, if there is a preceding declaration of the entity in the
|
| 778 |
+
same scope in which the bound was specified, an omitted array bound is
|
| 779 |
+
taken to be the same as in that earlier declaration, and similarly for
|
| 780 |
+
the definition of a static data member of a class.
|
| 781 |
|
| 782 |
+
[*Example 3*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 783 |
|
| 784 |
``` cpp
|
| 785 |
extern int x[10];
|
| 786 |
struct S {
|
| 787 |
static int y[10];
|
|
|
|
| 796 |
}
|
| 797 |
```
|
| 798 |
|
| 799 |
— *end example*]
|
| 800 |
|
| 801 |
+
[*Note 3*:
|
|
|
|
|
|
|
| 802 |
|
| 803 |
+
When several “array of” specifications are adjacent, a multidimensional
|
| 804 |
+
array type is created; only the first of the constant expressions that
|
| 805 |
+
specify the bounds of the arrays may be omitted.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 806 |
|
| 807 |
+
[*Example 4*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 808 |
|
| 809 |
``` cpp
|
| 810 |
+
int x3d[3][5][7];
|
| 811 |
```
|
| 812 |
|
| 813 |
+
declares an array of three elements, each of which is an array of five
|
| 814 |
+
elements, each of which is an array of seven integers. The overall array
|
| 815 |
+
can be viewed as a three-dimensional array of integers, with rank
|
| 816 |
+
3 × 5 × 7. Any of the expressions `x3d`, `x3d[i]`, `x3d[i][j]`,
|
| 817 |
+
`x3d[i][j][k]` can reasonably appear in an expression. The expression
|
| 818 |
+
`x3d[i]` is equivalent to `*(x3d + i)`; in that expression, `x3d` is
|
| 819 |
+
subject to the array-to-pointer conversion [[conv.array]] and is first
|
| 820 |
+
converted to a pointer to a 2-dimensional array with rank 5 × 7 that
|
| 821 |
+
points to the first element of `x3d`. Then `i` is added, which on
|
| 822 |
+
typical implementations involves multiplying `i` by the length of the
|
| 823 |
+
object to which the pointer points, which is `sizeof(int)`× 5 × 7. The
|
| 824 |
+
result of the addition and indirection is an lvalue denoting the `i`ᵗʰ
|
| 825 |
+
array element of `x3d` (an array of five arrays of seven integers). If
|
| 826 |
+
there is another subscript, the same argument applies again, so
|
| 827 |
+
`x3d[i][j]` is an lvalue denoting the `j`ᵗʰ array element of the `i`ᵗʰ
|
| 828 |
+
array element of `x3d` (an array of seven integers), and `x3d[i][j][k]`
|
| 829 |
+
is an lvalue denoting the `k`ᵗʰ array element of the `j`ᵗʰ array element
|
| 830 |
+
of the `i`ᵗʰ array element of `x3d` (an integer).
|
| 831 |
|
| 832 |
— *end example*]
|
| 833 |
|
| 834 |
+
The first subscript in the declaration helps determine the amount of
|
| 835 |
+
storage consumed by an array but plays no other part in subscript
|
| 836 |
+
calculations.
|
| 837 |
+
|
| 838 |
— *end note*]
|
| 839 |
|
| 840 |
+
[*Note 4*: Conversions affecting expressions of array type are
|
| 841 |
+
described in [[conv.array]]. — *end note*]
|
|
|
|
|
|
|
| 842 |
|
| 843 |
+
[*Note 5*: The subscript operator can be overloaded for a class
|
| 844 |
+
[[over.sub]]. For the operator’s built-in meaning, see
|
| 845 |
+
[[expr.sub]]. — *end note*]
|
| 846 |
+
|
| 847 |
+
#### Functions <a id="dcl.fct">[[dcl.fct]]</a>
|
| 848 |
|
| 849 |
In a declaration `T` `D` where `D` has the form
|
| 850 |
|
| 851 |
``` bnf
|
| 852 |
+
'D1' '(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 853 |
ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 854 |
```
|
| 855 |
|
| 856 |
and the type of the contained *declarator-id* in the declaration `T`
|
| 857 |
`D1` is “*derived-declarator-type-list* `T`”, the type of the
|
| 858 |
+
*declarator-id* in `D` is “*derived-declarator-type-list* `noexcept`ₒₚₜ
|
| 859 |
+
function of parameter-type-list *cv-qualifier-seq*ₒₚₜ
|
| 860 |
+
*ref-qualifier*ₒₚₜ returning `T`”, where
|
| 861 |
+
|
| 862 |
+
- the parameter-type-list is derived from the
|
| 863 |
+
*parameter-declaration-clause* as described below and
|
| 864 |
+
- the optional `noexcept` is present if and only if the exception
|
| 865 |
+
specification [[except.spec]] is non-throwing.
|
| 866 |
+
|
| 867 |
+
The optional *attribute-specifier-seq* appertains to the function type.
|
| 868 |
|
| 869 |
In a declaration `T` `D` where `D` has the form
|
| 870 |
|
| 871 |
``` bnf
|
| 872 |
+
'D1' '(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 873 |
ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-type
|
| 874 |
```
|
| 875 |
|
| 876 |
and the type of the contained *declarator-id* in the declaration `T`
|
| 877 |
`D1` is “*derived-declarator-type-list* `T`”, `T` shall be the single
|
| 878 |
*type-specifier* `auto`. The type of the *declarator-id* in `D` is
|
| 879 |
+
“*derived-declarator-type-list* `noexcept`ₒₚₜ function of
|
| 880 |
+
parameter-type-list *cv-qualifier-seq*ₒₚₜ *ref-qualifier*ₒₚₜ returning
|
| 881 |
+
`U`”, where
|
|
|
|
|
|
|
|
|
|
| 882 |
|
| 883 |
+
- the parameter-type-list is derived from the
|
| 884 |
+
*parameter-declaration-clause* as described below,
|
| 885 |
+
- `U` is the type specified by the *trailing-return-type*, and
|
| 886 |
+
- the optional `noexcept` is present if and only if the exception
|
| 887 |
+
specification is non-throwing.
|
| 888 |
+
|
| 889 |
+
The optional *attribute-specifier-seq* appertains to the function type.
|
| 890 |
+
|
| 891 |
+
A type of either form is a *function type*.[^2]
|
| 892 |
|
| 893 |
``` bnf
|
| 894 |
parameter-declaration-clause:
|
| 895 |
parameter-declaration-listₒₚₜ '...'ₒₚₜ
|
| 896 |
+
parameter-declaration-list ',' '...'
|
| 897 |
```
|
| 898 |
|
| 899 |
``` bnf
|
| 900 |
parameter-declaration-list:
|
| 901 |
parameter-declaration
|
|
|
|
| 921 |
[[expr.call]]. — *end note*]
|
| 922 |
|
| 923 |
If the *parameter-declaration-clause* is empty, the function takes no
|
| 924 |
arguments. A parameter list consisting of a single unnamed parameter of
|
| 925 |
non-dependent type `void` is equivalent to an empty parameter list.
|
| 926 |
+
Except for this special case, a parameter shall not have type cv `void`.
|
| 927 |
+
A parameter with volatile-qualified type is deprecated; see
|
| 928 |
+
[[depr.volatile.type]]. If the *parameter-declaration-clause* terminates
|
| 929 |
+
with an ellipsis or a function parameter pack [[temp.variadic]], the
|
| 930 |
+
number of arguments shall be equal to or greater than the number of
|
| 931 |
+
parameters that do not have a default argument and are not function
|
| 932 |
+
parameter packs. Where syntactically correct and where “`...`” is not
|
| 933 |
+
part of an *abstract-declarator*, “`, ...`” is synonymous with “`...`”.
|
| 934 |
|
| 935 |
[*Example 1*:
|
| 936 |
|
| 937 |
The declaration
|
| 938 |
|
|
|
|
| 947 |
printf("hello world");
|
| 948 |
printf("a=%d b=%d", a, b);
|
| 949 |
```
|
| 950 |
|
| 951 |
However, the first argument must be of a type that can be converted to a
|
| 952 |
+
`const` `char*`.
|
| 953 |
|
| 954 |
— *end example*]
|
| 955 |
|
| 956 |
[*Note 2*: The standard header `<cstdarg>` contains a mechanism for
|
| 957 |
accessing arguments passed using the ellipsis (see [[expr.call]] and
|
| 958 |
[[support.runtime]]). — *end note*]
|
| 959 |
|
| 960 |
+
The type of a function is determined using the following rules. The type
|
| 961 |
+
of each parameter (including function parameter packs) is determined
|
| 962 |
+
from its own *decl-specifier-seq* and *declarator*. After determining
|
| 963 |
+
the type of each parameter, any parameter of type “array of `T`” or of
|
| 964 |
+
function type `T` is adjusted to be “pointer to `T`”. After producing
|
| 965 |
+
the list of parameter types, any top-level *cv-qualifier*s modifying a
|
| 966 |
+
parameter type are deleted when forming the function type. The resulting
|
| 967 |
+
list of transformed parameter types and the presence or absence of the
|
| 968 |
+
ellipsis or a function parameter pack is the function’s
|
| 969 |
+
*parameter-type-list*.
|
|
|
|
|
|
|
|
|
|
| 970 |
|
| 971 |
[*Note 3*: This transformation does not affect the types of the
|
| 972 |
parameters. For example, `int(*)(const int p, decltype(p)*)` and
|
| 973 |
`int(*)(int, const int*)` are identical types. — *end note*]
|
| 974 |
|
|
|
|
| 978 |
|
| 979 |
- the function type for a non-static member function,
|
| 980 |
- the function type to which a pointer to member refers,
|
| 981 |
- the top-level function type of a function typedef declaration or
|
| 982 |
*alias-declaration*,
|
| 983 |
+
- the *type-id* in the default argument of a *type-parameter*
|
| 984 |
+
[[temp.param]], or
|
| 985 |
+
- the *type-id* of a *template-argument* for a *type-parameter*
|
| 986 |
+
[[temp.arg.type]].
|
| 987 |
|
| 988 |
[*Example 2*:
|
| 989 |
|
| 990 |
``` cpp
|
| 991 |
typedef int FIC(int) const;
|
| 992 |
+
FIC f; // error: does not declare a member function
|
| 993 |
struct S {
|
| 994 |
FIC f; // OK
|
| 995 |
};
|
| 996 |
FIC S::*pm = &S::f; // OK
|
| 997 |
```
|
|
|
|
| 1017 |
|
| 1018 |
— *end example*]
|
| 1019 |
|
| 1020 |
The return type, the parameter-type-list, the *ref-qualifier*, the
|
| 1021 |
*cv-qualifier-seq*, and the exception specification, but not the default
|
| 1022 |
+
arguments [[dcl.fct.default]] or the trailing *requires-clause*
|
| 1023 |
+
[[dcl.decl]], are part of the function type.
|
| 1024 |
|
| 1025 |
[*Note 5*: Function types are checked during the assignments and
|
| 1026 |
initializations of pointers to functions, references to functions, and
|
| 1027 |
pointers to member functions. — *end note*]
|
| 1028 |
|
|
|
|
| 1033 |
``` cpp
|
| 1034 |
int fseek(FILE*, long, int);
|
| 1035 |
```
|
| 1036 |
|
| 1037 |
declares a function taking three arguments of the specified types, and
|
| 1038 |
+
returning `int` [[dcl.type]].
|
| 1039 |
|
| 1040 |
— *end example*]
|
| 1041 |
|
| 1042 |
+
A single name can be used for several different functions in a single
|
| 1043 |
+
scope; this is function overloading [[over]]. All declarations for a
|
| 1044 |
+
function shall have equivalent return types, parameter-type-lists, and
|
| 1045 |
+
*requires-clause*s [[temp.over.link]].
|
| 1046 |
+
|
| 1047 |
Functions shall not have a return type of type array or function,
|
| 1048 |
although they may have a return type of type pointer or reference to
|
| 1049 |
such things. There shall be no arrays of functions, although there can
|
| 1050 |
be arrays of pointers to functions.
|
| 1051 |
|
| 1052 |
+
A volatile-qualified return type is deprecated; see
|
| 1053 |
+
[[depr.volatile.type]].
|
| 1054 |
+
|
| 1055 |
+
Types shall not be defined in return or parameter types.
|
|
|
|
| 1056 |
|
| 1057 |
A typedef of function type may be used to declare a function but shall
|
| 1058 |
+
not be used to define a function [[dcl.fct.def]].
|
| 1059 |
|
| 1060 |
[*Example 5*:
|
| 1061 |
|
| 1062 |
``` cpp
|
| 1063 |
typedef void F();
|
| 1064 |
F fv; // OK: equivalent to void fv();
|
| 1065 |
+
F fv { } // error
|
| 1066 |
void fv() { } // OK: definition of fv
|
| 1067 |
```
|
| 1068 |
|
| 1069 |
— *end example*]
|
| 1070 |
|
| 1071 |
An identifier can optionally be provided as a parameter name; if present
|
| 1072 |
+
in a function definition [[dcl.fct.def]], it names a parameter.
|
| 1073 |
|
| 1074 |
[*Note 6*: In particular, parameter names are also optional in function
|
| 1075 |
definitions and names used for a parameter in different declarations and
|
| 1076 |
the definition of a function need not be the same. If a parameter name
|
| 1077 |
is present in a function declaration that is not a definition, it cannot
|
| 1078 |
be used outside of its function declarator because that is the extent of
|
| 1079 |
+
its potential scope [[basic.scope.param]]. — *end note*]
|
| 1080 |
|
| 1081 |
[*Example 6*:
|
| 1082 |
|
| 1083 |
The declaration
|
| 1084 |
|
|
|
|
| 1143 |
A *non-template function* is a function that is not a function template
|
| 1144 |
specialization.
|
| 1145 |
|
| 1146 |
[*Note 8*: A function template is not a function. — *end note*]
|
| 1147 |
|
| 1148 |
+
An *abbreviated function template* is a function declaration that has
|
| 1149 |
+
one or more generic parameter type placeholders [[dcl.spec.auto]]. An
|
| 1150 |
+
abbreviated function template is equivalent to a function template
|
| 1151 |
+
[[temp.fct]] whose *template-parameter-list* includes one invented type
|
| 1152 |
+
*template-parameter* for each generic parameter type placeholder of the
|
| 1153 |
+
function declaration, in order of appearance. For a
|
| 1154 |
+
*placeholder-type-specifier* of the form `auto`, the invented parameter
|
| 1155 |
+
is an unconstrained *type-parameter*. For a *placeholder-type-specifier*
|
| 1156 |
+
of the form *type-constraint* `auto`, the invented parameter is a
|
| 1157 |
+
*type-parameter* with that *type-constraint*. The invented type
|
| 1158 |
+
*template-parameter* is a template parameter pack if the corresponding
|
| 1159 |
+
*parameter-declaration* declares a function parameter pack [[dcl.fct]].
|
| 1160 |
+
If the placeholder contains `decltype(auto)`, the program is ill-formed.
|
| 1161 |
+
The adjusted function parameters of an abbreviated function template are
|
| 1162 |
+
derived from the *parameter-declaration-clause* by replacing each
|
| 1163 |
+
occurrence of a placeholder with the name of the corresponding invented
|
| 1164 |
+
*template-parameter*.
|
| 1165 |
+
|
| 1166 |
+
[*Example 7*:
|
| 1167 |
+
|
| 1168 |
+
``` cpp
|
| 1169 |
+
template<typename T> concept C1 = /* ... */;
|
| 1170 |
+
template<typename T> concept C2 = /* ... */;
|
| 1171 |
+
template<typename... Ts> concept C3 = /* ... */;
|
| 1172 |
+
|
| 1173 |
+
void g1(const C1 auto*, C2 auto&);
|
| 1174 |
+
void g2(C1 auto&...);
|
| 1175 |
+
void g3(C3 auto...);
|
| 1176 |
+
void g4(C3 auto);
|
| 1177 |
+
```
|
| 1178 |
+
|
| 1179 |
+
These declarations are functionally equivalent (but not equivalent) to
|
| 1180 |
+
the following declarations.
|
| 1181 |
+
|
| 1182 |
+
``` cpp
|
| 1183 |
+
template<C1 T, C2 U> void g1(const T*, U&);
|
| 1184 |
+
template<C1... Ts> void g2(Ts&...);
|
| 1185 |
+
template<C3... Ts> void g3(Ts...);
|
| 1186 |
+
template<C3 T> void g4(T);
|
| 1187 |
+
```
|
| 1188 |
+
|
| 1189 |
+
Abbreviated function templates can be specialized like all function
|
| 1190 |
+
templates.
|
| 1191 |
+
|
| 1192 |
+
``` cpp
|
| 1193 |
+
template<> void g1<int>(const int*, const double&); // OK, specialization of g1<int, const double>
|
| 1194 |
+
```
|
| 1195 |
+
|
| 1196 |
+
— *end example*]
|
| 1197 |
+
|
| 1198 |
+
An abbreviated function template can have a *template-head*. The
|
| 1199 |
+
invented *template-parameters* are appended to the
|
| 1200 |
+
*template-parameter-list* after the explicitly declared
|
| 1201 |
+
*template-parameters*.
|
| 1202 |
+
|
| 1203 |
+
[*Example 8*:
|
| 1204 |
+
|
| 1205 |
+
``` cpp
|
| 1206 |
+
template<typename> concept C = /* ... */;
|
| 1207 |
+
|
| 1208 |
+
template <typename T, C U>
|
| 1209 |
+
void g(T x, U y, C auto z);
|
| 1210 |
+
```
|
| 1211 |
+
|
| 1212 |
+
This is functionally equivalent to each of the following two
|
| 1213 |
+
declarations.
|
| 1214 |
+
|
| 1215 |
+
``` cpp
|
| 1216 |
+
template<typename T, C U, C W>
|
| 1217 |
+
void g(T x, U y, W z);
|
| 1218 |
+
|
| 1219 |
+
template<typename T, typename U, typename W>
|
| 1220 |
+
requires C<U> && C<W>
|
| 1221 |
+
void g(T x, U y, W z);
|
| 1222 |
+
```
|
| 1223 |
+
|
| 1224 |
+
— *end example*]
|
| 1225 |
+
|
| 1226 |
+
A function declaration at block scope shall not declare an abbreviated
|
| 1227 |
+
function template.
|
| 1228 |
+
|
| 1229 |
A *declarator-id* or *abstract-declarator* containing an ellipsis shall
|
| 1230 |
+
only be used in a *parameter-declaration*. When it is part of a
|
| 1231 |
+
*parameter-declaration-clause*, the *parameter-declaration* declares a
|
| 1232 |
+
function parameter pack [[temp.variadic]]. Otherwise, the
|
| 1233 |
+
*parameter-declaration* is part of a *template-parameter-list* and
|
| 1234 |
+
declares a template parameter pack; see [[temp.param]]. A function
|
| 1235 |
+
parameter pack is a pack expansion [[temp.variadic]].
|
| 1236 |
|
| 1237 |
+
[*Example 9*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1238 |
|
| 1239 |
``` cpp
|
| 1240 |
template<typename... T> void f(T (* ...t)(int, int));
|
| 1241 |
|
| 1242 |
int add(int, int);
|
|
|
|
| 1252 |
There is a syntactic ambiguity when an ellipsis occurs at the end of a
|
| 1253 |
*parameter-declaration-clause* without a preceding comma. In this case,
|
| 1254 |
the ellipsis is parsed as part of the *abstract-declarator* if the type
|
| 1255 |
of the parameter either names a template parameter pack that has not
|
| 1256 |
been expanded or contains `auto`; otherwise, it is parsed as part of the
|
| 1257 |
+
*parameter-declaration-clause*.[^3]
|
| 1258 |
|
| 1259 |
+
#### Default arguments <a id="dcl.fct.default">[[dcl.fct.default]]</a>
|
| 1260 |
|
| 1261 |
If an *initializer-clause* is specified in a *parameter-declaration*
|
| 1262 |
+
this *initializer-clause* is used as a default argument.
|
| 1263 |
+
|
| 1264 |
+
[*Note 1*: Default arguments will be used in calls where trailing
|
| 1265 |
+
arguments are missing [[expr.call]]. — *end note*]
|
| 1266 |
|
| 1267 |
[*Example 1*:
|
| 1268 |
|
| 1269 |
The declaration
|
| 1270 |
|
|
|
|
| 1284 |
|
| 1285 |
— *end example*]
|
| 1286 |
|
| 1287 |
A default argument shall be specified only in the
|
| 1288 |
*parameter-declaration-clause* of a function declaration or
|
| 1289 |
+
*lambda-declarator* or in a *template-parameter* [[temp.param]]; in the
|
| 1290 |
+
latter case, the *initializer-clause* shall be an
|
| 1291 |
*assignment-expression*. A default argument shall not be specified for a
|
| 1292 |
+
template parameter pack or a function parameter pack. If it is specified
|
| 1293 |
+
in a *parameter-declaration-clause*, it shall not occur within a
|
| 1294 |
+
*declarator* or *abstract-declarator* of a *parameter-declaration*.[^4]
|
| 1295 |
|
| 1296 |
For non-template functions, default arguments can be added in later
|
| 1297 |
declarations of a function in the same scope. Declarations in different
|
| 1298 |
scopes have completely distinct sets of default arguments. That is,
|
| 1299 |
declarations in inner scopes do not acquire default arguments from
|
| 1300 |
declarations in outer scopes, and vice versa. In a given function
|
| 1301 |
declaration, each parameter subsequent to a parameter with a default
|
| 1302 |
argument shall have a default argument supplied in this or a previous
|
| 1303 |
+
declaration, unless the parameter was expanded from a parameter pack, or
|
| 1304 |
+
shall be a function parameter pack.
|
| 1305 |
+
|
| 1306 |
+
[*Note 2*: A default argument cannot be redefined by a later
|
| 1307 |
+
declaration (not even to the same value)
|
| 1308 |
+
[[basic.def.odr]]. — *end note*]
|
| 1309 |
|
| 1310 |
[*Example 2*:
|
| 1311 |
|
| 1312 |
``` cpp
|
| 1313 |
void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow
|
|
|
|
| 1326 |
void f(int, int = 5); // error: cannot redefine, even to same value
|
| 1327 |
}
|
| 1328 |
void n() {
|
| 1329 |
f(6); // OK, calls f(6, 7)
|
| 1330 |
}
|
| 1331 |
+
template<class ... T> struct C {
|
| 1332 |
+
void f(int n = 0, T...);
|
| 1333 |
+
};
|
| 1334 |
+
C<int> c; // OK, instantiates declaration void C::f(int n = 0, int)
|
| 1335 |
```
|
| 1336 |
|
| 1337 |
— *end example*]
|
| 1338 |
|
| 1339 |
For a given inline function defined in different translation units, the
|
| 1340 |
accumulated sets of default arguments at the end of the translation
|
| 1341 |
+
units shall be the same; no diagnostic is required. If a friend
|
| 1342 |
+
declaration specifies a default argument expression, that declaration
|
| 1343 |
+
shall be a definition and shall be the only declaration of the function
|
| 1344 |
+
or function template in the translation unit.
|
| 1345 |
|
| 1346 |
The default argument has the same semantic constraints as the
|
| 1347 |
initializer in a declaration of a variable of the parameter type, using
|
| 1348 |
+
the copy-initialization semantics [[dcl.init]]. The names in the default
|
| 1349 |
+
argument are bound, and the semantic constraints are checked, at the
|
| 1350 |
+
point where the default argument appears. Name lookup and checking of
|
| 1351 |
+
semantic constraints for default arguments in function templates and in
|
| 1352 |
+
member functions of class templates are performed as described in
|
| 1353 |
[[temp.inst]].
|
| 1354 |
|
| 1355 |
[*Example 3*:
|
| 1356 |
|
| 1357 |
In the following code, `g` will be called with the value `f(2)`:
|
|
|
|
| 1370 |
}
|
| 1371 |
```
|
| 1372 |
|
| 1373 |
— *end example*]
|
| 1374 |
|
| 1375 |
+
[*Note 3*: In member function declarations, names in default arguments
|
| 1376 |
are looked up as described in [[basic.lookup.unqual]]. Access checking
|
| 1377 |
+
applies to names in default arguments as described in
|
| 1378 |
[[class.access]]. — *end note*]
|
| 1379 |
|
| 1380 |
Except for member functions of class templates, the default arguments in
|
| 1381 |
a member function definition that appears outside of the class
|
| 1382 |
definition are added to the set of default arguments provided by the
|
| 1383 |
member function declaration in the class definition; the program is
|
| 1384 |
+
ill-formed if a default constructor [[class.default.ctor]], copy or move
|
| 1385 |
+
constructor [[class.copy.ctor]], or copy or move assignment operator
|
| 1386 |
+
[[class.copy.assign]] is so declared. Default arguments for a member
|
| 1387 |
+
function of a class template shall be specified on the initial
|
| 1388 |
+
declaration of the member function within the class template.
|
| 1389 |
|
| 1390 |
[*Example 4*:
|
| 1391 |
|
| 1392 |
``` cpp
|
| 1393 |
class C {
|
|
|
|
| 1399 |
void C::g(int i = 88, int j) {} // in this translation unit, C::g can be called with no argument
|
| 1400 |
```
|
| 1401 |
|
| 1402 |
— *end example*]
|
| 1403 |
|
| 1404 |
+
[*Note 4*: A local variable cannot be odr-used [[basic.def.odr]] in a
|
| 1405 |
+
default argument. — *end note*]
|
| 1406 |
|
| 1407 |
[*Example 5*:
|
| 1408 |
|
| 1409 |
``` cpp
|
| 1410 |
void f() {
|
|
|
|
| 1415 |
}
|
| 1416 |
```
|
| 1417 |
|
| 1418 |
— *end example*]
|
| 1419 |
|
| 1420 |
+
[*Note 5*:
|
| 1421 |
|
| 1422 |
The keyword `this` may not appear in a default argument of a member
|
| 1423 |
function; see [[expr.prim.this]].
|
| 1424 |
|
| 1425 |
[*Example 6*:
|
|
|
|
| 1451 |
```
|
| 1452 |
|
| 1453 |
— *end example*]
|
| 1454 |
|
| 1455 |
A non-static member shall not appear in a default argument unless it
|
| 1456 |
+
appears as the *id-expression* of a class member access expression
|
| 1457 |
+
[[expr.ref]] or unless it is used to form a pointer to member
|
| 1458 |
+
[[expr.unary.op]].
|
| 1459 |
|
| 1460 |
[*Example 8*:
|
| 1461 |
|
| 1462 |
The declaration of `X::mem1()` in the following example is ill-formed
|
| 1463 |
because no object is supplied for the non-static member `X::a` used as
|
|
|
|
| 1473 |
};
|
| 1474 |
```
|
| 1475 |
|
| 1476 |
The declaration of `X::mem2()` is meaningful, however, since no object
|
| 1477 |
is needed to access the static member `X::b`. Classes, objects, and
|
| 1478 |
+
members are described in [[class]].
|
| 1479 |
|
| 1480 |
— *end example*]
|
| 1481 |
|
| 1482 |
A default argument is not part of the type of a function.
|
| 1483 |
|
|
|
|
| 1496 |
```
|
| 1497 |
|
| 1498 |
— *end example*]
|
| 1499 |
|
| 1500 |
When a declaration of a function is introduced by way of a
|
| 1501 |
+
*using-declaration* [[namespace.udecl]], any default argument
|
| 1502 |
information associated with the declaration is made known as well. If
|
| 1503 |
the function is redeclared thereafter in the namespace with additional
|
| 1504 |
default arguments, the additional arguments are also known at any point
|
| 1505 |
following the redeclaration where the *using-declaration* is in scope.
|
| 1506 |
|
| 1507 |
+
A virtual function call [[class.virtual]] uses the default arguments in
|
| 1508 |
+
the declaration of the virtual function determined by the static type of
|
| 1509 |
+
the pointer or reference denoting the object. An overriding function in
|
| 1510 |
+
a derived class does not acquire default arguments from the function it
|
| 1511 |
+
overrides.
|
| 1512 |
|
| 1513 |
[*Example 10*:
|
| 1514 |
|
| 1515 |
``` cpp
|
| 1516 |
struct A {
|
|
|
|
| 1527 |
}
|
| 1528 |
```
|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