gpt4 book ai didi

C++11 3.3.1p4 - 同一声明区域中的声明?

转载 作者:行者123 更新时间:2023-11-30 02:54:04 25 4
gpt4 key购买 nike

在 C++ 标准 3.3.1p4 中:

Given a set of declarations in a single declarative region, each of which specifies the same unqualified name, they shall all refer to the same entity.

下面这两个int声明是不是在同一个声明区,指定了同一个非限定名,引用了两个不同的实体?

int main()
{
int i;
{
int i;
}
}

引用如何不适用并导致其格式错误?

如果引用不适用于此,它适用于什么?

(请注意,第一个 i 的声明区域确实包含第二个 i,如 3.3.1p2 中的示例所示。)

最佳答案

它们不在同一个单个声明区域。内部 i 的声明区域限制在最里面的大括号内。

事实上 3.3.1/2 的代码与您自己的非常相似:

int j = 24;
int main() {
int i = j, j;
j = 42;
}

在那里,用于设置ij24,但是那个外部j的范围> 在 之后停止, 并在 处重新启动。这两个 j 变量是不同的,尽管它们位于文件声明区域中,原因与您的示例相同:它们是两个声明区域。

由于没有一个单一的声明区域,范围控制。 C++11 3.3.1/1 状态(粗体):

Every name is introduced in some portion of program text called a declarative region, which is the largest part of the program in which that name is valid, that is, in which that name may be used as an unqualified name to refer to the same entity. In general, each particular name is valid only within some possibly discontiguous portion of program text called its scope.

The scope of a declaration is the same as its potential scope unless the potential scope contains another declaration of the same name. In that case, the potential scope of the declaration in the inner (contained) declarative region is excluded from the scope of the declaration in the outer (containing) declarative region.

可能不连续在这里很重要,内部 i (在您的示例中)“缩小”或隐藏外部 i即使外部声明区域可能包含内部声明区域。

关于C++11 3.3.1p4 - 同一声明区域中的声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17376399/

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