The easiest way to create a non-operational stream is to actually not create a custom stream class but rather to disable an existing stream. For example, you can disable formatting to an std::ostream by setting its stream buffer to null:
std::ostringstream out; out.std::ostream::rdbuf(0); // any attempt to write anything to out will fail.
If you need a stream which successfully fails to format data you can create a stream buffer which doesn't store any bytes and is always successful. However, when using this stream buffer the actually formatting will be performed:
struct nullbuf: std::streambuf { std::streambuf::int_type overflow(std::streambuf::int_type c) { return std::char_traits<char>::not_eof(c); } }; // ... nullbuf buf; std::ostringstream out; out.std::ostream::rdbuf(&buf);
Note that I would also recommend not to have functions take a std::ostringstream as arguments. Instead, any function which doesn't construct the stream should travel in terms of std::ostream&. If your existing interfaces already take an std::ostringstream you can create a a null stream by deriving from std::ostringstream and setting the stream buffer appropriately:
class onullstream : private virtual nullbuf , public std::ostringstream { public: nullstring() : std::ios(this) , std::ostringstgream() { this->std::ostream::rdbuf(this); } };
std::ostringstreamandonullstreamare different parts of the inheritance tree. While both inherits fromstd::ostreamthey are otherwise unrelated. Instead I think the requirement to usestd::ostringstreamhere is a false requirement, something that you don't really need. For example, if you have a function that you call with the stream you would have it take astd::ostreamreference. Then you could pass astd::ostringstreamobject or aonullstreamobject.