0 < negative loop condition bug or misunderstanding on my part

Ali Çehreli acehreli at yahoo.com
Wed Mar 7 20:39:21 PST 2012


On 03/07/2012 07:51 PM, Ellery Newcomer wrote:
> On 03/07/2012 12:23 AM, Ali Çehreli wrote:
>>
>> There are probably hundreds of discussions about that over the years on
>> many different language newsgroups and forums. :) There is no clear
>> winner: Both sides of the arguments seem to have good points.
>>
>> Ali
>>
>
> know any good ones off the top of your head?

Really randomly:

* Unsigned is better because some things are never negative: sizes, 
indexes, etc.

* But we occasionally need to subtract one from the other. The following 
is not safe unless we are sure that the left-hand side is greater:

    size_t i = /* ... */;
    size_t j = /* ... */;

    foo(i - j); // Is this the difference or a very large number?

Another bug because of an unsigned type:

    for (size_t i = /* ... */; i >= 0; --i) {

(Although I think most compilers catch that one.)

I searched for the following terms on comp.lang.c++.moderated and found 
many interesting discussions:

   size_t unsigned signed

I don't have a strong opinion myself.

Ali


More information about the Digitalmars-d-learn mailing list