【问题标题】:gcc 4.9 bug in structures initialization?结构初始化中的 gcc 4.9 错误?
【发布时间】:2015-04-07 08:43:23
【问题描述】:

我有代码:

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;
}

我有 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

但是,它不能编译:

g++-4.9 -std=c++11 -Wall test.cpp -o test

而编译器的输出是:

test.cpp:15:22: error: uninitialized const member ‘B::a’
 const C test = {0, {}};
                      ^
test.cpp:15:22: error: uninitialized const member ‘B::a’

这是一个错误还是我只是不明白什么?

【问题讨论】:

  • 你试过 Cland 或 GCC 5 吗?
  • @usr1234567 在 clang 3.5 中编译时没有警告
  • 还有compiles on GCC trunk。看起来已经修复了一个错误。
  • @T.C.请将其添加为答案,如果您不想代表,则将其设为 CW。正如我们在许多元问题中看到的那样,cmets 并不意味着会留下来,而且我之前已经看到重要的 cmets 被删除了。如此重要的细节不属于 cmets。

标签: c++ c++11 gcc gcc4.9


【解决方案1】:

这是一个错误,本质上减少了 GCC 抱怨聚合初始化中未显式初始化的const 数据成员。例如

struct {const int i;} bar = {};

Fails 因为在bar 的初始化程序中没有i 的初始化子句。但是,该标准在§8.5.1/7 中指定

如果列表中的 initializer-clauses 少于 聚合中的成员,然后 每个未显式初始化的成员 应该从它的 brace-or-equal-initializer 初始化,或者,如果有 不是 brace-or-equal-initializer,来自一个空的初始化列表 (8.5.4).

因此代码初始化i(好像是= {}),而GCC的投诉是不正确的。

事实上,这个 bug 早在四年前就被报告为 #49132,并在 GCC 5 中得到修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    相关资源
    最近更新 更多