[C++] conversion from 'int' to 'short' warning

Started by
14 comments, last by Promit 15 years, 7 months ago
Quote:Original post by Promit
Is this /W4?

Yes. Maybe a bit extreme ;)

But I have a workaround anyway, I was just curious as to see wheter today I'd learn yet another thing I did not know about C++
Advertisement
Quote:Original post by dmatter
I'd say it's a bug that was fixed in 2008. Either ignore it, disable the warning or overload the operator yourself.

Yeah i'll turn the warning level down to /W3.

I thought overloading an operator was possible only with at leas one user type as a parameter.
Quote:Original post by janta
I thought overloading an operator was possible only with at leas one user type as a parameter.
Thinking about it - you're right. I also just tried it - and you're even more right. It's not something I'd ever really considered before now. Hmmm, must go ponder things.
This behaviour is present in C# as well, which is extremely wierd because the operators that accept shorts return ints, but the operators that accept ints do not return longs (in C# a long is 8 bytes and an int is 4). You would think that there would be consistent behaviour for all types if they were trying to not let the result of an expression overflow, but then again, an overflow is more likely to occur when operating with 2 shorts (especially addition and multiplication) compared to adding 2 ints. I think they should leave overflow conditions upto the programmer to handle.......
Here is some info I found about this warning which is a MMVP blog post and bug report.
edit:
So it seems that it is actually a bug, but it is a bug in respect to when/if the warning is produced. Integral promotion is occurring as the above blog claims and as stated on msdn.
Quote:
Integral promotions are performed on the operands as follows:
...
If none of the preceding conditions are met, both operands are converted to type int.


[Edited by - dmail on September 19, 2008 6:46:08 PM]
Candidly, VS' /W4 is quirky as hell. It improves each version, but I guarantee that any substantial amount of code written and compiled using /W4 will at some point see a false positive. This is very annoying, but I've found it's still best to stick to /W4, and locally pragma disable warnings with a comment indicating that it's false.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

This topic is closed to new replies.

Advertisement