在GCC中使用Brace Initializer初始化成员STD ::数组时错误

Error when initializing a member std::array with a brace initializer in gcc

本文关键字:STD 成员 数组 错误 初始化 Initializer GCC Brace      更新时间:2023-10-16

考虑以下示例:

#include <array>
template <typename T>
struct A {
  A() {}
};
typedef A<int> B;
struct S {
  std::array<B, 1> b;
  S() : b{{B()}} {}
};
int main() {
  S s;
}

尝试用G 4.6.3和-std=c++0x编译时,我会得到此错误:

test.cc: In constructor ‘S::S()’:
test.cc:13:16: error: no matching function for call to ‘std::array<A<int>, 1ul>::array(<brace-enclosed initializer list>)’
test.cc:13:16: note: candidates are:
/usr/include/c++/4.6/array:60:12: note: std::array<A<int>, 1ul>::array()
/usr/include/c++/4.6/array:60:12: note:   candidate expects 0 arguments, 1 provided
/usr/include/c++/4.6/array:60:12: note: constexpr std::array<A<int>, 1ul>::array(const std::array<A<int>, 1ul>&)
/usr/include/c++/4.6/array:60:12: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const std::array<A<int>, 1ul>&’
/usr/include/c++/4.6/array:60:12: note: constexpr std::array<A<int>, 1ul>::array(std::array<A<int>, 1ul>&&)
/usr/include/c++/4.6/array:60:12: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::array<A<int>, 1ul>&&’

但是,定义变量时相同的支撑初始化可行:

std::array<B, 1> b{{B()}};

是因为GCC的特定版本没有完全实现C 11,或者我缺少某些内容,而第一个示例不正确?

是因为GCC的此特定版本未完全实现C 11

很可能是。GCC 4.6非常过时。

或我缺少一些东西,第一个示例不正确?

关于C 11语法,您的示例很好。它在Colliru上编译而没有错误或警告。