Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 761
K-ballo Avatar asked May 24 '12 16:05

K-ballo


1 Answers

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.

like image 190
David Rodríguez - dribeas Avatar answered Sep 22 '22 08:09

David Rodríguez - dribeas