std::stringstream 重新分配适用于 Visual Studio 2013,但不适用于 Linux

std::stringstream reasignment works on Visual Studio 2013 but not under Linux

在 Windows 上取得完美成功后,我只是想在 Linux 上编译以下代码:

std::string str = stream.str();

auto decrement = [](char c) { return c - 100; };

std::transform(str.begin(), str.end(), str.begin(), decrement);
stream = std::stringstream(str); // LINE ACCUSING ERROR

我在尝试重新分配 std::stringstream 时收到的错误是:

158: error: use of deleted function 'std::basic_stringstream& std::basic_stringstream::operator=(const std::basic_stringstream&)' stream = std::stringstream(str); ^

std::stringstream 不可复制,但只能移动 (C++11 起)。我的猜测是您使用 g++4.9 或更早版本,即使它支持 C++11,它也不完全支持流的移动语义。 g++5 及更高版本编译您的代码。

报告的错误可追溯到 4.7,已在 5.x https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316

中修复