2

我知道 C++11 有来自这个链接的移动语义: 现代 C++ 风格的元素

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

4

1 回答 1

8

像这样:

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;
}

return 语句的操作数符合复制省略的条件,如果没有省略复制,则该操作数被视为返回类型的移动构造函数,因此一切都尽可能好。

于 2015-04-11T15:18:45.070 回答