我想将一个大的std :: string移动到一个类的成员。
这是我的代码:
#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
的语义吗?
我误解了
std::move
的语义吗?
部分是的。您忘了将函数参数str
强制转换为右值。在some_method
内,它再次变成了左值。解决这个问题:
void some_method(std::string&& str) {
my_str = std::move(str);
// ^^^^^^^^^ Necessary: invoke std::move a 2nd time
}
但请注意以下内容。一个“移动”std::string
对象是你应该assert
没什么。来自here,重载#8(强调我的):
移动构造函数。使用move语义构造带有其他内容的字符串。其他是有效但未指定的状态。
您不希望您的程序依赖于未指定的状态(即使特定实现使得std::string::size
在消耗其资源后返回0)。