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?