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

Started by
14 comments, last by Promit 15 years, 7 months ago

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?
Advertisement
Hmm, that is bizarre.

For the sake of completeness can you post the complete code for program that exhibits this behavior?
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]

Probably there is a bug in the compiler... ... ... ...
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.
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.
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?
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
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.
I'd say it's a bug that was fixed in 2008. Either ignore it, disable the warning or overload the operator yourself.

This topic is closed to new replies.

Advertisement