Suppose I have an integer type T (signed or unsigned). I want to refer (at compile time) to the smallest integer type (signed or unsigned) which can hold, say, std::numeric_limits<T>::max()
plus 1 (in the non-overflowing sense, I mean).
What's a nice generic way of doing that?
For unsigned types, this does the trick:
template <typename T>
constexpr unsigned size_in_bits() { return sizeof(T) * CHAR_BIT; }
template <typename T>
using least_larger_uint_t =
typename boost::uint_t<size_in_bits<T>() + 1>::least;
And if we want something to work for any integer type:
template <typename T, int NumBits>
using boost_integer_type =
typename std::conditional<
std::is_unsigned<T>::value,
boost::uint_t<NumBits>,
boost::int_t<NumBits>
>::type;
template <typename T>
constexpr unsigned size_in_bits() { return sizeof(T) * CHAR_BIT; }
template <typename T>
using least_larger_integral_t =
typename boost_integer_type<T, size_in_bits<T>() + 1>::least;
See the documentation for Boost.Integer for details on int_t<N>
and uint_t<N>
.