The Tee filter from Boost.Iostreams can split an output stream into two.
Here's an example inspired heavily by the one given by Johannes Schaub in his answer here.
#include <sstream>
#include <iostream>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/tee.hpp>
int main()
{
namespace io = boost::iostreams;
typedef io::tee_device<std::ostream, std::stringstream> TeeDevice;
typedef io::stream<TeeDevice> TeeStream;
std::stringstream ss;
TeeDevice teeDevice(std::cout, ss);
TeeStream tee(teeDevice);
tee << "Hello World\n" << std::flush;
std::cout << "ss: " << ss.str() << "\n";
}
When I omit the flush manipulator, ss.str()
returns an empty string. I don't know whether or not this is the expected behaviour.