How to express "minimum integral type greater than T"?

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 contain, say, std::numeric_limits<T>::max()plus 1 (meaning without overflow, I mean).

What is a good general way to do this?

+4
source share
4 answers

For unsigned types, this does the trick:

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;

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 smallest_larger_integral_t = 
    typename boost_integer_type<T, size_in_bits<T>() + 1>::least;

For more information on int_t<N>and uint_t<N>see the documentation for Boost.Integer .

+2

:

#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;
}
+2

- , 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, - , , .

, _least cstdint, std::int_least32_t std::uint_least32_t.

0

, - , , . :

#define CREATE_SIGNED_META_OBJ(x) template <>\
    struct signed_integer_type<x> {\
        typedef int##x##_t type;\
    };

#define CREATE_UNSIGNED_META_OBJ(x) template <>\
    struct unsigned_integer_type<x> {\
        typedef uint##x##_t type;\
    };

template <std::size_t length>
struct signed_integer_type;

template <std::size_t len>
struct unsigned_integer_type;

CREATE_SIGNED_META_OBJ(8)
CREATE_SIGNED_META_OBJ(16)
CREATE_SIGNED_META_OBJ(32)
CREATE_SIGNED_META_OBJ(64)

CREATE_UNSIGNED_META_OBJ(8)
CREATE_UNSIGNED_META_OBJ(16)
CREATE_UNSIGNED_META_OBJ(32)
CREATE_UNSIGNED_META_OBJ(64)

, . std::numeric_limits<Int>::min() == 0...

template <typename Int, bool>
struct get_smallest_for_max_plus_one;

template <typename Int>
struct get_smallest_for_max_plus_one<Int, true> {
    typedef typename signed_integer_type<2*sizeof(Int)*8>::type type;
};

template <typename Int>
struct get_smallest_for_max_plus_one<Int, false> {
    typedef typename unsigned_integer_type<sizeof(Int)*8>::type type;
};

template <typename Int>
using get_fittest_int_type = get_smallest_for_max_plus_one<Int, std::numeric_limits<Int>::min() == 0>;

get_fittest_int_type ... Coliru.

, , signed unsigned... , get_smallest_for_max_plus_one :

// Unsigned type, we want to get the smallest unsigned type that can hold max + 1
template <typename Int>
struct get_smallest_for_max_plus_one<Int, true> {
    typedef typename unsigned_integer_type<2*sizeof(Int)*8>::type type;
};

// Signed type, we want the smallest signed type that can hold max + 1
template <typename Int>
struct get_smallest_for_max_plus_one<Int, false> {
    typedef typename signed_integer_type<2*sizeof(Int)*8>::type type;
};
0

Source: https://habr.com/ru/post/1649280/


All Articles