bearophile can say "i told you so" (re uint->int implicit conv)

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Apr 2 20:26:54 PDT 2013


On 4/2/13 11:10 PM, Steven Schveighoffer wrote:
> On Tue, 02 Apr 2013 16:32:21 -0400, Walter Bright
> <newshound2 at digitalmars.com> wrote:
>
>> On 4/2/2013 12:47 PM, Andrei Alexandrescu wrote:
>>> I used to lean a lot more toward this opinion until I got to work on
>>> a C++
>>> codebase using signed integers as array sizes and indices. It's an
>>> pain all over
>>> the code - two tests instead of one or casts all over, more cases to
>>> worry
>>> about... changing the code to use unsigned throughout ended up being an
>>> improvement.
>>
>> For example, with a signed array index, a bounds check is two
>> comparisons rather than one.
>
> Why?
>
> struct myArr
> {
> int length;
> int opIndex(int idx) { if(cast(uint)idx >= cast(uint)length) throw new
> RangeError(); ...}
> }
>
> -Steve

As I said - either two tests or casts all over.

Andrei


More information about the Digitalmars-d mailing list