我对point of declaration
和name-lookup
概念之间的形式耦合很感兴趣。特别是,当 nested-name-specifier
表示命名空间时,非限定名称查找会产生一组声明,如下所示:N4296::3.4.3.2 [namespace.qual]
For a namespace
X
and namem
, the namespace-qualified lookup setS(X,m)
is defined as follows: LetS0(X,m)
be the set of all declarations ofm
inX
and the inline namespace set ofX
(7.3.1). IfS0(X,m)
is not empty,S(X,m)
isS0(X,m)
; otherwise,S(X,m)
is the union ofS(Ni,m)
for all namespacesNi
nominated by using-directives inX
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; }
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; }
我提供的规则与声明点概念无关,但实际上我们可以看到它确实如此。因此,标准隐含地假定名称的 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/