Boost logo

Boost :

From: Daniel Wallin (dalwan01_at_[hidden])
Date: 2003-10-12 06:12:09

At 23:40 2003-10-11, Eric Niebler wrote:
>Daniel Wallin wrote:
>> 1. It operates on containers rather than iterators.
>Yes, containers, arrays and null-terminated strings. What is your
>objection exactly? If you are saying that iterating over a container is
>wrong *in principle*, then I disagree -- it's often exactly what you want.
>If you are saying that there should be a way to iterate over a
>sub-sequence, then I agree.

I just prefer to always specify ranges explicitly, mostly for consistency.
I also
dislike interfaces which tries to handle every possible use case.

   BOOST_FOREACH(int i, iter1, iter2)

Would be better IMO.

>> 2. It reevaluates the container expression on every iteration.
>True, but the "container expression" must be an lvalue, which makes it
>difficult to use incorrectly. The container expression cannot be a
>function call that returns a temporary, for instance.
>You may be wondering about in_range() above -- it is a function that
>returns a temporary, yet it is being used as a container expression.
>in_range() returns a special type, and BOOST_FOREACH accepts it even
>though it is not an lvalue. However, the *arguments* to in_range() must be
>iterator lvalues and not temporaries returned by functions.
>I understand your concerns, however. There is still the danger for this:
>BOOST_FOREACH( int i, in_range( ++iter1, iter2 ) )
> ^^^^^^^ oops!
>I'm not sure how to guard against that.

Yeah.. So when using in_range() you'd still need to name your iterators,
and also
place them in the "wrong" scope. I'd prefer

   for (iter_type iter = x.begin(), end = x.end();
      iter != end; ++iter)


   iter_type begin = x.begin();
   iter_type end = x.end();

   BOOST_FOREACH(xxx i, in_range(begin, end))

any day.

Daniel Wallin

Boost list run by bdawes at, gregod at, cpdaniel at, john at