paul23
paul23

Reputation: 9435

std::basic_istream or std::basic_streambuf

Well I'm heavenly confused right now; When would anyone use a streambuffer over a stream - or otherwise?

Reading cppreference and some online topics regarding this only added to the confusion. To me it seems std::basic_istream is an "abstraction" of the buffer. So that one should not have to deal with localization etc.
But you still have to do this when reading file data - so what does it actually abstract away?

On the other hand, what does std::basic_streambuf bring?

And then there's the std::istream_iterator and std::istreambuf_iterator. Which both read elements from the "stream". This adds more confusion: is there any difference in above iterators?

PS: using istream here, but could of course also be ostream or anything else.
PPS: I should add that confusion was added while googling for examples such as this stackoverflow question

Upvotes: 0

Views: 1257

Answers (1)

Cubbi
Cubbi

Reputation: 47448

std::basic_istream defines user interface: operator>>, read, etc. That's what you call when you want to do input.

std::basic_streambuf defines virtual member functions: underflow, sync, etc. That's what you derive from when you want to write your own input class. boost.iostreams makes it easy.

std::istream_iterator calls operator>> (so it interprets the input as a sequence of objects of some type for which operator>> is defined, goes through locale, skips whitespace, etc)

std::istreambuf_iterator accesses a streambuf directly (so it can only read characters, no locale involved, whitespace isn't special)

Upvotes: 2

Related Questions