[over.match.funcs] (original) (raw)

12 Overloading [over]

12.2 Overload resolution [over.match]

12.2.2 Candidate functions and argument lists [over.match.funcs]

12.2.2.1 General [over.match.funcs.general]

The subclauses of [over.match.funcs] describe the set of candidate functions and the argument list submitted to overload resolution in each context in which overload resolution is used.

The source transformations and constructions defined in these subclauses are only for the purpose of describing the overload resolution process.

An implementation is not required to use such transformations and constructions.

The set of candidate functions can contain both member and non-member functions to be resolved against the same argument list.

If a member function is

it is considered to have an extra first parameter, called the implicit object parameter, which represents the object for which the member function has been called.

Similarly, when appropriate, the context can construct an argument list that contains animplied object argumentas the first argument in the list to denote the object to be operated on.

For implicit object member functions, the type of the implicit object parameter is

whereXis the class of which the function is a direct member andcvis the cv-qualification on the member function declaration.

[Example 1:

For aconstmember function of classX, the extra parameter is assumed to have type “lvalue reference toconst X”.

— _end example_]

For conversion functions that are implicit object member functions, the function is considered to be a member of the class of the implied object argument for the purpose of defining the type of the implicit object parameter.

For non-conversion functions that are implicit object member functions nominated by a using-declarationin a derived class, the function is considered to be a member of the derived class for the purpose of defining the type of the implicit object parameter.

For static member functions, the implicit object parameter is considered to match any object (since if the function is selected, the object is discarded).

[Note 1:

No actual type is established for the implicit object parameter of a static member function, and no attempt will be made to determine a conversion sequence for that parameter ([over.match.best]).

— _end note_]

During overload resolution, the implied object argument is indistinguishable from other arguments.

The implicit object parameter, however, retains its identity since no user-defined conversions can be applied to achieve a type match with it.

For implicit object member functions declared without a ref-qualifier, even if the implicit object parameter is not const-qualified, an rvalue can be bound to the parameter as long as in all other respects the argument can be converted to the type of the implicit object parameter.

[Note 2:

The fact that such an argument is an rvalue does not affect the ranking of implicit conversion sequences.

— _end note_]

Because other than in list-initialization only one user-defined conversion is allowed in an implicit conversion sequence, special rules apply when selecting the best user-defined conversion ([over.match.best], [over.best.ics]).

[Example 2: class T { public: T();};class C : T { public: C(int);}; T a = 1; — _end example_]

In each case where conversion functions of a class S are considered for initializing an object or reference of type T, the candidate functions include the result of a search for the conversion-function-id operator Tin S.

[Note 3:

This search can find a specialization of a conversion function template ([basic.lookup]).

— _end note_]

Each such case also defines sets of permissible typesfor explicit and non-explicit conversion functions; each (non-template) conversion function that

is also a candidate function.

If initializing an object, for any permissible type cv U, any_cv2_ U, cv2 U&, or cv2 U&&is also a permissible type.

If the set of permissible types for explicit conversion functions is empty, any candidates that are explicit are discarded.

In each case where a candidate is a function template, candidate function template specializations are generated using template argument deduction ([temp.over], [temp.deduct]).

If a constructor template or conversion function template has an explicit-specifierwhose constant-expression is value-dependent ([temp.dep]), template argument deduction is performed first and then, if the context admits only candidates that are not explicit and the generated specialization is explicit ([dcl.fct.spec]), it will be removed from the candidate set.

Those candidates are then handled as candidate functions in the usual way.101

A given name can refer to, or a conversion can consider, one or more function templates as well as a set of non-template functions.

In such a case, the candidate functions generated from each function template are combined with the set of non-template candidate functions.

A defaulted move special member function ([class.copy.ctor], [class.copy.assign]) that is defined as deleted is excluded from the set of candidate functions in all contexts.

A constructor inherited from class type C ([class.inhctor.init]) that has a first parameter of type “reference to cv1 P” (including such a constructor instantiated from a template) is excluded from the set of candidate functions when constructing an object of type cv2 Dif the argument list has exactly one argument andC is reference-related to P andP is reference-related to D.

[Example 3: struct A { A(); A(A &&); template<typename T> A(T &&); };struct B : A { using A::A; B(const B &); B(B &&) = default; struct X { X(X &&) = delete; } x;};extern B b1; B b2 = static_cast<B&&>(b1); struct C { operator B&&(); }; B b3 = C(); — _end example_]

12.2.2.2 Function call syntax [over.match.call]

12.2.2.2.1 General [over.match.call.general]

If the postfix-expression denotes an object of class type, overload resolution is applied as specified in [over.call.object].

If the postfix-expression is the address of an overload set, overload resolution is applied using that set as described above.

[Note 1:

No implied object argument is added in this case.

— _end note_]

If the function selected by overload resolution is an implicit object member function, the program is ill-formed.

[Note 2:

The resolution of the address of an overload set in other contexts is described in [over.over].

— _end note_]

12.2.2.2.2 Call to named function [over.call.func]

Of interest in [over.call.func] are only those function calls in which the postfix-expressionultimately contains an id-expression that denotes one or more functions.

Such apostfix-expression, perhaps nested arbitrarily deep in parentheses, has one of the following forms:

These represent two syntactic subcategories of function calls: qualified function calls and unqualified function calls.

In qualified function calls, the function is named by an id-expressionpreceded by an -> or . operator.

Since the constructA->Bis generally equivalent to(*A).B, the rest of[over] assumes, without loss of generality, that all member function calls have been normalized to the form that uses an object and the.operator.

Furthermore, [over] assumes that thepostfix-expressionthat is the left operand of the.operator has type “cv T” whereTdenotes a class.102

The function declarations found by name lookup ([class.member.lookup]) constitute the set of candidate functions.

The argument list is theexpression-listin the call augmented by the addition of the left operand of the.operator in the normalized member function call as the implied object argument ([over.match.funcs]).

In unqualified function calls, the function is named by aprimary-expression.

The function declarations found by name lookup ([basic.lookup]) constitute the set of candidate functions.

Because of the rules for name lookup, the set of candidate functions consists either entirely of non-member functions or entirely of member functions of some classT.

In the former case or if the primary-expression is the address of an overload set, the argument list is the same as theexpression-listin the call.

Otherwise, the argument list is theexpression-listin the call augmented by the addition of an implied object argument as in a qualified function call.

If the current class is, or is derived from, T, and the keywordthis ([expr.prim.this]) refers to it, then the implied object argument is (*this).

Otherwise, a contrived object of typeTbecomes the implied object argument;103if overload resolution selects a non-static member function, the call is ill-formed.

[Example 1: struct C { void a();void b() { a(); } void c(this const C&); void c() &; static void c(int = 0); void d() { c(); (C::c)(); (&(C::c))(); (&C::c)(C{}); (&C::c)(*this); (&C::c)(); } void f(this const C&);void g() const { f(); f(*this); this->f(); } static void h() { f(); f(C{}); C{}.f(); } void k(this int);operator int() const;void m(this const C& c) { c.k(); } }; — _end example_]

12.2.2.2.3 Call to object of class type [over.call.object]

If the postfix-expression Ein the function call syntax evaluates to a class object of type “cv T”, then the set of candidate functions includes at least the function call operators of T.

The function call operators of Tare the results of a search for the name operator()in the scope of T.

In addition, for each non-explicit conversion function declared in T of the form

where the optionalcv-qualifier-seqis the same cv-qualification as, or a greater cv-qualification than,cv, and whereconversion-type-iddenotes the type “pointer to function of () returning R”, or the type “reference to pointer to function of () returning R”, or the type “reference to function of () returning R”, a surrogate call function with the unique name_call-function_and having the form

R call-function ( conversion-type-id F, P a, …, P a) { return F (a, …, a); }

is also considered as a candidate function.

Similarly, surrogate call functions are added to the set of candidate functions for each non-explicit conversion function declared in a base class ofTprovided the function is not hidden withinTby another intervening declaration.104

The argument list submitted to overload resolution consists of the argument expressions present in the function call syntax preceded by the implied object argument(E).

[Note 1:

When comparing the call against the function call operators, the implied object argument is compared against the object parameter of the function call operator.

When comparing the call against a surrogate call function, the implied object argument is compared against the first parameter of the surrogate call function.

— _end note_]

[Example 1: int f1(int);int f2(float);typedef int (*fp1)(int);typedef int (*fp2)(float);struct A { operator fp1() { return f1; } operator fp2() { return f2; } } a;int i = a(1); — _end example_]

12.2.2.3 Operators in expressions [over.match.oper]

If no operand of an operator in an expression has a type that is a class or an enumeration, the operator is assumed to be a built-in operator and interpreted according to [expr.compound].

[Note 1:

Because.,.*, and​::​cannot be overloaded, these operators are always built-in operators interpreted according to[expr.compound].

?:cannot be overloaded, but the rules in this subclause are used to determine the conversions to be applied to the second and third operands when they have class or enumeration type ([expr.cond]).

— _end note_]

[Example 1: struct String { String (const String&); String (const char*);operator const char* ();}; String operator + (const String&, const String&);void f() { const char* p= "one" + "two"; int I = 1 + 1; } — _end example_]

If either operand has a type that is a class or an enumeration, a user-defined operator function can be declared that implements this operator or a user-defined conversion can be necessary to convert the operand to a type that is appropriate for a built-in operator.

In this case, overload resolution is used to determine which operator function or built-in operator is to be invoked to implement the operator.

Therefore, the operator notation is first transformed to the equivalent function-call notation as summarized in Table 18(where @ denotes one of the operators covered in the specified subclause).

However, the operands are sequenced in the order prescribed for the built-in operator ([expr.compound]).

For a unary operator @with an operand of type cv1 T1, and for a binary operator @with a left operand of type cv1 T1and a right operand of type cv2 T2, four sets of candidate functions, designatedmember candidates,non-member candidates,built-in candidates, andrewritten candidates, are constructed as follows:

A non-template function or function template F named operator==is a rewrite target with first operand ounless a search for the name operator!= in the scope Sfrom the instantiation context of the operator expression finds a function or function template that would correspond ([basic.scope.scope]) to Fif its name were operator==, where S is the scope of the class type of oif F is a class member, and the namespace scope of which F is a member otherwise.

A function template specialization named operator== is a rewrite target if its function template is a rewrite target.

[Example 2: struct A {};template<typename T> bool operator==(A, T); bool a1 = 0 == A(); template<typename T> bool operator!=(A, T);bool a2 = 0 == A(); struct B { bool operator==(const B&); };struct C : B { C(); C(B);bool operator!=(const B&); };bool c1 = B() == C(); bool c2 = C() == B(); struct D {};template<typename T> bool operator==(D, T); inline namespace N { template<typename T> bool operator!=(D, T); } bool d1 = 0 == D(); — _end example_]

For the first parameter of the built-in assignment operators, only standard conversion sequences ([over.ics.scs]) are considered.

For all other operators, no such restrictions apply.

The set of candidate functions for overload resolution for some operator @is the union of the member candidates, the non-member candidates, the built-in candidates, and the rewritten candidates for that operator @.

The argument list contains all of the operands of the operator.

[Example 3: struct A { operator int();}; A operator+(const A&, const A&);void m() { A a, b; a + b; } — _end example_]

If a rewritten operator<=> candidate is selected by overload resolution for an operator @,x @ yis interpreted as0 @ (y <=> x)if the selected candidate is a synthesized candidate with reversed order of parameters, or (x <=> y) @ 0 otherwise, using the selected rewritten operator<=> candidate.

Rewritten candidates for the operator @are not considered in the context of the resulting expression.

If a rewritten operator== candidate is selected by overload resolution for an operator @, its return type shall be cv bool, andx @ y is interpreted as:

in each case using the selected rewritten operator== candidate.

If a built-in candidate is selected by overload resolution, the operands of class type are converted to the types of the corresponding parameters of the selected operation function, except that the second standard conversion sequence of a user-defined conversion sequence is not applied.

Then the operator is treated as the corresponding built-in operator and interpreted according to [expr.compound].

[Example 4: struct X { operator double();};struct Y { operator int*();};int *a = Y() + 100.0; int *b = Y() + X(); — _end example_]

The second operand of operator->is ignored in selecting anoperator->function, and is not an argument when theoperator->function is called.

Whenoperator->returns, the operator->is applied to the value returned, with the original second operand.106

If the operator is the operator,, the unary operator&, or the operator->, and there are no viable functions, then the operator is assumed to be the built-in operator and interpreted according to[expr.compound].

[Note 3:

The lookup rules for operators in expressions are different than the lookup rules for operator function names in a function call, as shown in the following example:struct A { };void operator + (A, A);struct B { void operator + (B);void f ();}; A a;void B::f() { operator+ (a,a); a + a; }

— _end note_]

12.2.2.4 Initialization by constructor [over.match.ctor]

When objects of class type are direct-initialized, copy-initialized from an expression of the same or a derived class type ([dcl.init]), or default-initialized, overload resolution selects the constructor.

For direct-initialization or default-initialization (including default-initialization in the context of copy-list-initialization), the candidate functions are all the constructors of the class of the object being initialized.

Otherwise, the candidate functions are all the converting constructors ([class.conv.ctor]) of that class.

For default-initialization in the context of copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed.

12.2.2.5 Copy-initialization of class by user-defined conversion [over.match.copy]

Under the conditions specified in [dcl.init], as part of a copy-initialization of an object of class type, a user-defined conversion can be invoked to convert an initializer expression to the type of the object being initialized.

Overload resolution is used to select the user-defined conversion to be invoked.

[Note 1:

The conversion performed for indirect binding to a reference to a possibly cv-qualified class type is determined in terms of a corresponding non-reference copy-initialization.

— _end note_]

Assuming that “cv1 T” is the type of the object being initialized, withTa class type, the candidate functions are selected as follows:

In both cases, the argument list has one argument, which is the initializer expression.

[Note 2:

This argument will be compared against the first parameter of the constructors and against the object parameter of the conversion functions.

— _end note_]

12.2.2.6 Initialization by conversion function [over.match.conv]

Under the conditions specified in [dcl.init], as part of an initialization of an object of non-class type, a conversion function can be invoked to convert an initializer expression of class type to the type of the object being initialized.

Overload resolution is used to select the conversion function to be invoked.

Assuming that “cv T” is the type of the object being initialized, the candidate functions are selected as follows:

The argument list has one argument, which is the initializer expression.

[Note 1:

This argument will be compared against the object parameter of the conversion functions.

— _end note_]

12.2.2.7 Initialization by conversion function for direct reference binding [over.match.ref]

Under the conditions specified in [dcl.init.ref], a reference can be bound directly to the result of applying a conversion function to an initializer expression.

Overload resolution is used to select the conversion function to be invoked.

Assuming that “reference to cv1 T” is the type of the reference being initialized, the candidate functions are selected as follows:

The argument list has one argument, which is the initializer expression.

[Note 1:

This argument will be compared against the object parameter of the conversion functions.

— _end note_]

12.2.2.8 Initialization by list-initialization [over.match.list]

When objects of non-aggregate class type T are list-initialized such that [dcl.init.list] specifies that overload resolution is performed according to the rules in this subclause or when forming a list-initialization sequence according to [over.ics.list], overload resolution selects the constructor in two phases:

In copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed.

[Note 1:

This differs from other situations ([over.match.ctor], [over.match.copy]), where only converting constructors are considered for copy-initialization.

This restriction only applies if this initialization is part of the final result of overload resolution.

— _end note_]

12.2.2.9 Class template argument deduction [over.match.class.deduct]

When resolving a placeholder for a deduced class type ([dcl.type.class.deduct]) where the template-name names a primary class template C, a set of functions and function templates, called the guides of C, is formed comprising:

In addition, if C is defined and its definition satisfies the conditions for an aggregate class ([dcl.init.aggr]) with the assumption that any dependent base class has no virtual functions and no virtual base classes, and the initializer is a non-empty braced-init-list or parenthesized expression-list, and there are no deduction-guides for C, the set contains an additional function template, called the aggregate deduction candidate, defined as follows.

For each , let be the corresponding aggregate element of C or of one of its (possibly recursive) subaggregates that would be initialized by ([dcl.init.aggr]) if

If there is no such aggregate element for any , the aggregate deduction candidate is not added to the set.

The aggregate deduction candidate is derived as above from a hypothetical constructor , where

except that additional parameter packs of the form are inserted into the parameter list in their original aggregate element position corresponding to each non-trailing aggregate element of type that was skipped because it was a parameter pack, and the trailing sequence of parameters corresponding to a trailing aggregate element that is a pack expansion (if any) is replaced by a single parameter of the form .

If A is a deducible template ([dcl.type.simple]), the set contains the guides of Awith the return type R of each guide replaced with typename CC<R>​::​type given a class templatetemplate <typename> class CC;whose primary template is not defined and with a single partial specialization whose template parameter list is that of A and whose template argument list is a specialization of A with the template argument list of A ([temp.dep.type]) having a member typedef type designating a template specialization with the template argument list of A but with C as the template.

[Note 2:

Equivalently, the template parameter list of the specialization is that of C, the template argument list of the specialization is B, and the member typedef names C with the template argument list of C.

— _end note_]

[Example 1: template <typename T> struct B { B(T);};template <typename T> struct C : public B<T> { using B<T>::B;};template <typename T> struct D : public B<T> {}; C c(42); D d(42); B(int) -> B<char>; C c2(42); template <typename T> struct E : public B<int> { using B<int>::B;}; E e(42); template <typename T, typename U, typename V> struct F { F(T, U, V);};template <typename T, typename U> struct G : F<U, T, int> { using G::F::F;}G g(true, 'a', 1); template<class T, std::size_t N> struct H { T array[N];};template<class T, std::size_t N> struct I { volatile T array[N];};template<std::size_t N> struct J { unsigned char array[N];}; H h = { "abc" }; I i = { "def" }; J j = { "ghi" }; — _end example_]

The guides of A are the set of functions or function templates formed as follows.

For each function or function template f in the guides of the template named by the simple-template-idof the defining-type-id, the template arguments of the return type of fare deduced from the defining-type-id of Aaccording to the process in [temp.deduct.type]with the exception that deduction does not fail if not all template arguments are deduced.

If deduction fails for another reason, proceed with an empty set of deduced template arguments.

Let g denote the result of substituting these deductions into f.

If substitution succeeds, form a function or function template f'with the following properties and add it to the set of guides of A:

The arguments of a template A are said to be deducible from a type T if, given a class templatetemplate <typename> class AA;with a single partial specialization whose template parameter list is that of A and whose template argument list is a specialization of Awith the template argument list of A ([temp.dep.type]),AA<T> matches the partial specialization.

Initialization and overload resolution are performed as described in [dcl.init] and [over.match.ctor], [over.match.copy], or [over.match.list] (as appropriate for the type of initialization performed) for an object of a hypothetical class type, where the guides of the template named by the placeholder are considered to be the constructors of that class type for the purpose of forming an overload set, and the initializer is provided by the context in which class template argument deduction was performed.

The following exceptions apply:

All such notional constructors are considered to be public members of the hypothetical class type.

[Example 2: template <class T> struct A { explicit A(const T&, ...) noexcept; A(T&&, ...); };int i; A a1 = { i, i }; A a2{i, i}; A a3{0, i}; A a4 = {0, i}; template <class T> A(const T&, const T&) -> A<T&>; template <class T> explicit A(T&&, T&&) -> A<T>; A a5 = {0, 1}; A a6{0,1}; A a7 = {0, i}; A a8{0,i}; template <class T> struct B { template <class U> using TA = T;template <class U> B(U, TA<U>);}; B b{(int*)0, (char*)0}; template <typename T> struct S { T x; T y;};template <typename T> struct C { S<T> s; T t;};template <typename T> struct D { S<int> s; T t;}; C c1 = {1, 2}; C c2 = {1, 2, 3}; C c3 = {{1u, 2u}, 3}; D d1 = {1, 2}; D d2 = {1, 2, 3}; template <typename T> struct E { T t;decltype(t) t2;}; E e1 = {1, 2}; template <typename... T> struct Types {};template <typename... T> struct F : Types<T...>, T... {};struct X {};struct Y {};struct Z {};struct W { operator Y(); }; F f1 = {Types<X, Y, Z>{}, {}, {}}; F f2 = {Types<X, Y, Z>{}, X{}, Y{}}; F f3 = {Types<X, Y, Z>{}, X{}, W{}}; — _end example_]

[Example 3: template <class T, class U> struct C { C(T, U); };template<class T, class U> C(T, U) -> C<T, std::type_identity_t<U>>; template<class V> using A = C<V *, V *>;template<std::integral W> using B = A<W>;int i{};double d{}; A a1(&i, &i); A a2(i, i); A a3(&i, &d); B b1(&i, &i); B b2(&d, &d);

Possible exposition-only implementation of the above procedure: template <class> class AA;template <class V> class AA<A<V>> { };template <class T> concept deduces_A = requires { sizeof(AA<T>); };template<class T, class U> auto f1(T, U) -> C<T, U>;template<class V> requires deduces_A<C<V *, V *>> auto f1_prime(V *, V*) -> C<V *, V *>;template<class T, class U> auto f2(T, U) -> C<T, std::type_identity_t<U>>;template<class V, class U> requires deduces_A<C<V *, std::type_identity_t<U>>> auto f2_prime(V *, U) -> C<V *, std::type_identity_t<U>>;template <class> class BB;template <class V> class BB<B<V>> { };template <class T> concept deduces_B = requires { sizeof(BB<T>); };template<std::integral W> requires deduces_A<C<W *, W *>> && deduces_B<C<W *, W *>> auto f1_prime_for_B(W *, W *) -> C<W *, W *>;template<std::integral W, class U> requires deduces_A<C<W *, std::type_identity_t<U>>> && deduces_B<C<W *, std::type_identity_t<U>>> auto f2_prime_for_B(W *, U) -> C<W *, std::type_identity_t<U>>;

— _end example_]