implicit ubyte casting

bearophile bearophileHUGS at lycos.com
Thu Oct 1 11:00:30 PDT 2009


Saaa:
>Does anybody ever use the =-z behaviour?<

Sometimes C programmers use something like:
unsigned int x = -1;

The interaction of signed-unsigned integral numbers in D is very error-prone, so much that I suggest to use unsigned integrals in D only where strictly necessary (generally when you need bitfields for bitwise operations (so not for arithmetic operations), or the less common situations where you need the full range of 1,2,4,8 bytes).

Sometimes I even cast array lengths to an int and then I keep and use only such int around because in D that's safer than using a size_t (example: if you compare an unsigned int length with a negative int, your code will have a bug).

I have discussed such topics several times in the main D newsgroup, and in the end no good solution has being found/accepted so far. But eventually some better solution must be found...

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list