Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::array movable?

Is std::array movable?

In Bjarne Native 2012 presentation slides (slide 41) it lists std::array as one of the only containers that isn't movable.

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

std::vector:

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

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

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?

like image 799
Alessandro Stamatto Avatar asked Jan 17 '13 01:01

Alessandro Stamatto


People also ask

Can STD array be moved?

Quick A: Yes.

Is std :: array contiguous?

Yes the memory of std::array is contiguous.

Is std :: array static?

std::array is a container that encapsulates fixed size arrays. This container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only non-static data member. Unlike a C-style array, it doesn't decay to T* automatically.

Is std :: array dynamic?

std::array<std::vector<int>,3> is the type you want. std::vector is a dynamicly sized array. this creates a 3 "major" element array of 22 "minor" size.


Video Answer


1 Answers

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

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.

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

like image 51
Potatoswatter Avatar answered Sep 22 '22 18:09

Potatoswatter