比较有符号性不同的整数

compare integers with different signedness

本文关键字:整数 符号 比较      更新时间:2023-10-16

我写了一些程序来测试我的怀疑。它包含超级超可靠的函数,在我看来是:),称为less,用于比较整数。对于某些类型的组合,它会产生与结果不同的结果,从而产生C++。当这种情况发生时,就会出现错误,你可以在屏幕上看到。

#include <iostream> 
#include <iomanip> 
#include <type_traits> 
#include <limits> 
#include <typeinfo> 
#include <cstdlib> 
#pragma GCC diagnostic ignored "-Wsign-compare" 
#pragma GCC diagnostic ignored "-Wtype-limits" 
template< typename T, typename U > 
inline 
bool less(T const & lhs, U const & rhs) 
{ 
    if (std::is_signed< T >::value && std::is_unsigned< U >::value) { 
        if (lhs < 0) { 
            return true; 
        } else if (rhs > std::numeric_limits< T >::max()) { 
            return true; 
        } else { 
            return static_cast< T >(lhs) < rhs; 
        } 
    } else if (std::is_unsigned< T >::value && std::is_signed< U >::value) { 
        if (rhs < 0) { 
            return false; 
        } else if (lhs > std::numeric_limits< T >::max()) { 
            return false; 
        } else { 
            return lhs < static_cast< T >(rhs); 
        } 
    } else { 
        return lhs < rhs; 
    } 
} 
#pragma GCC diagnostic warning "-Wtype-limits" 
#pragma GCC diagnostic warning "-Wsign-compare" 
#pragma GCC diagnostic ignored "-Wsign-compare" 
#pragma GCC diagnostic ignored "-Wtype-limits" 
template< typename U, typename S > 
void test() 
{ 
    std::cout << typeid(U).name() << " vs " << typeid(S).name() << std::endl; 
    static_assert(std::is_unsigned< U >::value && std::is_signed< S >::value, "signedness violated"); 
    static_assert(sizeof(U) != sizeof(S), "size should not be the same"); 
    U const x(std::numeric_limits< U >::max() - 2); 
    S const y(-1); 
    S const z(std::numeric_limits< S >::min()); 
    std::cout << std::boolalpha << (less(x, y) == (x < y)) << std::endl 
              << std::boolalpha << (less(y, x) == (y < x)) << std::endl 
              << std::boolalpha << (less(y, z) == (y < z)) << std::endl 
              << std::boolalpha << (less(z, y) == (z < y)) << std::endl 
              << std::boolalpha << (less(x, z) == (x < z)) << std::endl 
              << std::boolalpha << (less(z, x) == (z < x)) << std::endl 
              << std::endl; 
} 
#pragma GCC diagnostic warning "-Wtype-limits" 
#pragma GCC diagnostic warning "-Wsign-compare" 
int main() 
{ 
    using namespace std; 
    test< uint8_t,  int16_t >(); 
    test< uint8_t,  int32_t >(); 
    test< uint8_t,  int64_t >(); 
    test< uint16_t, int8_t  >(); 
    test< uint16_t, int32_t >(); 
    test< uint16_t, int64_t >(); 
    test< uint32_t, int8_t  >(); 
    test< uint32_t, int16_t >(); 
    test< uint32_t, int64_t >(); 
    test< uint64_t, int8_t  >(); 
    test< uint64_t, int16_t >(); 
    test< uint64_t, int32_t >(); 
    return EXIT_SUCCESS; 
}

我使用以下脚本编译(bash s.sh 2>&1 | tee s.log)程序:

#!/usr/bin/env sh 
set -o errexit 
set -o verbose 
g++ -std=gnu++11 -m64 s.cpp -o s64 
g++ -std=gnu++11 -m32 s.cpp -o s32 
MINGWDIR=/c/mingw64
PATH=/usr/bin:${MINGWDIR}/bin:/c/Windows/system32:${MINGWDIR}/x86_64-w64-mingw32/lib32 ./s64 2>&1 | tee s64.log | grep -c false
PATH=/usr/bin:${MINGWDIR}/bin:/c/Windows/system32:${MINGWDIR}/x86_64-w64-mingw32/lib32 ./s32 2>&1 | tee s32.log | grep -c false
diff s32.log s64.log 

结果(s.log)脚本给出以下内容:

g++ -std=gnu++11 -m64 s.cpp -o s64 
g++ -std=gnu++11 -m32 s.cpp -o s32 
MINGWDIR=/c/mingw64
PATH=/usr/bin:${MINGWDIR}/bin:/c/Windows/system32:${MINGWDIR}/x86_64-w64-mingw32/lib32 ./s64 2>&1 | tee s64.log | grep -c false
10
PATH=/usr/bin:${MINGWDIR}/bin:/c/Windows/system32:${MINGWDIR}/x86_64-w64-mingw32/lib32 ./s32 2>&1 | tee s32.log | grep -c false
10
diff s32.log s64.log  

正如您所看到的,结果是相同的(对于x32x64平台)。有些测试失败了。为什么会发生这种情况?我的程序错了,或者我对C++的了解很少?

您正在测试基本的operator <函数是否与less函数相同。事实并非如此。

您的函数解释了有符号/无符号的不匹配,并给出了数学上正确的答案。

当有符号/无符号不匹配时,基本C++运算符将把有符号值转换为无符号值。