gpt4 book ai didi

c++ - 在局部作用域中使用 extern 取消全局变量的阴影是否有效?

转载 作者:行者123 更新时间:2023-12-02 01:56:25 27 4
gpt4 key购买 nike

嵌套局部作用域中的这个 extern 声明是将全局 a 带回作用域的有效且已定义的方法吗?

int a = 1; // may be in another file
void main() {
int a = 2; // hides the global
{
cout << a << endl; // prints 2
cout << ::a << endl; // obviously 1
extern int a;
cout << a << endl; // also prints 1
}
}

最佳答案

这是 extern 的有效用法,尽管是一种晦涩难懂的用法。根据C++20标准([basic.link]/6):

The name of a function declared in block scope and the name of a variable declared by a block scopeextern declaration have linkage. If such a declaration is attached to a named module, the program isill-formed. If there is a visible declaration of an entity with linkage, ignoring entities declared outside theinnermost enclosing namespace scope, such that the block scope declaration would be a (possibly ill-formed)redeclaration if the two declarations appeared in the same declarative region, the block scope declarationdeclares that same entity and receives the linkage of the previous declaration. If there is more than one suchmatching entity, the program is ill-formed. Otherwise, if no matching entity is found, the block scope entityreceives external linkage. If, within a translation unit, the same entity is declared with both internal andexternal linkage, the program is ill-formed.

在OP的示例中, block 作用域声明extern int a;无法找到具有链接的匹配声明,因为本地声明int a = 2;没有链接隐藏了具有外部链接的全局声明int a = 1;。因此,extern int a; 默认为外部链接。另请参阅第 7 段:

When a block scope declaration of an entity with linkage is not found to refer to some other declaration,then that entity is a member of the innermost enclosing namespace. However such a declaration does notintroduce the member name in its namespace scope.

因此,extern int a;a 声明为最近的封闭命名空间(即全局命名空间)的成员。这意味着它与之前声明的全局 a 是相同的实体,因为两者都通过链接在同一命名空间中声明了一个具有相同名称的变量。换句话说,它为您提供了一种引用 block 内的全局变量的方法。

extern 的这种用法是从 C 继承的,但即使在 C 中也很晦涩。通常应避免使用它,而应使用 ::a

关于c++ - 在局部作用域中使用 extern 取消全局变量的阴影是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69572326/

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