disabling unary "-" for unsigned types
Steven Schveighoffer
schveiguy at yahoo.com
Mon Feb 15 13:52:18 PST 2010
On Sun, 14 Feb 2010 17:02:12 -0500, Andrei Alexandrescu
<SeeWebsiteForEmail at erdani.org> wrote:
> dsimcha wrote:
>> == Quote from Andrei Alexandrescu (SeeWebsiteForEmail at erdani.org)'s
>> article
>>> ulong x0;
>>> static assert(!__traits(compiles, -x0));
>>> uint x1;
>>> static assert(!__traits(compiles, -x1));
>>> ushort x2;
>>> static assert(!__traits(compiles, -x2));
>>> ubyte x3;
>>> static assert(!__traits(compiles, -x3));
>>> Sounds good?
>>> Andrei
>> The more you bring up features to give the axe to, the more I find it
>> funny that,
>> not being a very good language lawyer, I wasn't aware that half of
>> these features
>> existed in the first place. This is one of them. Yes, definitely get
>> rid of it.
>> It makes absolutely no sense. If you want to treat your number like a
>> signed
>> int, then it should require an explicit cast.
>
> I said the same. Walter's counter-argument is that 2's complement
> arithmetic is an inescapable reality that all coders must be aware of in
> D and its kin. Negation is really taking the two's complement of the
> thing. The fact that the type was unsigned is not of much import.
I'd say 99% of the cases where assigning a negative number to an unsigned
type is the literal -1 (i.e. all bits set). It's just as easy to type ~0.
In fact, in any case that you are using a literal besides that, it's
usually more helpful to type the hex representation, or use ~(n-1) where
you compute n-1 yourself (as in the -1 case).
are there any good cases besides this that Walter has? And even if there
are, we are not talking about silently mis-interpreting it. There is
precedent for making valid C code an error because it is error prone.
If no good cases exist, I'd say drop it.
-Steve
More information about the Digitalmars-d
mailing list