3

I was recently working with std::bitset<0> and noticed something interesting with operator>>:

The "specification" states that:

If no characters are extracted, is.setstate(ios_base::failbit) is called.

Does this still apply when "all possible N characters" (with N == 0) have been read? If so, does this imply that trying to read from an istream into a bitset<0> must always set failbit?

The following unit test exhibits no consistent behavior across the compilers I had at hand:

BOOST_AUTO_TEST_CASE(io0) {
    try {
        std::stringstream ss{"1"};
        ss.exceptions(std::ios::badbit | std::ios::failbit);
        std::bitset<0> b;
        ss >> b;
        BOOST_TEST(false);
    } catch(...) {
        BOOST_TEST(true);
    }
}

MSVC 2019 sets the failbit, whilst GCC 7.3.0 does not!

1 Answer 1

2

The specification (no scare quotes) said the same thing (when this question was asked), so yes. (Of course, skipping whitespace first may provoke some other kind of failure instead.) I thought this a clear error in the standard, so I filed an issue which has since been resolved for C++20.

Sign up to request clarification or add additional context in comments.

My only observation from reading through the linked draft is that a similar case (std::array<T, 0>) is explicitly mentioned in the standard, whilst bitset<0> is not mentioned whatsoever...
@MFH: So it sounds like 0 is valid (array has to mention it because of its otherwise-invalid exposition-only array member).

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.