用于安全整数转换的C++模板

JSB*_*ոգչ 11 c++ templates casting integer-overflow

我正在尝试编写一个C++模板函数,它将在不同整数类型之间的转换中抛出整数溢出的运行时异常,具有不同的宽度,以及可能的有符号/无符号不匹配.出于这些目的,我不关心从浮点类型转换为整数类型,也不关心其他对象到对象的转换.我想这样做而不必写很多特殊的案例代码.这就是我目前拥有的:

template< typename T, typename R > void safe_cast( const T& source, R& result )
{
    // get the maximum safe value of type R
    R rMax = (R) ~0;
    if ( rMax < 0 ) // R is a signed type
    {
        // assume that we're on an 8-bit twos-compliment machine
        rMax = ~( 0x80 << ( ( sizeof( R ) - 1 ) * 8 ) );
    }

    if ( ( source & rMax  ) != source )
    {
        throw new IntegerOverflowException( source );
    }

    result = static_cast<R>( source );
}
Run Code Online (Sandbox Code Playgroud)

这是正确有效的吗?

编辑:由于各种原因,stl不可用,所以我不能使用std :: numeric_limits,而Boost中的任何东西都是正确的.

Tyl*_*nry 12

您可以使用std::numeric_limits模板以更优雅的方式获取任何基本类型的最小和最大安全值(以及大量其他信息),例如std::numeric_limits<T>::max().你需要包括<limits>.

参考:http://www.cplusplus.com/reference/std/limits/numeric_limits/


Voi*_*oid 11

提升是一种选择吗?如果是这样,请尝试boost :: numeric_cast <>.它似乎提供了您正在寻找的特征.


Joh*_*itb 7

我认为现在这些都有效,无论你是否使用两个补码.请在使用前进行广泛测试.他们给出了以下结果.每一行都会导致一个断言失败(只需将它们更改为异常)

/* unsigned -> signed, overflow */
safe_cast<short>(UINT_MAX);

/* unsigned -> unsigned, overflow */
safe_cast<unsigned char>(ULONG_MAX);

/* signed -> unsigned, overflow */
safe_cast<unsigned long>(-1);

/* signed -> signed, overflow */
safe_cast<signed char>(INT_MAX);

/* always works (no check done) */
safe_cast<long>(INT_MAX);

// giving these assertion failures results
(type)f <= (type)is_signed<To>::v_max
f <= (To)-1
f >= 0
f >= is_signed<To>::v_min && f <= is_signed<To>::v_max
Run Code Online (Sandbox Code Playgroud)

实现.首先检查整数排名的一些实用程序(排名较高的类型将能够包含具有较低排名的类型的值,给定相同的符号.以及一些促销工具,以便能够找出一个常见的,安全的类型(这将永远不会如果涉及无符号类型,则生成有符号类型,如果有符号类型将无法存储无符号类型的所有值).

/* ranks */
template<typename> struct int_rank;
#define RANK(T, I) template<> struct int_rank<T> \
    { static int const value = I; }

RANK(char, 1); RANK(unsigned char, 1); RANK(signed char, 1); 
RANK(short, 2); RANK(unsigned short, 2);
RANK(int, 3); RANK(unsigned int, 3);
RANK(long, 4); RANK(unsigned long, 4);
#undef RANK

/* usual arith. conversions for ints (pre-condition: A, B differ) */
template<int> struct uac_at;
template<> struct uac_at<1> { typedef int type; };
template<> struct uac_at<2> { typedef unsigned int type; };
template<> struct uac_at<3> { typedef long type; };
template<> struct uac_at<4> { typedef unsigned long type; };

template<typename A, typename B>
struct uac_type { 
    static char (&f(int))[1];
    static char (&f(unsigned int))[2];
    static char (&f(long))[3];
    static char (&f(unsigned long))[4];
    typedef typename uac_at<sizeof f(0 ? A() : B())>::type type; 
};

/* signed games */
template<typename> struct is_signed { static bool const value = false; };
#define SG(X, TT) template<> struct is_signed<X> { \
    static bool const value = true;                \
    static X const v_min = TT##_MIN;               \
    static X const v_max = TT##_MAX;               \
}

SG(signed char, SCHAR); 
SG(short, SHRT); 
SG(int, INT); 
SG(long, LONG); 
#undef SG

template<> struct is_signed<char> { 
    static bool const value = (CHAR_MIN < 0); 
    static char const v_min = CHAR_MIN; // just in case it's signed...
    static char const v_max = CHAR_MAX;
};
Run Code Online (Sandbox Code Playgroud)

转换模板利用它们来确定每种情况何时需要完成或不完成.

template<typename To, typename From, 
         bool to_signed = is_signed<To>::value, 
         bool from_signed = is_signed<From>::value,
         bool rank_fine = (int_rank<To>::value >= int_rank<From>::value)>
struct do_conv;

/* these conversions never overflow, like int -> int, 
 * or  int -> long. */
template<typename To, typename From, bool Sign>
struct do_conv<To, From, Sign, Sign, true> {
    static To call(From f) {
        return (To)f; 
    }
};

template<typename To, typename From>
struct do_conv<To, From, false, false, false> {
    static To call(From f) {
        assert(f <= (To)-1);
        return (To)f;
    }
};

template<typename To, typename From>
struct do_conv<To, From, false, true, true> {
    typedef typename uac_type<To, From>::type type;
    static To call(From f) {
        /* no need to check whether To's positive range will
         * store From's positive range: Because the rank is
         * fine, and To is unsigned. 
         * Fixes GCC warning "comparison is always true" */
        assert(f >= 0);
        return (To)f;
    }
};

template<typename To, typename From>
struct do_conv<To, From, false, true, false> {
    typedef typename uac_type<To, From>::type type;
    static To call(From f) {
        assert(f >= 0 && (type)f <= (type)(To)-1);
        return (To)f;
    }
};

template<typename To, typename From, bool Rank>
struct do_conv<To, From, true, false, Rank> {
    typedef typename uac_type<To, From>::type type;
    static To call(From f) {
        assert((type)f <= (type)is_signed<To>::v_max);
        return (To)f;
    }
};

template<typename To, typename From>
struct do_conv<To, From, true, true, false> {
    static To call(From f) {
        assert(f >= is_signed<To>::v_min && f <= is_signed<To>::v_max);
        return (To)f;
    }
};

template<typename To, typename From>
To safe_cast(From f) { return do_conv<To, From>::call(f); }
Run Code Online (Sandbox Code Playgroud)


Jar*_*Par 5

你试过SafeInt吗?它是一个跨平台模板,可以对各种整数类型进行整数溢出检查.它可以在github上找到