disabling unary "-" for unsigned types

Michel Fortin michel.fortin at michelf.com
Mon Feb 15 16:29:27 PST 2010


On 2010-02-15 18:33:11 -0500, "Steven Schveighoffer" 
<schveiguy at yahoo.com> said:

> I should clarify, using - on an unsigned value should work, it just should
> not be assignable to an unsigned type.  I guess I disagree with the
> original statement for this post (that it should be disabled all
> together), but I think that the compiler should avoid something that is
> 99% of the time an error.
> 
> i.e.
> 
> uint a = -1; // error
> uint b = 5;
> uint c = -b; // error
> int d = -b; // ok
> auto e = -b; // e is type int

But should this work?

uint a = 0-1;
uint c = 0-b;
auto e = 0-b; // e is type int?

uint zero = 0;
uint a = zero-1;
uint c = zero-b;
auto e = zero-b; // e is type int?

This rule has good intentions, but it brings some strange 
inconsistencies. The current rules are much easier to predict since 
they behave always the same whether you have a variable, a literal or a 
constant expression.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list