Move or swap a stringstream

2020-02-11 04:10发布

问题:

I want to move a stringstream, in the real world application I have some stringstream class data member, which I want to reuse for different string's during operation.

stringstream does not have a copy-assignment or copy constructor, which makes sense. However, according to cppreference.com and cplusplus.com std::stringstream should have a move assignment and swap operation defined. I tried both, and both fail.

Move assignment

#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 assignment for all three statements, which is true. However, I fail to see why it is not using the move-assignment, especially since std::move is supposed to return a rvalue reference. Stringstream should have a move assignment, as shown here: http://en.cppreference.com/w/cpp/io/basic_stringstream/operator%3D

PS: I'm working with c++11, hence rvalue-references are part of the 'world'.

Swap

This I found really strange, I copied example 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 cplusplus.com source: 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 swap a stringstream? How should I swap or move a stringstream?

回答1:

This is a missing feature on GCC : see bug 54316 , it has been fixed (you can thank Jonathan Wakely) 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 it also compiles the example with std::stringstream::swap



回答2:

I have an alternative to moving or swapping, one can also clear and set a stringstream to a new string:

    #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;
    }

It's a clean work around that does not require one to refactor code, except for the localized section where the swap is changed to a clear() and str().