why $ is need to access array [negative index]?

IGotD- nise at nise.com
Sun Sep 20 14:06:10 UTC 2020

On Sunday, 20 September 2020 at 14:01:58 UTC, claptrap wrote:
> You dont need an extra check, you just do an unsigned 
> comparison, negative values will interpreted as larger. IE...
> int idx = length-i;
> assert(cast(unsigned) idx < length)
> catches anything outside the valid range.
> Yes you have the issue that the index could wrap around and end 
> up valid, but you already have that anyway. I mean if (idx-1) 
> is going to cause a problem so will (idx+0xFFFFFFFF)

No, if -1 is going to be interpreted as the element just before 
the end of the array you will need to have an extra check 
additional from the extra range check.

Still completely irrelevant for D as indexes are of type size_t 
which is unsigned.

More information about the Digitalmars-d mailing list