Unqualified name lookup: Why local declaration hides declaration from using directive

a 夏天 提交于 2019-12-18 08:31:22

问题


Consider this code:

namespace A
{
   int i = 24;
}

namespace B
{
    using namespace A;
    int i = 11;

    int k = i; // finds B::i, no ambiguity
}

And basic.lookup.unqual.2:

§6.4.1 Unqualified name lookup [basic.lookup.unqual]

  1. The declarations from the namespace nominated by a using-directive become visible in a namespace enclosing the using-directive; see [namespace.udir]. For the purpose of the unqualified name lookup rules described in [basic.lookup.unqual], the declarations from the namespace nominated by the using-directive are considered members of that enclosing namespace.

For me the standard says pretty clear that for the purpose of unqualified name lookup (the i in int k = i) the declaration of i from A is considered member of B so i should be ambiguous in int k = i, however both gcc and clang compile and resolve i to the local B::i. I have searched the standard (basic.scope.hiding and namespace.udir) and did not find an exception or a rule to contradict the above one. I have found that for qualified name lookup, but not for unqualified name lookup.

Why is i unambiguous?


回答1:


The key is 10.3.4/2 "During unqualified name lookup, the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace."

The nominated namespace is A, the using directive is in B, and the smallest (in fact only) common namespace is the global namespace. Thus i appears as if declared in the global namespace, and is hidden by B::i.



来源:https://stackoverflow.com/questions/48359856/unqualified-name-lookup-why-local-declaration-hides-declaration-from-using-dire

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