这是 g++ 错误吗?
#include <array>
struct inherit : std::array<int , 3>{
using std::array<int , 3>::array;
};
std::array<int, 3> ok1 = {1,2,3};
inherit ok2;
inherit bad = {1,2,3};
实例化 bad
, 我得到 error: could not convert ‘{1, 2, 3}’ from ‘<brace-enclosed initializer list>’ to ‘inherit’
.在我看来它绝对正确。
最佳答案
std::array
是一个集合。它没有用户定义的构造函数。
来自 C++ 标准
1 An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).
类继承不是聚合,因为它有一个基类。所以它可能不使用大括号初始化(除了一个空的初始化列表)。如果你想用一个非空的初始化列表初始化它,你应该显式地为类继承定义构造函数,
关于c++ - 继承构造函数不允许类似数组的初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22161773/