Address of an overloaded function

From cppreference.com
< cpp‎ | language

Besides function-call expressions, where overload resolution takes place, the name of an overloaded function may appear in the following 7 contexts:

1) initializer in a declaration of an object or reference
2) on the right-hand-side of an assignment expression
3) as a function call argument
4) as a user-defined operator argument
5) the return statement
6) explicit cast or static cast argument
7) non-type template argument

In each context, the name of an overloaded function may be preceded by address-of operator & and may be enclosed in a redundant set of parentheses.

In all these contexts, the function selected from the overload set is the function whose type matches the pointer to function, reference to function, or pointer to member function type that is expected by target: the object or reference being initialized, the left-hand side of the assignment, function or operator parameter, the return type of a function, the target type of a cast, or the type of the template parameter, respectively.

The type of the function must match the target exactly, no implicit conversions are considered (e.g. a function returning a pointer to derived won't get selected when initializing a pointer to function returning a pointer to base)

If the function name names a function template, then, first, template argument deduction is done, and if it succeeds, it produces a single template specialization which is added to the list of overloads to consider. If more than one function from the set matches the target, and at least one function is non-template, the template specializations are eliminated from consideration. If all candidates are template specializations, less specialized ones are removed if more specialized are available. If more than one candidate remains after the removals, the program is ill-formed.

Example

#include <iostream>
#include <algorithm>
#include <string>
#include <cctype> // int std::toupper(int)
#include <locale> // template<class CharT> CharT std::toupper(CharT,const locale&)
 
int f(int) { return 1; }
int f(double) { return 2; }
 
void g( int(&f1)(int), int(*f2)(double) ) {}
 
template< int(*F)(int) >
struct Templ {};
 
struct Foo {
    int mf(int) { return 3; }
    int mf(double) { return 4; }
};
 
// 5. return value
int (*(foo)(void))(int) {
    return f; // selects int f(int)
}
 
int main()
{
    // 1. initialization
    int (*pf)(double) = f; // selects int f(double)
    int (&rf)(int) = f; // selects int f(int)
    int (Foo::*mpf)(int) = &Foo::mf; // selects int mf(int)
 
    // 2. assignment
    pf = nullptr;
    pf = &f; // selects int f(double)
 
    // 3. function argument
    g(f, f); // selects int f(int) for the 1st argument
             // and int f(double) for the second
 
    // 4. user-defined operator
 
    // 6. cast
    std::string str = "example";
    std::transform(str.begin(), str.end(), str.begin(),
                   static_cast<int(*)(int)>(std::toupper)); // selects int toupper(int)
    std::cout << str << '\n';
 
    // 7. template argument
    Templ<f> t;  // selects int f(int)
}

Output:

EXAMPLE

References

  • C++11 standard (ISO/IEC 14882:2011):
  • 13.4 Address of overloaded function [over.over]
  • C++98 standard (ISO/IEC 14882:1998):
  • 13.4 Address of overloaded function [over.over]