Search code examples
c++ifstreambitmaskc++-ios

C++ set "blank" or reset exception mask of ifstrean (ios)


Is there a way to reset the exception mask of an ios object to a "default" value? (Is it defined by the standard?) I was originally interested in ifstream, I got to ios tracing the inheritance of ifstream.

I found a workaround here, but this depends on having stored the original state of the mask for the object in a variable, and this may not be available.


Solution

  • The exceptions member functions is inherited from ios. It is a std::ios_base::iostate which has 3 possible bits to be set: badbit, failbit and eofbit. The "default state" is std::ios_base::goodbit (0). So just my_ifstream.exceptions(0) will reset it (my_ifstream.exceptions(std::ios::goodbit) will also work and be more clear).