是的std ::阵列移动? [英] Is std::array movable?
问题描述
是的std ::阵列活动?
在 Bjarne的原生2012 presentation幻灯片(幻灯片41),它列出标准: :阵列作为不是可动的唯一容器之一
In Bjarne Native 2012 presentation slides (slide 41) it lists std::array as one of the only containers that isn't movable.
在GCC 4.8源代码库code就让我们来看看,似乎确认的std ::数组是不可移动的:
A quick look on gcc 4.8 libraries source code seems to confirm that std::array is not movable:
的std ::向量:
/* @brief %Vector move constructor.
... */
vector(vector&& __x) noexcept
: _Base(std::move(__x)) { }
而在的std ::数组,它接收一个右值引用参数的唯一方法是随机元素访问,从而避免了通过复制一回:
while in std::array the only method that receives a rvalue reference parameter is the random element access, which avoids a return by copy:
get(array<_Tp, _Nm>&& __arr) noexcept
{ /*...*/ return std::move(get<_Int>(__arr)); }
是移动构造函数和放大器;布展assignement性病::阵列拖欠创建的,或者是性病::阵列不可移动?如果它是不可移动的,为什么的std ::阵列不能移动,而性病::向量可以吗?
推荐答案
的std ::阵列
是可移动的仅当它包含的对象是可移动的。
std::array
is movable only if its contained objects are movable.
的std ::阵列
从其他容器完全不同,因为容器对象包含存储,而不只是指针入堆。移动的std ::矢量
只复制一些指针,包含的对象是毫无收获。
std::array
is quite different from the other containers because the container object contains the storage, not just pointers into the heap. Moving a std::vector
only copies some pointers, and the contained objects are none the wiser.
是的std ::阵列
使用默认的移动构造函数和赋值操作符。作为一个聚合类,它不能定义任何的构造函数。
Yes, std::array
uses the default move constructor and assignment operator. As an aggregate class, it's not allowed to define any constructors.
这篇关于是的std ::阵列移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!