what is the difference between warnings C4018 ('expression' : signed/unsigned mismatch) and C4389 ('operator' : signed/unsigned mismatch)

好久不见. 提交于 2019-12-24 08:26:57

问题


so what is the difference between C4018 ('expression' : signed/unsigned mismatch) and C4389 ('operator' : signed/unsigned mismatch) warnings?

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

I just don't get it.


回答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 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.

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.



来源:https://stackoverflow.com/questions/39479163/what-is-the-difference-between-warnings-c4018-expression-signed-unsigned-mi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!