disabling unary "-" for unsigned types

bearophile bearophileHUGS at lycos.cos
Wed Feb 17 01:45:30 PST 2010


Walter Bright:
> For example, let's take two indices into an array, i and j:
>      size_t i,j;
> size_t is, by convention, unsigned.

>From what I've seen so far unsigned integers are useful when:
- You need a bit array, for example to implement a bloom filter, a bitvector, a bit set, when you want to do SWAR, when you need bit arrays to deal with hardware, etc.
- When you really need the full range of numbers 0 .. 2^n, this happens but it's uncommon.

In most other situations using unsigned numbers is unsafe (because other rules of the language make them unsafe, mostly) and it's better to use signed values. So array indices are better signed, as almost everything else. If you mix signed and unsigned arithmetic to index an array or to measure its length you will often introduce bugs in the code (because the language seems unable to manage ranges of values in a tidy way). It seems integral numbers is one of the things CommonLisp gets right and C/D do wrong.

Bye,
bearophile



More information about the Digitalmars-d mailing list