56
votes
int main()
{
   std::stringstream s1("This is my string.");
   std::stringstream s2 = s1; // error, copying not allowed
}

I couldn't find a reason as to why i can't copy stringstream. could you provide some reference?

3
In case you are not aware of this, you can effectively achieve a copy using std::stringstream s2(s1.str());. - Björn Pollex
@Space: I think this would be more appropriate: std::stringstream s2; s2 << s1.rdbuf();. - GManNickG
@GManNickG Doesn't this has a lower performance? - StanE
@StanE: Than...what? Than the previous comment? I don't think so. You'd need to measure to be sure. - GManNickG
@StanE: The only way to be sure is to measure. :) - GManNickG

3 Answers

85
votes

Copying of ANY stream in C++ is disabled by having made the copy constructor private.

Any means ANY, whether it is stringstream, istream, ostream,iostream or whatever.

Copying of stream is disabled because it doesn't make sense. Its very very very important to understand what stream means, to actually understand why copying stream does not make sense. stream is not a container that you can make copy of. It doesn't contain data.

If a list/vector/map or any container is a bucket, then stream is a hose through which data flows. Think of stream as some pipe through which you get data; a pipe - at one side is the source (sender), on the other side is the sink (receiver). That is called unidirectional stream. There're also bidirectional streams through which data flows in both direction. So what does it make sense making a copy of such a thing? It doesn't contain any data at all. It is through which you get data.

Now suppose for a while if making a copy of stream is allowed, and you created a copy of std::cin which is in fact input stream. Say the copied object is copy_cin. Now ask yourself : does it make sense to read data from copy_cin stream when the very same data has already been read from std::cin. No, it doesn't make sense, because the user entered the data only once, the keyboard (or the input device) generated the electric signals only once and they flowed through all other hardwares and low-level APIs only once. How can your program read it twice or more?

Hence, creating copy is not allowed, but creating reference is allowed:

std::istream  copy_cin = std::cin; //error
std::istream & ref_cin = std::cin; //ok

Also note that you can create another instance of stream and can make it use the same underlying buffer which the old stream is currently using. See this : https://ideone.com/rijov

4
votes

To directly answer the question, you can't copy because the copy constructor for the stringstream class is declared as private.

It was probably declared that way because it seems awkward to copy a stream in most cases, so none of the stream classes have public copy constructors.

4
votes

As mentioned above you cannot copy stream, but if you need you could copy data:

std::stringstream from;
std::stringstream to;

std::copy(std::istream_iterator<char>(from), std::istream_iterator<char>(),
          std::ostream_iterator<char>(to));