I'm trying to understand why the following code doesn't issue a warning at the indicated place.
//from limits.h
#define UINT_MAX 0xffffffff /* maximum unsigned int value */
#define INT_MAX 2147483647 /* maximum (signed) int value */
/* = 0x7fffffff */
int a = INT_MAX;
//_int64 a = INT_MAX; // makes all warnings go away
unsigned int b = UINT_MAX;
bool c = false;
if(a < b) // warning C4018: '<' : signed/unsigned mismatch
c = true;
if(a > b) // warning C4018: '<' : signed/unsigned mismatch
c = true;
if(a <= b) // warning C4018: '<' : signed/unsigned mismatch
c = true;
if(a >= b) // warning C4018: '<' : signed/unsigned mismatch
c = true;
if(a == b) // no warning <--- warning expected here
c = true;
if(((unsigned int)a) == b) // no warning (as expected)
c = true;
if(a == ((int)b)) // no warning (as expected)
c = true;
I thought it was to do with background promotion, but the last two seem to say otherwise.
To my mind, the first ==
comparison is just as much a signed/unsigned mismatch as the others?
Best Answer
When comparing signed with unsigned, the compiler converts the signed value to unsigned. For equality, this doesn't matter,
-1 == (unsigned) -1
. For other comparisons it matters, e.g. the following is true:-1 > 2U
.EDIT: References:
5/9: (Expressions)
4.7/2: (Integral conversions)
EDIT2: MSVC warning levels
What is warned about on the different warning levels of MSVC is, of course, choices made by the developers. As I see it, their choices in relation to signed/unsigned equality vs greater/less comparisons make sense, this is entirely subjective of course:
-1 == -1
means the same as-1 == (unsigned) -1
- I find that an intuitive result.-1 < 2
does not mean the same as-1 < (unsigned) 2
- This is less intuitive at first glance, and IMO deserves an "earlier" warning.