开发者

Assignment across data types in C++ [duplicate]

开发者 https://www.devze.com 2022-12-08 03:58 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Make VS compiler catch signed/unsigned assignments?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Make VS compiler catch signed/unsigned assignments?

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 generate开发者_开发技巧d. Am I missing something obvious here?

Thanks


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).


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消