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

Steven Schveighoffer schveiguy at yahoo.com
Tue Apr 2 20:10:14 PDT 2013


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


More information about the Digitalmars-d mailing list