Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

c++11 - Move or swap a stringstream

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 << '
';
  bar >> val; std::cout << "bar: " << val << '
';

  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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...