13

I have the following simple C++ code:

#include "stdafx.h"
int main()
{
    int a = -10;
    unsigned int b = 10;
    //  Trivial error is placed here on purpose to trigger a warning.
    if( a < b ) {
        printf( "Error in line above: this will not be printed\n" );
    }
    return 0;
}

Compiled using Visual Studio 2010 (default C++ console application) it gives warning C4018: '<' : signed/unsigned mismatch" on line 7 as expected (code has a logical error).

But if I change unsigned int b = 10; into const unsigned int b = 10; warning disappears! Are where any known reasons for such behavior? gcc shows warning regardless of const.

Update

I can see from comments that lot of people suggest "it's just got optimized somehow so where is no warning needed". Unfortunately, warning is needed, since my code sample has actual logical error carefully placed to trigger a warning: the print statement will not be called regardless that -10 is actually less than 10. This error is well known and "signed/unsigned warning" is raised exactly to find such errors.

Update

I can also see from comments that lot of people have "found" a signed/unsigned logical error in my code and are explaining it. Where is no need to do so - this error is placed purely to trigger a warning, is trivial (-10 is conveted to (unsigned int)-10 that is 0xFFFFFFFF-10) and question is not about it :).

19
  • 5
    Sounds like a compiler bug. It might be optimizing away the variable, so you just have a < 10.
    – Apples
    Mar 25, 2013 at 20:18
  • 3
    @DBRalir: no, wait, that's it! a const variable of integer type initialized with a compile-time constant expression... it is a<10. Mar 25, 2013 at 20:19
  • 1
    @DBRalir since "debug" is not printed, condition is evaluated incorrectly, so warning must be given in any case.
    – grigoryvp
    Mar 25, 2013 at 20:51
  • 1
    @Yakk Since debug is not printed, the "it can make sure that the signed/unsigned mismatch doesn't cause a problem, and as such no warning needed" statement can't be correct - where is an obvious error in code above, adding 'const' do not remove error but warning about it is removed.
    – grigoryvp
    Mar 25, 2013 at 20:52
  • 2
    I'd hesitate to call that an error; the comparison is evaluated exactly as the language standard requires. It's likely to be a logical error (i.e., not what the programmer intended). Mar 25, 2013 at 21:02

1 Answer 1

9

It is a Visual Studio bug, but let's start by the aspects that are not bugs.

Section 5, Note 9 of the then applicable C++ standard first discusses what to do if the operands are of different bit widths, before proceeding what to do if they are the same but differ in the sign:

... Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type.

This is where we learn that the comparison has to operate in unsigned arithmetic. We now need to learn what this means for the value -10.

Section 4.6 tells us:

If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2 n where n is the number of bits used to represent the unsigned type). [Note: In a two’s complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). — end note ] 3 If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined.

As you can see, a specific pretty high value (4294967286, or 0xFFFFFFF6, assuming unsigned int is a 32-bit number) is being compared with 10, and so the standard guarantees that printf is really never called.

Now you can believe me that there is no rule in the standard requiring a diagnostic in this case, so the compiler is free not to issue any. (Indeed, some people write -1 with the intention of producing an all-ones bit pattern. Others use int for iterating arrays, which results in signed/unsigned comparisons between size_t and int. Ugly, but guaranteed to compile.)

Now Visual Studio issues some warnings "voluntarily".

This results in a warning already under default settings (level 3):

int a = -10;
unsigned int b = 10;
if( a < b ) // C4018
{
    printf( "Error in line above: this will not be printed\n" );
}

The following requires /W4 to get a warning. Notice that the warning was reclassified. It changed from a warning C4018 to a warning C4245. This is apparently by design. A logic error that breaks a comparison nearly always is less dangerous than one that appears to work with positive-positive comparisons but breaks down with positive-negative ones.

const int a = -10;
unsigned int b = 10;
if( a < b ) // C4245
{
    printf( "Error in line above: this will not be printed\n" );
}

But your case was yet different:

int a = -10;
const unsigned int b = 10;
if( a < b ) // no warning
{
    printf( "Error in line above: this will not be printed\n" );
}

And there is no warning whatsoever. (Well, you should retry with -Wall if you want to be sure.) This is a bug. Microsoft says about it:

Thank you for submitting this feedback. This is a scenario where we should emit a C4018 warning. Unfortunately, this particular issue is not a high enough priority to fix in the next release given the resources that we have available.

Out of curiosity, I checked using Visual Studio 2012 SP1 and the defect is still there - no warning with -Wall.

5
  • You're probably right about that -1 thing, I think I've done that myself. It would make more sense to use ~0 though. Mar 25, 2013 at 22:07
  • @MarkRansom: the theoretical trouble with ~0 is that it has type int, and its behavior and value depends on the signed representation. Obviously in practice everything uses 2's complement and so ~0 == -1. If you want to portably maximize an unsigned type (without repeating the name of the type or using decltype), then you need to assign -1 or ~0U. Use -1 to emphasize that unsigned types are arithmetic modulo 2^n, and ~0U to emphasize that unsigned types are bitfields. Mar 25, 2013 at 22:20
  • Oops, I should have said use ~0ULL in my comment above, since of course ~0U fails if the destination type is bigger than unsigned int. If you're willing to rely on a particular type then there's UINT_MAX, no need for - or ~. Personally I use -1 except when bit-twiddling. Mar 26, 2013 at 1:22
  • I have changed warning level from 3 (default) to 4 (near maximum) - i can't see any C4245 warnings :(. Have you checked it for yourself?
    – grigoryvp
    Mar 26, 2013 at 5:27
  • 1
    @EyeofHell - Oops - it depends on where you put the const. Answer updated to cover both ways. Mar 26, 2013 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.