使用声明和歧义声明的上下文 [英] Context of using declaration and ambiguous declaration

查看:115
本文介绍了使用声明和歧义声明的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第二秒有个引号. 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.

这篇关于使用声明和歧义声明的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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