Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2003-10-09 17:56:10


Brock Peabody wrote:
> Maybe it's just going to take some soaking in before the coolness of
> BOOST_FOREACH overcomes my macro bigotry.
>
> Brock

I agree that ALL CAPS is annoying, and that macros are often misused,
but let this one "soak in" for a bit. I've taken great care to make this
macro Just Work. In particular:

- BOOST_FOREACH will not accept a container that is not an lvalue. This
helps prevent the classic macro bug-a-boo: reevaluation of arguments
with side-effects. For instance, this won't compile:
    BOOST_FOREACH( int i, do_work_and_return_collection() )
(That is, it won't compile if it returns a collection by value.)

- BOOST_FOREACH expands into a single statement, so you can use it after
an "if" statement without braces, as in:
     if(condition) BOOST_FOREACH(...) ...

- The macro introduces no unmatched if/else pairs, so it won't change
the meaning of the code around it.

- All local variables introduced are properly scoped and given obscure
names, so as not to conflict with other identifiers, or other
BOOST_FOREACH statements in the same scope.

- It uses no runtime polymorphism and is designed to be easily inlined
and optimized, so you get no nasty surprises in performance.

I encourage people to play with the code and report their experiences.

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