Does using namespace cause name hiding?

Consider the following code:

namespace C {
    class X {};
}

namespace A {
    class X {};

    namespace B {
        using namespace C;

        X x;
    }
}

I was expecting the type of x to be C::X due to the using namespace directive, but instead both VS2010 and online LLVM/Clang compiler resolve X within the namespace B to be A::X . Changing the using directive with a using declaration ( using C::X ), then it does resolve to C::X as expected.

The standard says on using directives [7.3.4.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.

My reading of this is that C::X should appear as if declared within namespace B , effectively hiding A::X . Which section(s) of the standard are behind this inconsistency between using directives and using declarations? Is there any way to hide a name from an outer scope by a using directive?


The chapter on using directive seems to be somehow clear that you are seeing the expected behavior:

7.3.4p2 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.

7.3.4p3 A using-directive does not add any members to the declarative region in which it appears.

That is, the using-directive adds the members of the namespace to the lookup set of the common namespace ancestor of the directive and the used namespace, not directly to the scope where the using-directive is used. This is explicitly stated in the second quote: it does not add any members to the declarative region of the using-directive.

Later there is an example that is meant to illustrate something else but actually shows this:

7.3.4p4 [...] For another example

namespace A {
  int i;
}
namespace B {
  int i;
  int j;
  namespace C {
    namespace D {
      using namespace A;
      int j;
      int k;
      int a = i; // B::i hides A::i
    }

That last example is used to clarify transitivity (and contains more code), but it actually is equivalent to your code once you remove the extra code.

So it seems that in your case, the using-directive is not hiding, but rather being hidden.

链接地址: http://www.djcxy.com/p/29768.html

上一篇: 传统的标准C库标题和重载的C ++函数

下一篇: 使用名称空间会导致名称隐藏吗?