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

This topic is 3375 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
Hmm, that is bizarre.

For the sake of completeness can you post the complete code for program that exhibits this behavior?

##### 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 on other sites

Probably there is a bug in the compiler... ... ... ...

##### Share on other sites
I do not think it is a bug, what is happening is integer promotion.
Quote:
 4.5.1An 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 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 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)

Is this /W4?

##### Share on other sites
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 on other sites
I'd say it's a bug that was fixed in 2008. Either ignore it, disable the warning or overload the operator yourself.

##### Share on other sites
Quote:
 Original post by PromitIs 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 on other sites
Quote:
 Original post by dmatterI'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 on other sites
Quote:
 Original post by jantaI 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 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 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 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 on other sites

This topic is 3375 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.