加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C中的“using namespace”是否具有传递性?

发布时间:2020-12-16 07:22:44 所属栏目:百科 来源:网络整理
导读:令我惊讶的是,以下代码在VC 2017上编译并打
令我惊讶的是,以下代码在VC 2017上编译并打印“X”:

#include <string>
#include <iostream>


namespace A {
    using namespace std;
}

namespace B {
    using namespace A;
}

namespace C {
    using namespace B;
    string a;
}

int main()
{
    C::a = "X";
    std::cout << C::a;
    return 0;
}

看起来像使用命名空间std从命名空间A到命名空间B工作到命名空间C.

这是Visual C中的错误还是与语言规范一致?

我原本期望使用命名空间std在封闭范围的末尾结束,这是在命名空间A的定义的末尾.

编辑:我知道this question的接受答案也回答了我的问题.但是这篇文章更多的是关于匿名命名空间,而这个关于使用命名空间指令的传递性.所以我认为这是一个更好的例子,这个问题是有道理的.

解决方法

是:

[C++14: 7.3.4/4]: For unqualified lookup (3.4.1),the using-directive is transitive: if a scope contains a using-directive that nominates a second namespace that itself contains using-directives,the effect is as if the using-directives from the second namespace also appeared in the first. [..]

所以,编译器是正确的;在这里使用namespace有效地将名称导入封闭的命名空间:

[C++14: 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. [..]

这里的“范围”是命名空间的范围;范围的内容不会因为遇到}而消失.但是,熟悉块范围有时会让人感觉到这种感觉.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读