Consider the following auxiliary structures:
template <class T> struct bit_count_1: std::integral_constant< std::size_t, std::numeric_limits<typename std::make_unsigned<T>::type>::digits > {}; template <class T> struct bit_count_2: std::integral_constant< std::size_t, std::numeric_limits<T>::digits + std::is_signed<T>::value > {}; template <class T> constexpr std::size_t compute_bit_count() { using type = typename std::make_unsigned<T>::type; constexpr type zero = 0; constexpr type one = 1; constexpr type max = ~zero; type current = max; std::size_t i = 0; while (current) { current >>= one; ++i; } return i; } template <class T> struct bit_count_3: std::integral_constant< std::size_t, compute_bit_count<T>() > {};
For each integral type T such that std::is_integral<T>::value is true except for bool , I have a guarantee, by standard, that:
bit_count_1 , bit_count_2 and bit_count_3 have the same value NT x = 1; x <<= (N - 1) T x = 1; x <<= (N - 1) clearly definedT x = ~static_cast<T>(0); x >>= (N - 1) T x = ~static_cast<T>(0); x >>= (N - 1) is clearly defined
I am currently working on a proposal in C ++, so I need to be sure if this is true according to the standard or not, and at the moment this is a bit unclear to me.
source share