Boost logo

Boost :

From: Dave Harris (brangdon_at_[hidden])
Date: 2002-05-18 10:20:21


In-Reply-To: <3CE55787.7A214390_at_[hidden]>
On Fri, 17 May 2002 21:18:31 +0200 Daniel Frey (daniel.frey_at_[hidden])
wrote:
> > > while( distance >= 0 && first != last ) {
> >
> > This returns true if the distance is greater than that requested.
>
> The test-program I compiled with gcc 2.95.2 works as expected and does
> returns false if the distance is greater than that requested.

My mistake. I misread the distance >= 0 clause. I wasn't expecting the
loop to repeat distance+1 times, or that the loop counter would decrement
past 0. I usually try to avoid code like that when the counter is a
typedef because of bad experiences with unsigned types. Here distance
should be a distance_type, and I guess it is OK to rely on that always
being signed.

I'd still prefer:

    while (distance > 0 && first != last) {
       ++first;
       --distance;
    }
    return distance == 0 && first == last;

which adds an iterator comparison but avoids the negatives, avoids an
increment, a decrement and an integer comparison, and in my view makes the
return value clearer. But the previous code wasn't actually wrong. My
apologies for wasting your time.

-- Dave Harris


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk