假设我有一个整数类型T(有符号或无符号)。我想引用(在编译时)最小的整数类型(有符号或无符号),它可以容纳,比方说,std::numeric_limits<T>::max()加1(我的意思是,在非溢出意义上)。
有什么好的通用方法可以做到这一点呢?
发布于 2016-07-27 20:43:40
对于无符号类型,这样做是可行的:
template <typename T>
constexpr unsigned size_in_bits() { return sizeof(T) * CHAR_BIT; }
template <typename T>
using smallest_larger_uint_t =
typename boost::uint_t<size_in_bits<T>() + 1>::least;如果我们想让某些东西适用于任何整数类型:
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 smallest_larger_integral_t =
typename boost_integer_type<T, size_in_bits<T>() + 1>::least;有关int_t<N>和uint_t<N>的详细信息,请参阅documentation for Boost.Integer。
发布于 2016-07-27 21:17:54
您可以按所需大小指定整数,并相应地进行选择:
#include <cstdint>
namespace Detail {
template <std::size_t> struct integer_of_size_undefined {};
template <> struct integer_of_size_undefined<sizeof(int8_t)> {
typedef int8_t type;
};
template <> struct integer_of_size_undefined<sizeof(int16_t)> {
typedef int16_t type;
};
template <> struct integer_of_size_undefined<sizeof(int32_t)> {
typedef int32_t type;
};
template <> struct integer_of_size_undefined<sizeof(int64_t)> {
typedef int64_t type;
};
template <std::size_t> struct unsigned_integer_of_size_undefined {};
template <> struct unsigned_integer_of_size_undefined<sizeof(int8_t)> {
typedef uint8_t type;
};
template <> struct unsigned_integer_of_size_undefined<sizeof(int16_t)> {
typedef uint16_t type;
};
template <> struct unsigned_integer_of_size_undefined<sizeof(int32_t)> {
typedef uint32_t type;
};
template <> struct unsigned_integer_of_size_undefined<sizeof(int64_t)> {
typedef uint64_t type;
};
}
template <std::size_t N>
struct integer_of_size {
typedef typename Detail::integer_of_size_undefined<N>::type type;
};
template <std::size_t N>
struct unsigned_integer_of_size {
typedef typename Detail::unsigned_integer_of_size_undefined<N>::type type;
};
#include <type_traits>
template <typename T>
struct next_integer {
typedef typename std::conditional<std::is_signed<T>::value,
typename std::make_unsigned<T>::type,
typename integer_of_size<2*sizeof(T)>::type>::type
type;
};
int main ()
{
static_assert(std::is_same<next_integer<std::int16_t>::type, uint16_t>::value,
"Should be a unsigned 16 bit");
static_assert(std::is_same<next_integer<std::uint16_t>::type, int32_t>::value,
"Should be a signed 32 bit");
return 0;
}发布于 2016-07-27 20:51:13
您可以使用cstdint中的类型执行以下操作
template <typename T>
struct next_type {
typedef error_type type;
};
template <>
struct next_type<std::int32_t> {
typedef std::uint32_t type;
}
template <>
struct next_type<std::uint32_t> {
typedef std::int64_t type;
}
// Add more...为每个类型添加专门化。然后使用它:
typename next_type<T>::type因此,如果你已经使用了最大可能的类型,你就得到了error_type,你必须以某种方式定义它是什么,否则它就需要专门化。
使用cstdint中的_least类型可能会更好,比如std::int_least32_t to to std::uint_least32_t。
https://stackoverflow.com/questions/38612697
复制相似问题