Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::bitset::size non-static

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?

like image 741
Russell Greene Avatar asked Oct 30 '15 04:10

Russell Greene


People also ask

How large can a Bitset be?

The default size of the bit set is 64-bit space. If the bit is set at index larger than the current BitSet size, it increases its bit space in the multiplication of 64*n, where n starts from 1, 2, 3, so on.

What is std:: bitset in c++?

Defined in header <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

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)
like image 142
Cheers and hth. - Alf Avatar answered Sep 18 '22 06:09

Cheers and hth. - Alf