问题描述
在C ++ std 3.3.1p4中:
In C++ std 3.3.1p4:
在下面,不是两个 int
In the following, aren't the two
int
declarations in the same declarative region, specify the same unqualified name, and refer to two different entities?
int main()
{
int i;
{
int i;
}
}
报价如何不适用, ?
如果报价不适用于此,则适用于?
If the quote doesn't apply to this, what does it apply to?
第一个
i
的声明区域包括第二个 i
,如3.3.1p2中的示例所示。
(Note that the declarative region of the first
i
does include the second i
as demonstrated in the example in 3.3.1p2.)
推荐答案
它们不在同一个单一声明区域。
i
的声明区域限制在最内侧大括号内。
They are not in the same single declarative region. The declarative region of the inner
i
is limited to within the innermost braces.
其实
3.3.1 / 2
具有与您自己的代码非常相似的代码:
In fact
3.3.1/2
has code remarkably similar to your own:
int j = 24;
int main() {
int i = j, j;
j = 42;
}
其中
j
用于设置 i
的是 24
/ code>在,
后停止,并在}
下重新启动。这两个 j
变量是不同的,尽管事实上它们在文件声明区域,原因与您的示例相同:re是两个声明区域。
In that, the
j
used to set i
is the 24
one but the scope of that outer j
stops after the ,
and restarts at the }
. Those two j
variables are distinct despite the fact they're in the file declarative region for the same reasons as your example: the re are two declarative regions.
由于没有一个声明区域,范围控制。
C ++ 11 3.3.1 / 1
状态(我的粗体):
Since there is not a single declarative region, scope takes control.
C++11 3.3.1/1
states (my bold):
声明的范围是与其潜在范围相同,除非潜在范围包含相同名称的另一个声明。在这种情况下,内部(包含)声明性区域中的声明的潜在范围不包括在外部(包含)声明性区域中的声明范围。
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
,即使外部声明区域可以包围内部。
It's the
possibly discontiguous
that's important here, the inner i
(in your example) "descopes", or hides, the outer i
even though the outer declarative region may enclose the inner one.
这篇关于C ++ 11 3.3.1p4 - 在同一声明区域的声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!