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

Wyatt via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 21 08:32:20 PST 2014


On Thursday, 20 November 2014 at 20:17:15 UTC, deadalnix wrote:
> On Thursday, 20 November 2014 at 15:55:21 UTC, H. S. Teoh via
> Digitalmars-d wrote:
>> Using unsigned types for array length doesn't necessarily lead 
>> to subtle
>> bugs, if the language was stricter about mixing signed and 
>> unsigned
>> values.
>>
>
> Yes, I think that this is the real issue.

Thirded.

Array lengths are always non-negative integers.  This is 
axiomatic.  But the subtraction thing keeps coming up in this 
thread; what to do?

There's probably something fundamentally wrong with this and I'll 
probably be called an idiot by both "sides", but my gut feeling 
is that if expressions with subtraction simply returned a signed 
type by default, much of the problem would disappear.  It doesn't 
catch everything and stuff like:

uint x = 2;
uint y = 4;
uint z = x - y;

...is still going to overflow, but maybe you know what you're 
doing? More importantly, changing it to auto z = x - y; actually 
works as expected for the majority of cases.  (I'm actually on 
the fence re: pass/warn/error on mixing, but I _will_ note C's 
promotion rules have bitten me in the ass a few times and I have 
no particular love for them.)

-Wyatt

PS: I can't even believe how this thread has blown up, 
considering how it started.


More information about the Digitalmars-d mailing list