Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2002-05-21 04:20:19


Dave Harris wrote:
>
> 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.

Whether distance_type is signed or unsigned should be looked up in the
standard, but I don't have the standard :) Anyway we shouldn't guess if
we can know. If you have the standard, can you please check it?

> 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.

No need to apologise, I never think that serious discussion is a waste
of time. Actually I think your code is clearer and should thus be
preferred.

Regards, Daniel

--
Daniel Frey
aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de

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