Files
2025-10-25 03:02:53 +03:00

7.1 KiB

[over.over]

12 Overloading [over]

12.3 Address of an overload set [over.over]

1

#

An expression that designates an overload set S and that appears without arguments is resolved to a function, a pointer to function, or a pointer to member function for a specific function that is chosen from a set of functions selected from S determined based on the target type required in the context (if any), as described below.

The target can be

an object or reference being initialized ([dcl.init], [dcl.init.ref], [dcl.init.list]),

the left side of an assignment ([expr.assign]),

a parameter of a function ([expr.call]),

a parameter of a user-defined operator,

the return value of a function, operator function, or conversion ([stmt.return]),

an explicit type conversion ([expr.type.conv], [expr.static.cast], [expr.cast]), or

a constant template parameter ([temp.arg.nontype]).

If the target type contains a placeholder type, placeholder type deduction is performed ([dcl.type.auto.deduct]), and the remainder of this subclause uses the target type so deduced.

The expression can be preceded by the & operator.

[Note 1:

Any redundant set of parentheses surrounding the function name is ignored ([expr.prim.paren]).

— end note]

2

#

If there is no target, all non-template functions named are selected.

Otherwise, a non-template function with type F is selected for the function type FT of the target type if F (after possibly applying the function pointer conversion ([conv.fctptr])) is identical to FT.

[Note 2:

That is, the class of which the function is a member is ignored when matching a pointer-to-member-function type.

— end note]

3

#

The specialization, if any, generated by template argument deduction ([temp.over], [temp.deduct.funcaddr], [temp.arg.explicit]) for each function template named is added to the set of selected functions considered.

4

#

Non-member functions, static member functions, and explicit object member functions match targets of function pointer type or reference to function type.

Implicit object member functions match targets of pointer-to-member-function type.

[Note 3:

If an implicit object member function is chosen, the result can be used only to form a pointer to member ([expr.unary.op]).

— end note]

5

#

All functions with associated constraints that are not satisfied ([temp.constr.decl]) are eliminated from the set of selected functions.

If more than one function in the set remains, all function template specializations in the set are eliminated if the set also contains a function that is not a function template specialization.

Any given non-template functionF0 is eliminated if the set contains a second non-template function that is more partial-ordering-constrained thanF0 ([temp.constr.order]).

Any given function template specializationF1 is eliminated if the set contains a second function template specialization whose function template is more specialized than the function template ofF1 according to the partial ordering rules of [temp.func.order].

After such eliminations, if any, there shall remain exactly one selected function.

6

#

[Example 1: int f(double);int f(int);int (*pfd)(double) = &f; // selects f(double)int (*pfi)(int) = &f; // selects f(int)int (pfe)(...) = &f; // error: type mismatchint (&rfi)(int) = f; // selects f(int)int (&rfd)(double) = f; // selects f(double)void g() {(int ()(int))&f; // cast expression as selector}

The initialization ofpfe is ill-formed because nof() with typeint(...) has been declared, and not because of any ambiguity.

— end example]

[Example 2: struct X {int f(int); static int f(long);};

int (X::*p1)(int) = &X::f; // OKint (*p2)(int) = &X::f; // error: mismatchint (*p3)(long) = &X::f; // OKint (X::*p4)(long) = &X::f; // error: mismatchint (X::*p5)(int) = &(X::f); // error: wrong syntax for// pointer to memberint (*p6)(long) = &(X::f); // OK — end example]

[Example 3: template struct X {void f(short) requires B; void f(long); template void g(short) requires B; template void g(long);};void test() {&X::f; // error: ambiguous; constraints are not considered&X::g; // error: ambiguous; constraints are not considered} — end example]

7

#

[Note 4:

If f and g are both overload sets, the Cartesian product of possibilities is considered to resolve f(&g), or the equivalent expression f(g).

— end note]

8

#

[Note 5:

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]