7

以下代码在使用 -std=c++11 时在 gcc 4.9.1 和 clang-3.6 中编译和运行:

struct Bar
{
    int x;
};

struct Foo
{
    static constexpr Bar bars[] = {1, 2, 3};
};

constexpr Bar Foo::bars[];

但是,它在 gcc 4.8.3 中失败,导致错误消息

./cpptest.cpp:14:43: error: could not convert '1' from 'int' to 'const Bar'
     static constexpr Bar bars[] = {1, 2, 3};
                                           ^
./cpptest.cpp:14:43: error: could not convert '2' from 'int' to 'const Bar'
./cpptest.cpp:14:43: error: could not convert '3' from 'int' to 'const Bar'

顺便说一句,如果我做同样的事情但创建bars一个静态 const 全局数组,它在 gcc 4.8 和 clang 中编译得很好。如果我将列表中的每个整数文字都用一对额外的{}.

那么这是 gcc 4.8 中的错误吗?标准说什么是合适的语法?当我省略额外的大括号时,调用了 c++11 统一初始化标准的哪一部分?

编辑:看起来标准说这应该调用聚合初始化,这应该允许“大括号省略”。因此,这似乎是 gcc 4.8 中的一个错误,已由 gcc 4.9 修复,但我对阅读该标准完全没有信心。我似乎也无法在 gcc 的错误跟踪器中找到任何关于此的错误报告,所以我很容易出错。

4

1 回答 1

2

为了做你想做的事,你需要在 Foo 中指定一个 constexpr 构造函数:

struct Bar
{
    constexpr Bar(int c) : x(c)
    {}

    int x;
};

struct Foo
{
    static constexpr Bar bars[] = {1, 2, 3};
};

constexpr Bar Foo::bars[];

显然 gcc 4.8.3 不会将大括号内的值隐式转换为 Bar 对象,而 gcc 4.9.1 会。

于 2015-04-08T19:41:26.923 回答