implicit ubyte casting

Don nospam at nospam.com
Fri Oct 2 07:25:01 PDT 2009


Brad Roberts wrote:
> On Thu, 1 Oct 2009, Saaa wrote:
> 
>> I think is very bug-prone, isn't it obvious iub should be -5?
>>
>> ubyte ub = 5;
>> int iub = -ub; // iub now is 251
>>
>> What is the reasoning to do it this way? 
> 
> The inclusion of the 'int' part obscures what I think the real problem 
> is.. 
> 
>    Does it make sense to use uniary-minus on a unsigned type?
> 
> My answer.. no.


I agree. But you don't actually need unary minus to see the problem:

import std.stdio;

void main()
{
   uint a = 0;
   uint b = 5;
   long ib =  a - b;
   writefln("%s", ib); // prints: 4294967291
}


More information about the Digitalmars-d-learn mailing list