我使用的是g ++ 4.8.0,它不包含早期的constexprbug.因此下面的代码工作正常:
constexpr int size() { return 5; }
int array[size()];
int main () {}
Run Code Online (Sandbox Code Playgroud)
但是,如果我将变量括在一个classas中static,那么它会给出编译器错误:
struct X {
constexpr static int size() { return 5; }
static const int array[size()];
};
int main () {}
Run Code Online (Sandbox Code Playgroud)
这是错误:
错误:数组'array'的大小不是整数常量表达式
禁止以constexpr这种方式使用还是使用另一个g ++ bug?
struct sa
{
struct sb { int a = 123;};
inline static sb b;
};
Run Code Online (Sandbox Code Playgroud)
上面的代码生成错误:
main.cpp:25:20: error: default member initializer for ‘sa::sb::a’ required before the end of its enclosing class
inline static sb b;
^
main.cpp:24:21: note: defined here
struct sb { int a = 123;};
^~~~~~
Run Code Online (Sandbox Code Playgroud)
删除inline关键字或默认成员初始化程序有效.但仅从输出,我不明白为什么这种用法是错误的.