Стандарт C++ 11 (1119564), страница 62
Текст из файла (страница 62)
The user-defined conversion so selected is called to convert the initializerexpression into the object being initialized. If the conversion cannot be done or is ambiguous, theinitialization is ill-formed.— Otherwise, the initial value of the object being initialized is the (possibly converted) value of the initializer expression. Standard conversions (Clause 4) will be used, if necessary, to convert the initializerexpression to the cv-unqualified version of the destination type; no user-defined conversions are considered. If the conversion cannot be done, the initialization is ill-formed. [ Note: An expression of type“cv1 T” can initialize an object of type “cv2 T” independently of the cv-qualifiers cv1 and cv2.int a;const int b = a;int c = b;— end note ]17An initializer-clause followed by an ellipsis is a pack expansion (14.5.3).8.5.1Aggregates[dcl.init.aggr]1An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equalinitializers for non-static data members (9.2), no private or protected non-static data members (Clause 11),no base classes (Clause 10), and no virtual functions (10.3).2When an aggregate is initialized by an initializer list, as specified in 8.5.4, the elements of the initializer listare taken as initializers for the members of the aggregate, in increasing subscript or member order.
Eachmember is copy-initialized from the corresponding initializer-clause. If the initializer-clause is an expressionand a narrowing conversion (8.5.4) is required to convert the expression, the program is ill-formed. [ Note:If an initializer-clause is itself an initializer list, the member is list-initialized, which will result in a recursiveapplication of the rules in this section if the member is an aggregate. — end note ] [ Example:struct A {int x;struct B {int i;int j;} b;} a = { 1, { 2, 3 } };initializes a.x with 1, a.b.i with 2, a.b.j with 3.
— end example ]3An aggregate that is a class can also be initialized with a single expression not enclosed in braces, as describedin 8.5.§ 8.5.1© ISO/IEC 2011 – All rights reserved205ISO/IEC 14882:2011(E)4An array of unknown size initialized with a brace-enclosed initializer-list containing n initializer-clauses,where n shall be greater than zero, is defined as having n elements (8.3.4). [ Example:int x[] = { 1, 3, 5 };declares and initializes x as a one-dimensional array that has three elements since no size was specified andthere are three initializers. — end example ] An empty initializer list {} shall not be used as the initializerclause for an array of unknown bound.1045Static data members and anonymous bit-fields are not considered members of the class for purposes ofaggregate initialization.
[ Example:struct A {int i;static int s;int j;int :17;int k;} a = { 1, 2, 3 };Here, the second initializer 2 initializes a.j and not the static data member A::s, and the third initializer3 initializes a.k and not the anonymous bit-field before it. — end example ]6An initializer-list is ill-formed if the number of initializer-clauses exceeds the number of members or elementsto initialize.
[ Example:char cv[4] = { ’a’, ’s’, ’d’, ’f’, 0 };// erroris ill-formed. — end example ]7If there are fewer initializer-clauses in the list than there are members in the aggregate, then each membernot explicitly initialized shall be initialized from an empty initializer list (8.5.4). [ Example:struct S { int a; const char* b; int c; };S ss = { 1, "asdf" };initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an expression of the form int(), thatis, 0. — end example ]8If an aggregate class C contains a subaggregate member m that has no members for purposes of aggregateinitialization, the initializer-clause for m shall not be omitted from an initializer-list for an object of type Cunless the initializer-clauses for all members of C following m are also omitted.
[ Example:struct S { } s;struct A {S s1;int i1;S s2;int i2;S s3;int i3;} a = {{ },// Required initialization0,s,// Required initialization0};// Initialization not required for A::s3 because A::i3 is also not initialized104) The syntax provides for empty initializer-lists, but nonetheless C++ does not have zero length arrays.§ 8.5.1206© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)— end example ]9If an incomplete or empty initializer-list leaves a member of reference type uninitialized, the program isill-formed.10When initializing a multi-dimensional array, the initializer-clauses initialize the elements with the last (rightmost) index of the array varying the fastest (8.3.4). [ Example:int x[2][2] = { 3, 1, 4, 2 };initializes x[0][0] to 3, x[0][1] to 1, x[1][0] to 4, and x[1][1] to 2.
On the other hand,float y[4][3] = {{ 1 }, { 2 }, { 3 }, { 4 }};initializes the first column of y (regarded as a two-dimensional array) and leaves the rest zero. — endexample ]11In a declaration of the formT x = { a };braces can be elided in an initializer-list as follows.105 If the initializer-list begins with a left brace, thenthe succeeding comma-separated list of initializer-clauses initializes the members of a subaggregate; it iserroneous for there to be more initializer-clauses than members.
If, however, the initializer-list for a subaggregate does not begin with a left brace, then only enough initializer-clauses from the list are taken toinitialize the members of the subaggregate; any remaining initializer-clauses are left to initialize the nextmember of the aggregate of which the current subaggregate is a member. [ Example:float y[4][3] = {{ 1, 3, 5 },{ 2, 4, 6 },{ 3, 5, 7 },};is a completely-braced initialization: 1, 3, and 5 initialize the first row of the array y[0], namely y[0][0],y[0][1], and y[0][2]. Likewise the next two lines initialize y[1] and y[2].
The initializer ends early andtherefore y[3]s elements are initialized as if explicitly initialized with an expression of the form float(),that is, are initialized with 0.0. In the following example, braces in the initializer-list are elided; howeverthe initializer-list has the same effect as the completely-braced initializer-list of the above example,float y[4][3] = {1, 3, 5, 2, 4, 6, 3, 5, 7};The initializer for y begins with a left brace, but the one for y[0] does not, therefore three elements fromthe list are used.
Likewise the next three are taken successively for y[1] and y[2]. — end example ]12All implicit type conversions (Clause 4) are considered when initializing the aggregate member with anassignment-expression. If the assignment-expression can initialize a member, the member is initialized.Otherwise, if the member is itself a subaggregate, brace elision is assumed and the assignment-expressionis considered for the initialization of the first member of the subaggregate. [ Note: As specified above,brace elision cannot apply to subaggregates with no members for purposes of aggregate initialization; aninitializer-clause for the entire subobject is required.
— end note ]105) Braces cannot be elided in other uses of list-initialization.§ 8.5.1© ISO/IEC 2011 – All rights reserved207ISO/IEC 14882:2011(E)[ Example:struct A {int i;operator int();};struct B {A a1, a2;int z;};A a;B b = { 4, a, a };Braces are elided around the initializer-clause for b.a1.i. b.a1.i is initialized with 4, b.a2 is initializedwith a, b.z is initialized with whatever a.operator int() returns. — end example ]13[ Note: An aggregate array or an aggregate class may contain members of a class type with a user-providedconstructor (12.1). Initialization of these aggregate objects is described in 12.6.1.
— end note ]14[ Note: Whether the initialization of aggregates with static storage duration is static or dynamic is specifiedin 3.6.2 and 6.7. — end note ]15When a union is initialized with a brace-enclosed initializer, the braces shall only contain an initializer-clausefor the first non-static data member of the union. [ Example:unionu a =u b =u c =u d =u e =u { int a; const char* b; };{ 1 };a;1;// error{ 0, "asdf" };// error{ "asdf" };// error— end example ]16[ Note: As described above, the braces around the initializer-clause for a union member can be omitted ifthe union is a member of another aggregate. — end note ]8.5.21Character arrays[dcl.init.string]A char array (whether plain char, signed char, or unsigned char), char16_t array, char32_t array, orwchar_t array can be initialized by a narrow character literal, char16_t string literal, char32_t stringliteral, or wide string literal, respectively, or by an appropriately-typed string literal enclosed in braces.Successive characters of the value of the string literal initialize the elements of the array.
[ Example:char msg[] = "Syntax error on line %s\n";shows a character array whose members are initialized with a string-literal. Note that because ’\n’ is asingle character and because a trailing ’\0’ is appended, sizeof(msg) is 25. — end example ]2There shall not be more initializers than there are array elements. [ Example:char cv[4] = "asdf";// erroris ill-formed since there is no space for the implied trailing ’\0’. — end example ]§ 8.5.2208© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)3If there are fewer initializers than there are array elements, each element not explicitly initialized shall bezero-initialized (8.5).8.5.31References[dcl.init.ref ]A variable declared to be a T& or T&&, that is, “reference to type T” (8.3.2), shall be initialized by an object,or function, of type T or by an object that can be converted into a T.