C ++ 11 3.3.1p4 - 在同一声明区域的声明? [英] C++11 3.3.1p4 - Declarations in same declarative region?

查看:157
本文介绍了C ++ 11 3.3.1p4 - 在同一声明区域的声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ std 3.3.1p4中:

In C++ std 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

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 - 在同一声明区域的声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆