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

Don turnyourkidsintocash at nospam.com
Wed Apr 3 04:33:05 PDT 2013


On Wednesday, 3 April 2013 at 03:26:54 UTC, Andrei Alexandrescu 
wrote:
> 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

Yeah, but I think that what this is, is demonstrating what a 
useful concept a positive integer type is. There's huge value in 
statically knowing that the sign bit is never negative. 
Unfortunately, using uint for this purpose gives the wrong 
semantics, and introduces these signed/unsigned issues, which are 
basically silly.

Personally I suspect there aren't many uses for unsigned types of 
sizes other than the full machine word. In all the other sizes, a 
positive integer would be more useful.




More information about the Digitalmars-d mailing list