11

Basically I want to have multiple member functions with same name, but different signature, spread in multiple base classes.

Example:

#include <iostream>

struct A
{
    void print(int) { std::cout << "Got an int!" << std::endl; }
};

struct B
{
    void print(double) { std::cout << "Got a double!" << std::endl; }
};

struct C : A, B {};

int main()
{
    C c;
    c.print((int)0);

    return 0;
};

But I got this error on clang:

main.cpp:18:7: error: member 'print' found in multiple base classes of different types
    c.print((int)0);
      ^
main.cpp:5:10: note: member found by ambiguous name lookup
    void print(int) { std::cout << "Got an int!" << std::endl; }
         ^
main.cpp:10:10: note: member found by ambiguous name lookup
    void print(double) { std::cout << "Got a double!" << std::endl; }

Why is it ambiguous? Even with different number of arguments I get the same error.

Is there any workaround to get similar behavior?

curiousguy
  • 8,038
  • 2
  • 40
  • 58
user3762200
  • 447
  • 1
  • 6
  • 17

1 Answers1

24

Use a using declaration in the derived class - it will fix your issues. It makes both overloads visible and viable to participate in the resolution.

struct C : A, B {
    using A::print;
    using B::print;
};

To answer why this is ambiguous: it is actually not about visibility, but about the inability to participate in the overload resolution, due to not being defined in the same scope. The using declaration pulls those methods in the C scope, so both of them become valid overloading resolution options.

Thanks to @Pete Becker for participating in this answer and pretty much creating this paragraph.

Fureeish
  • 12,533
  • 4
  • 32
  • 62
  • 6
    Both versions of `print` are visible in th derived class without the `using` directive. The problem is that they are not overloads because they are not defined in the same scope the `using` directive pulls both names into the derived class, and because of that, they are overloads. +1. – Pete Becker Aug 05 '18 at 00:11
  • @PeteBecker this is a really good explanation - please, let me add it to my answer. I didn't include it originally simply because I wasn't sure whether my thought process is correct (it actually was kinda off). Thank you – Fureeish Aug 05 '18 at 00:12
  • @ElProfesor care to elaborate on the edit? The standard says that it's called a "*`using` directive*" (`9.7.3` - `[namespace.udir]`). – Fureeish Jul 04 '19 at 08:54
  • @Fureeish See point 6 in https://en.cppreference.com/w/cpp/language/namespace btw, you called it *using declaration* at the very beginning as well. – JFMR Jul 04 '19 at 09:02
  • `using` directive --> `using namespace ...` – JFMR Jul 04 '19 at 09:04
  • 1
    Thank you for the sources, rolled back to your edit – Fureeish Jul 04 '19 at 09:05