I know C++11 has move semantics from this link: Elements of Modern C++ Style
But it does not introduce how to return a vector using move semantics. How to do this?
I know C++11 has move semantics from this link: Elements of Modern C++ Style
But it does not introduce how to return a vector using move semantics. How to do this?
Like this:
std::vector<std::string> make_a_vector_of_strings()
{
std::vector<std::string> result;
// just an example; real logic goes here
result.push_back("Hello");
result.push_back("World");
return result;
}
The operand of the return statement is eligible for copy elision, and if the copy is not elided, the operand is considered for the move-constructor of the return type, so everything is as good as it can be.