是的std ::阵列移动? [英] Is std::array movable?

查看:106
本文介绍了是的std ::阵列移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆