Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 278
Trevor Hickey Avatar asked Jan 11 '16 17:01

Trevor Hickey


People also ask

Is std :: bitset efficient?

It's not like bitset is implemented very inefficiently for what it does. If you genuinely need to access a bunch of bits with a random access pattern which, for some reason or other, needs to check and set just one bit a time, then it might be ideally implemented for such a purpose.

How is bitset implemented in C++?

Bitset represents a fixed-size sequence of N bits and stores values either 0 or 1. Zero means value is false or bit is unset and one means value is true or bit is set. Bitset class emulates space efficient array of boolean values, where each element occupies only one bit.

What is std :: bitset?

template< std::size_t N > class bitset; The class template bitset represents a fixed-size sequence of N bits. Bitsets can be manipulated by standard logic operators and converted to and from strings and integers.


1 Answers

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).

like image 111
Jerry Coffin Avatar answered Sep 30 '22 11:09

Jerry Coffin