Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / December 2006

Tip: Looking for answers? Try searching our database.

VS2005 Pro SP1 and C4244

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
A Wieser - 28 Dec 2006 16:13 GMT
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
David Lowndes - 28 Dec 2006 16:50 GMT
>I've just upgraded to SP1, and am seeing C4244  warnings on code like the
>following:
[quoted text clipped - 16 lines]
>
>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
Bruno van Dooren [MVP VC++] - 28 Dec 2006 21:05 GMT
>>    a += b;        // warning C4244: '+=' : conversion from 'int' to
>>'short', possible loss of data i:\2005\CPP\NDS\testmsl\Calsetng.cpp 168
[quoted text clipped - 16 lines]
>
> 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...

Signature

Kind regards,
   Bruno van Dooren
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

A Wieser - 28 Dec 2006 21:28 GMT
| >>    a += b;        // warning C4244: '+=' : conversion from 'int' to
| >>'short', possible loss of data i:\2005\CPP\NDS\testmsl\Calsetng.cpp 168
[quoted text clipped - 24 lines]
| 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
Bruno van Dooren [MVP VC++] - 29 Dec 2006 06:57 GMT
> The documentation seems a bit misleading:
> it says here:
[quoted text clipped - 9 lines]
>     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 = +.

Signature

Kind regards,
   Bruno van Dooren
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

A Wieser - 29 Dec 2006 07:26 GMT
|> 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:
| > "+=
[quoted text clipped - 14 lines]
| I still think it is idiotic that += does this, since it is supposed to be
| equal to = +.

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!

--

Anthony Wieser
Wieser Software Ltd

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.