C++ 在C+;中私有继承聚合类的类的聚合初始化+;11

C++ 在C+;中私有继承聚合类的类的聚合初始化+;11,c++,inheritance,c++11,aggregate-initialization,C++,Inheritance,C++11,Aggregate Initialization,考虑以下代码: struct base { int x, y, z; }; struct derived : private base { using base::base; }; int main(int argc, const char *argv[]) { base b{1, 2, 3}; // Allowed derived d{1, 2, 3}; // Not allowed } 线导出d{1,2,3}使我的编译器(Clang 3.3)失败,错误为

考虑以下代码:

struct base
{
    int x, y, z;
};

struct derived : private base
{
    using base::base;
};

int main(int argc, const char *argv[])
{
    base b{1, 2, 3}; // Allowed
    derived d{1, 2, 3}; // Not allowed
}

线
导出d{1,2,3}使我的编译器(Clang 3.3)失败,错误为“没有用于初始化“派生”的匹配构造函数”。为什么会这样?有没有办法通过聚合初始化来初始化
派生的

派生的
有一个基类,所以它不是聚合(§8.5.1/1:“聚合是一个数组或一个类(第9条),其中[…]没有基类[…])

由于它不是聚合,因此无法使用聚合初始化对其进行初始化

最明显的解决方法可能是在base中添加一个
ctor
,并让
派生的ctor将参数传递给
base

struct base
{
    int x, y, z;

    base(int x, int y, int z) : x(x), y(y), z(z) {}
};

struct derived : private base
{
    derived(int a, int b, int c) : base(a, b, c) {}
};

int main(int argc, const char *argv[])
{
    base b{1, 2, 3}; // Allowed
    derived d{1, 2, 3}; // Allowed
}
当然,如果您将
base
设置为保持聚合状态,则这不起作用


编辑:对于已编辑的问题,我看不到在这里使用
std::initializer\u list
的方法--
std::array
没有任何东西可以接受
initializer\u list

派生的
有一个基类,所以它不是聚合(§8.5.1/1:“聚合是一个数组或一个类(第9条),其中[…]没有基类[…]”)

由于它不是聚合,因此无法使用聚合初始化对其进行初始化

最明显的解决方法可能是在base中添加一个
ctor
,并让
派生的ctor将参数传递给
base

struct base
{
    int x, y, z;

    base(int x, int y, int z) : x(x), y(y), z(z) {}
};

struct derived : private base
{
    derived(int a, int b, int c) : base(a, b, c) {}
};

int main(int argc, const char *argv[])
{
    base b{1, 2, 3}; // Allowed
    derived d{1, 2, 3}; // Allowed
}
当然,如果您将
base
设置为保持聚合状态,则这不起作用


编辑:对于已编辑的问题,我看不到在这里使用
std::initializer\u list
的方法--
std::array
没有任何东西可以接受
initializer\u list

啊,我现在觉得自己像个白痴。我猜唯一的解决办法是在
派生的
中添加一个构造函数,它采用一个初始值设定项列表并用它构造
?啊,我现在觉得自己像个白痴。我猜唯一的解决办法是向
派生的
中添加一个构造函数,它接受一个初始值设定项列表并用它构造