有没有办法将std :: move std :: string转换为std :: stringstream

W.F*_*.F. 13 c++ move-semantics c++11 c++14

在c ++引用中,我没有看到std::stringstream构造函数接受rvalue引用std::string.是否有任何其他辅助函数将字符串移动到字符串流而没有开销,或者是否有一个特殊的原因来制定这样的限制?

Pet*_*eld 10

您将能够将字符串移动到 C++20 中的字符串流中。

构造函数支持移动语义:

std::string myString{ "..." };
std::stringstream myStream{ std::move(myString) };
Run Code Online (Sandbox Code Playgroud)

也可以在构建后通过调用来完成str()

std::string myString{ "..." };
std::stringstream myStream;
myStream.str(std::move(myString));
Run Code Online (Sandbox Code Playgroud)


eml*_*lai 7

我没有看到std::stringstream构造函数接受rvalue引用std::string

那就对了.即使是strsetter也不使用移动语义,因此stringstream不支持移动字符串(不是在当前标准中,但希望在下一个标准中).