9

I can't possibly imagine why it was chose that std::bitset::size is non-static. It makes it much harder to get a constexpr size; you have to write something like this:

template<int val>
struct int_
{
   static const constexpr value = val;
};

template<size_t size>
auto getBitsetSizeIMPL(std::bitset<size>)
{
   return int_<size>{};
}

template<typename BitsetType>
constexpr size_t getBitsetSize()
{
    return decltype(getBitsetSizeIMPL(BitsetType{}))::value;
}

When if it were static all you would have to do would be

BitsetType::size()

and there would be no sacrifice of functionality.

Is there a historical reason that I am missing or is there a technical fact I am missing?

Russell Greene
  • 2,141
  • 17
  • 29

1 Answers1

1

The assumption of a not constexpr std::bitset::size is incorrect:

std::size_t size() const; // until C++11
constexpr std::size_t size();  // since C++11, until C++14
constexpr std::size_t size() const; // since C++14)
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • But in order to use those, you have to have a constexpr bitset. It isn't static. – Russell Greene Oct 30 '15 at 04:11
  • You aren't answering the question. I know that it is constexpr. I'm just curious why they chose that design choice. – Russell Greene Oct 30 '15 at 04:13
  • @RussellGreene: I don't know, but two characters, `{}` or `()` to create an instance, is really not much notational overhead. It's just a little imperfection. – Cheers and hth. - Alf Oct 30 '15 at 04:16
  • I totally agree. I'm just curious if there is a reason it isn't static : it will always return the same value for the same type. – Russell Greene Oct 30 '15 at 04:17
  • @RussellGreene: You may be on to something. It's the same with `std::array`, except there `std::tuple_size` plays the rôle of free size function. It's not overloaded for `std::bitset`. Curious. – Cheers and hth. - Alf Oct 30 '15 at 04:19