Dea*_*hen 1 c++ move-semantics copy-elision c++11
我知道C++ 11从这个链接中移动了语义: 现代C++ Style的元素
但它没有介绍如何使用移动语义返回向量.这该怎么做?
像这样:
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;
}
Run Code Online (Sandbox Code Playgroud)
return语句的操作数适用于复制省略,如果复制没有被省略,则操作数被认为是返回类型的move-constructor,所以一切都尽可能好.