Move or swap stringstream

I want to move a string stream, in a real world application, I have a data element of the stringstream class that I want to reuse for different strings while working.

stringstream does not have a copy or copy constructor, which makes sense. However, according to cppreference.com and cplusplus.com, std :: stringstream must have a move job and a swap operation. I tried both and both do not work.

Move destination

#include <string> // std::string #include <iostream> // std::cout #include <sstream> // std::stringstream int main () { std::stringstream stream("1234"); //stream = std::move(std::stringstream("5678")); stream.operator=(std::move(std::stringstream("5678"))); //stream.operator=(std::stringstream("5678")); return 0; } 

source: http://ideone.com/Izyanb

 prog.cpp:11:56: error: use of deleted function 'std::basic_stringstream<char>& std::basic_stringstream<char>::operator=(const std::basic_stringstream<char>&)' stream.operator=(std::move(std::stringstream("5678"))); 

The compiler states that there is no copy destination for all three statements, which is true. However, I donโ€™t understand why it doesnโ€™t use redirects, especially since std :: move should return an rvalue link. The string stream must have a move destination, as shown here: http://en.cppreference.com/w/cpp/io/basic_stringstream/operator%3D

PS: I work with C ++ 11, so rvalue links are part of the "world".

Exchange

This seemed really strange to me, I copied the sample code from cplusplus.com, and it failed:

 // swapping stringstream objects #include <string> // std::string #include <iostream> // std::cout #include <sstream> // std::stringstream int main () { std::stringstream foo; std::stringstream bar; foo << 100; bar << 200; foo.swap(bar); int val; foo >> val; std::cout << "foo: " << val << '\n'; bar >> val; std::cout << "bar: " << val << '\n'; return 0; } 

source: http://ideone.com/NI0xMS source cplusplus.com: http://www.cplusplus.com/reference/sstream/stringstream/swap/

 prog.cpp: In function 'int main()': prog.cpp:14:7: error: 'std::stringstream' has no member named 'swap' foo.swap(bar); 

What am I missing? Why can't I move or change the line? How do I swap or move a line?

+6
source share
2 answers

This is a missing feature in GCC: see bug 54316 , it has been fixed (you can thank Jonathan Wackel) for the next versions (gcc 5)

Clang with libC ++ compiles this code:

 int main () { std::stringstream stream("1234"); std::stringstream stream2 = std::move(std::stringstream("5678")); return 0; } 

Live demo

And he also compiles the example with std::stringstream::swap

+10
source

I have an alternative to moving or replacing, I can also clean and set the string stream to a new line:

  #include <string> // std::string #include <iostream> // std::cout #include <sstream> // std::stringstream int main () { std::stringstream ss("1234"); ss.clear(); ss.str("5678"); int val; ss >> val; std::cout << "val: " << val << '\n'; return 0; } 

This is pure work that does not require refactoring code, with the exception of the localized section, where the swap is changed to clear () and str ().

-2
source

Source: https://habr.com/ru/post/978733/


All Articles