且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使Clang忽略特定块中的特定警告?

更新时间:2021-09-14 22:43:40

你使用的是Clig的哪个版本?从诽谤用户手册,它应该工作方式你做了。但是你的范围断言将不会按你可能希望他们工作的方式工作:

Wich version of Clang are you using? From the Clang User Manual it should work exaclty the way you did. But your range assertions won't work the way you probably want them to work:

第一个断言本身没有什么意义,如果 destinationT 是无符号的,所以min给出0. originT 也是无符号的,那么它显然不是负数,这是编译器警告你的。或者 originT 被签名,比较将把操作数中的一个或两个转换为其他类型,例如。可能会将 value 转换为无符号(因此为正值)表示。

That first assertion itself does not make much sense, if destinationT is unsigned, so min gives 0. Either originT is unsigned as well, then it's clearly not negative, wich is what the compiler warns you about. Or originT is signed, the comparison will convert one or both of the operand to other types, e.g. possibly converting value to an unsigned (and thus positive) representation.

考虑例如

 assertForNumericRange<signed char, unsigned long>( (signed char)-1);  

(signed char)-1 unsigned long 将会提升-1到 unsigned long ,有效地给出32位长的以下断言:

The comparisons between (signed char)-1 and unsigned long will promote the -1 to unsigned long, effectively giving following assertions for 32bit long:

assertWithReason((unsigned long)0xFFFFFFFF >= std::numeric_limits<destinationT>::min());
assertWithReason((unsigned long)0xFFFFFFFF <= std::numeric_limits<destinationT>::max());

两个比较都会显示为true,而-1显然不是 范围 unsigned long 的值。

Both comparisons will give true, while -1 is clearly not in the range of unsigned long's values.