I've a customized std::streambuf. Something like:
class mybuf : public std::streambuf {
...
virtual std::streambuf::int_type underflow() {
if(eof) {
return traits_type::eof();
}
...
}
...
std::istream stream(new mybuf(...));
Setting the eof flag of the stream is done by returning eof on the underflow() function.
How can I set the badbit flag of the stream? Should the overflow() function throw an exception?
Throwing an exception is indeed the way to go.
Refer to the reference page for std::ios_base::iostate, and specifically this :
The
badbitis set by the following standard library functions:
- ...
- Every stream I/O function if an exception is thrown by any member function of the associated stream buffer (e.g.
sbumpc(),xsputn(),sgetc(),overflow(), etc)- ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With