微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

令我惊讶的是,以下代码在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. [..]

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐