警告C4018(“表达式”:有符号/无符号不匹配)和C4389(“操作员”:有符号/无符号不匹配)有什么区别? [英] what is the difference between warnings C4018 ('expression' : signed/unsigned mismatch) and C4389 ('operator' : signed/unsigned mismatch)

查看:154
本文介绍了警告C4018(“表达式”:有符号/无符号不匹配)和C4389(“操作员”:有符号/无符号不匹配)有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么 C4018有什么区别(表达式:有符号/无符号不匹配) C4389(运算符:有符号/无符号不匹配)警告?

unsigned int uc = 0;
int c = 0;
if (uc < c) uc = 0;   // C4018

vs

int a = 9;
unsigned int b = 10;
if (a == b)   // C4389

我就是不明白

推荐答案

最大的区别是生成这些诊断消息的警告级别。 C4018是3级警告。属于您不应忽略的事物类别。只需尝试使用uc = 1和c = -1的代码,并思考1如何可能小于-1。几乎没有程序员期望这种结果。这使它成为一个错误生成器,并值得进行可见的诊断。

Biggest difference is the warning level at which these diagnostic messages are generated. C4018 is a level 3 warning. Falls in the "things you should not ignore" category. Just try the code you have with uc = 1 and c = -1 and ponder how 1 could possibly be less than -1. Just about no programmer expects that kind of outcome. That makes it a bug generator and deserves a visible diagnostic.

C4389是4级警告。属于看起来不对,但可能仍然可以工作类别。操作数的符号性不会影响相等性比较。

C4389 is a level 4 warning. Falls in the "looks wrong but probably works anyway" category. The signed-ness of the operands doesn't affect an equality comparison.

默认警告级别为3,除非您更改了项目设置。这会让您看到不应忽略诊断,而不是可能仍然有效的诊断。

The default warning level is 3, unless you changed the project setting. That makes you see the "should not ignore" diagnostic and not the "probably works anyway" diagnostic.

这篇关于警告C4018(“表达式”:有符号/无符号不匹配)和C4389(“操作员”:有符号/无符号不匹配)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆