VS2005 Pro SP1 and C4244

  • Thread starter Thread starter A Wieser
  • Start date Start date
A

A Wieser

I've just upgraded to SP1, and am seeing C4244 warnings on code like the
following:

void fn()
{
short a = 1;
short b = 2;

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/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=99575

Does anyone else get this error with or without SP1?

Anthony Wieser
Wieser Software Ltd
 
I've just upgraded to SP1, and am seeing C4244 warnings on code like the
following:

void fn()
{
short a = 1;
short b = 2;

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/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=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.

Dave
 
a += b; // warning C4244: '+=' : conversion from 'int' to
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...

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
in message | >> 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:
|| >>
| >>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:'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
 
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.

Hi,

http://msdn2.microsoft.com/en-us/library/k630sk6z.aspx
bullet point 1 applies.

See here for the rules:
http://msdn2.microsoft.com/en-us/library/3t4w2bkb.aspx

The behavior of += is not explicitly specified, but the you can see that the
documentation allows for it. It is allowed by the C/C++ standards as well.

I still think it is idiotic that += does this, since it is supposed to be
equal to = +.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
in message |> 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.
|
| Hi,
|
| http://msdn2.microsoft.com/en-us/library/k630sk6z.aspx
| bullet point 1 applies.
|
| See here for the rules:
| http://msdn2.microsoft.com/en-us/library/3t4w2bkb.aspx
|
| The behavior of += is not explicitly specified, but the you can see that
the
| documentation allows for it. It is allowed by the C/C++ standards as well.
|
| I still think it is idiotic that += does this, since it is supposed to be
| equal to = +.
|
| --
|
| Kind regards,
| Bruno van Dooren
| (e-mail address removed)
| Remove only "_nos_pam"
|
|

OK, I see here that it says:
If none of the above conditions are met, both operands are converted to type
int.

So that means the compiler really should be warning me in the second case
too, where I write

short a=1;
short b=2;

a = a + b;

That's even more depressing! So in the next version of the compiler, I'll
get even more warnings.

Maybe the next version will have C4416 defined.

// warning C4416: Ha Ha Ha! You're still using 16 bit values!
 
Back
Top