Nik*_*sky 10 c++ gcc c++11 gcc4.9
我有代码:
struct A {
int a;
};
struct B {
int b;
const A a[2];
};
struct C {
int c;
const B b[2];
};
const C test = {0, {}};
int main()
{
return test.c;
}
Run Code Online (Sandbox Code Playgroud)
我有gcc 4.8.2和4.9.2.它可以很好地编译:
g++-4.9 -Wall test.cpp -o test
g++-4.8 -std=c++11 -Wall test.cpp -o test
g++-4.8 -Wall test.cpp -o test
Run Code Online (Sandbox Code Playgroud)
但是,无法编译:
g++-4.9 -std=c++11 -Wall test.cpp -o test
Run Code Online (Sandbox Code Playgroud)
编译器输出是:
test.cpp:15:22: error: uninitialized const member ‘B::a’
const C test = {0, {}};
^
test.cpp:15:22: error: uninitialized const member ‘B::a’
Run Code Online (Sandbox Code Playgroud)
这是一个错误还是我不明白的东西?
这是一个本质上减少为 GCC 抱怨聚合初始化中未显式初始化const数据成员的错误。例如
struct {const int i;} bar = {};\nRun Code Online (Sandbox Code Playgroud)\n\n失败i,因为in bar\ 的初始化程序中没有初始化程序子句。\xc2\xa78.5.1/7然而,该标准规定
\n\n\n如果列表中的初始化子句少于聚合中的成员,则每个未显式初始化的成员应从其大括号或等于初始化器初始化,或者,如果没有大括号,则应从其初始化子句初始化。 or-equal-initializer,来自空的初始值设定项列表\n (8.5.4)。
\n
因此,代码初始化i(就像通过= {}),并且 GCC 的抱怨是不正确的。
事实上,这个 bug 在四年前就已被报告为#49132,并在 GCC 5 中得到修复。
\n