Стандарт C++ 98 (1119566), страница 53
Текст из файла (страница 53)
]8Default constructors are called implicitly to create class objects of static or automatic storage duration(3.7.1, 3.7.2) defined without an initializer (8.5), are called to create class objects of dynamic storage duration (3.7.3) created by a new-expression in which the new-initializer is omitted (5.3.4), or are called whenthe explicit type conversion syntax (5.2.3) is used. A program is ill-formed if the default constructor for anobject is implicitly used and the constructor is not accessible (clause 11).9[Note: 12.6.2 describes the order in which constructors for base classes and non-static data members arecalled and describes how arguments can be specified for the calls to these constructors. ]10A copy constructor for a class X is a constructor with a first parameter of type X& or of type const X&.[Note: see 12.8 for more information on copy constructors.
]11A union member shall not be of a class type (or array thereof) that has a non-trivial constructor.12No return type (not even void) shall be specified for a constructor. A return statement in the body of aconstructor shall not specify a return value. The address of a constructor shall not be taken.13A functional notation type conversion (5.2.3) can be used to create new objects of its type.
[Note: The syntax looks like an explicit call of the constructor. ] [Example:complex zz = complex(1,2.3);cprint( complex(7.8,1.2) );—end example] An object created in this way is unnamed. [Note: 12.2 describes the lifetime of temporaryobjects. ] [Note: explicit constructor calls do not yield lvalues, see 3.10.
]14[Note: some language constructs have special semantics when used during construction; see 12.6.2 and12.7. ]15During the construction of a const object, if the value of the object or any of its subobjects is accessedthrough an lvalue that is not obtained, directly or indirectly, from the constructor’s this pointer, the valueof the object or subobject thus obtained is unspecified. [Example:struct C;void no_opt(C*);struct C {int c;C() : c(0) { no_opt(this); }};186© ISO/IECISO/IEC 14882:1998(E)12 Special member functions12.1 Constructorsconst C cobj;void no_opt(C* cptr) {int i = cobj.c * 100;cptr->c = 1;cout << cobj.c * 100<< ’\n’;}// value of cobj.c is unspecified// value of cobj.c is unspecified—end example]12.2 Temporary objects[class.temporary]1Temporaries of class type are created in various contexts: binding an rvalue to a reference (8.5.3), returningan rvalue (6.6.3), a conversion that creates an rvalue (4.1, 5.2.9, 5.2.11, 5.4), throwing an exception (15.1),entering a handler (15.3), and in some initializations (8.5).
[Note: the lifetime of exception objects isdescribed in 15.1. ] Even when the creation of the temporary object is avoided (12.8), all the semanticrestrictions must be respected as if the temporary object was created. [Example: even if the copy constructor is not called, all the semantic restrictions, such as accessibility (clause 11), shall be satisfied. ]2[Example:class X {// ...public:// ...X(int);X(const X&);~X();};X f(X);void g(){X a(1);X b = f(X(2));a = f(a);}Here, an implementation might use a temporary in which to construct X(2) before passing it to f() usingX’s copy-constructor; alternatively, X(2) might be constructed in the space used to hold the argument.Also, a temporary might be used to hold the result of f(X(2)) before copying it to b using X’s copyconstructor; alternatively, f()’s result might be constructed in b. On the other hand, the expressiona=f(a) requires a temporary for either the argument a or the result of f(a) to avoid undesired aliasing ofa.
]3When an implementation introduces a temporary object of a class that has a non-trivial constructor (12.1), itshall ensure that a constructor is called for the temporary object. Similarly, the destructor shall be called fora temporary with a non-trivial destructor (12.4). Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true evenif that evaluation ends in throwing an exception.4There are two contexts in which temporaries are destroyed at a different point than the end of the fullexpression.
The first context is when an expression appears as an initializer for a declarator defining anobject. In that context, the temporary that holds the result of the expression shall persist until the object’sinitialization is complete. The object is initialized from a copy of the temporary; during this copying, animplementation can call the copy constructor many times; the temporary is destroyed after it has beencopied, before or when the initialization completes. If many temporaries are created by the evaluation ofthe initializer, the temporaries are destroyed in reverse order of the completion of their construction.187ISO/IEC 14882:1998(E)12.2 Temporary objects5© ISO/IEC12 Special member functionsThe second context is when a reference is bound to a temporary.
The temporary to which the reference isbound or the temporary that is the complete object to a subobject of which the temporary is bound persistsfor the lifetime of the reference except as specified below. A temporary bound to a reference member in aconstructor’s ctor-initializer (12.6.2) persists until the constructor exits. A temporary bound to a referenceparameter in a function call (5.2.2) persists until the completion of the full expression containing the call.A temporary bound to the returned value in a function return statement (6.6.3) persists until the functionexits.
In all these cases, the temporaries created during the evaluation of the expression initializing the reference, except the temporary to which the reference is bound, are destroyed at the end of the full-expressionin which they are created and in the reverse order of the completion of their construction. If the lifetime oftwo or more temporaries to which references are bound ends at the same point, these temporaries aredestroyed at that point in the reverse order of the completion of their construction. In addition, thedestruction of temporaries bound to references shall take into account the ordering of destruction of objectswith static or automatic storage duration (3.7.1, 3.7.2); that is, if obj1 is an object with static or automaticstorage duration created before the temporary is created, the temporary shall be destroyed before obj1 isdestroyed; if obj2 is an object with static or automatic storage duration created after the temporary is created, the temporary shall be destroyed after obj2 is destroyed.
[Example:class C {// ...public:C();C(int);friend C operator+(const C&, const C&);~C();};C obj1;const C& cr = C(16)+C(23);C obj2;the expression C(16)+C(23) creates three temporaries. A first temporary T1 to hold the result of theexpression C(16), a second temporary T2 to hold the result of the expression C(23), and a third temporary T3 to hold the result of the addition of these two expressions.
The temporary T3 is then bound to thereference cr. It is unspecified whether T1 or T2 is created first. On an implementation where T1 is created before T2, it is guaranteed that T2 is destroyed before T1. The temporaries T1 and T2 are bound tothe reference parameters of operator+; these temporaries are destroyed at the end of the full expressioncontaining the call to operator+. The temporary T3 bound to the reference cr is destroyed at the end ofcr’s lifetime, that is, at the end of the program. In addition, the order in which T3 is destroyed takes intoaccount the destruction order of other objects with static storage duration. That is, because obj1 is constructed before T3, and T3 is constructed before obj2, it is guaranteed that obj2 is destroyed before T3,and that T3 is destroyed before obj1.
]12.3 Conversions[class.conv]1Type conversions of class objects can be specified by constructors and by conversion functions. These conversions are called user-defined conversions and are used for implicit type conversions (clause 4), forinitialization (8.5), and for explicit type conversions (5.4, 5.2.9).2User-defined conversions are applied only where they are unambiguous (10.2, 12.3.2). Conversions obeythe access control rules (clause 11). Access control is applied after ambiguity resolution (3.4).3[Note: See 13.3 for a discussion of the use of conversions in function calls as well as examples below.
]4At most one user-defined conversion (constructor or conversion function) is implicitly applied to a singlevalue. [Example:188© ISO/IEC12 Special member functionsISO/IEC 14882:1998(E)12.3 Conversionsclass X {// ...public:operator int();};class Y {// ...public:operator X();};Y a;int b = a;int c = X(a);// error:// a.operator X().operator int() not tried// OK: a.operator X().operator int()—end example]5User-defined conversions are used implicitly only if they are unambiguous. A conversion function in aderived class does not hide a conversion function in a base class unless the two functions convert to thesame type. Function overload resolution (13.3.3) selects the best conversion function to perform the conversion.