28

I have std::stringstream object ss1. Now, I would like to create another copy from this one.

I try this:

std::stringstream ss2 = ss1;

or:

std::stringstream ss2(ss1)

neither works

The error message is like this:

std::ios::basic_ios(const std::ios &) is not accessible from bsl::basic_stringstream, bsl::allocator>::basic_stringstream(const bsl::basic_stringstream, bsl::allocator>&).

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
skydoor
  • 25,218
  • 52
  • 147
  • 201

2 Answers2

44

Indeed, streams are non-copyable (though they are movable).

Depending on your usage, the following works quite well:

#include <iostream>
#include <sstream>

int main()
{
    std::stringstream ss1;
    ss1 << "some " << 123 << " stuff" << std::flush;

    std::stringstream ss2;
    ss2 << ss1.rdbuf(); // copy everything inside ss1's buffer to ss2's buffer

    std::cout << ss1.str() << std::endl;
    std::cout << ss2.str() << std::endl;
}

Output:

some 123 stuff
some 123 stuff

GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • Is this more efficient than Pedro d'Aquino's answer, since it seems to be more straight forward. – alfC Mar 29 '13 at 01:02
  • @alfC: You have to profile to see, but just guessing this is likely more efficient because it copies from one buffer directly into another, and does not dynamically allocate a string in-between. – GManNickG Mar 29 '13 at 01:08
  • (Now I scrolled down to see the comment in the other answer. Sorry for the repeated question) – alfC Mar 29 '13 at 01:13
  • 5
    Notice: this will not work with ostringstream / wostringstream because rdbuf() is a reading operation. For `o` streams you still need str() anyway. – JustAMartin Jan 09 '14 at 14:20
  • Actually this is only a partial copy. All formattings are lost. They have to be copied manually. – TrueY May 09 '16 at 09:12
  • 3
    @GManNickG Isn't this not only a copy - but rather, depending on the implementation (of rdbuf), a move? Means that ss2 now has the data but ss1 does not? – David Feurle May 08 '18 at 10:49
11

As std::stringstream does not provide a copy constructor, you have to build it from the std::string ss1outputs:

std::stringstream ss2(ss1.str());
Pedro d'Aquino
  • 5,130
  • 6
  • 36
  • 46
  • 3
    The only problem I can see with this is it forces `ss1` to cough up a string, which will then be re-inserted into `ss2`. `rdbuf` just does it directly. – GManNickG Aug 09 '10 at 17:34
  • 1
    Agreed. I think this solution is less efficient than the one above, because it requires allocation of a string and two copies. – bhekman Dec 03 '12 at 05:16