Lin*_*gxi 1 c++ aggregate initialization language-lawyer c++14
以下代码在clang 3.6(C++ 14)上编译,但在GCC 5.3(C++ 14)上没有编译
#include <array>
#include <utility>
struct super: std::array<int, 3> {
using base = std::array<int, 3>;
template <typename... Ts>
super(Ts&&... xs)
: base{std::forward<Ts>(xs)...} {
// nop
}
};
int main() {
super obj(1, 2, 3);
}
Run Code Online (Sandbox Code Playgroud)
产生的错误消息是
/tmp/gcc-explorer-compiler116029-73-105rz4g/example.cpp: In instantiation of 'super::super(Ts&& ...) [with Ts = {int, int, int}]':
15 : required from here
9 : error: array must be initialized with a brace-enclosed initializer
: base{std::forward<Ts>(xs)...} {
^
9 : error: too many initializers for 'std::array<int, 3ul>'
Run Code Online (Sandbox Code Playgroud)
我想我正在用括号封闭的初始化器初始化基本聚合.没有?标准对这里的语法有什么看法?
这肯定是允许的; 请参阅[array.overview],它保证聚合初始化以array
您的示例所需的方式工作.您在GCC的大括号中遇到了一个错误,如果聚合是基类,则该错误无法正常工作:
struct A {int i[1];}; // Equivalent to libstdc++'s std::array<int, 1> implementation
// for this demonstration
struct C : A {
C() : A{0} {}
};
Run Code Online (Sandbox Code Playgroud)
这已在trunk中修复:使用GCC trunk进行演示.
归档时间: |
|
查看次数: |
117 次 |
最近记录: |