std::array 是可移动的吗? [英] Is std::array movable?

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

问题描述

std::array 是否可移动?

Bjarne Native 2012 演示幻灯片(幻灯片 41)中,它列出了std::array 作为唯一不可移动的容器之一.

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 库源代码似乎确认 std::array 不可移动:

A quick look on gcc 4.8 libraries source code seems to confirm that std::array is not movable:

标准::向量:

/* @brief  %Vector move constructor.
   ...       */
  vector(vector&& __x) noexcept
  : _Base(std::move(__x)) { }

而在 std::array 中,唯一接收右值引用参数的方法是随机元素访问,这避免了通过复制返回:

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)); }

std::array 的移动构造函数和移动赋值是默认创建的,还是 std::array 不可移动?如果是不可移动的,为什么std::array不能移动而std::vector可以移动?

Is move-constructor and move-assignment for std::array defaulted created, or is std::array unmovable? If it is unmovable, why std::array cannot be moved while std::vector can?

推荐答案

std::array 仅当其包含的对象是可移动的时才是可移动的.

std::array is movable only if its contained objects are movable.

std::array 与其他容器有很大不同,因为容器对象包含存储,而不仅仅是指向堆的指针.移动一个 std::vector 只会复制一些指针,并且包含的​​对象并不明智.

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::array 使用默认的移动构造函数和赋值运算符.作为聚合类,不允许定义任何构造函数.

Yes, std::array uses the default move constructor and assignment operator. As an aggregate class, it's not allowed to define any constructors.

这篇关于std::array 是可移动的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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