使用命名空间是否导致名称隐藏? [英] Does using namespace cause name hiding?

查看:160
本文介绍了使用命名空间是否导致名称隐藏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

namespace C {
    class X {};
}

namespace A {
    class X {};

    namespace B {
        using namespace C;

        X x;
    }
}

我期待类型 使用命名空间指令,因此 C :: X 编译器在 B 中解析 X c $ c>为 A :: X 。使用声明(使用C :: X )更改using指令,然后它会解析为 C :: 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.

标准对使用指令[7.3.4.2]说:

The standard says on using directives [7.3.4.2]:


using-directive指定指定命名空间中的名称可以在使用指令出现在use-directive之后的范围中使用。在非限定名称查找(3.4.1)中,名称看起来好像它们在包含using-directive和指定命名空间的最近的包围命名空间中声明的那样。

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.

我的看法是, C :: X 看起来好像在命名空间中声明 B ,有效隐藏 A :: X 。标准的哪些部分背后是使用指令和使用声明之间的这种不一致?有什么方法可以通过使用指令从外部范围隐藏一个名称?

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指定在指定的命名空间中的名称可以在use-directive出现在use-directive之后的范围中使用。在非限定名称查找(3.4.1)中,名称显示为在包含 both using-directive和指定命名空间的最近的包围命名空间中声明。

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 use-directive不会向它出现的声明区域添加任何成员。

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

也就是说, using-directive 将命名空间的成员添加到指令和使用的命名空间的公共命名空间祖先的查找集中,而不是直接添加到 using-指令。这在第二个引号中明确说明:它不会向 using-directive 的声明区域添加任何成员。

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.


7.3.4p4 [...]另一个例子

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.

所以看来,在你的情况下, em> using-directive 不隐藏,而是隐藏。

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

这篇关于使用命名空间是否导致名称隐藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆