当使用 -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
设为静态常量全局数组,它在 gcc 4.8 和 clang 中编译得很好。如果我用一对额外的 {}
包围列表中的每个整数文字,它也可以正常编译。
那么这是 gcc 4.8 中的错误吗?标准说什么是合适的语法?当我省略额外的大括号时,调用了 c++11 统一初始化标准的哪一部分?
编辑:看起来标准说这应该调用聚合初始化,这应该允许“大括号省略”。所以它似乎是 gcc 4.8 中的一个错误,由 gcc 4.9 修复,但我对我对标准的阅读完全没有信心。我似乎也无法在 gcc 的错误跟踪器中找到任何关于此的错误报告,所以我很容易出错。
最佳答案
为了做你想做的事,你需要在 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 会。
关于c++ - gcc 4.8 中静态 constexpr 成员数组的初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29269525/