Should there be an overload of array bool?

50 views
Skip to first unread message

vic...@gmail.com

unread,
Jun 9, 2018, 1:03:30 PM6/9/18
to ISO C++ Standard - Discussion
I was just thinking that the new std::array function make more sense the overload then the std::vector for std::array of bool as is could work algorithms on a bit array. the array size would be N / (number of bits is a byte). I just want to know if there is anything wrong with this proposal?

Nicol Bolas

unread,
Jun 9, 2018, 2:05:57 PM6/9/18
to ISO C++ Standard - Discussion, vic...@gmail.com
On Saturday, June 9, 2018 at 1:03:30 PM UTC-4, vic...@gmail.com wrote:
I was just thinking that the new std::array function make more sense the overload then the std::vector for std::array of bool as is could work algorithms on a bit array. the array size would be N / (number of bits is a byte). I just want to know if there is anything wrong with this proposal?

So, you want to take one of the biggest mistakes of C++98's standard library, the consequences of which we are still feeling to this day... and proliferate it?

No. If you want to have some optimized bit-array, that's fine. In fact, we already have that; it's called `std::bitset`. Go use that. Specializations of container templates should not be used to create optimizations like that.

vic...@gmail.com

unread,
Jun 9, 2018, 4:51:59 PM6/9/18
to ISO C++ Standard - Discussion, vic...@gmail.com
I think it is a better idea but I do not like modify operators. Also, I would like an iterator for accessing bits as either true of false

Richard Hodges

unread,
Jun 9, 2018, 5:03:01 PM6/9/18
to std-dis...@isocpp.org
On Sat, 9 Jun 2018 at 22:52, <vic...@gmail.com> wrote:
I think it is a better idea but I do not like modify operators. Also, I would like an iterator for accessing bits as either true of false

I don't disagree, but it's fairly trivial to write one

#include <bitset>
#include <iostream>

template<std::size_t N>
struct bool_test_iterator
{
  using value_type = bool;

  bool_test_iterator(std::bitset<N> const& bs, std::size_t bit)
  : bs(bs), bit(bit)
  {}

  bool operator!=(bool_test_iterator const& other) const
  {
    return (bit != other.bit);
  }

  value_type operator*() const
  {
    return bs[bit];
  }

  bool_test_iterator& operator++()
  {
    ++bit;
    return *this;
  }

  std::bitset<N> const& bs;
  std::size_t bit;
};

int main()
{
  extern std::bitset<64> get_bs();
  auto x = get_bs();

  auto first = bool_test_iterator(x, 0);
  auto last = bool_test_iterator(x, x.size());
  while(first != last)
  {
    std::cout << *first << std::endl;
    ++first;
  }
}


 

On Saturday, June 9, 2018 at 1:05:57 PM UTC-5, Nicol Bolas wrote:
On Saturday, June 9, 2018 at 1:03:30 PM UTC-4, vic...@gmail.com wrote:
I was just thinking that the new std::array function make more sense the overload then the std::vector for std::array of bool as is could work algorithms on a bit array. the array size would be N / (number of bits is a byte). I just want to know if there is anything wrong with this proposal?

So, you want to take one of the biggest mistakes of C++98's standard library, the consequences of which we are still feeling to this day... and proliferate it?

No. If you want to have some optimized bit-array, that's fine. In fact, we already have that; it's called `std::bitset`. Go use that. Specializations of container templates should not be used to create optimizations like that.

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Reply all
Reply to author
Forward
0 new messages