From: Thomas Witt (witt_at_[hidden])
Date: 2003-10-09 15:22:10
You asked why there was so little public reply to your posting. Well as
far as I am concerned, I have trouble coming to a conclusion.
Eric Niebler wrote:
> You can use it as follows to loop over an STL collection, for example:
> std::list<int> int_list;
> BOOST_FOREACH( int &i, int_list )
What I don't like about this syntax is that the assignment to i in the
loop is far from obvious.
> // mutates the int in the list:
> i += 20;
> // break works as you would expect
> if ( i == 100 )
In generally I pretty much hate that kind of macros. Constructs that
look very similar have given me a lot of trouble. Though in this case
being against it is more a feeling than that I can come up with a good
technical argument (at this time).
> It has several advantages over std::for_each. In particular:
> - You don't have to define a predicate at namespace scope
> - You can break, continue, goto or return from the middle
> of the loop body.
This is not neccessarily an advantage. It can also be seen as
encouraging bad style.
> - In the loop body, you have complete access to the local
> variables from the surrounding code.
> - You don't need to worry about off-by-one errors, iterators,
> half-open sequences, binders, adapters, lambdas....
As far as lambda is concerned, I do believe that it is worrisome only
because of some restrictions of the language. If I had a builtin lambda
or a library that can make use of a decltype I would always prefer
std::for_each over the macro.
> So, any interest?
I would be interested about real world experience. In that sense I am
interested in a boost implementation. That being said I wouldn't use it
in my code (yet).
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk