C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Make std::bitset a C++20 range?

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Wed, 30 Aug 2023 09:49:43 -0800
On Wed, Aug 30, 2023 at 5:40 AM Hewill Kang via Std-Proposals <
std-proposals_at_[hidden]> wrote:

>
> As a classic bit operation utility in C++, std::bitset has always been
> recommended for bit storage representation.
>
FWIW, I don't think that's true. std::bitset has some weird failings, and
lots of projects have invented their own "better bitsets." std::bitset
isn't quite in the same bucket as std::regex or std::function or
std::unordered_map, but I still don't think it's exactly "always been
recommended." I'd agree it has always *existed*.


> Although it lacks begin()/end() member, its interface still looks like a
> container to me. I'm curious if there has been a proposal to turn it into a
> C++20 range? [...] Is this an enhancement worth considering, or is it a
> bad idea?
>
Bad idea. See
https://quuxplusone.github.io/blog/2022/11/05/bit-vectors/#from-the-vantage-point-of-2022-y

std::bitset is one of those “not quite really STL” types, like std::string
> and std::valarray, that tries to fit a couple of use-cases but none of
> them are “STL iterable container.” Arguably, the hint is in its name:
> whereas vector<bool> is clearly a *vector*, a sequence container, of
> boolean values, bitset is a *set*, a collection of small integer indices.
> bs.test(42) is the equivalent of s.find(42); bs.set(42) is the equivalent
> of s.insert(42); and so on. (As usual, these named methods throw
> std::out_of_range on error, and bitset provides operator[] for faster
> unchecked access.)
> If you were going to iterate over the “contents” of such an object, what
> would you expect to see?
> std::bitset<1000> bs;
> bs.set(42);
> for (auto elt : bs) {
> std::cout << bs;
> }
> Surely you wouldn’t expect to see “false, false, false, false, …”!


–Arthur

Received on 2023-08-30 17:49:57