std :: array构造函数继承 [英] std::array constructor inheritance
问题描述
我试图获得 std :: array
的扩展变体(和数组向量一样暴露相同的接口 array
没有样板代码)。我知道 std :: valarray
但我想要固定大小在矩阵乘法中正确键入。因此,我 array
完美匹配。但是当我尝试继承构造函数时它失败。
struct vec2d:std :: array< float,2&
{using array :: array; }; // simplified
struct vec:std :: vector< float>
{using vector :: vector; };
std :: array< float,2> x = {1,2};
vec y = {1,2};
vec2d z = {1,2}; //错误:无法将{1,2}
//从& quot;包含初始化程序列表>'转换为vec2d
此错误为GCC 4.8.2和clang 3.4报告。最后说, vec2d
只有隐式的默认/复制/移动构造函数。是的,数组
只有隐式ctor与向量
相反,它们具有来自 initializer_list
。但是因为ctors是继承的,所以继承初始化的可能性是很自然的,就像初始化
array
一样。
问题:为什么我们有这个错误,而不是预期的行为(类似 array
初始化)?
注意:我可以手动编写转发以使其工作,但这看起来并不像ctor继承那么优雅。
struct vec2d:std :: array< float,2>
{
使用array :: array;
//讨厌的样板代码我不想在C ++ 11
template< typename ... Args>
vec2d(Args& ... args):array({float(std :: forward< Args>(args))...}){}
};
std :: array
被设计为一个聚合,所以它故意不定义任何构造函数。
不幸的是,这意味着它不可能继承它,
为什么需要继承 std :: array
无论如何?你计划添加任何私人成员吗?如果没有,那么你可以在 std :: array
上运行的自由函数构建你的框架,或者是一个typedef。
如果你真的想继承 std :: array
,你必须接受丢失聚合状态,并提供你自己想要的构造函数。 p>
I'm trying to get extended variant of std::array
for math vectors (and expose same interface as array
does without boilerplate code). I know about std::valarray
but I want fixed size for proper typing in matrix multiplications. Thus I array
fits perfectly. But when I try to inherit constructor it fails.
struct vec2d : std::array<float, 2>
{ using array::array; }; // simplified
struct vec : std::vector<float>
{ using vector::vector; };
std::array<float, 2> x = {1, 2};
vec y = {1, 2};
vec2d z = {1, 2}; // error: could not convert ‘{1, 2}’
// from ‘<brace-enclosed initializer list>’ to ‘vec2d’
This error reported for GCC 4.8.2 and for clang 3.4. Last says that vec2d
have only implicit default/copy/move constructors. Yes, array
have only implicit ctor in contrary to vector
which have ctor from initializer_list
. But since ctors are inherited it is natural to inherit possibility to initialize it in a same way as array
initialized.
Question: Why we have that error instead of expected behavior (similar to array
initialization)?
Note: I that I can write forwarding manually to make it work, but this doesn't look as elegant as ctor inheritance.
struct vec2d : std::array<float, 2>
{
using array::array;
// nasty boilerplate code I don't want to have in C++11
template <typename... Args>
vec2d(Args &&... args) : array({float(std::forward<Args>(args))...}) {}
};
std::array
is designed to be an aggregate, so it intentionally does not define any constructors.
Unfortunately, this means it's not possible to inherit from it and get the same behaviour, as aggregates cannot have base classes.
Why do you need to inherit from std::array
anyway? Do you plan to add any private members? If not, then you could just build your framework around free functions operating on std::array
, or perhaps a typedef to it.
If you really want to inherit from std::array
, you'll have to accept losing the aggregate status and provide any constructors you want yourself.
这篇关于std :: array构造函数继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!