は、以下のヘルパー構造体を考えてみましょう:整数値表現ビットの数は、標準によると?
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>()
> {};
すべての整数型の場合はstd::is_integral<T>::value
は、私は、標準で、その保証を持っているんbool
以外true
であることをT
な:
bit_count_1
、bit_count_2
とbit_count_3
を同じ値を有するN
T x = 1; x <<= (N - 1)
はよく定義されているT x = ~static_cast<T>(0); x >>= (N - 1)
がよく
を定義している私は現在、C++の提案に取り組んでいますので、私はこれが標準に従って真実であるか、ない、と一瞬それが私のために少しは不明だかどうかを確認する必要があります。
この質問のタグの1つを[tag:language-lawyer]に置き換えて、適切なユーザーを引き付けるのに役立ちます。 – Angew
... C++ 11とC++ 14よりもはるかに多くの「C++」タグのためのもう1つ。私は[tag:C++ 11]か[tag:standards]のいずれかを削除しますが、私はあなたにそれを残します。 –