2
#include <iostream>
struct A {
  void test() { std::cout << "A\n"; }
};
struct B : A {
  void test() { std::cout << "B\n"; }
};
struct C : B {
  using A::test;
  using B::test;
};
int main() {
  C().test();  // Is this ambiguous?
  return 0;
}

In this example, g++ 8.1.0 compiles successfully and calls test() from B.

clang++ 3.8.0 reports: error: call to member function 'test' is ambiguous.

Which is correct? If it is g++, what is the rule that picks B::test over A::test?

TimK
  • 4,635
  • 2
  • 27
  • 27
  • This question might help you: https://stackoverflow.com/questions/42573771/why-is-this-call-to-member-function-ambiguous – Trevor Tracy Jun 13 '18 at 17:27

2 Answers2

1

I believe Clang is correct. According to [namespace.udecl]/13:

Since a using-declaration is a declaration, the restrictions on declarations of the same name in the same declarative region ([basic.scope]) also apply to using-declarations.

Since you can't declare two identical member functions, the same applies to using declarations.

rustyx
  • 80,671
  • 25
  • 200
  • 267
0

GCC lets it compile and test() call becomes the first declaration. In the example given, it'll call the A::test(). But ISO C++ defines it as ambiguous. Visual Studio and clang won't let you compile it. Additionally, this the VS error message: 'B::test': ambiguous call to overloaded function. In my opinion, GCC is wrong by letting it compile.

João Paulo
  • 6,300
  • 4
  • 51
  • 80