Why doesn't std::bitset come with iterators?

Trevor Hickey picture Trevor Hickey · Jan 11, 2016 · Viewed 9.1k times · Source

It appears that std::bitset does not come with STL iterators.
Therefore, I cannot do the following:

std::bitset<8> bs;
for (auto it: bs) {
    std::cout << "this can not be done out of the box\n";
}

Instead I must:

std::bitset<8> bs;
for (std::size_t i = 0; i < bs.size(); ++i) {
    std::cout << bs[i] << '\n';
}

Without iterators, I also can't use bitsets with any of the STL algorithms.
Why did the committee decide to exclude iterators from bitset?

Answer

Jerry Coffin picture Jerry Coffin · Jan 11, 2016

I don't think there was ever an actual decision to exclude iterators from bitset.

Rather, bitset is one of the classes that predates the proposal to add the original Standard Template Library to the C++ standard. When it was designed, essentially none of the standard library included iterators.

Then, Stepanov's library was proposed for addition, and quite a bit of it was accepted. In response to that, additions were made to some existing classes (e.g., std::string) to allow them to be used like the new container classes.

This was all happening quite late in the standards process though--in fact, they already bent the rules in a few places to add what they did. Among other things, just about the same time as the containers/iterators/algorithms were added to the library, the committee voted to consider the standard "feature complete", so from that point onward they'd only work on fixing bugs and such, not adding new features.

As such, even if a proposal had been written to add an iterator interface to bitset, about the only way the committee could have accepted it would have been to treat this as a bug being fixed rather than a new feature being added. If there'd been a really solid proposal, I suppose they could have done that, but I don't think there was such a proposal, and it would have been stretching the point quite a bit, so even a really good proposal might easily have been rejected.

Since then, there was one proposal, LEWG 1112, that would have added an iterator interface to std::bitset. This was proposed for C++11, and was proposed specifically to support the range-based for loop that was also being added in C++11. It suffered a rather ignominious fate: it was originally accepted, and wording was drafted. Then it looked like the proposal for adding Concepts to the language would be accepted, so this wording was rewritten to use the shiny, wonderful new concepts. Sometime later, concepts were removed from the language, and rather than rewording the proposal so it no longer depended on concepts, they tentatively marked it as "NAD Future", which means they treated it as not being a defect, and deferred any further work until some (indefinite) time in the future (and as far as I can see, haven't revisited it since).