I have a function what creates a vector, does somthing with it and then return it as an rvalue using std::move
.
When I am assigning it to a variable, I'm expecting to call std::vector
's move constructor std::vector::vector(vector&&)
.
But I'm getting a segmentation fault.
Here's my code:
#include <iostream>
#include <vector>
std::vector<int>&& getVector(int start)
{
auto result = std::vector<int>();
result.reserve(10);
for (int i = start; i < start + 10; i++)
result.push_back(i);
return std::move(result);
}
int main()
{
std::cout << "Did I break here 1" << std::endl;
auto vec = getVector(10);
std::cout << "Did I break here 2" << std::endl;
}
It gives me
Did I break here 1
[1] 55419 segmentation fault <executable_file>
I just guessing there's someting wrong with the move constructor or the rvalues. Btw I return an rvalue to not copy the vector's value an steal the pointer to it.