C++ Logo

std-proposals

Advanced search

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

From: Julien Jorge <julien.jorge_at_[hidden]>
Date: Sun, 03 Sep 2023 00:29:06 +0200
Le 30 août 2023 19:49:43 GMT+02:00, Arthur O'Dwyer via Std-Proposals <std-proposals_at_[hidden]> a écrit :
> Bad idea. See
> https://quuxplusone.github.io/blog/2022/11/05/bit-vectors/#from-the-vantage-point-of-2022-y
>> 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, …”!

Why would you expect another output? The standard says:
> The header <bitset> defines a class template and several related functions for representing and manipulating fixed-size sequences of bits.

Since it's fixed-size these zero bits exist and thus they must be considered by the loop. The loop must do 1000 iterations.

Am I missing something?

Julien

Received on 2023-09-02 22:29:10