1

When the function is called it's clear how name-lookup and overload resolution is performed. But what happens when the funtion is not being called? E.g.

#include <iostream>

using std::cout;
using std::endl;

void foo(int){ cout << "foo(int)" << endl; }
void foo(int, int){ cout << "foo(int, int)" << endl; }

void (*baz)(int, int);


int main()
{ 
    baz = foo; //1
    baz(1, 1); 
}

DEMO

In that case we have two functions with the name foo and formally, the unqualified name lookup finds both them. The clause 13 of the Standard doesn't cover that case because it's concerned only in the function call context N3797:13.3/2 [over.match]:

Overload resolution selects the function to call in seven distinct contexts within the language:

3
  • 2
    foo is implicitly being converted to void (*)(int, int) so there is no overload resolution happening here -- you are implicitly selecting a specific overload when you assign to a function pointer of a particular type. Commented Jan 18, 2015 at 7:38
  • @cdhowie not quite clear. The name lookup finds both two functions and then tries to implcitly covert each function to void (*)(int, int), right? Commented Jan 18, 2015 at 7:41
  • A variable cannot have two values - so the compiler selects the one with the same type,. In this case it is foo(int,int) as given when you run your demo Commented Jan 18, 2015 at 7:42

1 Answer 1

2

The behavior in this case is governed by this verbiage in the C++11 standard (there is a similar section in N3797):

A use of an overloaded function name without arguments is resolved in certain contexts to a function, a pointer to function or a pointer to member function for a specific function from the overload set. ... The function selected is the one whose type is identical to the function type of the target type required in the context. -- ISO/IEC 14882:2011(E) §13.4 [over.over] (emphasis mine)

The standard overload resolution rules aren't used here because you are assigning to a function pointer type, so the compiler will simply select the function overload that exactly matches the type of the function pointer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.