0 < negative loop condition bug or misunderstanding on my part

James Miller james at aatch.net
Wed Mar 7 19:15:08 PST 2012


On 8 March 2012 15:39, Ary Manzana <ary at esperanto.org.ar> wrote:
> On 3/7/12 2:28 AM, Ali Çehreli wrote:
>>
>> On 03/06/2012 09:11 PM, ixid wrote:
>>  > I'm writing my first basic algorithms, this one is merge sort. This
>>  > version throws an exception when array.length - setSize is negative
>>  > (which should be fine, the rest of my function would deal with it):
>>  >
>>  > template mergeSort(T)
>>  > {
>>  > void mergeSort(ref T[] array, const T setSize = 100)
>>  > {
>>  > T[][] merge;
>>  > merge.length = array.length / setSize;
>>  > T ii, jj;
>>  > for(ii = 0, jj = 0;ii < array.length - setSize;ii += setSize, ++jj)
>>
>> We don't know what T is, but I assume a signed type like int.
>>
>> array.length is size_t, i.e. an unsigned type. Unsigned types have this
>> nasty habit of converting the entire expression to unsigned (that is a
>> rule since C). So array.length - setSize above is size_t.
>
>
> Wouldn't it make more sense to convert the entire expression to signed if
> there's at list one signed integer?
>
> I really don't get it...
>
> It's like déjà-vu all the time in this newsgroup.

Its the semantics in C/C++ and D explicitly tries to have the same
semantics as them. From what I remember its to aid people moving from
those language to D.

--
James Miller


More information about the Digitalmars-d-learn mailing list