Overload Resolution/Ambiguity in name lookup(which one)

冷暖自知 提交于 2019-12-01 19:50:55

This is caused by a twist in name-lookup in C++03: Checking for an unambiguous subobject was part of class member name lookup in C++03. Lookup in C++03 will find D::X and C::x and A::x, where A::x matches, but is associated with two distinct subobjects of type A.

In C++0x, the checking for an unambiguous subobject is now part of the respective subclauses, see DR #39: The class where x is directly a member of is an ambiguous base - so clause 5 will cause a compile error, instead of clause 10.

Note that the comment talks about the subobjects of A. There is one subobject of A that goes over path B, and another subobject of A that goes over path C. This is why the comment says "B::x or C::x". The presence of multiple subobjects of the same class type can be determined by just trying to convert to its class type, ignoring accessibility issues: If the conversion is ambiguous, the subobject appeared multiple times.

Clang++ gives somewhat a combination of the errors produced by g++ and Comeau

C:\Users\SUPER USER\Desktop>clang++ chubsdad.cpp
chubsdad.cpp(12) :  error: ambiguous conversion from derived class 'D' to base class
      'A':
    struct D -> struct B -> struct A
    struct D -> struct C -> struct A
   return d->x(); // ambiguous: B::x or C::x
          ^
1 error generated.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!