Consider the following example of a class super
that inherits from two base classes that each provide a method with same name, but different signatures:
struct a {};
struct b {};
class foo {
public:
void method(a const &) {}
};
class bar {
public:
void method(b const &) {}
};
class super : public foo, public bar {
public:
void something()
{
method(b {}); // <- Ambiguous?
}
};
This fails to compile with GCC and clang due to the compiler claiming that the call to method
is ambiguous:
overload.cpp: In member function ‘void super::something()’:
overload.cpp:18:5: error: reference to ‘method’ is ambiguous
18 | method(b {});
| ^~~~~~
overload.cpp:11:8: note: candidates are: ‘void bar::method(const b&)’
11 | void method(b const &) {}
| ^~~~~~
overload.cpp:6:8: note: ‘void foo::method(const a&)’
6 | void method(a const &) {}
| ^~~~~~
It looks like it is perfectly decidable which method to call depending on the method signature.
Is this failure to resolve the method call a compiler issue or is this behavior mandated by the C++ standard?
Our current workaround is to place using foo::method; using bar::method;
declarations in class super
. With that the compiler is able to resolve calls to method
without problem.