"Bruno van Dooren [MVP VC++]" <br**********************@hotmail.comwrote
in message news:OU**************@TK2MSFTNGP02.phx.gbl...
| > a += b; // warning C4244: '+=' : conversion from 'int' to
| >>'short', possible loss of data i:\2005\CPP\NDS\testmsl\Calsetng.cpp 168
| >>
| > // but
| > a = a + b; // doesn't give a warning.
| >>}
| >>
| >>I'm not sure if this was wrong before SP1 or not, but it's reported as
| >>fixed
| >>here:
|
>>https://connect.microsoft.com/Visual...edbackID=99575
| >>
| >>Does anyone else get this error with or without SP1?
| >
| Anthony,
| >
| I'm surprised by the comment to that bug report. I believe this is one
| of those C/C++ quirks that throws most of us at some time or another.
| >
| As far as I know it's expected behaviour not a bug.
|
| This is documented in MSDN if you look for the += operator documentation
| (you have to follow one or two links from there)
| Basically, the C standard says that for those operators, the arguments are
| automatically converted to int.
| It's one of those things that must have seemed like a good idea at the
| time...
|
The documentation seems a bit misleading:
it says here:
ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vclang/html/a6135e34-4fce-4c79-87b6-b6447db3507e.htm
"The assignment operators return the value of the object specified by the
left operand after the assignment. The resultant type is the type of the
left operand. "
Also, the documentation here:
ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vclang/html/b028cf35-2ff1-4f14-9027-fd53ebec8aa0.htm
says:
"+=
Add the value of the second operand to the value of the first operand;
store the result in the object specified by the first operand.
"
So the two lines below should be equivalent:
> a += b; // warning C4244: '+=' : conversion from 'int' to
'short'.
| > // but
| > a = a + b; // doesn't give a warning.
|
Where's your reference that implied that it was ok to convert to int? I
can't find it anywhere (but maybe my google searches are wrong).
Anthony Wieser
Wieser Software Ltd