我想将一个大的 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
的语义?
最佳答案
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
}
但请注意以下几点。 “移出”std::string
对象不是您应该断言
的对象。来自 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
在其资源消耗后返回 0)。
关于c++ - 将 std::string 移动到函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55280922/