2

Why does this snippet compile (and prints "Fi Fi"):

#include <iostream>

using namespace std;

void f(int) { cout << "Fi" << endl; }
void f(float) { cout << "Ff" << endl; }

struct A
{
   void f(int) { cout << "Fi" << endl; }
   void f(float) { cout << "Ff" << endl; }
};

int main()
{
  f(0);
  A().f(0);
}

but this one doesn't?

#include <iostream>

using namespace std;

struct A { void f(int) { cout << "Fi" << endl; } };
struct B { void f(float) { cout << "Ff" << endl; } };

struct C : A, B
{};

int main()
{
   C().f(0); // (1)
}

In my understanding, line (1) should call A::f, but the call is ambiguous instead.

ABu
  • 10,423
  • 6
  • 52
  • 103
  • I'm sure this is a duplicate, but I can't find it right now. I closed a similar question just a few days ago. – pmr Jun 04 '14 at 21:21
  • What compiler / version are you using (in case it is compiler specific) – Soren Jun 04 '14 at 21:22
  • @pmr I've found other similar question, but always related to virtual functions and so on, and not regular overloads. – ABu Jun 04 '14 at 21:23
  • 1
    The duplicate is exactly what you are asking. – pmr Jun 04 '14 at 21:23
  • @Soren The same as the one given by coliru (check the links). – ABu Jun 04 '14 at 21:23
  • It's because name lookup (that happens before overload resolution) is ambiguous - it finds name `f` in two different base classes. – jrok Jun 04 '14 at 21:24
  • Note that the error will be the same if you, for example, change `B:foo` to be data member instead of function. – jrok Jun 04 '14 at 21:26

0 Answers0