Стандарт C++ 11 (1119564), страница 64
Текст из файла (страница 64)
[ Note: As usual, the binding will fail and the programis ill-formed if the reference type is an lvalue reference to a non-const type. — end note ][ Example:struct S {S(std::initializer_list<double>);S(const std::string&);// ...};const S& r1 = { 1, 2, 3.0 };const S& r2 { "Spinach" };S& r3 = { 1, 2, 3 };const int& i1 = { 1 };const int& i2 = { 1.1 };const int (&iar)[2] = { 1, 2 };// #1// #2////////////OK: invoke #1OK: invoke #2error: initializer is not an lvalueOKerror: narrowingOK: iar is bound to temporary array— end example ]— Otherwise, if the initializer list has a single element, the object or reference is initialized from thatelement; if a narrowing conversion (see below) is required to convert the element to T, the program isill-formed.[ Example:int x1 {2};int x2 {2.0};// OK// error: narrowing— end example ]§ 8.5.4© ISO/IEC 2011 – All rights reserved213ISO/IEC 14882:2011(E)— Otherwise, if the initializer list has no elements, the object is value-initialized.[ Example:int** pp {};// initialized to null pointer— end example ]— Otherwise, the program is ill-formed.[ Example:struct A { int i; int j; };A a1 { 1, 2 };A a2 { 1.2 };struct B {B(std::initializer_list<int>);};B b1 { 1, 2 };B b2 { 1, 2.0 };struct C {C(int i, double j);};C c1 = { 1, 2.2 };C c2 = { 1.1, 2 };int j { 1 };int k { };// aggregate initialization// error: narrowing// creates initializer_list<int> and calls constructor// error: narrowing// calls constructor with arguments (1, 2.2)// error: narrowing// initialize to 1// initialize to 0— end example ]4Within the initializer-list of a braced-init-list, the initializer-clauses, including any that result from packexpansions (14.5.3), are evaluated in the order in which they appear.
That is, every value computation andside effect associated with a given initializer-clause is sequenced before every value computation and sideeffect associated with any initializer-clause that follows it in the comma-separated list of the initializer-list.[ Note: This evaluation ordering holds regardless of the semantics of the initialization; for example, it applieswhen the elements of the initializer-list are interpreted as arguments of a constructor call, even thoughordinarily there are no sequencing constraints on the arguments of a call. — end note ]5An object of type std::initializer_list<E> is constructed from an initializer list as if the implementationallocated an array of N elements of type E, where N is the number of elements in the initializer list.Each element of that array is copy-initialized with the corresponding element of the initializer list, andthe std::initializer_list<E> object is constructed to refer to that array.
If a narrowing conversion isrequired to initialize any of the elements, the program is ill-formed.[ Example:struct X {X(std::initializer_list<double> v);};X x{ 1,2,3 };The initialization will be implemented in a way roughly equivalent to this:double __a[3] = {double{1}, double{2}, double{3}};X x(std::initializer_list<double>(__a, __a+3));assuming that the implementation can construct an initializer_list object with a pair of pointers. — endexample ]§ 8.5.4214© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)6The lifetime of the array is the same as that of the initializer_list object. [ Example:typedef std::complex<double> cmplx;std::vector<cmplx> v1 = { 1, 2, 3 };void f() {std::vector<cmplx> v2{ 1, 2, 3 };std::initializer_list<int> i3 = { 1, 2, 3 };}For v1 and v2, the initializer_list object and array created for { 1, 2, 3 } have full-expression lifetime.For i3, the initializer_list object and array have automatic lifetime.
— end example ] [ Note: Theimplementation is free to allocate the array in read-only memory if an explicit array with the same initializercould be so allocated. — end note ]7A narrowing conversion is an implicit conversion— from a floating-point type to an integer type, or— from long double to double or float, or from double to float, except where the source is a constantexpression and the actual value after conversion is within the range of values that can be represented(even if it cannot be represented exactly), or— from an integer type or unscoped enumeration type to a floating-point type, except where the sourceis a constant expression and the actual value after conversion will fit into the target type and willproduce the original value when converted back to the original type, or— from an integer type or unscoped enumeration type to an integer type that cannot represent all thevalues of the original type, except where the source is a constant expression and the actual value afterconversion will fit into the target type and will produce the original value when converted back to theoriginal type.[ Note: As indicated above, such conversions are not allowed at the top level in list-initializations.
— endnote ] [ Example:int x = 999;const int y = 999;const int z = 99;char c1 = x;char c2{x};char c3{y};char c4{z};unsigned char uc1 = {5};unsigned char uc2 = {-1};unsigned int ui1 = {-1};signed int si1 ={ (unsigned int)-1 };int ii = {2.0};float f1 { x };float f2 { 7 };int f(int);int a[] ={ 2, f(2), f(2.0) };// x is not a constant expression//////////////OK, though it might narrow (in this case, it does narrow)error: might narrowerror: narrows (assuming char is 8 bits)OK: no narrowing neededOK: no narrowing needederror: narrowserror: narrows////////error: narrowserror: narrowserror: might narrowOK: 7 can be exactly represented as a float// OK: the double-to-int conversion is not at the top level— end example ]§ 8.5.4© ISO/IEC 2011 – All rights reserved215ISO/IEC 14882:2011(E)91Classes[class]A class is a type. Its name becomes a class-name (9.1) within its scope.class-name:identifiersimple-template-idClass-specifiers and elaborated-type-specifiers (7.1.6.3) are used to make class-names.
An object of a classconsists of a (possibly empty) sequence of members and base class objects.class-specifier:class-head { member-specificationopt }class-head:class-key attribute-specifier-seqopt class-head-name class-virt-specifieropt base-clauseoptclass-key attribute-specifier-seqopt base-clauseoptclass-head-name:nested-name-specifieropt class-nameclass-virt-specifier:finalclass-key:classstructunionA class-specifier whose class-head omits the class-head-name defines an unnamed class. [ Note: An unnamedclass thus can’t be final. — end note ]2A class-name is inserted into the scope in which it is declared immediately after the class-name is seen.The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name.For purposes of access checking, the injected-class-name is treated as if it were a public member name.
Aclass-specifier is commonly referred to as a class definition. A class is considered defined after the closingbrace of its class-specifier has been seen even though its member functions are in general not yet defined.The optional attribute-specifier-seq appertains to the class; the attributes in the attribute-specifier-seq arethereafter considered attributes of the class whenever it is named.3If a class is marked with the class-virt-specifier final and it appears as a base-type-specifier in a base-clause(Clause 10), the program is ill-formed.4Complete objects and member subobjects of class type shall have nonzero size.107 [ Note: Class objects canbe assigned, passed as arguments to functions, and returned by functions (except objects of classes for whichcopying or moving has been restricted; see 12.8).
Other plausible operators, such as equality comparison,can be defined by the user; see 13.5. — end note ]5A union is a class defined with the class-key union; it holds only one data member at a time (9.5). [ Note:Aggregates of class type are described in 8.5.1. — end note ]6A trivially copyable class is a class that:— has no non-trivial copy constructors (12.8),107) Base class subobjects are not so constrained.216© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)— has no non-trivial move constructors (12.8),— has no non-trivial copy assignment operators (13.5.3, 12.8),— has no non-trivial move assignment operators (13.5.3, 12.8), and— has a trivial destructor (12.4).A trivial class is a class that has a trivial default constructor (12.1) and is trivially copyable.[ Note: In particular, a trivially copyable or trivial class does not have virtual functions or virtual baseclasses. — end note ]7A standard-layout class is a class that:— has no non-static data members of type non-standard-layout class (or array of such types) or reference,— has no virtual functions (10.3) and no virtual base classes (10.1),— has the same access control (Clause 11) for all non-static data members,— has no non-standard-layout base classes,— either has no non-static data members in the most derived class and at most one base class withnon-static data members, or has no base classes with non-static data members, and— has no base classes of the same type as the first non-static data member.1088A standard-layout struct is a standard-layout class defined with the class-key struct or the class-key class.A standard-layout union is a standard-layout class defined with the class-key union.9[ Note: Standard-layout classes are useful for communicating with code written in other programming languages.