Can ostringstream rdbuf be changed?

I am trying the following code:

ostringstream oss;

streambuf *psbuf, *backup;

backup = oss.rdbuf();     
psbuf = cout.rdbuf();        
oss.rdbuf(psbuf);        
oss << things << endl;
oss.rdbuf(backup);   

But unfortunately, I get this error:

error: too many arguments to function call, expected 0, have 1
    oss.rdbuf(backup);   

But rdbuf has two overloads:

get (1) basic_streambuf <char_type, traits_type> * rdbuf () const;

set (2) basic_streambuf <char_type, traits_type> * rdbuf (basic_streambuf <char_type, traits_type> * sb);

Yes, I know what he says basic_streambuf, but it's really just a typedef typedef basic_streambuf<char> streambuf;, so it should work. Why doesn't it work?

+4
source share
2 answers

std::ostringstream rdbuf std::basic_ios, - , std::basic_ostream - rdbuf, .

-, std::ostringstream std::ostream, :

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

std::basic_ostringstream rdbuf?

rdbuf std::basic_streambuf*, std::ostringstream std::basic_stringbuf.

, .

std::basic_stringbuf, , std::ostringstream stringbuf , str std::basic_stringbuf (, std::basic_streambuf).

+4

ostringstream (typedef of basic_ostringstream<char>) rdbuf() -, -, , hided, - rdbuf() oss , ostream.

0

Source: https://habr.com/ru/post/1530545/


All Articles