Стандарт C++ 98 (1119566), страница 43
Текст из файла (страница 43)
The applicable constructors are enumerated (13.3.1.3), and the best one ischosen through overload resolution (13.3). The constructor so selected is called to initialize theobject, with the initializer expression(s) as its argument(s). If no constructor applies, or the overloadresolution is ambiguous, the initialization is ill-formed.— Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequences thatcan convert from the source type to the destination type or (when a conversion function is used) to aderived class thereof are enumerated as described in 13.3.1.4, and the best one is chosen throughoverload resolution (13.3). If the conversion cannot be done or is ambiguous, the initialization isill-formed. The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the destination type.
The result of the call(which is the temporary for the constructor case) is then used to direct-initialize, according to therules above, the object that is the destination of the copy-initialization. In certain cases, an implementation is permitted to eliminate the copying inherent in this direct-initialization by constructingthe intermediate result directly into the object being initialized; see 12.2, 12.8.— Otherwise, if the source type is a (possibly cv-qualified) class type, conversion functions are considered.The applicable conversion functions are enumerated (13.3.1.5), and the best one is chosen through overload resolution (13.3).
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]143ISO/IEC 14882:1998(E)© ISO/IEC8.5.1 Aggregates8 Declarators8.5.1 Aggregates[dcl.init.aggr]1An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), 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 the initializer can be an initializer-clause consisting of a brace-enclosed,comma-separated list of initializers for the members of the aggregate, written in increasing subscript ormember order.
If the aggregate contains subaggregates, this rule applies recursively to the members of thesubaggregate. [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. ]3An aggregate that is a class can also be initialized with a single expression not enclosed in braces, asdescribed in 8.5.4An array of unknown size initialized with a brace-enclosed initializer-list containing n initializers, where nshall 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. ] An empty initializer list {} shall not be used as the initializer for an array ofunknown bound.91)5Static data members are not considered members of the class for purposes of aggregate initialization.[Example:struct A {int i;static int s;int j;} a = { 1, 2 };Here, the second initializer 2 initializes a.j and not the static data member A::s. ]6An initializer-list is ill-formed if the number of initializers exceeds the number of members or elements toinitialize.
[Example:char cv[4] = { ’a’, ’s’, ’d’, ’f’, 0 };// erroris ill-formed. ]7If there are fewer initializers in the list than there are members in the aggregate, then each member notexplicitly initialized shall be default-initialized (8.5). [Example:struct S { int a; 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 formint(), that is, 0. ]8An initializer for an aggregate member that is an empty class shall have the form of an empty initializer-list{}.
[Example:__________________91) The syntax provides for empty initializer-lists, but nonetheless C++ does not have zero length arrays.144© ISO/IECISO/IEC 14882:1998(E)8 Declarators8.5.1 Aggregatesstruct S { };struct A {S s;int i;} a = { { } , 3 };—end example] An empty initializer-list can be used to initialize any aggregate. If the aggregate is not anempty class, then each member of the aggregate shall be initialized with a value of the form T() (5.2.3),where T represents the type of the uninitialized member.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 initializers 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. ]11Braces can be elided in an initializer-list as follows. If the initializer-list begins with a left brace, then thesucceeding comma-separated list of initializers initializes the members of a subaggregate; it is erroneousfor there to be more initializers than members. If, however, the initializer-list for a subaggregate does notbegin with a left brace, then only enough initializers from the list are taken to initialize the members of thesubaggregate; any remaining initializers are left to initialize the next member of the aggregate of which thecurrent subaggregate is a member.
[Example:float{{{};y[4][3]1, 3, 52, 4, 63, 5, 7= {},},},is a completely-braced initialization: 1, 3, and 5 initialize the first row of the array y[0], namelyy[0][0], y[0][1], and y[0][2]. Likewise the next two lines initialize y[1] and y[2]. The initializer ends early and therefore y[3]’s elements are initialized as if explicitly initialized with an expressionof the form float(), that is, are initialized with 0.0. In the following example, braces in the initializerlist are elided; however the initializer-list has the same effect as the completely-braced initializer-list of theabove 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 an initializer from an initializer-list. If the initializer can initialize a member, the member is initialized. Otherwise, if the member is itself a non-empty subaggregate, brace elision is assumed and the initializer is considered for the initialization of the first member of the subaggregate.145ISO/IEC 14882:1998(E)© ISO/IEC8.5.1 Aggregates8 Declarators[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 for b.a1.i. b.a1.i is initialized with 4, b.a2 is initialized witha, b.z is initialized with whatever a.operator int() returns.
]13[Note: An aggregate array or an aggregate class may contain members of a class type with a user-declaredconstructor (12.1). Initialization of these aggregate objects is described in 12.6.1. ]14When an aggregate with static storage duration is initialized with a brace-enclosed initializer-list, if all themember initializer expressions are constant expressions, and the aggregate is a POD type, the initializationshall be done during the static phase of initialization (3.6.2); otherwise, it is unspecified whether the initialization of members with constant expressions takes place during the static phase or during the dynamicphase of initialization.15When a union is initialized with a brace-enclosed initializer, the braces shall only contain an initializer forthe first member of the union. [Example:union u { int a; char* b; };uuuuuabcde====={ 1 };a;1;{ 0, "asdf" };{ "asdf" };// error// error// error—end example] [Note: as described above, the braces around the initializer for a union member can beomitted if the union is a member of another aggregate.