|
Boost : |
From: Eric Niebler (eric_at_[hidden])
Date: 2003-10-11 16:40:31
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 uploaded a new version that lets you
iterate over an iterator range like this:
using boost::for_each::in_range;
BOOST_FOREACH( int i, in_range( iter1, iter2 ) )
{
...
}
> 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.
-- Eric Niebler Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk