Стандарт C++ 98 (1119566), страница 64
Текст из файла (страница 64)
]7[Note: there are no standard conversions (clause 4) of one pointer-to-function type into another. In particular, even if B is a public base of D, we haveD* f();B* (*p1)() = &f;// errorvoid g(D*);void (*p2)(B*) = &g;// error—end note]13.5 Overloaded operators1[over.oper]A function declaration having one of the following operator-function-ids as its name declares an operatorfunction. An operator function is said to implement the operator named in its operator-function-id.operator-function-id:operator operator227ISO/IEC 14882:1998(E)© ISO/IEC13.5 Overloaded operators13 Overloadingoperator: one ofnew+!^=<=()delete*=<&=|=>=&&[]new[]/%>+=<<>>||++delete[]^&-=*=>>= <<=-,|/===->*~%=!=->[Note: the last two operators are function call (5.2.2) and subscripting (5.2.1).
The operators new[],delete[], (), and [] are formed from more than one token. ]2Both the unary and binary forms of+-*&can be overloaded.3The following operators cannot be overloaded:..*::?:nor can the preprocessing symbols # and ## (clause 16).4Operator functions are usually not called directly; instead they are invoked to evaluate the operators theyimplement (13.5.1 - 13.5.7). They can be explicitly called, however, using the operator-function-id as thename of the function in the function call syntax (5.2.2). [Example:complex z = a.operator+(b);// complex z = a+b;void* p = operator new(sizeof(int)*n);—end example]5The allocation and deallocation functions, operator new, operator new[], operator deleteand operator delete[], are described completely in 3.7.3. The attributes and restrictions found in therest of this subclause do not apply to them unless explicitly stated in 3.7.3.6An operator function shall either be a non-static member function or be a non-member function and have atleast one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration.
It is not possible to change the precedence, grouping, or number of operands of operators. Themeaning of the operators =, (unary) &, and , (comma), predefined for each type, can be changed for specific class and enumeration types by defining operator functions that implement these operators. Operatorfunctions are inherited in the same manner as other base class functions.7The identities among certain predefined operators applied to basic types (for example, ++a ≡ a+=1) neednot hold for operator functions. Some predefined operators, such as +=, require an operand to be an lvaluewhen applied to basic types; this is not required by operator functions.8An operator function cannot have default arguments (8.3.6), except where explicitly stated below.
Operatorfunctions cannot have more or fewer parameters than the number required for the corresponding operator,as described in the rest of this subclause.9Operators not mentioned explicitly in subclauses 13.5.3 through 13.5.7 act as ordinary unary and binaryoperators obeying the rules of 13.5.1 or 13.5.2.13.5.1 Unary operators1[over.unary]A prefix unary operator shall be implemented by a non-static member function (9.3) with no parameters ora non-member function with one parameter.
Thus, for any prefix unary operator @, @x can be interpreted aseither x.operator@() or operator@(x). If both forms of the operator function have been declared,the rules in 13.3.1.2 determine which, if any, interpretation is used. See 13.5.7 for an explanation of thepostfix unary operators ++ and --.228© ISO/IECISO/IEC 14882:1998(E)13 Overloading213.5.1 Unary operatorsThe unary and binary forms of the same operator are considered to have the same name. [Note: consequently, a unary operator can hide a binary operator from an enclosing scope, and vice versa. ]13.5.2 Binary operators1[over.binary]A binary operator shall be implemented either by a non-static member function (9.3) with one parameter orby a non-member function with two parameters. Thus, for any binary operator @, x@y can be interpreted aseither x.operator@(y) or operator@(x,y).
If both forms of the operator function have beendeclared, the rules in 13.3.1.2 determines which, if any, interpretation is used.13.5.3 Assignment[over.ass]1An assignment operator shall be implemented by a non-static member function with exactly one parameter.Because a copy assignment operator operator= is implicitly declared for a class if not declared by theuser (12.8), a base class assignment operator is always hidden by the copy assignment operator of thederived class.2Any assignment operator, even the copy assignment operator, can be virtual.
[Note: for a derived class Dwith a base class B for which a virtual copy assignment has been declared, the copy assignment operator inD does not override B’s virtual copy assignment operator. [Example:struct B {virtualvirtual};struct D : B {virtualvirtual};int operator= (int);B& operator= (const B&);int operator= (int);D& operator= (const B&);D dobj1;D dobj2;B* bptr = &dobj1;void f() {bptr->operator=(99);*bptr = 99;bptr->operator=(dobj2);*bptr = dobj2;dobj1 = dobj2;// calls D::operator=(int)// ditto// calls D::operator=(const B&)// ditto// calls implicitly-declared// D::operator=(const D&)}—end example] —end note]13.5.4 Function call1[over.call]operator() shall be a non-static member function with an arbitrary number of parameters.
It can havedefault arguments. It implements the function call syntaxpostfix-expression ( expression-listopt )where the postfix-expression evaluates to a class object and the possibly empty expression-list matches theparameter list of an operator() member function of the class. Thus, a call x(arg1,...) is interpreted as x.operator()(arg1,...) for a class object x of type T if T::operator()(T1, T2,T3) exists and if the operator is selected as the best match function by the overload resolution mechanism(13.3.3).229ISO/IEC 14882:1998(E)© ISO/IEC13.5.5 Subscripting13 Overloading13.5.5 Subscripting1[over.sub]operator[] shall be a non-static member function with exactly one parameter. It implements the subscripting syntaxpostfix-expression [ expression ]Thus, a subscripting expression x[y] is interpreted as x.operator[](y) for a class object x of type Tif T::operator[](T1) exists and if the operator is selected as the best match function by the overloadresolution mechanism (13.3.3).13.5.6 Class member access1[over.ref]operator-> shall be a non-static member function taking no parameters.
It implements class memberaccess using ->postfix-expression -> id-expressionAn expression x->m is interpreted as (x.operator->())->m for a class object x of type T ifT::operator->() exists and if the operator is selected as the best match function by the overload resolution mechanism (13.3).13.5.7 Increment and decrement1[over.inc]The user-defined function called operator++ implements the prefix and postfix ++ operator.
If thisfunction is a member function with no parameters, or a non-member function with one parameter of class orenumeration type, it defines the prefix increment operator ++ for objects of that type. If the function is amember function with one parameter (which shall be of type int) or a non-member function with twoparameters (the second of which shall be of type int), it defines the postfix increment operator ++ forobjects of that type.
When the postfix increment is called as a result of using the ++ operator, the intargument will have value zero.125) [Example:class X {public:X&operator++();Xoperator++(int);};// prefix ++a// postfix a++class Y { };Y&operator++(Y&);Yoperator++(Y&, int);// prefix ++b// postfix b++void f(X a, Y b) {++a;a++;++b;b++;// a.operator++();// a.operator++(0);// operator++(b);// operator++(b, 0);a.operator++();a.operator++(0);operator++(b);operator++(b, 0);// explicit call: like ++a;// explicit call: like a++;// explicit call: like ++b;// explicit call: like b++;}—end example]2The prefix and postfix decrement operators -- are handled analogously.__________________125) Calling operator++ explicitly, as in expressions like a.operator++(2), has no special properties: The argument tooperator++ is 2.230© ISO/IECISO/IEC 14882:1998(E)13 Overloading13.6 Built-in operators13.6 Built-in operators[over.built]1The candidate operator functions that represent the built-in operators defined in clause 5 are specified inthis subclause.
These candidate functions participate in the operator overload resolution process asdescribed in 13.3.1.2 and are used for no other purpose. [Note: because built-in operators take onlyoperands with non-class type, and operator overload resolution occurs only when an operand expressionoriginally has class or enumeration type, operator overload resolution can resolve to a built-in operator onlywhen an operand has a class type that has a user-defined conversion to a non-class type appropriate for theoperator, or when an operand has an enumeration type that can be converted to a type appropriate for theoperator. Also note that some of the candidate operator functions given in this subclause are more permissive than the built-in operators themselves. As described in 13.3.1.2, after a built-in operator is selected byoverload resolution the expression is subject to the requirements for the built-in operator given in clause 5,and therefore to any additional semantic constraints given there.
If there is a user-written candidate withthe same name and parameter types as a built-in candidate operator function, the built-in operator functionis hidden and is not included in the set of candidate functions. ]2In this subclause, the term promoted integral type is used to refer to those integral types which are preserved by integral promotion (including e.g.