使用声明和歧义声明的上下文

编程入门 行业动态 更新时间:2024-10-19 14:34:11
本文介绍了使用声明和歧义声明的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

第二秒有个引号. 3.4.3.2/3:

There is a quote from the sec. 3.4.3.2/3:

给出X :: m(其中X是用户声明的命名空间),或者给出:: m(其中 X是全局名称空间),如果S(X,m)是空集,则程序 格式不正确. 否则,如果S(X,m)仅有一个成员,或者 引用的上下文是使用声明(7.3.3),S(X,m) 是m的必需声明集.

Given X::m (where X is a user-declared namespace), or given ::m (where X is the global namespace), if S(X, m) is the empty set, the program is ill-formed. Otherwise, if S(X, m) has exactly one member, or if the context of the reference is a using-declaration (7.3.3), S(X, m) is the required set of declarations of m.

S(X,m)的定义如下. 3.4.3.2/2:

Definition of S(X,m) is the following sec. 3.4.3.2/2:

对于名称空间X和名称m,符合名称空间的查询集S(X, m)定义如下:令S(X,m)为所有声明的集合 X中的m和X的内联名称空间集(7.3.1).如果S(X,m)为 不为空,S(X,m)为S(X,m);否则,S(X,m)是 X和X中使用指令指定的所有命名空间Ni的S(Ni,m) 其内联名称空间集.

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, S(X, m) is the union of S(Ni , m) for all namespaces Ni nominated by using-directives in X and its inline namespace set.

现在考虑以下代码:

#include <iostream> using namespace std; namespace N { int cout = 6; } using namespace N; int main() { using ::cout;//error: reference to ‘cout’ is ambiguous return 0; }

我不明白这些错误.上面的代码不违反规则:

I don't understand thar error. The code above does not contradict to the rule:

如果S(X,m)仅具有一个成员,或者引用的上下文 是使用声明(7.3.3),S(X,m)是必需的 m的声明.

if S(X, m) has exactly one member, or if the context of the reference is a using-declaration (7.3.3), S(X, m) is the required set of declarations of m.

您能解释一下该规则的含义吗?

Can you explain the sense of that rule?

推荐答案

德米特里(Dmitry),我怀疑您在误解如果引用的上下文是 using-declaration "的意思. 在 using-declaration 的上下文中"并不表示当在 using-declaration 中使用时".相反,它的意思是当引用是 using-declaration 的主题时".假设您的代码更改如下:

Dmitry, I suspect you are misinterpreting what "if the context of the reference is a using-declaration" means. "In the context of a using-declaration" does not mean "when used in a using-declaration". It instead means "when the reference is the subject of a using-declaration". Suppose your code is changed as follows:

int main() { using N::cout; std::cout << "value=" << cout << '\n'; }

请注意在std::cout << "value=" << cout << '\n'中使用不合格的cout.该不合格引用的上下文是 using-declaration using N::cout.

Note the use of the unqualified cout in std::cout << "value=" << cout << '\n'. The context of that unqualified reference is the using-declaration using N::cout.

查看标准含义的另一种方法是, using-declaration 优先于 using-directive .

Another way to look at what the standard means is that a using-declaration takes precedence over a using-directive.

更多推荐

使用声明和歧义声明的上下文

本文发布于:2023-11-14 09:35:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586859.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声明   歧义   上下文

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!