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));
}
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With