我写了一些程序来测试我的怀疑。它包含超级超可靠的功能,在我的脑海中:)
,称为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
正如您所看到的结果相同(对于 x32 和 x64 平台)。有些测试失败了。为什么会这样?我的程序错了,或者我对 C ++ 的了解不多?
答案 0 :(得分:2)
您正在测试基本operator <
功能是否与您的less
功能相同。它没有。
您的函数会考虑签名/未签名的不匹配,并提供数学上正确的答案。
当有符号/无符号不匹配时,基本C ++运算符会将有符号值转换为无符号值。