3
votes

Is it possible to make a custom stream work like the stanadrd ones in regard for errors? That is by default use the good/fail/bad/eof bits rather than exceptions?

The boost docs only mention throwing an std::failure for stream errors and letting other error propagate (eg a badalloc from trying to allocate a buffer), however the boost code does not seem to catch these, instead relying on the user code to handle them, but all my existing code relies on the good(), bad() etc methods and the clear() method in cases where it needs to try again after an error.

1
Which Boost docs are you looking at? - John Zwinck
In boost.org/doc/libs/1_39_0/libs/iostreams/doc/guide/… "A third possibility would have been to follow the example of std::basic_streambuf and allow member functions of Filters and Devices to signal errors either by throwing exceptions or by returning designated error indicators. This was rejected because it would have complicated the specifications of the various Filter and Device concepts and made the internals of stream_buffer more difficult to understand and maintain." - akramer
@akramer: You should probably make your comment an answer. - André Caron

1 Answers

1
votes

From http://www.trip.net/~bobwb/cppnotes/lec08.htm

The error state can be set using:

void clear(iostate = 0);

The default value of zero results in ios_base::goodbit being set.

clear();

is therefore equivalent to

clear(0);

which is equivalent to

clear(ios_base::goodbit);

Note that ios_base::goodbit is a non-zero value. clear() might be used to set one of the other bits as part of a programmer's code for operator>>() for a particular object. For example:

if (bad_char) is.clear(ios_base::badbit); // set istream's badbit