Стандарт C++ 11 (1119564), страница 81
Текст из файла (страница 81)
If that user-written constructor would be ill-formed, the program isill-formed. Each expression in the expression-list is of the form static_cast<T&&>(p), where p is the nameof the corresponding constructor parameter and T is the declared type of p.9[ Example:struct B1 {B1(int) { }};struct B2 {B2(double) { }};struct D1 : B1 {using B1::B1;int x;};void test() {D1 d(6);D1 e;}struct D2 : B2 {using B2::B2;B1 b;};D2 f(1.0);// implicitly declares D1(int)// OK: d.x is not initialized// error: D1 has no default constructor// OK: implicitly declares D2(double)// error: B1 has no default constructortemplate< class T >struct D : T {using T::T;// declares all constructors from class T~D() { std::clog << "Destroying wrapper" << std::endl; }};Class template D wraps any class and forwards all of its constructors, while writing a message to the standardlog whenever an object of class D is destroyed.
— end example ]§ 12.9288© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)13Overloading[over]1When two or more different declarations are specified for a single name in the same scope, that name is saidto be overloaded. By extension, two declarations in the same scope that declare the same name but withdifferent types are called overloaded declarations. Only function and function template declarations can beoverloaded; variable and type declarations cannot be overloaded.2When an overloaded function name is used in a call, which overloaded function declaration is being referencedis determined by comparing the types of the arguments at the point of use with the types of the parametersin the overloaded declarations that are visible at the point of use. This function selection process is calledoverload resolution and is defined in 13.3.
[ Example:double abs(double);int abs(int);abs(1);abs(1.0);// calls abs(int);// calls abs(double);— end example ]13.1Overloadable 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). — end note ]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-type-list cannot be overloaded if any of them is a static member function declaration (9.4). Likewise, member functiontemplate declarations with the same name, the same parameter-type-list, and the same template parameter lists cannot be overloaded if any of them is a static member function template declaration.The types of the implicit object parameters constructed for the member functions for the purpose ofoverload resolution (13.3.1) are not considered when comparing parameter-type-lists for enforcement ofthis rule.
In contrast, if there is no static member function declaration among a set of member function declarations with the same name and the same parameter-type-list, then these member functiondeclarations can be overloaded if they 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§ 13.1© ISO/IEC 2011 – All rights reserved289ISO/IEC 14882:2011(E)— end example ]— Member function declarations with the same name and the same parameter-type-list as well as member function template declarations with the same name, the same parameter-type-list, and the sametemplate parameter lists cannot be overloaded if any of them, but not all, have a ref-qualifier (8.3.5).[ Example:class Y {void h()void h()void h()void i()void i()&;const &;&&;&;const;// OK// OK, all declarations have a ref-qualifier// ill-formed, prior declaration of i// has a ref-qualifier};— 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:— 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 ]§ 13.1290© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)— Parameter declarations that differ only in that one is a function type and the other is a pointer tothe same function type are equivalent.
That is, the function type is adjusted to become a pointer tofunction type (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())— end example ]— 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 ignored whendetermining 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.124 Inparticular, for any type T, “pointer to T,” “pointer to const T,” and “pointer to volatile T” areconsidered 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.
[ Example:consider the following:voidvoidvoidvoidffff(int i, int j);(int i, int j = 99);(int i = 88, int j);();void prog () {f (1, 2);f (1);f ();}// OK: redeclaration of f(int, int)// OK: redeclaration of f(int, int)// OK: overloaded declaration of f// OK: call f(int, int)// OK: call f(int, int)// Error: f(int, int) or f()?— end example ] — end note ]13.21Declaration matching[over.dcl]Two function declarations of the same name refer to the same function if they are in the same scope andhave equivalent parameter declarations (13.1). A function member of a derived class is not in the same scopeas a function member of the same name in a base class. [ Example:124) When a parameter type includes a function type, such as in the case of a parameter type that is a pointer to function, theconst and volatile type-specifiers at the outermost level of the parameter type specifications for the inner function type arealso ignored.§ 13.2© ISO/IEC 2011 – All rights reserved291ISO/IEC 14882:2011(E)struct B {int f(int);};struct D : B {int f(const char*);};Here D::f(const char*) hides B::f(int) rather than overloading it.void h(D* pd) {pd->f(1);pd->B::f(1);pd->f("Ben");////////error:D::f(const char*) hides B::f(int)OKOK, calls D::f}— end example ]2A locally declared function is not in the same scope as a function in a containing scope.
[ Example:void f(const char*);void g() {extern void f(int);f("asdf");// error: f(int) hides f(const char*)// so there is no f(const char*) in this scope}void caller () {extern void callee(int, int);{extern void callee(int);// hides callee(int, int)callee(88, 99);// error: only callee(int) in scope}}— end example ]3Different versions of an overloaded member function can be given different access rules. [ Example:class buffer {private:char* p;int size;protected:buffer(int s, char* store) { size = s; p = store; }public:buffer(int s) { p = new char[size = s]; }};— end example ]13.31Overload resolution[over.match]Overload resolution is a mechanism for selecting the best function to call given a list of expressions that areto be the arguments of the call and a set of candidate functions that can be called based on the context ofthe call.
The selection criteria for the best function are the number of arguments, how well the arguments§ 13.3292© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)match the parameter-type-list of the candidate function, how well (for non-static member functions) theobject matches the implicit object parameter, and certain other properties of the candidate function. [ Note:The function selected by overload resolution is not guaranteed to be appropriate for the context. Otherrestrictions, such as the accessibility of the function, can make its use in the calling context ill-formed.— end note ]2Overload resolution selects the function to call in seven distinct contexts within the language:— invocation of a function named in the function call syntax (13.3.1.1.1);— invocation of a function call operator, a pointer-to-function conversion function, a reference-to-pointerto-function conversion function, or a reference-to-function conversion function on a class object namedin the function call syntax (13.3.1.1.2);— invocation of the operator referenced in an expression (13.3.1.2);— invocation of a constructor for direct-initialization (8.5) of a class object (13.3.1.3);— invocation of a user-defined conversion for copy-initialization (8.5) of a class object (13.3.1.4);— invocation of a conversion function for initialization of an object of a nonclass type from an expressionof class type (13.3.1.5); and— invocation of a conversion function for conversion to a glvalue or class prvalue to which a reference (8.5.3) will be directly bound (13.3.1.6).Each of these contexts defines the set of candidate functions and the list of arguments in its own unique way.But, once the candidate functions and argument lists have been identified, the selection of the best functionis the same in all cases:— First, a subset of the candidate functions (those that have the proper number of arguments and meetcertain other conditions) is selected to form a set of viable functions (13.3.2).— Then the best viable function is selected based on the implicit conversion sequences (13.3.3.1) neededto match each argument to the corresponding parameter of each viable function.3If a best viable function exists and is unique, overload resolution succeeds and produces it as the result.Otherwise overload resolution fails and the invocation is ill-formed.
When overload resolution succeeds, andthe best viable function is not accessible (Clause 11) in the context in which it is used, the program isill-formed.13.3.1Candidate functions and argument lists[over.match.funcs]1The subclauses of 13.3.1 describe the set of candidate functions and the argument list submitted to overloadresolution in each of the seven contexts in which overload resolution is used.