Resolving addresses of overloaded functions (C++ only)
If you use an overloaded function name
f without
any arguments, that name can refer to a function, a pointer to a function,
a pointer to member function, or a specialization of a function template.
Because you did not provide any arguments, the compiler cannot perform
overload resolution the same way it would for a function call or for
the use of an operator. Instead, the compiler will try to choose the
best viable function that matches the type of one of the following
expressions, depending on where you have used f: - An object or reference you are initializing
- The left side of an assignment
- A parameter of a function or a user-defined operator
- The return value of a function, operator, or conversion
- An explicit type conversion
f, the compiler
matched the declaration with an expression of type pointer-to-function
or reference-to-function. If the compiler chose a declaration of a
nonstatic member function, the compiler matched that declaration with
an expression of type pointer-to-member function. The following example
demonstrates this: struct X {
int f(int) { return 0; }
static int f(char) { return 0; }
};
int main() {
int (X::*a)(int) = &X::f;
// int (*b)(int) = &X::f;
}The compiler will not allow the initialization of the
function pointer b. No nonmember function or static
function of type int(int) has been declared.If
f is a template function, the compiler
will perform template argument deduction to determine which template
function to use. If successful, it will add that function to the list
of viable functions. If there is more than one function in this
set, including a non-template function, the compiler will eliminate
all template functions from the set and choose the non-template function. If
there are only template functions in this set, the compiler will choose
the most specialized template function. The following example demonstrates
this: template<class T> int f(T) { return 0; }
template<> int f(int) { return 0; }
int f(int) { return 0; }
int main() {
int (*a)(int) = f;
a(1);
}The function call a(1) calls int
f(int).