Стандарт C++ 11 (1119564), страница 61
Текст из файла (страница 61)
move-only, by using deleted definitions of the copyconstructor and copy assignment operator, and then providing defaulted definitions of the move constructorand move assignment operator.struct moveonly {moveonly() = default;moveonly(const moveonly&) = delete;moveonly(moveonly&&) = default;moveonly& operator=(const moveonly&) = delete;moveonly& operator=(moveonly&&) = default;~moveonly() = default;};moveonly *p;moveonly q(*p); // error, deleted copy constructor§ 8.4.3© ISO/IEC 2011 – All rights reserved201ISO/IEC 14882:2011(E)— end example ]4A deleted function is implicitly inline.
[ Note: The one-definition rule (3.2) applies to deleted definitions.— end note ] A deleted definition of a function shall be the first declaration of the function or, for an explicitspecialization of a function template, the first declaration of that specialization. [ Example:struct sometype {sometype();};sometype::sometype() = delete;// ill-formed; not first declaration— end example ]8.51Initializers[dcl.init]A declarator can specify an initial value for the identifier being declared. The identifier designates a variablebeing initialized. The process of initialization described in the remainder of 8.5 applies also to initializations specified by other syntactic contexts, such as the initialization of function parameters with argumentexpressions (5.2.2) or the initialization of return values (6.6.3).initializer:brace-or-equal-initializer( expression-list )brace-or-equal-initializer:= initializer-clausebraced-init-listinitializer-clause:assignment-expressionbraced-init-listinitializer-list:initializer-clause ...optinitializer-list , initializer-clause ...optbraced-init-list:{ initializer-list ,opt }{}2Except for objects declared with the constexpr specifier, for which see 7.1.5, an initializer in the definitionof a variable can consist of arbitrary expressions involving literals and previously declared variables andfunctions, regardless of the variable’s storage duration.
[ Example:intintintintf(int);a = 2;b = f(a);c(b);— end example ]3[ Note: Default arguments are more restricted; see 8.3.6.4The order of initialization of variables with static storage duration is described in 3.6 and 6.7. — end note ]5To zero-initialize an object or reference of type T means:— if T is a scalar type (3.9), the object is set to the value 0 (zero), taken as an integral constant expression,converted to T;103103) As specified in 4.10, converting an integral constant expression whose value is 0 to a pointer type results in a null pointervalue.§ 8.5202© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)— if T is a (possibly cv-qualified) non-union class type, each non-static data member and each base-classsubobject is zero-initialized and padding is initialized to zero bits;— if T is a (possibly cv-qualified) union type, the object’s first non-static named data member is zeroinitialized and padding is initialized to zero bits;— if T is an array type, each element is zero-initialized;— if T is a reference type, no initialization is performed.6To default-initialize an object of type T means:— if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and theinitialization is ill-formed if T has no accessible default constructor);— if T is an array type, each element is default-initialized;— otherwise, no initialization is performed.If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class typewith a user-provided default constructor.7To value-initialize an object of type T means:— if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then thedefault constructor for T is called (and the initialization is ill-formed if T has no accessible defaultconstructor);— if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the objectis zero-initialized and, if T’s implicitly-declared default constructor is non-trivial, that constructor iscalled.— if T is an array type, then each element is value-initialized;— otherwise, the object is zero-initialized.An object that is value-initialized is deemed to be constructed and thus subject to provisions of this International Standard applying to “constructed” objects, objects “for which the constructor has completed,” etc.,even if no constructor is invoked for the object’s initialization.8A program that calls for default-initialization or value-initialization of an entity of reference type is ill-formed.9[ Note: Every object of static storage duration is zero-initialized at program startup before any other initialization takes place.
In some cases, additional initialization is done later. — end note ]10An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.[ Note: Since () is not permitted by the syntax for initializer,X a();is not the declaration of an object of class X, but the declaration of a function taking no argument andreturning an X. The form () is permitted in certain other initialization contexts (5.3.4, 5.2.3, 12.6.2). — endnote ]11If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, anobject with automatic or dynamic storage duration has indeterminate value. [ Note: Objects with static orthread storage duration are zero-initialized, see 3.6.2.
— end note ]12An initializer for a static member is in the scope of the member’s class. [ Example:§ 8.5© ISO/IEC 2011 – All rights reserved203ISO/IEC 14882:2011(E)int a;struct X {static int a;static int b;};int X::a = 1;int X::b = a;// X::b = X::a— end example ]13The form of initialization (using parentheses or =) is generally insignificant, but does matter when theinitializer or the entity being initialized has a class type; see below. If the entity being initialized does nothave class type, the expression-list in a parenthesized initializer shall be a single expression.14The initialization that occurs in the formT x = a;as well as in argument passing, function return, throwing an exception (15.1), handling an exception (15.3),and aggregate member initialization (8.5.1) is called copy-initialization.
[ Note: Copy-initialization mayinvoke a move (12.8). — end note ]15The initialization that occurs in the formsT x(a);T x{a};as well as in new expressions (5.3.4), static_cast expressions (5.2.9), functional notation type conversions(5.2.3), and base and member initializers (12.6.2) is called direct-initialization.16The semantics of initializers are as follows. The destination type is the type of the object or reference beinginitialized and the source type is the type of the initializer expression.
If the initializer is not a single (possiblyparenthesized) expression, the source type is not defined.— If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized (8.5.4).— If the destination type is a reference type, see 8.5.3.— If the destination type is an array of characters, an array of char16_t, an array of char32_t, or anarray of wchar_t, and the initializer is a string literal, see 8.5.2.— If the initializer is (), the object is value-initialized.— Otherwise, if the destination type is an array, the program is ill-formed.— If the destination type is a (possibly cv-qualified) class type:— If the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualifiedversion of the source type is the same class as, or a derived class of, the class of the destination,constructors are considered.
The applicable constructors are enumerated (13.3.1.3), and the bestone is chosen through overload resolution (13.3). The constructor so selected is called to initializethe object, with the initializer expression or expression-list as its argument(s). If no constructorapplies, or the overload resolution is ambiguous, the initialization is ill-formed.— Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequencesthat can convert from the source type to the destination type or (when a conversion functionis used) to a derived class thereof are enumerated as described in 13.3.1.4, and the best one is§ 8.5204© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)chosen through overload resolution (13.3).
If the conversion cannot be done or is ambiguous, theinitialization is ill-formed. The function selected is called with the initializer expression as itsargument; if the function is a constructor, the call initializes a temporary of the cv-unqualifiedversion of the destination type. The temporary is a prvalue. The result of the call (which is thetemporary for the constructor case) is then used to direct-initialize, according to the rules above,the object that is the destination of the copy-initialization. In certain cases, an implementationis permitted to eliminate the copying inherent in this direct-initialization by constructing theintermediate 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 throughoverload resolution (13.3).