Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

visual c++ why does std::move crash

I want to append a vector to the end of another vector. According to my knowledge, the function std::move() is the "function of choice" for this task. Why is std::move() from Microsoft Visual C++ Express crashing while a hand-crafted loop works as expected?

I am using Microsoft Visual C++ 2015 Update 3. Unfortunately I'm not able to test this with other compilers.

// The setup code for the two vectors:
vector<unique_ptr<channel>> channels, added_channels;

// ...  here is some code that adds some elements to both vectors

According to my knowledge the following two pieces of code should work the same way. They should move the elements of added_channels to the end of channels.

This is the first variant that crashes:

std::move(added_channels.begin(), added_channels.end(), channels.end());

This is the second version that works:

for(auto & ref : added_channels)
{
    channels.push_back(std::move(ref));
}
like image 923
user23573 Avatar asked Jan 22 '18 11:01

user23573


1 Answers

std::move moves into the the specific location.
If you want to insert it to the back of the vector, you should use std::back_inserter

std::move(added_channels.begin(), added_channels.end(),  std::back_inserter(channels));
like image 73
Yochai Timmer Avatar answered Sep 21 '22 14:09

Yochai Timmer