- tmp/tmpt774dk64/{from.md → to.md} +214 -131
tmp/tmpt774dk64/{from.md → to.md}
RENAMED
|
@@ -54,15 +54,18 @@ int c(b);
|
|
| 54 |
Default arguments are more restricted; see [[dcl.fct.default]].
|
| 55 |
|
| 56 |
The order of initialization of variables with static storage duration is
|
| 57 |
described in [[basic.start]] and [[stmt.dcl]].
|
| 58 |
|
|
|
|
|
|
|
|
|
|
| 59 |
To *zero-initialize* an object or reference of type `T` means:
|
| 60 |
|
| 61 |
-
- if `T` is a scalar type ([[basic.types]]), the object is
|
| 62 |
-
value `0` (zero)
|
| 63 |
-
|
| 64 |
- if `T` is a (possibly cv-qualified) non-union class type, each
|
| 65 |
non-static data member and each base-class subobject is
|
| 66 |
zero-initialized and padding is initialized to zero bits;
|
| 67 |
- if `T` is a (possibly cv-qualified) union type, the object’s first
|
| 68 |
non-static named data member is zero-initialized and padding is
|
|
@@ -71,29 +74,33 @@ To *zero-initialize* an object or reference of type `T` means:
|
|
| 71 |
- if `T` is a reference type, no initialization is performed.
|
| 72 |
|
| 73 |
To *default-initialize* an object of type `T` means:
|
| 74 |
|
| 75 |
- if `T` is a (possibly cv-qualified) class type (Clause [[class]]),
|
| 76 |
-
the default constructor for `T` is called (and the
|
| 77 |
-
ill-formed if `T` has no
|
|
|
|
|
|
|
|
|
|
| 78 |
- if `T` is an array type, each element is default-initialized;
|
| 79 |
- otherwise, no initialization is performed.
|
| 80 |
|
| 81 |
If a program calls for the default initialization of an object of a
|
| 82 |
const-qualified type `T`, `T` shall be a class type with a user-provided
|
| 83 |
default constructor.
|
| 84 |
|
| 85 |
To *value-initialize* an object of type `T` means:
|
| 86 |
|
| 87 |
- if `T` is a (possibly cv-qualified) class type (Clause [[class]])
|
| 88 |
-
with
|
| 89 |
-
constructor
|
| 90 |
-
|
| 91 |
-
- if `T` is a (possibly cv-qualified)
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
constructor
|
|
|
|
| 95 |
- if `T` is an array type, then each element is value-initialized;
|
| 96 |
- otherwise, the object is zero-initialized.
|
| 97 |
|
| 98 |
An object that is value-initialized is deemed to be constructed and thus
|
| 99 |
subject to provisions of this International Standard applying to
|
|
@@ -121,14 +128,48 @@ is not the declaration of an object of class `X`, but the declaration of
|
|
| 121 |
a function taking no argument and returning an `X`. The form `()` is
|
| 122 |
permitted in certain other initialization contexts ([[expr.new]],
|
| 123 |
[[expr.type.conv]], [[class.base.init]]).
|
| 124 |
|
| 125 |
If no initializer is specified for an object, the object is
|
| 126 |
-
default-initialized
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
An initializer for a static member is in the scope of the member’s
|
| 132 |
class.
|
| 133 |
|
| 134 |
``` cpp
|
|
@@ -242,15 +283,14 @@ An *initializer-clause* followed by an ellipsis is a pack expansion (
|
|
| 242 |
[[temp.variadic]]).
|
| 243 |
|
| 244 |
### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
|
| 245 |
|
| 246 |
An *aggregate* is an array or a class (Clause [[class]]) with no
|
| 247 |
-
user-provided constructors ([[class.ctor]]), no
|
| 248 |
-
|
| 249 |
-
[[class.
|
| 250 |
-
[[class.
|
| 251 |
-
virtual functions ([[class.virtual]]).
|
| 252 |
|
| 253 |
When an aggregate is initialized by an initializer list, as specified
|
| 254 |
in [[dcl.init.list]], the elements of the initializer list are taken as
|
| 255 |
initializers for the members of the aggregate, in increasing subscript
|
| 256 |
or member order. Each member is copy-initialized from the corresponding
|
|
@@ -286,11 +326,11 @@ int x[] = { 1, 3, 5 };
|
|
| 286 |
```
|
| 287 |
|
| 288 |
declares and initializes `x` as a one-dimensional array that has three
|
| 289 |
elements since no size was specified and there are three initializers.
|
| 290 |
An empty initializer list `{}` shall not be used as the
|
| 291 |
-
*initializer-clause * for an array of unknown bound.[^
|
| 292 |
|
| 293 |
Static data members and anonymous bit-fields are not considered members
|
| 294 |
of the class for purposes of aggregate initialization.
|
| 295 |
|
| 296 |
``` cpp
|
|
@@ -317,20 +357,30 @@ char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
|
|
| 317 |
|
| 318 |
is ill-formed.
|
| 319 |
|
| 320 |
If there are fewer *initializer-clause*s in the list than there are
|
| 321 |
members in the aggregate, then each member not explicitly initialized
|
| 322 |
-
shall be initialized from
|
|
|
|
| 323 |
[[dcl.init.list]]).
|
| 324 |
|
| 325 |
``` cpp
|
| 326 |
-
struct S { int a; const char* b; int c; };
|
| 327 |
S ss = { 1, "asdf" };
|
| 328 |
```
|
| 329 |
|
| 330 |
-
initializes `ss.a` with 1, `ss.b` with `"asdf"`,
|
| 331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
If an aggregate class `C` contains a subaggregate member `m` that has no
|
| 334 |
members for purposes of aggregate initialization, the
|
| 335 |
*initializer-clause* for `m` shall not be omitted from an
|
| 336 |
*initializer-list* for an object of type `C` unless the
|
|
@@ -375,17 +425,11 @@ float y[4][3] = {
|
|
| 375 |
```
|
| 376 |
|
| 377 |
initializes the first column of `y` (regarded as a two-dimensional
|
| 378 |
array) and leaves the rest zero.
|
| 379 |
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
``` cpp
|
| 383 |
-
T x = { a };
|
| 384 |
-
```
|
| 385 |
-
|
| 386 |
-
braces can be elided in an *initializer-list* as follows.[^17] If the
|
| 387 |
*initializer-list* begins with a left brace, then the succeeding
|
| 388 |
comma-separated list of *initializer-clause*s initializes the members of
|
| 389 |
a subaggregate; it is erroneous for there to be more
|
| 390 |
*initializer-clause*s than members. If, however, the *initializer-list*
|
| 391 |
for a subaggregate does not begin with a left brace, then only enough
|
|
@@ -474,17 +518,17 @@ As described above, the braces around the *initializer-clause* for a
|
|
| 474 |
union member can be omitted if the union is a member of another
|
| 475 |
aggregate.
|
| 476 |
|
| 477 |
### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
|
| 478 |
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
characters of the value of the string literal initialize the
|
| 485 |
-
the array.
|
| 486 |
|
| 487 |
``` cpp
|
| 488 |
char msg[] = "Syntax error on line %s\n";
|
| 489 |
```
|
| 490 |
|
|
@@ -542,33 +586,30 @@ extern int& r2; // OK
|
|
| 542 |
```
|
| 543 |
|
| 544 |
Given types “ `T1`” and “ `T2`,” “ `T1`” is to “ `T2`” if `T1` is the
|
| 545 |
same type as `T2`, or `T1` is a base class of `T2`. “ `T1`” is with “
|
| 546 |
`T2`” if `T1` is reference-related to `T2` and *cv1* is the same
|
| 547 |
-
cv-qualification as, or greater cv-qualification than, *cv2*.
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
program that necessitates such a binding is ill-formed if `T1` is an
|
| 555 |
-
inaccessible (Clause [[class.access]]) or ambiguous (
|
| 556 |
-
[[class.member.lookup]]) base class of `T2`.
|
| 557 |
|
| 558 |
A reference to type “*cv1* `T1`” is initialized by an expression of type
|
| 559 |
“*cv2* `T2`” as follows:
|
| 560 |
|
| 561 |
- If the reference is an lvalue reference and the initializer expression
|
| 562 |
- is an lvalue (but is not a bit-field), and “ `T1`” is
|
| 563 |
reference-compatible with “ `T2`,” or
|
| 564 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 565 |
-
reference-related to `T2`, and can be
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
|
| 571 |
then the reference is bound to the initializer expression lvalue in
|
| 572 |
the first case and to the lvalue result of the conversion in the
|
| 573 |
second case (or, in either case, to the appropriate base class
|
| 574 |
subobject of the object). The usual lvalue-to-rvalue ([[conv.lval]]),
|
|
@@ -594,16 +635,18 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
|
|
| 594 |
int i = 2;
|
| 595 |
double& rd3 = i; // error: type mismatch and reference not const
|
| 596 |
```
|
| 597 |
|
| 598 |
- If the initializer expression
|
| 599 |
-
- is an xvalue, class prvalue, array prvalue
|
| 600 |
-
“*cv1* `T1`” is reference-compatible with
|
|
|
|
| 601 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 602 |
-
reference-related to `T2`, and can be
|
| 603 |
-
|
| 604 |
-
|
|
|
|
| 605 |
|
| 606 |
then the reference is bound to the value of the initializer
|
| 607 |
expression in the first case and to the result of the conversion in
|
| 608 |
the second case (or, in either case, to an appropriate base class
|
| 609 |
subobject). In the second case, if the reference is an rvalue
|
|
@@ -625,18 +668,40 @@ A reference to type “*cv1* `T1`” is initialized by an expression of type
|
|
| 625 |
int&& rri = static_cast<int&&>(i2); // bound directly to i2
|
| 626 |
B&& rrb = x; // bound directly to the result of operator B
|
| 627 |
int&& rri2 = X(); // error: lvalue-to-rvalue conversion applied to the
|
| 628 |
// result of operator int&
|
| 629 |
```
|
| 630 |
-
- Otherwise
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 637 |
``` cpp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 638 |
const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
|
| 639 |
double&& rrd = 2; // rrd refers to temporary with value 2.0
|
| 640 |
const volatile int cvi = 1;
|
| 641 |
const int& r2 = cvi; // error: type qualifiers dropped
|
| 642 |
double d2 = 1.0;
|
|
@@ -665,10 +730,11 @@ list-initialization in a copy-initialization context is called
|
|
| 665 |
*copy-list-initialization*. List-initialization can be used
|
| 666 |
|
| 667 |
- as the initializer in a variable definition ([[dcl.init]])
|
| 668 |
- as the initializer in a new expression ([[expr.new]])
|
| 669 |
- in a return statement ([[stmt.return]])
|
|
|
|
| 670 |
- as a function argument ([[expr.call]])
|
| 671 |
- as a subscript ([[expr.sub]])
|
| 672 |
- as an argument to a constructor invocation ([[dcl.init]],
|
| 673 |
[[expr.type.conv]])
|
| 674 |
- as an initializer for a non-static data member ([[class.mem]])
|
|
@@ -690,22 +756,24 @@ A constructor is an *initializer-list constructor* if its first
|
|
| 690 |
parameter is of type `std::initializer_list<E>` or reference to possibly
|
| 691 |
cv-qualified `std::initializer_list<E>` for some type `E`, and either
|
| 692 |
there are no other parameters or else all other parameters have default
|
| 693 |
arguments ([[dcl.fct.default]]). Initializer-list constructors are
|
| 694 |
favored over other constructors in list-initialization (
|
| 695 |
-
[[over.match.list]]).
|
| 696 |
-
|
| 697 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 698 |
type is not named ([[dcl.spec.auto]]) — the program is ill-formed.
|
| 699 |
|
| 700 |
List-initialization of an object or reference of type `T` is defined as
|
| 701 |
follows:
|
| 702 |
|
| 703 |
-
- If
|
| 704 |
-
|
| 705 |
-
- Otherwise, if `T` is an aggregate, aggregate initialization is
|
| 706 |
-
performed ([[dcl.init.aggr]]).
|
| 707 |
``` cpp
|
| 708 |
double ad[] = { 1, 2.0 }; // OK
|
| 709 |
int ai[] = { 1, 2.0 }; // error: narrowing
|
| 710 |
|
| 711 |
struct S2 {
|
|
@@ -714,13 +782,15 @@ follows:
|
|
| 714 |
};
|
| 715 |
S2 s21 = { 1, 2, 3.0 }; // OK
|
| 716 |
S2 s22 { 1.0, 2, 3 }; // error: narrowing
|
| 717 |
S2 s23 { }; // OK: default to 0,0,0
|
| 718 |
```
|
| 719 |
-
- Otherwise, if `T` is a
|
| 720 |
-
|
| 721 |
-
|
|
|
|
|
|
|
| 722 |
initialization of an object from a class of the same type (
|
| 723 |
[[dcl.init]]).
|
| 724 |
- Otherwise, if `T` is a class type, constructors are considered. The
|
| 725 |
applicable constructors are enumerated and the best one is chosen
|
| 726 |
through overload resolution ([[over.match]], [[over.match.list]]).
|
|
@@ -754,15 +824,25 @@ follows:
|
|
| 754 |
};
|
| 755 |
S s1 = { 1, 2, 3.0 }; // OK: invoke #1
|
| 756 |
S s2 { 1.0, 2, 3 }; // error: narrowing
|
| 757 |
S s3 { }; // OK: invoke #2
|
| 758 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 759 |
- Otherwise, if `T` is a reference type, a prvalue temporary of the type
|
| 760 |
-
referenced by `T` is list-initialized
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
type
|
|
|
|
| 764 |
``` cpp
|
| 765 |
struct S {
|
| 766 |
S(std::initializer_list<double>); // #1
|
| 767 |
S(const std::string&); // #2
|
| 768 |
// ...
|
|
@@ -772,18 +852,10 @@ follows:
|
|
| 772 |
S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
|
| 773 |
const int& i1 = { 1 }; // OK
|
| 774 |
const int& i2 = { 1.1 }; // error: narrowing
|
| 775 |
const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
|
| 776 |
```
|
| 777 |
-
- Otherwise, if the initializer list has a single element, the object or
|
| 778 |
-
reference is initialized from that element; if a narrowing conversion
|
| 779 |
-
(see below) is required to convert the element to `T`, the program is
|
| 780 |
-
ill-formed.
|
| 781 |
-
``` cpp
|
| 782 |
-
int x1 {2}; // OK
|
| 783 |
-
int x2 {2.0}; // error: narrowing
|
| 784 |
-
```
|
| 785 |
- Otherwise, if the initializer list has no elements, the object is
|
| 786 |
value-initialized.
|
| 787 |
``` cpp
|
| 788 |
int** pp {}; // initialized to null pointer
|
| 789 |
```
|
|
@@ -818,17 +890,19 @@ ordering holds regardless of the semantics of the initialization; for
|
|
| 818 |
example, it applies when the elements of the *initializer-list* are
|
| 819 |
interpreted as arguments of a constructor call, even though ordinarily
|
| 820 |
there are no sequencing constraints on the arguments of a call.
|
| 821 |
|
| 822 |
An object of type `std::initializer_list<E>` is constructed from an
|
| 823 |
-
initializer list as if the implementation allocated
|
| 824 |
-
elements of type `E`, where N is the number of elements in the
|
| 825 |
initializer list. Each element of that array is copy-initialized with
|
| 826 |
the corresponding element of the initializer list, and the
|
| 827 |
`std::initializer_list<E>` object is constructed to refer to that array.
|
| 828 |
-
|
| 829 |
-
the
|
|
|
|
|
|
|
| 830 |
|
| 831 |
``` cpp
|
| 832 |
struct X {
|
| 833 |
X(std::initializer_list<double> v);
|
| 834 |
};
|
|
@@ -837,35 +911,47 @@ X x{ 1,2,3 };
|
|
| 837 |
|
| 838 |
The initialization will be implemented in a way roughly equivalent to
|
| 839 |
this:
|
| 840 |
|
| 841 |
``` cpp
|
| 842 |
-
double __a[3] = {double{1}, double{2}, double{3}};
|
| 843 |
X x(std::initializer_list<double>(__a, __a+3));
|
| 844 |
```
|
| 845 |
|
| 846 |
assuming that the implementation can construct an `initializer_list`
|
| 847 |
object with a pair of pointers.
|
| 848 |
|
| 849 |
-
The
|
| 850 |
-
|
|
|
|
|
|
|
| 851 |
|
| 852 |
``` cpp
|
| 853 |
typedef std::complex<double> cmplx;
|
| 854 |
std::vector<cmplx> v1 = { 1, 2, 3 };
|
| 855 |
|
| 856 |
void f() {
|
| 857 |
std::vector<cmplx> v2{ 1, 2, 3 };
|
| 858 |
std::initializer_list<int> i3 = { 1, 2, 3 };
|
| 859 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 860 |
```
|
| 861 |
|
| 862 |
-
For `v1` and `v2`, the `initializer_list` object
|
| 863 |
-
`{ 1, 2, 3 }`
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 867 |
|
| 868 |
A *narrowing conversion* is an implicit conversion
|
| 869 |
|
| 870 |
- from a floating-point type to an integer type, or
|
| 871 |
- from `long double` to `double` or `float`, or from `double` to
|
|
@@ -876,13 +962,12 @@ A *narrowing conversion* is an implicit conversion
|
|
| 876 |
type, except where the source is a constant expression and the actual
|
| 877 |
value after conversion will fit into the target type and will produce
|
| 878 |
the original value when converted back to the original type, or
|
| 879 |
- from an integer type or unscoped enumeration type to an integer type
|
| 880 |
that cannot represent all the values of the original type, except
|
| 881 |
-
where the source is a constant expression
|
| 882 |
-
|
| 883 |
-
value when converted back to the original type.
|
| 884 |
|
| 885 |
As indicated above, such conversions are not allowed at the top level in
|
| 886 |
list-initializations.
|
| 887 |
|
| 888 |
``` cpp
|
|
@@ -920,12 +1005,13 @@ int a[] =
|
|
| 920 |
[basic.lookup.udir]: basic.md#basic.lookup.udir
|
| 921 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 922 |
[basic.lval]: basic.md#basic.lval
|
| 923 |
[basic.namespace]: #basic.namespace
|
| 924 |
[basic.scope]: basic.md#basic.scope
|
| 925 |
-
[basic.scope.
|
| 926 |
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
|
|
|
| 927 |
[basic.scope.proto]: basic.md#basic.scope.proto
|
| 928 |
[basic.start]: basic.md#basic.start
|
| 929 |
[basic.start.init]: basic.md#basic.start.init
|
| 930 |
[basic.stc]: basic.md#basic.stc
|
| 931 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
|
@@ -949,11 +1035,10 @@ int a[] =
|
|
| 949 |
[class.inhctor]: special.md#class.inhctor
|
| 950 |
[class.init]: special.md#class.init
|
| 951 |
[class.mem]: class.md#class.mem
|
| 952 |
[class.member.lookup]: class.md#class.member.lookup
|
| 953 |
[class.mfct]: class.md#class.mfct
|
| 954 |
-
[class.mfct.non-static]: class.md#class.mfct.non-static
|
| 955 |
[class.name]: class.md#class.name
|
| 956 |
[class.qual]: basic.md#class.qual
|
| 957 |
[class.static]: class.md#class.static
|
| 958 |
[class.static.data]: class.md#class.static.data
|
| 959 |
[class.temporary]: special.md#class.temporary
|
|
@@ -961,19 +1046,21 @@ int a[] =
|
|
| 961 |
[class.union]: class.md#class.union
|
| 962 |
[class.virtual]: class.md#class.virtual
|
| 963 |
[conv]: conv.md#conv
|
| 964 |
[conv.array]: conv.md#conv.array
|
| 965 |
[conv.func]: conv.md#conv.func
|
|
|
|
| 966 |
[conv.lval]: conv.md#conv.lval
|
| 967 |
[conv.prom]: conv.md#conv.prom
|
| 968 |
[conv.ptr]: conv.md#conv.ptr
|
| 969 |
[dcl.align]: #dcl.align
|
| 970 |
[dcl.ambig.res]: #dcl.ambig.res
|
| 971 |
[dcl.array]: #dcl.array
|
| 972 |
[dcl.asm]: #dcl.asm
|
| 973 |
[dcl.attr]: #dcl.attr
|
| 974 |
[dcl.attr.depend]: #dcl.attr.depend
|
|
|
|
| 975 |
[dcl.attr.grammar]: #dcl.attr.grammar
|
| 976 |
[dcl.attr.noreturn]: #dcl.attr.noreturn
|
| 977 |
[dcl.constexpr]: #dcl.constexpr
|
| 978 |
[dcl.dcl]: #dcl.dcl
|
| 979 |
[dcl.decl]: #dcl.decl
|
|
@@ -1011,15 +1098,18 @@ int a[] =
|
|
| 1011 |
[except.throw]: except.md#except.throw
|
| 1012 |
[expr]: expr.md#expr
|
| 1013 |
[expr.alignof]: expr.md#expr.alignof
|
| 1014 |
[expr.ass]: expr.md#expr.ass
|
| 1015 |
[expr.call]: expr.md#expr.call
|
|
|
|
|
|
|
|
|
|
| 1016 |
[expr.const]: expr.md#expr.const
|
| 1017 |
[expr.const.cast]: expr.md#expr.const.cast
|
| 1018 |
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 1019 |
[expr.new]: expr.md#expr.new
|
| 1020 |
-
[expr.prim]: expr.md#expr.prim
|
| 1021 |
[expr.ref]: expr.md#expr.ref
|
| 1022 |
[expr.static.cast]: expr.md#expr.static.cast
|
| 1023 |
[expr.sub]: expr.md#expr.sub
|
| 1024 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 1025 |
[expr.unary]: expr.md#expr.unary
|
|
@@ -1030,19 +1120,19 @@ int a[] =
|
|
| 1030 |
[intro.multithread]: intro.md#intro.multithread
|
| 1031 |
[lex.charset]: lex.md#lex.charset
|
| 1032 |
[lex.digraph]: lex.md#lex.digraph
|
| 1033 |
[lex.key]: lex.md#lex.key
|
| 1034 |
[lex.name]: lex.md#lex.name
|
|
|
|
| 1035 |
[namespace.alias]: #namespace.alias
|
| 1036 |
[namespace.def]: #namespace.def
|
| 1037 |
[namespace.memdef]: #namespace.memdef
|
| 1038 |
[namespace.qual]: basic.md#namespace.qual
|
| 1039 |
[namespace.udecl]: #namespace.udecl
|
| 1040 |
[namespace.udir]: #namespace.udir
|
| 1041 |
[namespace.unnamed]: #namespace.unnamed
|
| 1042 |
[over]: over.md#over
|
| 1043 |
-
[over.ics.rank]: over.md#over.ics.rank
|
| 1044 |
[over.match]: over.md#over.match
|
| 1045 |
[over.match.conv]: over.md#over.match.conv
|
| 1046 |
[over.match.copy]: over.md#over.match.copy
|
| 1047 |
[over.match.ctor]: over.md#over.match.ctor
|
| 1048 |
[over.match.list]: over.md#over.match.list
|
|
@@ -1058,11 +1148,10 @@ int a[] =
|
|
| 1058 |
[stmt.select]: stmt.md#stmt.select
|
| 1059 |
[stmt.stmt]: stmt.md#stmt.stmt
|
| 1060 |
[support.runtime]: language.md#support.runtime
|
| 1061 |
[tab:simple.type.specifiers]: #tab:simple.type.specifiers
|
| 1062 |
[temp]: temp.md#temp
|
| 1063 |
-
[temp.arg]: temp.md#temp.arg
|
| 1064 |
[temp.arg.type]: temp.md#temp.arg.type
|
| 1065 |
[temp.class.spec]: temp.md#temp.class.spec
|
| 1066 |
[temp.deduct.call]: temp.md#temp.deduct.call
|
| 1067 |
[temp.dep]: temp.md#temp.dep
|
| 1068 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
|
@@ -1077,39 +1166,35 @@ int a[] =
|
|
| 1077 |
|
| 1078 |
[^1]: The “implicit int” rule of C is no longer supported.
|
| 1079 |
|
| 1080 |
[^2]: The inline keyword has no effect on the linkage of a function.
|
| 1081 |
|
| 1082 |
-
[^3]:
|
| 1083 |
-
conversion ([[conv.lval]]) if the corresponding copy-initialization
|
| 1084 |
-
requires one.
|
| 1085 |
-
|
| 1086 |
-
[^4]: There is no special provision for a *decl-specifier-seq* that
|
| 1087 |
lacks a *type-specifier* or that has a *type-specifier* that only
|
| 1088 |
specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
|
| 1089 |
supported.
|
| 1090 |
|
| 1091 |
-
[^
|
| 1092 |
semantics for the enumeration type. It does not preclude an
|
| 1093 |
expression of enumeration type from having a value that falls
|
| 1094 |
outside this range.
|
| 1095 |
|
| 1096 |
-
[^
|
| 1097 |
linkage, they are effectively qualified by a name unique to their
|
| 1098 |
translation unit and therefore can never be seen from any other
|
| 1099 |
translation unit.
|
| 1100 |
|
| 1101 |
-
[^
|
| 1102 |
unqualified.
|
| 1103 |
|
| 1104 |
-
[^
|
| 1105 |
resolved by considering whether one member hides the other along
|
| 1106 |
some paths ([[class.member.lookup]]). There is no such
|
| 1107 |
disambiguation when considering the set of names found as a result
|
| 1108 |
of following *using-directive*s.
|
| 1109 |
|
| 1110 |
-
[^
|
| 1111 |
the corresponding sequence of declarations each with a single
|
| 1112 |
declarator. That is
|
| 1113 |
|
| 1114 |
`T D1, D2, ... Dn;`
|
| 1115 |
|
|
@@ -1139,42 +1224,40 @@ int a[] =
|
|
| 1139 |
`auto i = 1, j = 2.0; \textrm{// error: deduced types for \tcode{i} and \tcode{j} do not match}`
|
| 1140 |
as opposed to
|
| 1141 |
`auto i = 1; \textrm{// OK: \tcode{i} deduced to have type \tcode{int}}`
|
| 1142 |
`auto j = 2.0; \textrm{// OK: \tcode{j} deduced to have type \tcode{double}}`
|
| 1143 |
|
| 1144 |
-
[^
|
| 1145 |
in function return types.
|
| 1146 |
|
| 1147 |
-
[^
|
| 1148 |
to array of unknown bound of `T`” and where means any sequence of
|
| 1149 |
“pointer to” and “array of” derived declarator types. This exclusion
|
| 1150 |
applies to the parameters of the function, and if a parameter is a
|
| 1151 |
pointer to function or pointer to member function then to its
|
| 1152 |
parameters also, etc.
|
| 1153 |
|
| 1154 |
-
[^
|
| 1155 |
comma (so the ellipsis will be parsed as part of the
|
| 1156 |
*parameter-declaration-clause*) or by introducing a name for the
|
| 1157 |
parameter (so the ellipsis will be parsed as part of the
|
| 1158 |
*declarator-id*).
|
| 1159 |
|
| 1160 |
-
[^
|
| 1161 |
declarations of pointers to functions, references to functions, or
|
| 1162 |
`typedef` declarations.
|
| 1163 |
|
| 1164 |
-
[^
|
| 1165 |
variables with names that are reserved to the implementation (
|
| 1166 |
[[global.names]]). If a predefined variable is not odr-used (
|
| 1167 |
[[basic.def.odr]]), its string value need not be present in the
|
| 1168 |
program image.
|
| 1169 |
|
| 1170 |
-
[^
|
| 1171 |
-
|
| 1172 |
-
|
| 1173 |
|
| 1174 |
-
[^
|
| 1175 |
nonetheless C++does not have zero length arrays.
|
| 1176 |
|
| 1177 |
-
[^
|
| 1178 |
-
|
| 1179 |
-
[^18]: This requires a conversion function ([[class.conv.fct]])
|
| 1180 |
returning a reference type.
|
|
|
|
| 54 |
Default arguments are more restricted; see [[dcl.fct.default]].
|
| 55 |
|
| 56 |
The order of initialization of variables with static storage duration is
|
| 57 |
described in [[basic.start]] and [[stmt.dcl]].
|
| 58 |
|
| 59 |
+
A declaration of a block-scope variable with external or internal
|
| 60 |
+
linkage that has an *initializer* is ill-formed.
|
| 61 |
+
|
| 62 |
To *zero-initialize* an object or reference of type `T` means:
|
| 63 |
|
| 64 |
+
- if `T` is a scalar type ([[basic.types]]), the object is initialized
|
| 65 |
+
to the value obtained by converting the integer literal `0` (zero) to
|
| 66 |
+
`T`;[^14]
|
| 67 |
- if `T` is a (possibly cv-qualified) non-union class type, each
|
| 68 |
non-static data member and each base-class subobject is
|
| 69 |
zero-initialized and padding is initialized to zero bits;
|
| 70 |
- if `T` is a (possibly cv-qualified) union type, the object’s first
|
| 71 |
non-static named data member is zero-initialized and padding is
|
|
|
|
| 74 |
- if `T` is a reference type, no initialization is performed.
|
| 75 |
|
| 76 |
To *default-initialize* an object of type `T` means:
|
| 77 |
|
| 78 |
- if `T` is a (possibly cv-qualified) class type (Clause [[class]]),
|
| 79 |
+
the default constructor ([[class.ctor]]) for `T` is called (and the
|
| 80 |
+
initialization is ill-formed if `T` has no default constructor or
|
| 81 |
+
overload resolution ([[over.match]]) results in an ambiguity or in a
|
| 82 |
+
function that is deleted or inaccessible from the context of the
|
| 83 |
+
initialization);
|
| 84 |
- if `T` is an array type, each element is default-initialized;
|
| 85 |
- otherwise, no initialization is performed.
|
| 86 |
|
| 87 |
If a program calls for the default initialization of an object of a
|
| 88 |
const-qualified type `T`, `T` shall be a class type with a user-provided
|
| 89 |
default constructor.
|
| 90 |
|
| 91 |
To *value-initialize* an object of type `T` means:
|
| 92 |
|
| 93 |
- if `T` is a (possibly cv-qualified) class type (Clause [[class]])
|
| 94 |
+
with either no default constructor ([[class.ctor]]) or a default
|
| 95 |
+
constructor that is user-provided or deleted, then the object is
|
| 96 |
+
default-initialized;
|
| 97 |
+
- if `T` is a (possibly cv-qualified) class type without a user-provided
|
| 98 |
+
or deleted default constructor, then the object is zero-initialized
|
| 99 |
+
and the semantic constraints for default-initialization are checked,
|
| 100 |
+
and if `T` has a non-trivial default constructor, the object is
|
| 101 |
+
default-initialized;
|
| 102 |
- if `T` is an array type, then each element is value-initialized;
|
| 103 |
- otherwise, the object is zero-initialized.
|
| 104 |
|
| 105 |
An object that is value-initialized is deemed to be constructed and thus
|
| 106 |
subject to provisions of this International Standard applying to
|
|
|
|
| 128 |
a function taking no argument and returning an `X`. The form `()` is
|
| 129 |
permitted in certain other initialization contexts ([[expr.new]],
|
| 130 |
[[expr.type.conv]], [[class.base.init]]).
|
| 131 |
|
| 132 |
If no initializer is specified for an object, the object is
|
| 133 |
+
default-initialized. When storage for an object with automatic or
|
| 134 |
+
dynamic storage duration is obtained, the object has an *indeterminate
|
| 135 |
+
value*, and if no initialization is performed for the object, that
|
| 136 |
+
object retains an indeterminate value until that value is replaced (
|
| 137 |
+
[[expr.ass]]). Objects with static or thread storage duration are
|
| 138 |
+
zero-initialized, see [[basic.start.init]]. If an indeterminate value
|
| 139 |
+
is produced by an evaluation, the behavior is undefined except in the
|
| 140 |
+
following cases:
|
| 141 |
+
|
| 142 |
+
- If an indeterminate value of unsigned narrow character type (
|
| 143 |
+
[[basic.fundamental]]) is produced by the evaluation of:
|
| 144 |
+
- the second or third operand of a conditional expression (
|
| 145 |
+
[[expr.cond]]),
|
| 146 |
+
- the right operand of a comma expression ([[expr.comma]]),
|
| 147 |
+
- the operand of a cast or conversion to an unsigned narrow character
|
| 148 |
+
type ([[conv.integral]], [[expr.type.conv]], [[expr.static.cast]],
|
| 149 |
+
[[expr.cast]]), or
|
| 150 |
+
- a discarded-value expression (Clause [[expr]]),
|
| 151 |
+
|
| 152 |
+
then the result of the operation is an indeterminate value.
|
| 153 |
+
- If an indeterminate value of unsigned narrow character type is
|
| 154 |
+
produced by the evaluation of the right operand of a simple assignment
|
| 155 |
+
operator ([[expr.ass]]) whose first operand is an lvalue of unsigned
|
| 156 |
+
narrow character type, an indeterminate value replaces the value of
|
| 157 |
+
the object referred to by the left operand.
|
| 158 |
+
- If an indeterminate value of unsigned narrow character type is
|
| 159 |
+
produced by the evaluation of the initialization expression when
|
| 160 |
+
initializing an object of unsigned narrow character type, that object
|
| 161 |
+
is initialized to an indeterminate value.
|
| 162 |
+
|
| 163 |
+
``` cpp
|
| 164 |
+
int f(bool b) {
|
| 165 |
+
unsigned char c;
|
| 166 |
+
unsigned char d = c; // OK, d has an indeterminate value
|
| 167 |
+
int e = d; // undefined behavior
|
| 168 |
+
return b ? d : 0; // undefined behavior if b is true
|
| 169 |
+
}
|
| 170 |
+
```
|
| 171 |
|
| 172 |
An initializer for a static member is in the scope of the member’s
|
| 173 |
class.
|
| 174 |
|
| 175 |
``` cpp
|
|
|
|
| 283 |
[[temp.variadic]]).
|
| 284 |
|
| 285 |
### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
|
| 286 |
|
| 287 |
An *aggregate* is an array or a class (Clause [[class]]) with no
|
| 288 |
+
user-provided constructors ([[class.ctor]]), no private or protected
|
| 289 |
+
non-static data members (Clause [[class.access]]), no base classes
|
| 290 |
+
(Clause [[class.derived]]), and no virtual functions (
|
| 291 |
+
[[class.virtual]]).
|
|
|
|
| 292 |
|
| 293 |
When an aggregate is initialized by an initializer list, as specified
|
| 294 |
in [[dcl.init.list]], the elements of the initializer list are taken as
|
| 295 |
initializers for the members of the aggregate, in increasing subscript
|
| 296 |
or member order. Each member is copy-initialized from the corresponding
|
|
|
|
| 326 |
```
|
| 327 |
|
| 328 |
declares and initializes `x` as a one-dimensional array that has three
|
| 329 |
elements since no size was specified and there are three initializers.
|
| 330 |
An empty initializer list `{}` shall not be used as the
|
| 331 |
+
*initializer-clause * for an array of unknown bound.[^15]
|
| 332 |
|
| 333 |
Static data members and anonymous bit-fields are not considered members
|
| 334 |
of the class for purposes of aggregate initialization.
|
| 335 |
|
| 336 |
``` cpp
|
|
|
|
| 357 |
|
| 358 |
is ill-formed.
|
| 359 |
|
| 360 |
If there are fewer *initializer-clause*s in the list than there are
|
| 361 |
members in the aggregate, then each member not explicitly initialized
|
| 362 |
+
shall be initialized from its *brace-or-equal-initializer* or, if there
|
| 363 |
+
is no *brace-or-equal-initializer*, from an empty initializer list (
|
| 364 |
[[dcl.init.list]]).
|
| 365 |
|
| 366 |
``` cpp
|
| 367 |
+
struct S { int a; const char* b; int c; int d = b[a]; };
|
| 368 |
S ss = { 1, "asdf" };
|
| 369 |
```
|
| 370 |
|
| 371 |
+
initializes `ss.a` with 1, `ss.b` with `"asdf"`, `ss.c` with the value
|
| 372 |
+
of an expression of the form `int{}` (that is, `0`), and `ss.d` with the
|
| 373 |
+
value of `ss.b[ss.a]` (that is, `'s'`), and in
|
| 374 |
+
|
| 375 |
+
``` cpp
|
| 376 |
+
struct X { int i, j, k = 42; };
|
| 377 |
+
X a[] = { 1, 2, 3, 4, 5, 6 };
|
| 378 |
+
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
|
| 379 |
+
```
|
| 380 |
+
|
| 381 |
+
`a` and `b` have the same value
|
| 382 |
|
| 383 |
If an aggregate class `C` contains a subaggregate member `m` that has no
|
| 384 |
members for purposes of aggregate initialization, the
|
| 385 |
*initializer-clause* for `m` shall not be omitted from an
|
| 386 |
*initializer-list* for an object of type `C` unless the
|
|
|
|
| 425 |
```
|
| 426 |
|
| 427 |
initializes the first column of `y` (regarded as a two-dimensional
|
| 428 |
array) and leaves the rest zero.
|
| 429 |
|
| 430 |
+
Braces can be elided in an *initializer-list* as follows. If the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
*initializer-list* begins with a left brace, then the succeeding
|
| 432 |
comma-separated list of *initializer-clause*s initializes the members of
|
| 433 |
a subaggregate; it is erroneous for there to be more
|
| 434 |
*initializer-clause*s than members. If, however, the *initializer-list*
|
| 435 |
for a subaggregate does not begin with a left brace, then only enough
|
|
|
|
| 518 |
union member can be omitted if the union is a member of another
|
| 519 |
aggregate.
|
| 520 |
|
| 521 |
### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
|
| 522 |
|
| 523 |
+
An array of narrow character type ([[basic.fundamental]]), `char16_t`
|
| 524 |
+
array, `char32_t` array, or `wchar_t` array can be initialized by a
|
| 525 |
+
narrow string literal, `char16_t` string literal, `char32_t` string
|
| 526 |
+
literal, or wide string literal, respectively, or by an
|
| 527 |
+
appropriately-typed string literal enclosed in braces ([[lex.string]]).
|
| 528 |
+
Successive characters of the value of the string literal initialize the
|
| 529 |
+
elements of the array.
|
| 530 |
|
| 531 |
``` cpp
|
| 532 |
char msg[] = "Syntax error on line %s\n";
|
| 533 |
```
|
| 534 |
|
|
|
|
| 586 |
```
|
| 587 |
|
| 588 |
Given types “ `T1`” and “ `T2`,” “ `T1`” is to “ `T2`” if `T1` is the
|
| 589 |
same type as `T2`, or `T1` is a base class of `T2`. “ `T1`” is with “
|
| 590 |
`T2`” if `T1` is reference-related to `T2` and *cv1* is the same
|
| 591 |
+
cv-qualification as, or greater cv-qualification than, *cv2*. In all
|
| 592 |
+
cases where the reference-related or reference-compatible relationship
|
| 593 |
+
of two types is used to establish the validity of a reference binding,
|
| 594 |
+
and `T1` is a base class of `T2`, a program that necessitates such a
|
| 595 |
+
binding is ill-formed if `T1` is an inaccessible (Clause
|
| 596 |
+
[[class.access]]) or ambiguous ([[class.member.lookup]]) base class of
|
| 597 |
+
`T2`.
|
|
|
|
|
|
|
|
|
|
| 598 |
|
| 599 |
A reference to type “*cv1* `T1`” is initialized by an expression of type
|
| 600 |
“*cv2* `T2`” as follows:
|
| 601 |
|
| 602 |
- If the reference is an lvalue reference and the initializer expression
|
| 603 |
- is an lvalue (but is not a bit-field), and “ `T1`” is
|
| 604 |
reference-compatible with “ `T2`,” or
|
| 605 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 606 |
+
reference-related to `T2`, and can be converted to an lvalue of type
|
| 607 |
+
“ `T3`,” where “ `T1`” is reference-compatible with “ `T3`”[^16]
|
| 608 |
+
(this conversion is selected by enumerating the applicable
|
| 609 |
+
conversion functions ([[over.match.ref]]) and choosing the best one
|
| 610 |
+
through overload resolution ([[over.match]])),
|
| 611 |
|
| 612 |
then the reference is bound to the initializer expression lvalue in
|
| 613 |
the first case and to the lvalue result of the conversion in the
|
| 614 |
second case (or, in either case, to the appropriate base class
|
| 615 |
subobject of the object). The usual lvalue-to-rvalue ([[conv.lval]]),
|
|
|
|
| 635 |
int i = 2;
|
| 636 |
double& rd3 = i; // error: type mismatch and reference not const
|
| 637 |
```
|
| 638 |
|
| 639 |
- If the initializer expression
|
| 640 |
+
- is an xvalue (but not a bit-field), class prvalue, array prvalue
|
| 641 |
+
or function lvalue and “*cv1* `T1`” is reference-compatible with
|
| 642 |
+
“*cv2* `T2`”, or
|
| 643 |
- has a class type (i.e., `T2` is a class type), where `T1` is not
|
| 644 |
+
reference-related to `T2`, and can be converted to an xvalue,
|
| 645 |
+
class prvalue, or function lvalue of type “*cv3* `T3`”, where
|
| 646 |
+
“*cv1* `T1`” is reference-compatible with “*cv3* `T3`” (see
|
| 647 |
+
[[over.match.ref]]),
|
| 648 |
|
| 649 |
then the reference is bound to the value of the initializer
|
| 650 |
expression in the first case and to the result of the conversion in
|
| 651 |
the second case (or, in either case, to an appropriate base class
|
| 652 |
subobject). In the second case, if the reference is an rvalue
|
|
|
|
| 668 |
int&& rri = static_cast<int&&>(i2); // bound directly to i2
|
| 669 |
B&& rrb = x; // bound directly to the result of operator B
|
| 670 |
int&& rri2 = X(); // error: lvalue-to-rvalue conversion applied to the
|
| 671 |
// result of operator int&
|
| 672 |
```
|
| 673 |
+
- Otherwise:
|
| 674 |
+
- If `T1` is a class type, user-defined conversions are considered
|
| 675 |
+
using the rules for copy-initialization of an object of type “
|
| 676 |
+
`T1`” by user-defined conversion ([[dcl.init]],
|
| 677 |
+
[[over.match.copy]]); the program is ill-formed if the
|
| 678 |
+
corresponding non-reference copy-initialization would be
|
| 679 |
+
ill-formed. The result of the call to the conversion function, as
|
| 680 |
+
described for the non-reference copy-initialization, is then used
|
| 681 |
+
to direct-initialize the reference. The program is ill-formed if
|
| 682 |
+
the direct-initialization does not result in a direct binding or
|
| 683 |
+
if it involves a user-defined conversion.
|
| 684 |
+
- If `T1` is a non-class type, a temporary of type “ `T1`” is
|
| 685 |
+
created and copy-initialized ([[dcl.init]]) from the initializer
|
| 686 |
+
expression. The reference is then bound to the temporary.
|
| 687 |
+
|
| 688 |
+
If `T1` is reference-related to `T2`:
|
| 689 |
+
- *cv1* shall be the same cv-qualification as, or greater
|
| 690 |
+
cv-qualification than, *cv2*; and
|
| 691 |
+
- if the reference is an rvalue reference, the initializer
|
| 692 |
+
expression shall not be an lvalue.
|
| 693 |
+
|
| 694 |
``` cpp
|
| 695 |
+
struct Banana { };
|
| 696 |
+
struct Enigma { operator const Banana(); };
|
| 697 |
+
void enigmatic() {
|
| 698 |
+
typedef const Banana ConstBanana;
|
| 699 |
+
Banana &&banana1 = ConstBanana(); // ill-formed
|
| 700 |
+
Banana &&banana2 = Enigma(); // ill-formed
|
| 701 |
+
}
|
| 702 |
+
|
| 703 |
const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
|
| 704 |
double&& rrd = 2; // rrd refers to temporary with value 2.0
|
| 705 |
const volatile int cvi = 1;
|
| 706 |
const int& r2 = cvi; // error: type qualifiers dropped
|
| 707 |
double d2 = 1.0;
|
|
|
|
| 730 |
*copy-list-initialization*. List-initialization can be used
|
| 731 |
|
| 732 |
- as the initializer in a variable definition ([[dcl.init]])
|
| 733 |
- as the initializer in a new expression ([[expr.new]])
|
| 734 |
- in a return statement ([[stmt.return]])
|
| 735 |
+
- as a *for-range-initializer* ([[stmt.iter]])
|
| 736 |
- as a function argument ([[expr.call]])
|
| 737 |
- as a subscript ([[expr.sub]])
|
| 738 |
- as an argument to a constructor invocation ([[dcl.init]],
|
| 739 |
[[expr.type.conv]])
|
| 740 |
- as an initializer for a non-static data member ([[class.mem]])
|
|
|
|
| 756 |
parameter is of type `std::initializer_list<E>` or reference to possibly
|
| 757 |
cv-qualified `std::initializer_list<E>` for some type `E`, and either
|
| 758 |
there are no other parameters or else all other parameters have default
|
| 759 |
arguments ([[dcl.fct.default]]). Initializer-list constructors are
|
| 760 |
favored over other constructors in list-initialization (
|
| 761 |
+
[[over.match.list]]). Passing an initializer list as the argument to the
|
| 762 |
+
constructor template `template<class T> C(T)` of a class `C` does not
|
| 763 |
+
create an initializer-list constructor, because an initializer list
|
| 764 |
+
argument causes the corresponding parameter to be a non-deduced
|
| 765 |
+
context ([[temp.deduct.call]]). The template `std::initializer_list` is
|
| 766 |
+
not predefined; if the header `<initializer_list>` is not included prior
|
| 767 |
+
to a use of `std::initializer_list` — even an implicit use in which the
|
| 768 |
type is not named ([[dcl.spec.auto]]) — the program is ill-formed.
|
| 769 |
|
| 770 |
List-initialization of an object or reference of type `T` is defined as
|
| 771 |
follows:
|
| 772 |
|
| 773 |
+
- If `T` is an aggregate, aggregate initialization is performed (
|
| 774 |
+
[[dcl.init.aggr]]).
|
|
|
|
|
|
|
| 775 |
``` cpp
|
| 776 |
double ad[] = { 1, 2.0 }; // OK
|
| 777 |
int ai[] = { 1, 2.0 }; // error: narrowing
|
| 778 |
|
| 779 |
struct S2 {
|
|
|
|
| 782 |
};
|
| 783 |
S2 s21 = { 1, 2, 3.0 }; // OK
|
| 784 |
S2 s22 { 1.0, 2, 3 }; // error: narrowing
|
| 785 |
S2 s23 { }; // OK: default to 0,0,0
|
| 786 |
```
|
| 787 |
+
- Otherwise, if the initializer list has no elements and `T` is a class
|
| 788 |
+
type with a default constructor, the object is value-initialized.
|
| 789 |
+
- Otherwise, if `T` is a specialization of `std::initializer_list<E>`, a
|
| 790 |
+
prvalue `initializer_list` object is constructed as described below
|
| 791 |
+
and used to initialize the object according to the rules for
|
| 792 |
initialization of an object from a class of the same type (
|
| 793 |
[[dcl.init]]).
|
| 794 |
- Otherwise, if `T` is a class type, constructors are considered. The
|
| 795 |
applicable constructors are enumerated and the best one is chosen
|
| 796 |
through overload resolution ([[over.match]], [[over.match.list]]).
|
|
|
|
| 824 |
};
|
| 825 |
S s1 = { 1, 2, 3.0 }; // OK: invoke #1
|
| 826 |
S s2 { 1.0, 2, 3 }; // error: narrowing
|
| 827 |
S s3 { }; // OK: invoke #2
|
| 828 |
```
|
| 829 |
+
- Otherwise, if the initializer list has a single element of type `E`
|
| 830 |
+
and either `T` is not a reference type or its referenced type is
|
| 831 |
+
reference-related to `E`, the object or reference is initialized from
|
| 832 |
+
that element; if a narrowing conversion (see below) is required to
|
| 833 |
+
convert the element to `T`, the program is ill-formed.
|
| 834 |
+
``` cpp
|
| 835 |
+
int x1 {2}; // OK
|
| 836 |
+
int x2 {2.0}; // error: narrowing
|
| 837 |
+
```
|
| 838 |
- Otherwise, if `T` is a reference type, a prvalue temporary of the type
|
| 839 |
+
referenced by `T` is copy-list-initialized or direct-list-initialized,
|
| 840 |
+
depending on the kind of initialization for the reference, and the
|
| 841 |
+
reference is bound to that temporary. As usual, the binding will fail
|
| 842 |
+
and the program is ill-formed if the reference type is an lvalue
|
| 843 |
+
reference to a non-const type.
|
| 844 |
``` cpp
|
| 845 |
struct S {
|
| 846 |
S(std::initializer_list<double>); // #1
|
| 847 |
S(const std::string&); // #2
|
| 848 |
// ...
|
|
|
|
| 852 |
S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
|
| 853 |
const int& i1 = { 1 }; // OK
|
| 854 |
const int& i2 = { 1.1 }; // error: narrowing
|
| 855 |
const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array
|
| 856 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 857 |
- Otherwise, if the initializer list has no elements, the object is
|
| 858 |
value-initialized.
|
| 859 |
``` cpp
|
| 860 |
int** pp {}; // initialized to null pointer
|
| 861 |
```
|
|
|
|
| 890 |
example, it applies when the elements of the *initializer-list* are
|
| 891 |
interpreted as arguments of a constructor call, even though ordinarily
|
| 892 |
there are no sequencing constraints on the arguments of a call.
|
| 893 |
|
| 894 |
An object of type `std::initializer_list<E>` is constructed from an
|
| 895 |
+
initializer list as if the implementation allocated a temporary array of
|
| 896 |
+
N elements of type `const E`, where N is the number of elements in the
|
| 897 |
initializer list. Each element of that array is copy-initialized with
|
| 898 |
the corresponding element of the initializer list, and the
|
| 899 |
`std::initializer_list<E>` object is constructed to refer to that array.
|
| 900 |
+
A constructor or conversion function selected for the copy shall be
|
| 901 |
+
accessible (Clause [[class.access]]) in the context of the initializer
|
| 902 |
+
list. If a narrowing conversion is required to initialize any of the
|
| 903 |
+
elements, the program is ill-formed.
|
| 904 |
|
| 905 |
``` cpp
|
| 906 |
struct X {
|
| 907 |
X(std::initializer_list<double> v);
|
| 908 |
};
|
|
|
|
| 911 |
|
| 912 |
The initialization will be implemented in a way roughly equivalent to
|
| 913 |
this:
|
| 914 |
|
| 915 |
``` cpp
|
| 916 |
+
const double __a[3] = {double{1}, double{2}, double{3}};
|
| 917 |
X x(std::initializer_list<double>(__a, __a+3));
|
| 918 |
```
|
| 919 |
|
| 920 |
assuming that the implementation can construct an `initializer_list`
|
| 921 |
object with a pair of pointers.
|
| 922 |
|
| 923 |
+
The array has the same lifetime as any other temporary object (
|
| 924 |
+
[[class.temporary]]), except that initializing an `initializer_list`
|
| 925 |
+
object from the array extends the lifetime of the array exactly like
|
| 926 |
+
binding a reference to a temporary.
|
| 927 |
|
| 928 |
``` cpp
|
| 929 |
typedef std::complex<double> cmplx;
|
| 930 |
std::vector<cmplx> v1 = { 1, 2, 3 };
|
| 931 |
|
| 932 |
void f() {
|
| 933 |
std::vector<cmplx> v2{ 1, 2, 3 };
|
| 934 |
std::initializer_list<int> i3 = { 1, 2, 3 };
|
| 935 |
}
|
| 936 |
+
|
| 937 |
+
struct A {
|
| 938 |
+
std::initializer_list<int> i4;
|
| 939 |
+
A() : i4{ 1, 2, 3 } {} // creates an A with a dangling reference
|
| 940 |
+
};
|
| 941 |
```
|
| 942 |
|
| 943 |
+
For `v1` and `v2`, the `initializer_list` object is a parameter in a
|
| 944 |
+
function call, so the array created for `{ 1, 2, 3 }` has
|
| 945 |
+
full-expression lifetime. For `i3`, the `initializer_list` object is a
|
| 946 |
+
variable, so the array persists for the lifetime of the variable. For
|
| 947 |
+
`i4`, the `initializer_list` object is initialized in a constructor’s
|
| 948 |
+
*ctor-initializer*, so the array persists only until the constructor
|
| 949 |
+
exits, and so any use of the elements of `i4` after the constructor
|
| 950 |
+
exits produces undefined behavior. The implementation is free to
|
| 951 |
+
allocate the array in read-only memory if an explicit array with the
|
| 952 |
+
same initializer could be so allocated.
|
| 953 |
|
| 954 |
A *narrowing conversion* is an implicit conversion
|
| 955 |
|
| 956 |
- from a floating-point type to an integer type, or
|
| 957 |
- from `long double` to `double` or `float`, or from `double` to
|
|
|
|
| 962 |
type, except where the source is a constant expression and the actual
|
| 963 |
value after conversion will fit into the target type and will produce
|
| 964 |
the original value when converted back to the original type, or
|
| 965 |
- from an integer type or unscoped enumeration type to an integer type
|
| 966 |
that cannot represent all the values of the original type, except
|
| 967 |
+
where the source is a constant expression whose value after integral
|
| 968 |
+
promotions will fit into the target type.
|
|
|
|
| 969 |
|
| 970 |
As indicated above, such conversions are not allowed at the top level in
|
| 971 |
list-initializations.
|
| 972 |
|
| 973 |
``` cpp
|
|
|
|
| 1005 |
[basic.lookup.udir]: basic.md#basic.lookup.udir
|
| 1006 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 1007 |
[basic.lval]: basic.md#basic.lval
|
| 1008 |
[basic.namespace]: #basic.namespace
|
| 1009 |
[basic.scope]: basic.md#basic.scope
|
| 1010 |
+
[basic.scope.block]: basic.md#basic.scope.block
|
| 1011 |
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
| 1012 |
+
[basic.scope.pdecl]: basic.md#basic.scope.pdecl
|
| 1013 |
[basic.scope.proto]: basic.md#basic.scope.proto
|
| 1014 |
[basic.start]: basic.md#basic.start
|
| 1015 |
[basic.start.init]: basic.md#basic.start.init
|
| 1016 |
[basic.stc]: basic.md#basic.stc
|
| 1017 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
|
|
|
| 1035 |
[class.inhctor]: special.md#class.inhctor
|
| 1036 |
[class.init]: special.md#class.init
|
| 1037 |
[class.mem]: class.md#class.mem
|
| 1038 |
[class.member.lookup]: class.md#class.member.lookup
|
| 1039 |
[class.mfct]: class.md#class.mfct
|
|
|
|
| 1040 |
[class.name]: class.md#class.name
|
| 1041 |
[class.qual]: basic.md#class.qual
|
| 1042 |
[class.static]: class.md#class.static
|
| 1043 |
[class.static.data]: class.md#class.static.data
|
| 1044 |
[class.temporary]: special.md#class.temporary
|
|
|
|
| 1046 |
[class.union]: class.md#class.union
|
| 1047 |
[class.virtual]: class.md#class.virtual
|
| 1048 |
[conv]: conv.md#conv
|
| 1049 |
[conv.array]: conv.md#conv.array
|
| 1050 |
[conv.func]: conv.md#conv.func
|
| 1051 |
+
[conv.integral]: conv.md#conv.integral
|
| 1052 |
[conv.lval]: conv.md#conv.lval
|
| 1053 |
[conv.prom]: conv.md#conv.prom
|
| 1054 |
[conv.ptr]: conv.md#conv.ptr
|
| 1055 |
[dcl.align]: #dcl.align
|
| 1056 |
[dcl.ambig.res]: #dcl.ambig.res
|
| 1057 |
[dcl.array]: #dcl.array
|
| 1058 |
[dcl.asm]: #dcl.asm
|
| 1059 |
[dcl.attr]: #dcl.attr
|
| 1060 |
[dcl.attr.depend]: #dcl.attr.depend
|
| 1061 |
+
[dcl.attr.deprecated]: #dcl.attr.deprecated
|
| 1062 |
[dcl.attr.grammar]: #dcl.attr.grammar
|
| 1063 |
[dcl.attr.noreturn]: #dcl.attr.noreturn
|
| 1064 |
[dcl.constexpr]: #dcl.constexpr
|
| 1065 |
[dcl.dcl]: #dcl.dcl
|
| 1066 |
[dcl.decl]: #dcl.decl
|
|
|
|
| 1098 |
[except.throw]: except.md#except.throw
|
| 1099 |
[expr]: expr.md#expr
|
| 1100 |
[expr.alignof]: expr.md#expr.alignof
|
| 1101 |
[expr.ass]: expr.md#expr.ass
|
| 1102 |
[expr.call]: expr.md#expr.call
|
| 1103 |
+
[expr.cast]: expr.md#expr.cast
|
| 1104 |
+
[expr.comma]: expr.md#expr.comma
|
| 1105 |
+
[expr.cond]: expr.md#expr.cond
|
| 1106 |
[expr.const]: expr.md#expr.const
|
| 1107 |
[expr.const.cast]: expr.md#expr.const.cast
|
| 1108 |
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 1109 |
[expr.new]: expr.md#expr.new
|
| 1110 |
+
[expr.prim.lambda]: expr.md#expr.prim.lambda
|
| 1111 |
[expr.ref]: expr.md#expr.ref
|
| 1112 |
[expr.static.cast]: expr.md#expr.static.cast
|
| 1113 |
[expr.sub]: expr.md#expr.sub
|
| 1114 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 1115 |
[expr.unary]: expr.md#expr.unary
|
|
|
|
| 1120 |
[intro.multithread]: intro.md#intro.multithread
|
| 1121 |
[lex.charset]: lex.md#lex.charset
|
| 1122 |
[lex.digraph]: lex.md#lex.digraph
|
| 1123 |
[lex.key]: lex.md#lex.key
|
| 1124 |
[lex.name]: lex.md#lex.name
|
| 1125 |
+
[lex.string]: lex.md#lex.string
|
| 1126 |
[namespace.alias]: #namespace.alias
|
| 1127 |
[namespace.def]: #namespace.def
|
| 1128 |
[namespace.memdef]: #namespace.memdef
|
| 1129 |
[namespace.qual]: basic.md#namespace.qual
|
| 1130 |
[namespace.udecl]: #namespace.udecl
|
| 1131 |
[namespace.udir]: #namespace.udir
|
| 1132 |
[namespace.unnamed]: #namespace.unnamed
|
| 1133 |
[over]: over.md#over
|
|
|
|
| 1134 |
[over.match]: over.md#over.match
|
| 1135 |
[over.match.conv]: over.md#over.match.conv
|
| 1136 |
[over.match.copy]: over.md#over.match.copy
|
| 1137 |
[over.match.ctor]: over.md#over.match.ctor
|
| 1138 |
[over.match.list]: over.md#over.match.list
|
|
|
|
| 1148 |
[stmt.select]: stmt.md#stmt.select
|
| 1149 |
[stmt.stmt]: stmt.md#stmt.stmt
|
| 1150 |
[support.runtime]: language.md#support.runtime
|
| 1151 |
[tab:simple.type.specifiers]: #tab:simple.type.specifiers
|
| 1152 |
[temp]: temp.md#temp
|
|
|
|
| 1153 |
[temp.arg.type]: temp.md#temp.arg.type
|
| 1154 |
[temp.class.spec]: temp.md#temp.class.spec
|
| 1155 |
[temp.deduct.call]: temp.md#temp.deduct.call
|
| 1156 |
[temp.dep]: temp.md#temp.dep
|
| 1157 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
|
|
|
| 1166 |
|
| 1167 |
[^1]: The “implicit int” rule of C is no longer supported.
|
| 1168 |
|
| 1169 |
[^2]: The inline keyword has no effect on the linkage of a function.
|
| 1170 |
|
| 1171 |
+
[^3]: There is no special provision for a *decl-specifier-seq* that
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1172 |
lacks a *type-specifier* or that has a *type-specifier* that only
|
| 1173 |
specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
|
| 1174 |
supported.
|
| 1175 |
|
| 1176 |
+
[^4]: This set of values is used to define promotion and conversion
|
| 1177 |
semantics for the enumeration type. It does not preclude an
|
| 1178 |
expression of enumeration type from having a value that falls
|
| 1179 |
outside this range.
|
| 1180 |
|
| 1181 |
+
[^5]: Although entities in an unnamed namespace might have external
|
| 1182 |
linkage, they are effectively qualified by a name unique to their
|
| 1183 |
translation unit and therefore can never be seen from any other
|
| 1184 |
translation unit.
|
| 1185 |
|
| 1186 |
+
[^6]: this implies that the name of the class or function is
|
| 1187 |
unqualified.
|
| 1188 |
|
| 1189 |
+
[^7]: During name lookup in a class hierarchy, some ambiguities may be
|
| 1190 |
resolved by considering whether one member hides the other along
|
| 1191 |
some paths ([[class.member.lookup]]). There is no such
|
| 1192 |
disambiguation when considering the set of names found as a result
|
| 1193 |
of following *using-directive*s.
|
| 1194 |
|
| 1195 |
+
[^8]: A declaration with several declarators is usually equivalent to
|
| 1196 |
the corresponding sequence of declarations each with a single
|
| 1197 |
declarator. That is
|
| 1198 |
|
| 1199 |
`T D1, D2, ... Dn;`
|
| 1200 |
|
|
|
|
| 1224 |
`auto i = 1, j = 2.0; \textrm{// error: deduced types for \tcode{i} and \tcode{j} do not match}`
|
| 1225 |
as opposed to
|
| 1226 |
`auto i = 1; \textrm{// OK: \tcode{i} deduced to have type \tcode{int}}`
|
| 1227 |
`auto j = 2.0; \textrm{// OK: \tcode{j} deduced to have type \tcode{double}}`
|
| 1228 |
|
| 1229 |
+
[^9]: As indicated by syntax, cv-qualifiers are a significant component
|
| 1230 |
in function return types.
|
| 1231 |
|
| 1232 |
+
[^10]: This excludes parameters of type “ `T2`” where `T2` is “pointer
|
| 1233 |
to array of unknown bound of `T`” and where means any sequence of
|
| 1234 |
“pointer to” and “array of” derived declarator types. This exclusion
|
| 1235 |
applies to the parameters of the function, and if a parameter is a
|
| 1236 |
pointer to function or pointer to member function then to its
|
| 1237 |
parameters also, etc.
|
| 1238 |
|
| 1239 |
+
[^11]: One can explicitly disambiguate the parse either by introducing a
|
| 1240 |
comma (so the ellipsis will be parsed as part of the
|
| 1241 |
*parameter-declaration-clause*) or by introducing a name for the
|
| 1242 |
parameter (so the ellipsis will be parsed as part of the
|
| 1243 |
*declarator-id*).
|
| 1244 |
|
| 1245 |
+
[^12]: This means that default arguments cannot appear, for example, in
|
| 1246 |
declarations of pointers to functions, references to functions, or
|
| 1247 |
`typedef` declarations.
|
| 1248 |
|
| 1249 |
+
[^13]: Implementations are permitted to provide additional predefined
|
| 1250 |
variables with names that are reserved to the implementation (
|
| 1251 |
[[global.names]]). If a predefined variable is not odr-used (
|
| 1252 |
[[basic.def.odr]]), its string value need not be present in the
|
| 1253 |
program image.
|
| 1254 |
|
| 1255 |
+
[^14]: As specified in [[conv.ptr]], converting an integer literal
|
| 1256 |
+
whose value is `0` to a pointer type results in a null pointer
|
| 1257 |
+
value.
|
| 1258 |
|
| 1259 |
+
[^15]: The syntax provides for empty *initializer-list*s, but
|
| 1260 |
nonetheless C++does not have zero length arrays.
|
| 1261 |
|
| 1262 |
+
[^16]: This requires a conversion function ([[class.conv.fct]])
|
|
|
|
|
|
|
| 1263 |
returning a reference type.
|