考虑以下帮助器结构:
templatestruct bit_count_1: std::integral_constant< std::size_t, std::numeric_limits ::type>::digits > {}; template struct bit_count_2: std::integral_constant< std::size_t, std::numeric_limits ::digits + std::is_signed ::value > {}; template constexpr std::size_t compute_bit_count() { using type = typename std::make_unsigned ::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 struct bit_count_3: std::integral_constant< std::size_t, compute_bit_count () > {};
对于每一个整数类型T
,从而std::is_integral
是true
除bool
我必须保证,在标准,即:
bit_count_1
,bit_count_2
并bit_count_3
具有相同的价值N
T x = 1; x <<= (N - 1)
很明确
T x = ~static_cast
很明确
我目前正在制定一个C++提案,所以我需要确定这是否符合标准,或者不是,目前对我来说有点不清楚.