gpt4 book ai didi

c++ - 非唯一 namespace 限定查找的代码示例

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:22:18 25 4
gpt4 key购买 nike

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

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.

引自 3.4.3.2/3:

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. Otherwise if the use of m is not one that allows a unique declaration to be chosen from S(X, m), the program is ill-formed.

你能举个例子来证明这个规则吗:

if the use of m is not one that allows a unique declaration to be chosen from S(X, m), the program is ill-formed.

最佳答案

标准草案实际上提供了一个示例,虽然它不是很容易找到,但在第 3 段下,如下所示,给出以下内容:

namespace A {
using namespace Y;
void f(int);
void g(int);
int i;
}
namespace B {
using namespace Z;
void f(char);
int i;
}
namespace AB {
using namespace A;
using namespace B;
void g();
}

隐藏在示例中的是以下内容:

AB::i++; // i is not declared directly in AB so the rules are
// applied recursively to A and B,
// S is { A::i , B::i } so the use is ambiguous
// and the program is ill-formed

关于c++ - 非唯一 namespace 限定查找的代码示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24268240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com