Sign in to follow this  
janta

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

Recommended Posts

short a = 2;
short b = 1;
short c = a - b; // OK
a -= b; // Level 4 warning in VS2008, "warning C4244: '-=' : conversion from 'int' to 'short', possible loss of data
Anyone knows the exact reason?

Share this post


Link to post
Share on other sites
sure

int _tmain(int argv, const char** argc)
{
short a = 2;
short b = 1;
short c = a - b;
a -= b;
}


[smile]

Edit: it's VS2005 actually (I doubt that matters tho.)

[Edited by - janta on September 19, 2008 1:10:17 PM]

Share this post


Link to post
Share on other sites
I do not think it is a bug, what is happening is integer promotion.
Quote:

4.5.1
An rvalue of type char, signed char, unsigned char, short int, or unsigned short int can be converted to an rvalue of type int if int can represent all the values of the source type; otherwise, the source rvalue can be converted to an rvalue of type unsigned int.

The key word here I think is "can", as to why this might be happening I can only guess it is to stop an overflow and truncation.

Share this post


Link to post
Share on other sites
The code doesn't reproduce the issue in 2008.

I'd think there should be no reason for promotion issues to be involved here; there should be no reason for conversion issues of any kind to be involved, both types are the same type. The standard specifies that "X n= Y" for any suitable n is the same as "X = X n Y" except that X is evaluated but once, so if the operator-= warned I'd expect the operator- to warn as well.

Share this post


Link to post
Share on other sites
Am I the only one to reproduce the issue in VS2005?
(@JPetrie: Have you seen my edit in post#3? Actually I'm using 2005 and not 2008)

Share this post


Link to post
Share on other sites
Yes, I saw your edit.
It reproduces in 2005, just not in 2008. I don't see a (documented) command line switch that seems like it would produce the results, so I'm not clear at all why its happening.

Share this post


Link to post
Share on other sites
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++

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.......

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this