将 std::string 移动到函数参数

Move a std::string to function param

我想将大 std::string 移动到 class 的成员。

这是我的代码:

#include <cassert>
#include <string>
#include <iostream>
using namespace std;

class SomeClass {
public:
    void some_method(std::string&& str) {
        my_str = str;
    }

    std::string my_str;
};

int main() {
    SomeClass some_class;

    std::string large_str(100000, 'a');

    some_class.some_method(std::move(large_str));

    assert(large_str.empty());
    assert(!some_class.my_str.empty());
    return 0;
}

移动后,我预计 large_str 为空,但此代码断言在第 assert(large_str.empty()) 行失败。

我是不是误解了std::move的语义?

Did i misunderstand the semantics of std::move?

部分是的。您忘记将函数参数 str 转换为右值。在some_method内,它又变成了左值。要解决此问题:

void some_method(std::string&& str) {
    my_str = std::move(str);
    //       ^^^^^^^^^ Necessary: invoke std::move a 2nd time
}

但请注意以下几点。 "moved-from" std::string 对象不是你应该 assert 的对象。来自 here,重载 #8(强调我的):

Move constructor. Constructs the string with the contents of other using move semantics. other is left in valid, but unspecified state.

您不希望您的程序依赖于未指定的状态(即使碰巧特定实现在消耗其资源后使 std::string::size return 0)。