c – 课堂成员的范围

在以下示例中,阵列v的大小是否保证为2或3?
static const int i = 3;

class X {

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

从标准来看,

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

我认为这意味着’我’将是2,重新评估的东西在这里意味着什么?

解决方法

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

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:

该示例与您的示例类似(使用枚举而不是静态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}; (或静态const int i = 3;,但是当已知完整类声明时,char v [i]会有所不同,因为我将被重新评估为2.

相关文章

/** C+⬑ * 默认成员函数 原来C++类中,有6个默认成员函数: 构造函数 析构函数 拷贝...
#pragma once // 1. 设计一个不能被拷贝的类/* 解析:拷贝只会放生在两个场景中:拷贝构造函数以及赋值运...
C类型转换 C语言:显式和隐式类型转换 隐式类型转化:编译器在编译阶段自动进行,能转就转,不能转就编译...
//异常的概念/*抛出异常后必须要捕获,否则终止程序(到最外层后会交给main管理,main的行为就是终止) try...
#pragma once /*Smart pointer 智能指针;灵巧指针 智能指针三大件//1.RAII//2.像指针一样使用//3.拷贝问...
目录<future>future模板类成员函数:promise类promise的使用例程:packaged_task模板类例程...