views:

109

answers:

3

Hi,

I've compiled the following snippet of code in VC++ 2005/2008:

unsigned long ul = ...;
signed long l = ...;

l = ul;

and was expecting to see a compiler warning (Warning Level set to 4), but none was generated. Am I missing something obvious here?

Thanks

+2  A: 

If the omitted initializers are compile-time constants, the static analyzer may be able to determine that no overflow can occur and let it slide without a warning. Try initializing ul to something > 2^31 -1 and see what happens (assuming you're on a 32-bit platform).

Drew Hall
I'd actually tried that before. No warnings were generated no matter what border value ul was initialized to.
TestUser
+1  A: 

I think it's a duplicate (here).
Quoting the accepted answer:

You need to enable warning 4365 to catch the assignment.
That might be tricky - you need to enable ALL warnings - use /Wall which enables lots of warnings, so you may have some trouble seeing the warning occur, but it does. (quamrana)

You could also use #pragma warning(default: 4365) to enable. (ChrisN)

Oren S
Thank you. They certainly did a good job of hiding this option!
TestUser
+1  A: 

Warnings are compiler specific. You "should" see a warning in the sense that "it would help you" to see one, but the Visual C++ team did not choose to display one by default.

Daniel Daranas