c – 通过using-directive在using声明中查找名称

c – 通过using-directive在using声明中查找名称,第1张

概述根据c标准,以下程序是否格式良好或格式不正确? namespace N { int i; }using namespace N;using ::i;int main() {} 我用不同的编译器得到不同的结果: > Clang(http://melpon.org/wandbox/permlink/c8vl7XbumyyS6vsw):没有错误. > GCC(http://melpon.org/w 根据c标准,以下程序是否格式良好或格式不正确?
namespace N { int i; }using namespace N;using ::i;int main() {}

我用不同的编译器得到不同的结果:

> Clang(http://melpon.org/wandbox/permlink/c8vl7XbumyyS6vsw):没有错误.
> GCC(http://melpon.org/wandbox/permlink/immhNeWFCMcCA800):错误:’我’没有声明.

根据c标准,该程序是否格式良好或格式不正确?需要参考c标准.

我正在试图找出应该提交错误的编译器.

解决方法 良好的.

using-directive不在全局命名空间中引入名称i,但在查找期间使用它. using-declaration使用限定查找来查找i;在使用指令的情况下进行限定查找在[3.4.3.2 p1,p2]中指定(引用来自N4527,当前的工作草案):

If the nested-name-specifIEr of a qualifIEd-ID nominates a namespace
(including the case where the nested-name-specifIEr is ::,i.e.,
nominating the global namespace),the name specifIEd after the
nested-name-specifIEr is looked up in the scope of the namespace. […]

For a namespace X and name m,the namespace-qualifIEd lookup set
S(X,m) is defined as follows: Let S'(X,m) be the set of all
declarations of m in X and the inline namespace set of X (7.3.1). If
S'(X,m) is not empty,S(X,m) is S'(X,m); otherwise,m) is the
union of S(Ni,m) for all namespaces Ni nominated
by using-directives in X and its inline namespace set.

因此,对于限定查找,第一步是查找直接在由nested-name-specifIEr(在本例中为::)指示的命名空间中生成的声明.没有这样的声明,因此查找然后进行到第二步,即在全局命名空间中由using-directives指定的所有命名空间中形成由限定查找找到的i的所有声明的集合.该集由N :: i组成,它是名称查找的结果,并通过using声明在全局名称空间中作为名称引入.

我发现值得注意(虽然很明显)这个限定查找的定义是递归的:在引号中使用符号,在每个命名空间中进行限定查找Ni首先会查找直接在Ni中生成的声明,然后,如果没有找到,将会接着继续查看Ni中using-directives指定的名称空间,依此类推.

对于它的价值,MSVC也接受代码.

总结

以上是内存溢出为你收集整理的c – 通过using-directive在using声明中查找名称全部内容,希望文章能够帮你解决c – 通过using-directive在using声明中查找名称所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1257872.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存