Signed-unsigned comparisons in Phobos

Simen Kjaeraas simen.kjaras at gmail.com
Fri Aug 12 11:26:14 PDT 2011


On Fri, 12 Aug 2011 19:19:09 +0200, Marco Leise <Marco.Leise at gmx.de> wrote:

> Am 12.08.2011, 17:48 Uhr, schrieb Simen Kjaeraas  
> <simen.kjaras at gmail.com>:
>
>> On Fri, 12 Aug 2011 14:55:26 +0200, Marco Leise <Marco.Leise at gmx.de>  
>> wrote:
>>
>>> Am 12.08.2011, 12:22 Uhr, schrieb kennytm <kennytm at gmail.com>:
>>>
>>>> Don <nospam at nospam.com> wrote:
>>>>
>>>>> I've had a look at a dozen or so of these, and they were all real. I
>>>>> didn't see any which require a cast to "make the compiler shut up".
>>>>> That's pretty impressive. In C++ I find that such messages are nearly
>>>>> always false positives.
>>>>>
>>>>> The one case where it's a bit annoying is this:
>>>>>
>>>>> int [] x = new int[6]; // or x = some array literal.
>>>>> for (int i = 0; i < x.length; ++i) {...}
>>>>>
>>>>> Here is a suggestion for how we could eliminate such false positives.
>>>>> http://d.puremagic.com/issues/show_bug.cgi?id=6478
>>>>
>>>> Doesn't this require flow analysis? And the type of index 'i' should  
>>>> be
>>>> 'size_t' anyway.
>>>
>>> I think I once shot myself in the foot with this when I used 'auto'  
>>> for 'i' and the code wouldn't compile on x86_64, because I assigned  
>>> the variable to an int or uint later on in the loop. You just have to  
>>> be aware that this is an unsigned integer of machine word length. So I  
>>> agree with kennytm on this.
>>>
>>> Just remember that reverse loops are written like this:
>>>
>>> for (size_t i = x.length; i-- > 0; ) {...}
>>
>> I like using the long arrow operator for this:
>>    i --> 0 // i goes to 0
>
> This way it is actually fun to cripple the for loop, yay. Still if  
> people started to argue that it is a bad idea to modify variables in the  
> condition I'd silently agree. So a look at "foreach_reverse (i;  
> 0..x.length) {...}" might be worth it. I guess after a while I will get  
> used to it. It even reminds me of the Pascal "for"-syntax a bit, which  
> is "for i := 0 to 9 do ...". It has no receipt for the reverse  
> zero-length array loop though as far as I know.

Pascal has downto:

for i := 9 downto 0 do ...

-- 
   Simen


More information about the Digitalmars-d mailing list