disabling unary "-" for unsigned types

Ellery Newcomer ellery-newcomer at utulsa.edu
Mon Feb 15 21:40:00 PST 2010


On 02/15/2010 11:35 PM, Ellery Newcomer wrote:
> On 02/15/2010 09:17 PM, Steven Schveighoffer wrote:
>> On Mon, 15 Feb 2010 21:32:21 -0500, Rainer Deyke <rainerd at eldwood.com>
>> wrote:
>>
>>> Ellery Newcomer wrote:
>>>> On 02/15/2010 05:33 PM, Steven Schveighoffer wrote:
>>>>> uint a = -1; // error
>>>>
>>>> I can't say I would appreciate having to write
>>>>
>>>> uint a = 0xFFFFFFFF;
>>>>
>>>> or the equivalent for ulong.
>>>
>>> uint a = ~0u;
>>
>> even ~0 works, no need for the u (although it makes things clearer).
>>
>> Ellery, you didn't read my original post thoroughly, I said this was the
>> most common case of wanting to use unary negative on an unsigned value,
>> and it's easily rewritten, with the same number of characters no less.
>>
>> -Steve
>
> Ohhh! that post! You're right; I missed that part.
>
> Alright, here's something I found myself writing just today or yesterday:
>
> //x,r are long, n is ulong
>
> if(x < 0){
> ulong ux = -x;
> ...
> }
>
> I also have
>
> if(r < 0){
> return n - (-r) % n;
> }
>
> emphasis on ensuring dividend is positive before it gets promoted to
> ulong, etc etc, and I do guard that r is not remotely close to
> ulong.max/min.
>
> assuming that the return type is long (it isn't, but it might as well
> be, since n is always within [2,long.max]) or gets assigned to long or
> whatever.
>
> -The bottom one obeys your rules.
> -The top one doesn't.
> -The bottom one is much less clear than the top.
> -Whatever I was trying to prove, I think I just inadvertently
> strengthened your argument tenfold.
>
> and no, I expect this doesn't fall within the 99% use case of unary -

Oh, darn it. nvm



More information about the Digitalmars-d mailing list