Стандарт C++ 11 (1119564), страница 75
Текст из файла (страница 75)
An implicitly-declared copy/moveconstructor is not an explicit constructor; it may be called for implicit type conversions.12.3.21Conversion functions[class.conv.fct]A member function of a class X having no parameters with a name of the formconversion-function-id:operator conversion-type-idconversion-type-id:type-specifier-seq conversion-declaratoroptconversion-declarator:ptr-operator conversion-declaratoropt§ 12.3.2© ISO/IEC 2011 – All rights reserved263ISO/IEC 14882:2011(E)specifies a conversion from X to the type specified by the conversion-type-id.
Such functions are calledconversion functions. No return type can be specified. If a conversion function is a member function, thetype of the conversion function (8.3.5) is “function taking no parameter returning conversion-type-id”. Aconversion function is never used to convert a (possibly cv-qualified) object to the (possibly cv-qualified)same object type (or a reference to it), to a (possibly cv-qualified) base class of that type (or a reference toit), or to (possibly cv-qualified) void.116[ Example:struct X {operator int();};void f(X a) {int i = int(a);i = (int)a;i = a;}In all three cases the value assigned will be converted by X::operator int().
— end example ]2A conversion function may be explicit (7.1.2), in which case it is only considered as a user-defined conversionfor direct-initialization (8.5). Otherwise, user-defined conversions are not restricted to use in assignmentsand initializations. [ Example:class Y { };struct Z {explicit operator Y() const;};void h(Z z) {Y y1(z);Y y2 = z;Y y3 = (Y)z;}// OK: direct-initialization// ill-formed: copy-initialization// OK: cast notationvoid g(X a, X b) {int i = (a) ? 1+a : 0;int j = (a&&b) ? a+b : i;if (a) {}}— end example ]3The conversion-type-id shall not represent a function type nor an array type. The conversion-type-id ina conversion-function-id is the longest possible sequence of conversion-declarators.
[ Note: This preventsambiguities between the declarator operator * and its expression counterparts. [ Example:&ac.operator int*i; // syntax error:// parsed as: &(ac.operator int *)i// not as: &(ac.operator int)*i116) These conversions are considered as standard conversions for the purposes of overload resolution (13.3.3.1, 13.3.3.1.4) andtherefore initialization (8.5) and explicit casts (5.2.9). A conversion to void does not invoke any conversion function (5.2.9).Even though never directly called to perform a conversion, such conversion functions can be declared and can potentially bereached through a call to a virtual conversion function in a base class.§ 12.3.2264© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)The * is the pointer declarator and not the multiplication operator.
— end example ] — end note ]4Conversion functions are inherited.5Conversion functions can be virtual.6Conversion functions cannot be declared static.12.4Destructors[class.dtor]1A special declarator syntax using an optional function-specifier (7.1.2) followed by ˜ followed by the destructor’s class name followed by an empty parameter list is used to declare the destructor in a class definition.In such a declaration, the ˜ followed by the destructor’s class name can be enclosed in optional parentheses;such parentheses are ignored. A typedef-name shall not be used as the class-name following the ∼ in thedeclarator for a destructor declaration.2A destructor is used to destroy objects of its class type.
A destructor takes no parameters, and no return typecan be specified for it (not even void). The address of a destructor shall not be taken. A destructor shallnot be static. A destructor can be invoked for a const, volatile or const volatile object. A destructorshall not be declared const, volatile or const volatile (9.3.2). const and volatile semantics (7.1.6.1)are not applied on an object under destruction. They stop being in effect when the destructor for the mostderived object (1.8) starts. A destructor shall not be declared with a ref-qualifier.3A declaration of a destructor that does not have an exception-specification is implicitly considered to havethe same exception-specification as an implicit declaration (15.4).4If a class has no user-declared destructor, a destructor is implicitly declared as defaulted (8.4). An implicitlydeclared destructor is an inline public member of its class.5A defaulted destructor for a class X is defined as deleted if:— X is a union-like class that has a variant member with a non-trivial destructor,— any of the non-static data members has class type M (or array thereof) and M has a deleted destructoror a destructor that is inaccessible from the defaulted destructor,— any direct or virtual base class has a deleted destructor or a destructor that is inaccessible from thedefaulted destructor,— or, for a virtual destructor, lookup of the non-array deallocation function results in an ambiguity or ina function that is deleted or inaccessible from the defaulted destructor.A destructor is trivial if it is not user-provided and if:— the destructor is not virtual,— all of the direct base classes of its class have trivial destructors, and— for all of the non-static data members of its class that are of class type (or array thereof), each suchclass has a trivial destructor.Otherwise, the destructor is non-trivial.6A destructor that is defaulted and not defined as deleted is implicitly defined when it is odr-used (3.2) todestroy an object of its class type (3.7) or when it is explicitly defaulted after its first declaration.7Before the defaulted destructor for a class is implicitly defined, all the non-user-provided destructors for itsbase classes and its non-static data members shall have been implicitly defined.8After executing the body of the destructor and destroying any automatic objects allocated within the body, adestructor for class X calls the destructors for X’s direct non-variant non-static data members, the destructors§ 12.4© ISO/IEC 2011 – All rights reserved265ISO/IEC 14882:2011(E)for X’s direct base classes and, if X is the type of the most derived class (12.6.2), its destructor calls thedestructors for X’s virtual base classes.
All destructors are called as if they were referenced with a qualifiedname, that is, ignoring any possible virtual overriding destructors in more derived classes. Bases andmembers are destroyed in the reverse order of the completion of their constructor (see 12.6.2). A returnstatement (6.6.3) in a destructor might not directly return to the caller; before transferring control to thecaller, the destructors for the members and bases are called.
Destructors for elements of an array are calledin reverse order of their construction (see 12.6).9A destructor can be declared virtual (10.3) or pure virtual (10.4); if any objects of that class or anyderived class are created in the program, the destructor shall be defined. If a class has a base class with avirtual destructor, its destructor (whether user- or implicitly-declared) is virtual.10[ Note: some language constructs have special semantics when used during destruction; see 12.7.
— endnote ]11Destructors are invoked implicitly— for constructed objects with static storage duration (3.7.1) at program termination (3.6.3),— for constructed objects with thread storage duration (3.7.2) at thread exit,— for constructed objects with automatic storage duration (3.7.3) when the block in which an object iscreated exits (6.7),— for constructed temporary objects when the lifetime of a temporary object ends (12.2),— for constructed objects allocated by a new-expression (5.3.4), through use of a delete-expression (5.3.5),— in several situations due to the handling of exceptions (15.3).A program is ill-formed if an object of class type or array thereof is declared and the destructor for the classis not accessible at the point of the declaration. Destructors can also be invoked explicitly.12At the point of definition of a virtual destructor (including an implicit definition (12.8)), the non-arraydeallocation function is looked up in the scope of the destructor’s class (10.2), and, if no declaration isfound, the function is looked up in the global scope.
If the result of this lookup is ambiguous or inaccessible,or if the lookup selects a placement deallocation function or a function with a deleted definition (8.4), theprogram is ill-formed. [ Note: This assures that a deallocation function corresponding to the dynamic typeof an object is available for the delete-expression (12.5). — end note ]13In an explicit destructor call, the destructor name appears as a ˜ followed by a type-name or decltypespecifier that denotes the destructor’s class type.