Search code examples
c++linuxstringstream

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


I was just trying to compile the following code on Linux after having perfect success on Windows:

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

The error I receive for trying to reasign the std::stringstream is:

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


Solution

  • std::stringstream is not copyable, but can be only moved (since C++11). My guess is that you use g++4.9 or earlier, which even if it supports C++11, it doesn't fully support the move semantics for streams. g++5 and later compiles your code.

    Reported bug dates back to 4.7, fixed in 5.x https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316