Skip to main content
GameDev.net gamedev.net
🔒 Locked

unexplained warning

Started by rick_appleton May 27, 2006 at 1:07 PM 11 replies 1k views
Original Post
rick_appleton
rick_appleton
Why does the third line of the following snippet generate this warning: warning C4244: '+=' : conversion from 'int' to 'unsigned short', possible loss of data

unsigned short a = 1;
unsigned short b = 2;
a += b;
Edit: changed u16 to unsigned short to clarify typedef [Edited by - rick_appleton on May 27, 2006 2:30:27 PM]
janta
janta
Maybe becayse operator += returns an int.

int a = 1;unsigned short b = 2;b += a; // warninga += b; // no warning
rick_appleton
rick_appleton
Quote:
Original post by janta
Maybe becayse operator += returns an int.

int a = 1;unsigned short b = 2;b += a; // warninga += b; // no warning


If that were the case it would be pretty stupid wouldn't it?

Also, in your example, the b+=a will trigger another warning anyway (loss of precision), so there isn't need for this one.
Enselic
Enselic
a += b <=> a = a + b, and a + b is not guaranteed to fit in an unsigned short, so the compiler makes it an int?
Evil Steve
Evil Steve
I've had this warning loads of times. As others have said, operator += returns an int.
Washu
Washu
Quote:
Original post by Evil Steve
I've had this warning loads of times. As others have said, operator += returns an int.

Wrong, section 5.17 of the holy standard states that the type of the expression is the same as teh left operand, which is an unsigned short in this case. This appears to just be a quirk with WL 4.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
janta
janta
Quote:
Original post by rick_appleton
If that were the case it would be pretty stupid wouldn't it?

Also, in your example, the b+=a will trigger another warning anyway (loss of precision), so there isn't need for this one.


Dude, I just wanted to show you what would trigger a warning and what would not. My example sure is silly, but I tested it - compiled it on VC++ Express 2005 /W4 - and it gave exacly the warning you talked about on the very precise line I pointed out. Nothing more, and nothing less either.

Janta.

Promit
Promit
I've encountered a number of false positives in VC's W4 (including this one), and all you can do about it is to put in bogus code like a type cast or something to convince it that it's ok.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
MaulingMonkey
MaulingMonkey
Quote:
Original post by Promit
I've encountered a number of false positives in VC's W4 (including this one), and all you can do about it is to put in bogus code like a type cast or something to convince it that it's ok.


Indeed. For warnings which are silly for large blocks of code, #pragma warning( push | pop | disable : number ) can be useful.

For example, I did this recently in my bugfix/port to VS2k5 of industry.multitype.hpp - a warning about multiple assignment operators. It was with this class:

	template < TYPENAME_NIL_Tn >	struct multitype : BOOST_PP_ENUM( INDUSTRY_MULTITYPE_LIMIT , INDUSTRY_MULTITYPE_ASPECT_name , T ) {		BOOST_PP_REPEAT( INDUSTRY_MULTITYPE_LIMIT , INDUSTRY_MULTITYPE_ASPECT_use_assigner , T );	};


Where the BOOST_PP_REPEAT line expands out into:

using detail::multitype_aspect< multitype< Tn > , 0 , T1 >::operator=;using detail::multitype_aspect< multitype< Tn > , 0 , T2 >::operator=;using detail::multitype_aspect< multitype< Tn > , 0 , T3 >::operator=;...


So, yeah, I felt confident that a header entirely dedicated to a class who's sole purpouse revolved around allowing multiple assignment operations based on type, had no need to generate warnings that yes, that was in fact what it was doing :).
joanusdmentia
joanusdmentia
Quote:
Original post by Washu
Quote:
Original post by Evil Steve
I've had this warning loads of times. As others have said, operator += returns an int.

Wrong, section 5.17 of the holy standard states that the type of the expression is the same as teh left operand, which is an unsigned short in this case. This appears to just be a quirk with WL 4.


The type of a+=b will certainly be an unsigned short, but that's not the problem here. What's happening isn't some qwirk of VC8 either, it's 100% standard. Quoted from said holy standard:

Quote:

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.


For integal operations the above integer types will be promoted, so a+b is calculated as an int and then assigned to the unsigned short, hence the warning.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
dfranzi
dfranzi
I figured the internal promotion was the case. You beat me to it! :)

Quote:
Original post by joanusdmentia
Quote:
Original post by Washu
Quote:
Original post by Evil Steve
I've had this warning loads of times. As others have said, operator += returns an int.

Wrong, section 5.17 of the holy standard states that the type of the expression is the same as teh left operand, which is an unsigned short in this case. This appears to just be a quirk with WL 4.


The type of a+=b will certainly be an unsigned short, but that's not the problem here. What's happening isn't some qwirk of VC8 either, it's 100% standard. Quoted from said holy standard:

Quote:

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.


For integal operations the above integer types will be promoted, so a+b is calculated as an int and then assigned to the unsigned short, hence the warning.


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.