Ambiguous name lookup with using-directive

眉间皱痕 提交于 2019-12-19 16:32:12

问题


It's not allowed to put a namespace and a class with the same name into one declarative region, i.e.

namespace A {}
class A{};

is ill-formed (see §3.3.1/4). However, one can introduce the name of either one via a using-directive:

namespace N { namespace A {int i;} }

struct A {static int i;};

using namespace N;

int i = A::i; // The global struct, or namespace N::A?

Is this code ill-formed? VC++ thinks so, as well as Clang:

main.cpp:7:9: error: reference to 'A' is ambiguous
int i = A::i;
        ^
main.cpp:3:8: note: candidate found by name lookup is 'A'
struct A {static int i;};
       ^
main.cpp:1:25: note: candidate found by name lookup is 'N::A'
namespace N { namespace A {int i;} }
                        ^

However, GCC accepts it.

Who is right?


回答1:


The code is ill-formed. When looking up A, §7.3.4/6 steps in:

If name lookup finds a declaration for a name in two different namespaces, and the declarations do not declare the same entity and do not declare functions, the use of the name is ill-formed.

Here, the namespaces are the global namespace and N, and the entities are the namespace N::A and the class ::A.



来源:https://stackoverflow.com/questions/29883977/ambiguous-name-lookup-with-using-directive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!