Стандарт C++ 11 (1119564), страница 87
Текст из файла (страница 87)
— endnote ]13.41Address of overloaded function[over.over]A use of an overloaded function name without arguments is resolved in certain contexts to a function, apointer to function or a pointer to member function for a specific function from the overload set. A functiontemplate name is considered to name a set of overloaded functions in such contexts. The function selectedis the one whose type is identical to the function type of the target type required in the context. [ Note:That is, the class of which the function is a member is ignored when matching a pointer-to-member-functiontype. — end note ] The target can be— an object or reference being initialized (8.5, 8.5.3),— the left side of an assignment (5.17),— a parameter of a function (5.2.2),— a parameter of a user-defined operator (13.5),— the return value of a function, operator function, or conversion (6.6.3),— an explicit type conversion (5.2.3, 5.2.9, 5.4), or— a non-type template-parameter (14.3.2).The overloaded function name can be preceded by the & operator.
An overloaded function name shall notbe used without arguments in contexts other than those listed. [ Note: Any redundant set of parenthesessurrounding the overloaded function name is ignored (5.1). — end note ]§ 13.4© ISO/IEC 2011 – All rights reserved311ISO/IEC 14882:2011(E)2If the name is a function template, template argument deduction is done (14.8.2.2), and if the argumentdeduction succeeds, the resulting template argument list is used to generate a single function templatespecialization, which is added to the set of overloaded functions considered. [ Note: As described in 14.8.1,if deduction fails and the function template name is followed by an explicit template argument list, thetemplate-id is then examined to see whether it identifies a single function template specialization.
If it does,the template-id is considered to be an lvalue for that function template specialization. The target type isnot used in that determination. — end note ]3Non-member functions and static member functions match targets of type “pointer-to-function” or “referenceto-function.” Nonstatic member functions match targets of type “pointer-to-member-function”. If a nonstatic member function is selected, the reference to the overloaded function name is required to have theform of a pointer to member as described in 5.3.1.4If more than one function is selected, any function template specializations in the set are eliminated if theset also contains a non-template function, and any given function template specialization F1 is eliminated ifthe set contains a second function template specialization whose function template is more specialized thanthe function template of F1 according to the partial ordering rules of 14.5.6.2.
After such eliminations, ifany, there shall remain exactly one selected function.5[ Example:int f(double);int f(int);int (*pfd)(double) = &f;int (*pfi)(int) = &f;int (*pfe)(...) = &f;int (&rfi)(int) = f;int (&rfd)(double) = f;void g() {(int (*)(int))&f;}//////////selects f(double)selects f(int)error: type mismatchselects f(int)selects f(double)// cast expression as selectorThe initialization of pfe is ill-formed because no f() with type int(...) has been declared, and not becauseof any ambiguity. For another example,struct X {int f(int);static int f(long);};int (X::*p1)(int)int(*p2)(int)int(*p3)(long)int (X::*p4)(long)int (X::*p5)(int)int=====&X::f;&X::f;&X::f;&X::f;&(X::f);(*p6)(long) = &(X::f);//////////////OKerror: mismatchOKerror: mismatcherror: wrong syntax forpointer to memberOK— end example ]6[ Note: If f() and g() are both overloaded functions, the cross product of possibilities must be consideredto resolve f(&g), or the equivalent expression f(g).
— end note ]7[ Note: There are no standard conversions (Clause 4) of one pointer-to-function type into another. Inparticular, even if B is a public base of D, we have§ 13.4312© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)D* f();B* (*p1)() = &f;// errorvoid g(D*);void (*p2)(B*) = &g;// error— end note ]13.51Overloaded operators[over.oper]A function declaration having one of the following operator-function-ids as its name declares an operatorfunction.
A function template declaration having one of the following operator-function-ids as its namedeclares an operator function template. A specialization of an operator function template is also an operatorfunction. An operator function is said to implement the operator named in its operator-function-id.operator-function-id:operator operatoroperator: 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.
— end note ]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 delete and operatordelete[], are described completely in 3.7.4. The attributes and restrictions found in the rest of thissubclause do not apply to them unless explicitly stated in 3.7.4.6An operator function shall either be a non-static member function or be a non-member function and haveat least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to anenumeration.
It is not possible to change the precedence, grouping, or number of operands of operators.The meaning of the operators =, (unary) &, and , (comma), predefined for each type, can be changed for§ 13.5© ISO/IEC 2011 – All rights reserved313ISO/IEC 14882:2011(E)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.1Unary operators[over.unary]1A 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 interpretedas either x.operator@() or operator@(x). If both forms of the operator function have been declared, therules in 13.3.1.2 determine which, if any, interpretation is used. See 13.5.7 for an explanation of the postfixunary operators ++ and --.2The 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.
— endnote ]13.5.21Binary operators[over.binary]A binary operator shall be implemented either by a non-static member function (9.3) with one parameteror by a non-member function with two parameters. Thus, for any binary operator @, x@y can be interpretedas either x.operator@(y) or operator@(x,y). If both forms of the operator function have been declared,the rules in 13.3.1.2 determine which, if any, interpretation is used.13.5.3Assignment[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 and move assignment operators, can be virtual.
[ Note: For a derivedclass D with a base class B for which a virtual copy/move assignment has been declared, the copy/moveassignment operator in D does not override B’s virtual copy/move assignment operator. [ Example:struct B {virtual int operator= (int);virtual B& operator= (const B&);};struct D : B {virtual int operator= (int);virtual D& operator= (const B&);};D dobj1;D dobj2;B* bptr = &dobj1;§ 13.5.3314© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)void f() {bptr->operator=(99);*bptr = 99;bptr->operator=(dobj2);*bptr = dobj2;dobj1 = dobj2;////////////calls D::operator=(int)dittocalls D::operator=(const B&)dittocalls implicitly-declaredD::operator=(const D&)}— end example ] — end note ]13.5.41Function call[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 asx.operator()(arg1, ...) for a class object x of type T if T::operator()(T1, T2, T3) exists and if theoperator is selected as the best match function by the overload resolution mechanism (13.3.3).13.5.51Subscripting[over.sub]operator[] shall be a non-static member function with exactly one parameter. It implements the subscripting syntaxpostfix-expression [ expression ]orpostfix-expression [ braced-init-list ]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).