【发布时间】:2012-01-20 08:26:26
【问题描述】:
我正在做这样的事情
类.hpp:
class Class {
private:
static const unsigned int arraySize;
int ar[arraySize+2];
};
类.cpp:
#include <Class.hpp>
const unsigned int arraySize = 384;
编译器(q++,基于 g++ 的 QNX 操作系统的 c++ 编译器)在编译包含 Class.hpp 的单元时给了我error: array bound is not an integer constant(不是在编译 Class.cpp 时)。
为什么这不起作用?我知道静态 const 成员可以用作数组绑定,由 C++ 标准保证(请参阅this anwser)。但是为什么编译器看不到static const + const 的结果为常量呢?
【问题讨论】:
-
对我来说编译得很好(gcc 4.6.1),因为它应该。可能是那个编译器的错误?
-
我更新了我的示例以更好地匹配我的真实代码。也许arraySize的前向声明有问题?
-
请看这里:DR#721。总是打算(并且在 C++11 中明确表示这种情况)常量的初始化程序应该在静态数据成员或常量变量用于使其限定为常量表达式的点处可见。跨度>