【发布时间】:2014-04-05 09:17:30
【问题描述】:
这是一个 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有一个隐式声明的构造函数。所以构造使用聚合初始化。聚合不能有基类,所以inherit不是聚合,因此不能使用聚合初始化。 -
有一个关于聚合初始化的链接。 en.cppreference.com/w/cpp/language/aggregate_initialization
标签: c++ inheritance c++11 constructor