Bjoern Milcke wrote:
Hi Stephan,
I came across the following piece of code:
String a;
xub_StrLen n = 0;
n += a.Len();
This breaks on Windows (due to -werror). Because of the warning:
warning C4244: '+=' : conversion from 'int' to 'USHORT', possible
loss of data (in the last line of the fragment)
[...]
Does anybody have an idea what the problem is here?
By the holy standard, x += y where x, y are short works by promoting
x, y to int x', y', adding x' + y', converting int x' to short x'' and
assigning x'' back to x. MSC chose to warn about this (about the
potentially lossy "converting int x' to short x''" part).
Where is that in the standard? Is there a C-Standard, or is this in the
C++-Standard. I couldn't find it.
C++ Standard, 5.17/7, 5.7/1, 5/9, 4.5/1.
I still think this warning doesn't make sense. I am not interested what
the compiler does internally as long as the result is what I expect
(adding 60,000 + 60,000 is an overflow and that's what I expect when
adding shorts. If a possible overflow would cause a warning, almost any
arithmetic operation would have to warn for any type.)
It is not about what the compiler does internally, it is about the C++
language as can be used by a programmer. And the warning is not about
potential overflow during addition, but about potential value-garbling
during assignment. Also see
<http://msdn2.microsoft.com/en-us/library/th7a07tz.aspx>.
I noticed that we didn't have this warning in an m172. Is there a new
warning that is responsible for this? Or is this a general warning for
possible-loss conversions (I thought we had already some before).
I do not understand what you mean. If C4244 is enabled, you get this
specific warning (and many others).
Rewriting this to
n = n + a.Len();
works. But it doesn't look much different.
Yes, MSC does not warn about that one, I guess it is a bug.
What? You mean "short = short + short" is also converted to "short =
(short)((int) + (int))" ?
Yes, of course.
If it would be a warning, I could never add two short values while
having -werror! I can't believe that!
You could add two shorts without warning, but you could not assign the
result to a short lvalue.
-Stephan
Still confused,
-Bjoern
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]