Why copying stringstream is not allowed?

C++Stringstream

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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

Solution 2 - C++

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.

Solution 3 - C++

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));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser756327View Question on Stackoverflow
Solution 1 - C++NawazView Answer on Stackoverflow
Solution 2 - C++Himadri ChoudhuryView Answer on Stackoverflow
Solution 3 - C++tvnView Answer on Stackoverflow