【发布时间】:2015-05-29 22:36:25
【问题描述】:
以下代码在使用-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 的错误跟踪器中也找不到任何关于此的错误报告,所以我很容易出错。
【问题讨论】:
-
您认为这是一个已修复的错误吗?
标签: c++ c++11 gcc constexpr uniform-initialization