On Wed, 01 Oct 2014 13:29:39 -0700, Gert-Jan de Vos wrote:
> On Wednesday, 1 October 2014 19:51:52 UTC+2, Paavo Helde wrote:
>
>> Beware that there are two parties in this group, ones who think that
>> unsigned types are the Right Thing, and others who think they are Real
>> Evil. I predict another 500 posts in this thread ;-(
Seems you were right.
> The problem is not with the unsigned types. The problem is the
> conversion of mixed signed/unsigned expressions to unsigned that C++
Right.
> inherited from C. In practice, unsigned types don't behave so nice in
> arithmetic expression where they often lead to unexpected wrap arounds.
And such wrap around is defined, unlike signed types, which also suffer
from wrap around.
> Consider the difference of two unsigned values. The result will also be
> unsigned whereas the arithmetic result can be negative.
Consider the sum of two signed positive vales. The result can be negative
whereas the arithmetic result should be positive.
What you are probably trying to say, is that the difference between two
unsigned integers, lets say indexes into an array, can wrap instead of
becoming negative. On a twos complement machine, this will give the
correct result if assigned to a signed integer of the same size. However,
this behavior is not guaranteed by the standard.
Or in other words, the difference between two unsigned integrals may not
be represented correctly, even when that difference is cast to a signed
type.
> I use unsigned for bit manipulation only and convert between unsigned
> and int on API boundaries like container's size(). This has kept my code
> free of mixed signed/unsigned trouble.
Absolutely sound advice.
M4