班级成员范围

发布于 2024-12-11 16:36:51 字数 304 浏览 0 评论 0原文

在下面的例子中,数组v的大小保证是2还是3?

static const int i = 3;

class X {

    char v[i];
    static const int i = 2;
};

从标准来看,

3.3.6/2 类 S 中使用的名称 N 应在其上下文中以及在 S 的完整范围内重新评估时引用相同的声明

我认为这意味着“i”应为 2重新评估的真正含义是什么?

In the following example, will the size of array v guaranteed to be 2 or 3?

static const int i = 3;

class X {

    char v[i];
    static const int i = 2;
};

From the standard,

3.3.6/2 A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S

I think this means 'i' shall be 2 and what does the re-evaluation thing really means here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各自安好 2024-12-18 16:36:51

正确的行为是它应该导致错误,因为重新评估会改变含义:

第 3.3.6 节中的示例:

延伸到或超过类定义末尾的声明的潜在范围也延伸到由其成员定义定义的区域,即使成员是在类外部按词法定义的(这包括静态数据成员定义、嵌套数据成员定义)类定义、成员函数定义(包括成员函数体,以及构造函数 (12.1) 的构造函数初始化程序 (12.6.2))以及此类定义中跟随标识符的声明符部分的任何部分,包括参数声明子句和任何默认参数(8.3.6)[示例:

该示例与您的示例类似(使用 enum 而不是 static const int):

typedef int  c;
enum { i = 1 };
class X {
    char  v[i];    // error: i refers to ::i
                   // but when reevaluated is X::i
    int  f() { return sizeof(c); } // OK X::c
    char  c;
    enum { i = 2 };
};

当时 遇到 v[i] 时,编译器只知道 enum { i = 1 }; (或 static const int i = 3;),但是当完整的类声明已知, char v[i] 会有所不同,因为 i 将被重新评估为 2

The correct behavior is that it should cause an error because re-evaluation would change the meaning:

Example from section 3.3.6:

The potential scope of a declaration that extends to or past the end of a class definition also extends to the regions defined by its member definitions, even if the members are defined lexically outside the class (this includes static data member definitions, nested class definitions, member function definitions (including the member function body and, for constructor functions (12.1), the ctor-initializer (12.6.2)) and any portion of the declarator part of such definitions which follows the identifier, including a parameter-declaration-clause and any default arguments (8.3.6). [Example:

The example is similar to yours (using enum instead of a static const int):

typedef int  c;
enum { i = 1 };
class X {
    char  v[i];    // error: i refers to ::i
                   // but when reevaluated is X::i
    int  f() { return sizeof(c); } // OK X::c
    char  c;
    enum { i = 2 };
};

At the time v[i] is encountered, the compiler only knows about enum { i = 1 }; (or static const int i = 3;, but when the full class declaration is known, char v[i] would be different because i would be re-evaluated to 2.

寄离 2024-12-18 16:36:51

在这种情况下,数组大小应为 3。如果您逐行查看代码。编译器在构造数组时对 X::i 一无所知。如果当数组大小变为 2 时更改类内的行,那么我将首先隐藏。

Array size should be 3 in this case. If you look in your code line by line. Compliler know nothing about X::i when construct array. If you change lines inside class when size of array become 2 and second i will hide first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文