Resolving ambiguity with using declaration

后端 未结 1 726
日久生厌
日久生厌 2021-01-12 11:18

Look at this snippet:

namespace A {
int fn();
}
namespace B {
int fn();
}

// namespace Ns {

using namespace A;
using namespace B;
using A::fn;

int z = fn(         


        
相关标签:
1条回答
  • 2021-01-12 11:34

    See [namespace.udir]/2

    A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive. During unqualified name lookup (3.4.1), the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace.

    Thus, when you have the namespace Ns, the directives using namespace A; and using namespace B make A::fn and B::fn appear in the global namespace, whereas using A::fn; makes fn appear in Ns. The latter declaration "wins" during name lookup.

    0 讨论(0)
提交回复
热议问题