c++ - 比较具有不同符号的整数

标签 c++ c++11

我写了一些程序来验证我的怀疑。它包含 super 可靠的功能,在我看来 :),称为 less 来比较整数。对于某些类型的组合,它产生的结果与结果不同,这给出了 C++。当发生这种情况时,它会给出 false,您可以在屏幕上看到这一点。

#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++ 运算符会将有符号值转换为无符号值。

关于c++ - 比较具有不同符号的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14801879/

相关文章:

已知类型的 C++ 可变参数函数模板

c++ - x = std::move(x) 未定义吗?

C++ 从 std::vector<std::function<...>> 中删除 std::function

c++ - 创建指向更多 vector 的指针 vector

c++ - 本地结构中是否允许成员声明 `decltype(name) name;`,其中第一个名称指的是封闭范围?

c++ - 模板参数包的参数到 T

c++ - 指针逻辑

c++ - C++中 'glue'两个类实现的正确方法

c++ - C++线程安全 vector 插入

c++ - Qt - 如何拦截应用程序的关闭事件(如果有的话)