We compile our code under warning level 4 and want to have the C4244 warning
enabled as it sometimes catch problems in the code.

However, consider the following code:

unsigned char a, b;
a += b; // Generates warning
a = a + b; // Does not generate warning

The problem is that C++ will expand the type of the + expression to int and
then implicitly convert the int to unsigned char, which should generate a
warning.

We dunno if the compiler folks at Microsoft thought "hey thats silly to warn
here" and disabled the warning for the regular assigment but forgot to
disable it for the += assignment or if they made an error not reporting the
implicit conversion of the type in the regular assignment with the C4244
warning.

We would very much like VC would not warn for either assignment operators
when types are implicitly expanded and then implicitly reduced as this is
never a warning you care about.

Currently, we have to use the second form to avoid the warning but if the VC
teams decides to warn here also we really need to write:

a = (unsigned char)(a + b);

And this kinda sucks. (Although it is really C++ that sucks here)

Regards

Ulf Johansen

Re: Changes to the C4244 warning? by Doug

Doug
Thu Jun 22 11:48:59 CDT 2006

On Thu, 22 Jun 2006 02:34:02 -0700, ulf_johansen
<ulfjohansen@discussions.microsoft.com> wrote:

>We compile our code under warning level 4 and want to have the C4244 warning
>enabled as it sometimes catch problems in the code.
>
>However, consider the following code:
>
>unsigned char a, b;
>a += b; // Generates warning
>a = a + b; // Does not generate warning
>
>The problem is that C++ will expand the type of the + expression to int and
>then implicitly convert the int to unsigned char, which should generate a
>warning.
>
>We dunno if the compiler folks at Microsoft thought "hey thats silly to warn
>here" and disabled the warning for the regular assigment but forgot to
>disable it for the += assignment or if they made an error not reporting the
>implicit conversion of the type in the regular assignment with the C4244
>warning.
>
>We would very much like VC would not warn for either assignment operators
>when types are implicitly expanded and then implicitly reduced as this is
>never a warning you care about.
>
>Currently, we have to use the second form to avoid the warning but if the VC
>teams decides to warn here also we really need to write:
>
>a = (unsigned char)(a + b);
>
>And this kinda sucks. (Although it is really C++ that sucks here)

See:

C4244 when adding unsigned shorts w/ +=
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=99575

--
Doug Harrison
Visual C++ MVP