Стандарт C++ 98 (1119566), страница 58
Текст из файла (страница 58)
Template assignment operators participate in overload resolution with otherassignment operators, including copy assignment operators, and a template assignment operator may be used to assign an object if itprovides a better match than other assignment operators.205ISO/IEC 14882:1998(E)© ISO/IEC12.8 Copying class objects12 Special member functions— each direct base class B of X has a copy assignment operator whose parameter is of type const B&,const volatile B& or B, and— for all the nonstatic data members of X that are of a class type M (or array thereof), each such class typehas a copy assignment operator whose parameter is of type const M&, const volatile M& orM.110)Otherwise, the implicitly declared copy constructor will have the formX& X::operator=(X&)The implicitly-declared copy assignment operator for class X has the return type X&; it returns the object forwhich the assignment operator is invoked, that is, the object assigned to.
An implicitly-declared copyassignment operator is an inline public member of its class. Because a copy assignment operator isimplicitly declared for a class if not declared by the user, a base class copy assignment operator is alwayshidden by the copy assignment operator of a derived class (13.5.3). A using-declaration (7.3.3) that bringsin from a base class an assignment operator with a parameter type that could be that of a copy-assignmentoperator for the derived class is not considered an explicit declaration of a copy-assignment operator anddoes not suppress the implicit declaration of the derived class copy-assignment operator; the operator introduced by the using-declaration is hidden by the implicitly-declared copy-assignment operator in thederived class.11A copy assignment operator for class X is trivial if it is implicitly declared and if— class X has no virtual functions (10.3) and no virtual base classes (10.1), and— each direct base class of X has a trivial copy assignment operator, and— for all the nonstatic data members of X that are of class type (or array thereof), each such class type hasa trivial copy assignment operator;otherwise the copy assignment operator is non-trivial.12An implicitly-declared copy assignment operator is implicitly defined when an object of its class type isassigned a value of its class type or a value of a class type derived from its class type.
A program is illformed if the class for which a copy assignment operator is implicitly defined has:— a nonstatic data member of const type, or— a nonstatic data member of reference type, or— a nonstatic data member of class type (or array thereof) with an inaccessible copy assignment operator,or— a base class with an inaccessible copy assignment operator.Before the implicitly-declared copy assignment operator for a class is implicitly defined, all implicitlydeclared copy assignment operators for its direct base classes and its nonstatic data members shall havebeen implicitly defined. [Note: an implicitly-declared copy assignment operator has an exceptionspecification (15.4).
]13The implicitly-defined copy assignment operator for class X performs memberwise assignment of its subobjects. The direct base classes of X are assigned first, in the order of their declaration in the base-specifierlist, and then the immediate nonstatic data members of X are assigned, in the order in which they weredeclared in the class definition.
Each subobject is assigned in the manner appropriate to its type:— if the subobject is of class type, the copy assignment operator for the class is used (as if by explicit qualification; that is, ignoring any possible virtual overriding functions in more derived classes);— if the subobject is an array, each element is assigned, in the manner appropriate to the element type;__________________110) This implies that the reference parameter of the implicitly-declared copy assignment operator cannot bind to a volatile lvalue;see C.1.8.206© ISO/IECISO/IEC 14882:1998(E)12 Special member functions12.8 Copying class objects— if the subobject is of scalar type, the built-in assignment operator is used.It is unspecified whether subobjects representing virtual base classes are assigned more than once by theimplicitly-defined copy assignment operator.
[Example:structstructstructstructVABC{:::};virtual V { };virtual V { };B, A { };it is unspecified whether the virtual base class subobject V is assigned twice by the implicitly-defined copyassignment operator for C. —end example]14A program is ill-formed if the copy constructor or the copy assignment operator for an object is implicitlyused and the special member function is not accessible (clause 11). [Note: Copying one object into anotherusing the copy constructor or the copy assignment operator does not change the layout or size of eitherobject. ]15Whenever a temporary class object is copied using a copy constructor, and this object and the copy have thesame cv-unqualified type, an implementation is permitted to treat the original and the copy as two differentways of referring to the same object and not perform a copy at all, even if the class copy constructor ordestructor have side effects.
For a function with a class return type, if the expression in the return statementis the name of a local object, and the cv-unqualified type of the local object is the same as the functionreturn type, an implementation is permitted to omit creating the temporary object to hold the function returnvalue, even if the class copy constructor or destructor has side effects. In these cases, the object isdestroyed at the later of times when the original and the copy would have been destroyed without the optimization.111) [Example:class Thing {public:Thing();~Thing();Thing(const Thing&);Thing operator=(const Thing&);void fun();};Thing f() {Thing t;return t;}Thing t2 = f();Here t does not need to be copied when returning from f. The return value of f may be constructeddirectly into the object t2.
]__________________111) Because only one object is destroyed instead of two, and one copy constructor is not executed, there is still one object destroyedfor each one constructed.207ISO/IEC 14882:1998(E)©(Blank page)208ISO/IEC© ISO/IECISO/IEC 14882:1998(E)13 Overloading13 Overloading13 Overloading[over]1When two or more different declarations are specified for a single name in the same scope, that name issaid to be overloaded. By extension, two declarations in the same scope that declare the same name butwith different types are called overloaded declarations. Only function declarations can be overloaded;object and type declarations cannot be overloaded.2When an overloaded function name is used in a call, which overloaded function declaration is being referenced is determined by comparing the types of the arguments at the point of use with the types of theparameters in the overloaded declarations that are visible at the point of use.
This function selection process is called overload resolution and is defined in 13.3. [Example:double abs(double);int abs(int);abs(1);abs(1.0);// call abs(int);// call abs(double);—end example]13.1 Overloadable declarations[over.load]1Not all function declarations can be overloaded. Those that cannot be overloaded are specified here.
Aprogram is ill-formed if it contains two such non-overloadable declarations in the same scope. [Note: thisrestriction applies to explicit declarations in a scope, and between such declarations and declarations madethrough a using-declaration (7.3.3).
It does not apply to sets of functions fabricated as a result of namelookup (e.g., because of using-directives) or overload resolution (e.g., for operator functions). ]2Certain function declarations cannot be overloaded:— Function declarations that differ only in the return type cannot be overloaded.— Member function declarations with the same name and the same parameter types cannot be overloadedif any of them is a static member function declaration (9.4). Likewise, member function templatedeclarations with the same name, the same parameter types, and the same template parameter lists cannot be overloaded if any of them is a static member function template declaration. The types of theimplicit object parameters constructed for the member functions for the purpose of overload resolution(13.3.1) are not considered when comparing parameter types for enforcement of this rule.
In contrast, ifthere is no static member function declaration among a set of member function declarations with thesame name and the same parameter types, then these member function declarations can be overloaded ifthey differ in the type of their implicit object parameter. [Example: the following illustrates this distinction:class X {static void f();void f();void f() const;void f() const volatile;void g();void g() const;void g() const volatile;};// ill-formed// ill-formed// ill-formed// OK: no static g// OK: no static g—end example]3[Note: as specified in 8.3.5, function declarations that have equivalent parameter declarations declare thesame function and therefore cannot be overloaded:209ISO/IEC 14882:1998(E)© ISO/IEC13.1 Overloadable declarations13 Overloading— Parameter declarations that differ only in the use of equivalent typedef “types” are equivalent.
Atypedef is not a separate type, but only a synonym for another type (7.1.3). [Example:typedef int Int;voidvoidvoidvoidf(intf(Intf(intf(Inti);i);i) { /* ... */ }i) { /* ... */ }// OK: redeclaration of f(int)// error: redefinition of f(int)—end example]Enumerations, on the other hand, are distinct types and can be used to distinguish overloaded functiondeclarations. [Example:enum E { a };void f(int i) { /* ... */ }void f(E i){ /* ... */ }—end example]— Parameter declarations that differ only in a pointer * versus an array [] are equivalent. That is, thearray declaration is adjusted to become a pointer declaration (8.3.5). Only the second and subsequentarray dimensions are significant in parameter types (8.3.4). [Example:intintintintf(char*);f(char[]);f(char[7]);f(char[9]);// same as f(char*);// same as f(char*);// same as f(char*);intintintintg(char(*)[10]);g(char[5][10]);g(char[7][10]);g(char(*)[20]);// same as g(char(*)[10]);// same as g(char(*)[10]);// different from g(char(*)[10]);—end example]— Parameter declarations that differ only in that one is a function type and the other is a pointer to thesame function type are equivalent.
That is, the function type is adjusted to become a pointer to functiontype (8.3.5). [Example:voidvoidvoidvoidh(int());h(int (*)());h(int x()) { }h(int (*x)()) { }// redeclaration of h(int())// definition of h(int())// ill-formed: redefinition of h(int())]— Parameter declarations that differ only in the presence or absence of const and/or volatile areequivalent. That is, the const and volatile type-specifiers for each parameter type are ignoredwhen determining which function is being declared, defined, or called. [Example:typedef const int cInt;intintintintffff(int);(const int);(int) { ...
}(cInt) { ... }// redeclaration of f(int)// definition of f(int)// error: redefinition of f(int)—end example]Only the const and volatile type-specifiers at the outermost level of the parameter type specification are ignored in this fashion; const and volatile type-specifiers buried within a parameter typespecification are significant and can be used to distinguish overloaded function declarations.112) In__________________112) When a parameter type includes a function type, such as in the case of a parameter type that is a pointer to function, the constand volatile type-specifiers at the outermost level of the parameter type specifications for the inner function type are also ignored.210© ISO/IECISO/IEC 14882:1998(E)13 Overloading13.1 Overloadable declarationsparticular, for any type T, “pointer to T,” “pointer to const T,” and “pointer to volatile T” are considered distinct parameter types, as are “reference to T,” “reference to const T,” and “reference tovolatile T.”— Two parameter declarations that differ only in their default arguments are equivalent.