debonair
debonair

Reputation: 2593

std::move() with different data types

I have boost::array<int,8> array1 and I have std::vector<int> temp(8); how do I perform std::move() from boost::array to std::vector, I want to avoid memcpy().

Upvotes: 1

Views: 650

Answers (3)

Jans
Jans

Reputation: 11250

boost::array and std::vector are unrelated types in the sense that std::vector knows nothing about how to be built out of boost::array.

On the other hand you can make use of std::move with iterators:

boost::array<int, 8> a;
std::vector<int> v;
v.reserve(a.size());

std::move(a.begin(), a.end(), std::back_inserter(v));

Upvotes: 5

NathanOliver
NathanOliver

Reputation: 180945

You can't just move the guts of the container into a std::vector. std::vector doesn't provide a way to take ownership of a buffer. You will have to copy/move the individual elements into the std::vector.


One thing you could do is move the individual elements into the vector using it's iterator constructor and std::make_move_iterator. You won't see any benefit with an int but if the type is faster to move than it is to copy then you will. That would look like

some_container foo;
// populate foo
std::vector<some_type> moved_into{std::make_move_iterator(std::begin(foo)),
                                  std::make_move_iterator(std::end(foo))};

Upvotes: 8

David Haim
David Haim

Reputation: 26506

You move something if it makes sense to move it.

array doesn't hold a dynamically allocated buffer, so there is nothing to "steal". int is not an object that makes sense to move, so again, you can't even move individual elements of the array.

In a nutshell, there is no meaning of moving an array<int>.

Upvotes: 0

Related Questions