gpt4 book ai didi

c++ - C++11 类型检查代码的奇怪编译器警告

转载 作者:行者123 更新时间:2023-11-28 07:43:25 25 4
gpt4 key购买 nike

我正在编写一个带有进位/溢出检查的通用加法器,并且我大量使用 c++11 类型检查功能。

这是我的代码:

#include <iostream>
using namespace std;
#define MIN_OF(TYPE) ( (std::is_signed<decltype(res)>::value) ? \
(1 << ( ( sizeof(decltype(res)) * 8 ) - 1)) : \
0 )

#define MAX_OF(TYPE) (~MIN_OF(TYPE))


#define ABS(x) (x < 0 ? -x : x)

class Flags
{
public:
void setSign(bool x)
{
cout << boolalpha;
cout << "setSign: " << x << endl;
}
void setOverflow(bool x)
{
cout << boolalpha;
cout << "setOverflow: " << x << endl;
}
void setCarry(bool x)
{
cout << boolalpha;
cout << "setCarry: " << x << endl;
}
void setZero(bool x)
{
cout << boolalpha;
cout << "setZero: " << x << endl;
}
};

template <typename TYPE, TYPE def>
class Value
{
public:
static inline TYPE get()
{
return def;
}
static inline void set(TYPE x)
{
cout << "value: " << hex << x << endl;
}
};


template <class A, class B, class RES>
struct ADD
{
static void Do(Flags* _flags)
{
if (std::is_convertible<decltype(A::get()),decltype(RES::get())>::value)
{
decltype(A::get()) _a = A::get();
decltype(B::get()) _b = B::get();

decltype(RES::get()) res = _a;

if (_b != 0)
{
res = res + _b;

if (std::is_signed<decltype(res)>::value)
{
unsigned char highestbit_a = static_cast<unsigned char>(0x1 & (_a >> (( sizeof(decltype(_a)) * 8 ) - 1)));
unsigned char highestbit_b = static_cast<unsigned char>(0x1 & (_b >> (( sizeof(decltype(_b)) * 8 ) - 1)));
unsigned char highestbit_res = static_cast<unsigned char>(0x1 & (res >> (( sizeof(decltype(res)) * 8 ) - 1)));

_flags->setSign( (res < 0) );
_flags->setOverflow( ((highestbit_a & highestbit_b) != highestbit_res) );
}
else
{
_flags->setSign( false );
_flags->setOverflow( false );
}

bool setCarryFlag = false;

if (std::is_signed<decltype(_b)>::value)
{
if(_b < 0)
{
/* as _b is negative, we add _b to lowest_res, if the result
* is greater as _a, _a + _b (with _b as negative number) would
* result in an carry out
*/
setCarryFlag = (static_cast<decltype(_a)>(ABS((MIN_OF(decltype(res)) - _b))) > _a);
}
else
{
setCarryFlag = (static_cast<decltype(_a)>((MAX_OF(decltype(res)) - _b)) < _a);
}
}
else
{
//get difference of one summand to results highest until carry
/* MARKED LINE: this branch gets wrongly checked */
setCarryFlag = ((MAX_OF(decltype(res)) - _b) < _a);
}

_flags->setCarry( setCarryFlag );
}
else
{
if (std::is_signed<decltype(res)>::value)
{
_flags->setSign( (res < 0) );
}
}

_flags->setZero( (res == 0) );

//store result
RES::set(res);
}
}
};



int main()
{
Flags* f = new Flags();
ADD<Value<unsigned int, 1>, Value<signed int, 6>, Value<unsigned int, 1>>::Do(f);

return 0;
}

问题出现在“MARKED LINE:”处。通常,我会理解编译器不会使用这个分支,因为 _b 是 signed int 类型,所以 is_signed 应该是 true,所以编译器应该只使用 if 分支中的 whats 并丢弃 else 分支。但它似乎没有这样做,因为我收到警告:

 warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|

指向这一行。但这不是我想要的。有什么想法可以告诉编译器做正确的事吗?

编译器是:gcc 4.7.2 on x86-64, debian

谢谢!

最佳答案

这是警告,不是错误。警告是为了警告您可能不想做但不合法的事情。在这种情况下你想这样做,所以忽略它。您可以使用 pragma 来忽略警告,但我建议记录您如何知道它对 future 的开发人员是安全的。

用于禁用此警告的 GCC 特定编译指示是:<​​/p>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
setCarryFlag = ((MAX_OF(decltype(res)) - _b) < _a);
#pragma GCC diagnostic pop

关于c++ - C++11 类型检查代码的奇怪编译器警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15372276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com