gpt4 book ai didi

c++ - 名称查找和声明点概念

转载 作者:行者123 更新时间:2023-11-28 02:33:58 25 4
gpt4 key购买 nike

我对point of declarationname-lookup 概念之间的形式耦合很感兴趣。特别是,当 nested-name-specifier 表示命名空间时,非限定名称查找会产生一组声明,如下所示:N4296::3.4.3.2 [namespace.qual]

For a namespace X and name m, the namespace-qualified lookup set S(X,m) is defined as follows: Let S0(X,m) be the set of all declarations of m in X and the inline namespace set of X (7.3.1). If S0(X,m) is not empty, S(X,m) is S0(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.

让我举几个例子:

1.

#include <iostream>

namespace A
{
int b = 42;
}

int a = A::a; //Error

namespace A
{
int a = 24;
}

int main(){ std::cout << a << std::endl; }

DEMO

2.

#include <iostream>

namespace A
{
int b = 42;
}

namespace A
{
int a = 24;
}

int a = A::a; //OK

int main(){ std::cout << a << std::endl; }

DEMO

我提供的规则与声明点概念无关,但实际上我们可以看到它确实如此。因此,标准隐含地假定名称的 m 声明点应该在使用名称的点之前。我认为应该明确指定。也许我丢失了指定它的子句...如果是这样,你能指出那个吗?

最佳答案

来自 [basic.scope.namespace] (§3.3.6/1),强调我的:

A namespace member name has namespace scope. Its potential scope includes its namespace from the name’s point of declaration (3.3.2) onwards

a 只能在命名空间 A 声明之后 中找到。所以示例 (1) 无效,因为 a 尚未声明,示例 (2) 有效,因为它已经声明。

关于c++ - 名称查找和声明点概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28163389/

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