为什么这些具有外部链接的名称不表示同一实体?
Why don't these names with external linkage denote the same entity?
考虑以下代码片段:
#include <iostream>
int a;
void address_of_a(void) {
std::cout << &a << std::endl;
}
namespace N {
int a;
void address_of_a(void) {
std::cout << &a << std::endl;
}
};
int main(void) {
address_of_a();
N::address_of_a();
return 0;
}
全局命名空间和命名空间 N
被赋予外部链接 因为 3.5 [basic.link] 来自 N4567[= 的第 4 段37=] 说
An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has internal linkage. All other namespaces have external linkage...
另外,::a
和N::a
也给出了外部链接,因为它们不适用于3.5 [basic.link]第3段,
A name having namespace scope (3.3.6) has internal linkage if it is the name of
- a variable, function or function template that is explicitly declared static; or,
- a variable of non-volatile const-qualified type that is neither explicitly declared extern nor previously declared to have external linkage; or
- a data member of an anonymous union.
但到 3.5 [basic.link] 第 4 段,
... A name having namespace scope that has not been given internal linkage above has the same linkage as the enclosing namespace if it is the name of
- a variable; or
- ...
这意味着它们继承了与全局命名空间和N
相同的链接(=外部链接)。总之,它们应表示相同的实体 因为 3.5 [basic.link] 第 9 段说
Two names that are the same (Clause 3) and that are declared in different scopes shall denote the same variable, function, type, enumerator, template or namespace if
- both names have external linkage or both names have internal linkage and are declared in the same translation unit; and
- ...
但是,它们似乎表示不同的实体,因为它们的地址不一致。这是为什么?
参见第二个项目符号。
Two names that are the same (Clause 3) and that are declared in different scopes shall denote the same variable, function, type, enumerator, template or namespace if
both names have external linkage or else both names have internal linkage and are declared in the same translation unit; and
both names refer to members of the same namespace or to members, not by inheritance, of the same class; and
when both names denote functions, the parameter-type-lists of the functions (8.3.5) are identical; and
when both names denote function templates, the signatures (14.5.6.1) are the same.
::a
和 N::a
不引用同一命名空间的成员,因此它们不表示相同的变量。
考虑以下代码片段:
#include <iostream>
int a;
void address_of_a(void) {
std::cout << &a << std::endl;
}
namespace N {
int a;
void address_of_a(void) {
std::cout << &a << std::endl;
}
};
int main(void) {
address_of_a();
N::address_of_a();
return 0;
}
全局命名空间和命名空间 N
被赋予外部链接 因为 3.5 [basic.link] 来自 N4567[= 的第 4 段37=] 说
An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has internal linkage. All other namespaces have external linkage...
另外,::a
和N::a
也给出了外部链接,因为它们不适用于3.5 [basic.link]第3段,
A name having namespace scope (3.3.6) has internal linkage if it is the name of
- a variable, function or function template that is explicitly declared static; or,
- a variable of non-volatile const-qualified type that is neither explicitly declared extern nor previously declared to have external linkage; or
- a data member of an anonymous union.
但到 3.5 [basic.link] 第 4 段,
... A name having namespace scope that has not been given internal linkage above has the same linkage as the enclosing namespace if it is the name of
- a variable; or
- ...
这意味着它们继承了与全局命名空间和N
相同的链接(=外部链接)。总之,它们应表示相同的实体 因为 3.5 [basic.link] 第 9 段说
Two names that are the same (Clause 3) and that are declared in different scopes shall denote the same variable, function, type, enumerator, template or namespace if
- both names have external linkage or both names have internal linkage and are declared in the same translation unit; and
- ...
但是,它们似乎表示不同的实体,因为它们的地址不一致。这是为什么?
参见第二个项目符号。
Two names that are the same (Clause 3) and that are declared in different scopes shall denote the same variable, function, type, enumerator, template or namespace if
both names have external linkage or else both names have internal linkage and are declared in the same translation unit; and
both names refer to members of the same namespace or to members, not by inheritance, of the same class; and
when both names denote functions, the parameter-type-lists of the functions (8.3.5) are identical; and
when both names denote function templates, the signatures (14.5.6.1) are the same.
::a
和 N::a
不引用同一命名空间的成员,因此它们不表示相同的变量。