Date
2013-12-06.00:00:00
Message id
6784

Content

The formatted input function requirement says in [istream.formatted.reqmts]:

"If an exception is thrown during input then ios::badbit is turned on in *this's error state. If (exceptions()&badbit) != 0 then the exception is rethrown."

while some formatted function may throw an exception from basic_ios::clear, for example in [bitset.operators] p6:

"If no characters are stored in str, calls is.setstate(ios_base::failbit) (which may throw ios_base::failure)"

So should this exception be considered as "an exception [...] thrown during input"? And here is an implementation divergence (or you can read the following as "a bug libc++ only has" :)

cin.exceptions(ios_base::failbit);
bitset<N> b;
try {
  cin >> b;  // type 'a' and return
} catch (...)
{}

Now cin.rdstate() is just failbit in libstdc++ (and Dinkumware, by PJ), but failbit & badbit libc++. Similar difference found in other places, like eofbit & badbid after std::getline.

PJ and Matt both agree that the intention (of badbit + rethrow) is "to signify an exception arising in user code, not the iostreams package".

In addition, I found the following words in unformatted input function's requirements ([istream.unformatted]):

If an exception is thrown during input then ios::badbit is turned on in *this's error state. (Exceptions thrown from basic_ios<>::clear() are not caught or rethrown.) If (exceptions()&badbit) != 0 then the exception is rethrown.

The content within the parenthesis is added by LWG defect 61, and does fix the ambiguity. However, it only fixed the 1 of 4 requirements, and it lost some context (the word "rethrown" is not seen before this sentence within this section).