Get std :: bitset size without instance

Given the typedef value for std::bitset some size, I need to determine this size at compile time. For instance:

 typedef std::bitset<37> permission_bits; static_assert(permission_bits::size() == 37, "size must be 37"); // not valid 

The above is a little far-fetched, but shows a common problem.

As far as I can see in the standard, there is no static constexpr member from std::bitset which will allow me to extract the size. Did I miss something? And if not, what can I do to extract size at compile time?

+4
source share
2 answers

Try:

 template< typename > struct bitset_size; template< std::size_t N > struct bitset_size< std::bitset< N > > : std::integral_constant< std::size_t, N > {}; 

and

 static_assert( bitset_size<permission_bits>::value == 37, "size must be 37"); 
+8
source

You can do this with metaprogramming templates:

 template<class> struct bitset_traits; template<size_t N> struct bitset_traits< std::bitset<N> > { static const size_t size = N; }; 
+5
source

Source: https://habr.com/ru/post/1502523/


All Articles