Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find out the size of a std::bitset at compile time

Tags:

c++

Is there a way to find out size of a std::bitset?

I have

 typedef std::bitset<64> Bitset;

and I want to know the size without creating an instance. Eg. like Bitset::size

When I look to the sources in bitset.h it is totally unreadable to me, but even though I found these lines

public:
    enum {_EEN_BITS = _Bits};   
    typedef _Bitset_base<_Bits <= 8 ? 1
        : _Bits <= 16 ? 2
        : _Bits <= 32 ? 4
        : 8> _Mybase;
    typedef typename    // sic
        _Mybase::_Ty _Ty;

which I thought tell me, that _Ty can contain size, but when I try to call Bitset::_Ty i get illegal use of this type as an expression

I know I can store the size somewhere before I typedef the bitset but that is not what I want.

like image 247
relaxxx Avatar asked Mar 26 '12 07:03

relaxxx


People also ask

How big is a BitSet?

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?

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

Use Bitset::digits instead, which corresponds to 64 in your case.

EDIT: As that turned out wrong, here's a different solution:

template< typename T >
struct GetSize;

template< size_t Len > 
struct GetSize< std::bitset< Len > >
{
    enum { Length = Len };
};

Used such as:

const size_t bitsetLength = GetSize< std::bitset< 1024 > >::Length;
like image 112
Ylisar Avatar answered Nov 03 '22 00:11

Ylisar