'int' is enough for 'length' to migrate code from x86 to x64

FrankLike via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 21 18:39:12 PST 2014


On Saturday, 22 November 2014 at 01:57:05 UTC, Andrei 
Alexandrescu wrote:
> On 11/21/14 1:55 PM, Marco Leise wrote:
>> Am Wed, 19 Nov 2014 18:20:24 +0000
>> schrieb "Marc Schütz" <schuetzm at gmx.net>:
>>
>>> I'd say length being unsigned is fine. The real mistake is 
>>> that
>>> the difference between two unsigned values isn't signed, which
>>> would be the most "correct" behaviour.
>>
>> Now take my position where I explicitly write code relying
>> on the fact that `bigger - smaller` yields correct results.
>>
>> uint bigger = uint.max;
>> uint smaller = 2;
>> if (bigger > smaller)
>> {
>>     auto added = bigger - smaller;
>>     // Now 'added' is an int with the value -3 !
>> }
>> else
>> {
>>     auto removed = smaller - bigger;
>> }
>
> Interesting insight. Thanks for the many analytical examples 
> you're giving in this thread. -- Andrei

It's right.

------smalltest----------------
import std.stdio;
void main()
{
	uint a =100;
	int b =80;
	auto c = a-b;
	auto d = b-a;
	writeln("c is ",c,", c's type is ",typeid(c));
	writeln("d is ",d,", d's type is ",typeid(d));

     auto  e =  b- a;
	writeln("e is ",e," e's type is ",typeid(e));
	auto  f =  cast(int)b- cast(int)a;
	writeln("f is ",f," f's type is ",typeid(f));
}
-------------end-----------------------------
Only c and f's result is ok.





More information about the Digitalmars-d mailing list