3

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.

Danilo Piazzalunga
  • 7,590
  • 5
  • 49
  • 75
Fire Lancer
  • 29,364
  • 31
  • 116
  • 182
  • Which Boost docs are you looking at? – John Zwinck Nov 15 '09 at 01:22
  • http://www.boost.org/doc/libs/1_39_0/libs/iostreams/doc/index.html – Fire Lancer Nov 16 '09 at 16:01
  • 2
    In http://www.boost.org/doc/libs/1_39_0/libs/iostreams/doc/guide/exceptions.html#standard_iostreams "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 Dec 27 '11 at 21:02
  • @akramer: You should probably make your comment an answer. – André Caron Apr 03 '12 at 20:57

1 Answers1

1

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

gymbrall
  • 2,063
  • 18
  • 21
  • This doesn't answer the question. The question is about whether or not [Boost](http://www.boost.org/) I/O streams expose a similar interface. – André Caron Apr 03 '12 at 20:56
  • The question as it read when I answered it was asking about using a custom stream and then references the boost streams. It wasn't clear to me whether he is interested only in the boost streams as they are, if he subclassed them himself, or his own custom streams. – gymbrall Apr 03 '12 at 21:41