   unexplained warning
  Why does the third line of the following snippet generate this warning:warning C4244: '+=' : conversion from 'int' to 'unsigned short', possible loss of dataunsigned 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]
  what happens when you do this?u16 a = 1;u16 b = 2;a += b;;  Updated original post to more clear example.And yes it still happens that way.This is compiled on MS VCExpress with W4   ;  Maybe becayse operator += returns an int.int a = 1;unsigned short b = 2;b += a; // warninga += b; // no warning;Quote:Original post by jantaMaybe becayse operator += returns an int.int a = 1;unsigned short b = 2;b += a; // warninga += b; // no warningIf 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.   ;  a += b <=> a = a + b, and a + b is not guaranteed to fit in an unsigned short, so the compiler makes it an int?   ;  I've had this warning loads of times. As others have said, operator += returns an int.   ;Quote:Original post by Evil SteveI'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.   ;Quote:Original post by rick_appletonIf 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.;  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.   