让Visual Studio的警告未签名的比较 [英] Make Visual Studio warn for unsigned comparisons
问题描述
这code,当使用Visual Studio 2010编译
This code, when compiled with Visual Studio 2010
#include <cstdlib>
#include <cstdio>
int numbers[] = { 23, 24, 25, 25, 28, 20, 20 };
int main(void) {
int d = -1, x=0;
size_t count = sizeof(numbers) / sizeof(numbers[0]);
if (d <= (sizeof(numbers) / sizeof(numbers[0]))-2)
x = numbers[d+1];
if (d <= count-2)
x = numbers[d+1];
}
给我一个符号/无符号不匹配的警告如果(D&LT; =计数2)
而不是在如果(D&LT; = (sizeof的(数字)/ sizeof的(编号[0])) - 2)
。为什么是这样?我已经启用的所有的警告。
gives me a signed/unsigned mismatch warning on if (d <= count-2)
but not on if (d <= (sizeof(numbers) / sizeof(numbers[0]))-2)
. Why is this? I have enabled all warnings.
推荐答案
这是在Visual C ++编译器不会发出在这种情况下警告C4018的错误。请参阅Microsoft连接错误报告,<一个href=\"http://connect.microsoft.com/VisualStudio/feedback/details/569898/warning-c4018-signed-unsigned-mismatch-for-sizeof-operator\"相对=nofollow>警告C4018(符号/无符号不匹配)的sizeof操作符。
It is a bug that the Visual C++ compiler does not emit warning C4018 in this case. See the Microsoft Connect bug report, "warning C4018 (signed/unsigned mismatch) for sizeof operator."
这个问题似乎只有间接关系的的使用的sizeof
。编译器还没有发出在其他情况下,警告,其中无符号类型的一个恒定值是参与了比较。例如,如果您添加常量
企业资质为计数
,C4018甚至没有你的第二个比较发射。
The issue appears to be only indirectly related to usage of sizeof
. The compiler also fails to emit the warning in other cases where a constant value of unsigned type is involved in the comparison. For example, if you add const
-qualification to count
, C4018 is not emitted even for your second comparison.
这篇关于让Visual Studio的警告未签名的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!