Jump to content
  • Advertisement
Sign in to follow this  
janta

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

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

If you intended to correct an error in the post then please contact us.

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

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

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!