Even though std::ostringstream
inherits rdbuf
from std::basic_ios
that overload will not be accessible due to the fact that std::basic_ostream
also defines its own member-function named rdbuf
, effectively hiding the overload(s) you are looking for.
You can get access to the member-function by casting your std::ostringstream
instance to a std::ostream
reference as in the below example:
std::ostringstream oss;
std::ostream& oss_ref = oss;
auto prev = oss_ref.rdbuf (std::cout.rdbuf ());
oss << "hello world"; // will print to the buffer of std::cout
oss_ref.rdbuf (prev); // reset
static_cast<std::ostream&> (oss).rdbuf (...) // equivalent
Why does std::basic_ostringstream
define its own rdbuf
?
Normally rdbuf
returns a std::basic_streambuf*
but std::ostringstream
uses a std::basic_stringbuf
.
Since one cannot overload based on return-type a decision was made to effectively hide the former overload in favour of the new one.
By returning a std::basic_stringbuf
it's easy to see that std::ostringstream
uses a stringbuf internally, and it also provides easier access to the str
functions of std::basic_stringbuf
(functions which doesn't exist in std::basic_streambuf
).