如何使用C++ 11移动语义从函数返回std :: vector?

Dea*_*hen 1 c++ move-semantics copy-elision c++11

我知道C++ 11从这个链接中移动了语义: 现代C++ Style的元素

但它没有介绍如何使用移动语义返回向量.这该怎么做?

Ker*_* SB 7

像这样:

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,所以一切都尽可能好.

  • @DeanChen:这两个想法听起来完全错了. (2认同)