Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2005-05-02 13:32:26

Dave Harris wrote:
> In-Reply-To: <4276462D.1020501_at_[hidden]>
> eric_at_[hidden] (Eric Niebler) wrote (abridged):
>>I'm not sure why users will need to understand how FOREACH uses
>>its hidden variables. Can you explain?
> When I am debugging, I need to understand what the code is doing else how
> I can I understand what it is doing wrong?
> For example, from looking at the implementation it seems that the macro
> caches the end() value in a variable called _foreach_end. This means that
> if some random memory corruption changes that variable the loop may never
> terminate. I need to know.

OK, that's true. But the same can be said of any third-party library. I
guess the only problem is that most debuggers don't let you step into a

> Incidently, if it is caching then code like:
> BOOST_FOREACH( int i, vec )
> if (condition(i))
> vec.push_back( filter(i) );
> will not work the same as:
> for (iterator i = vec.begin(); i != vec.end(); ++i)
> if (condition(*i))
> vec.push_back( filter(*i) );

Neither of these loops "work" -- they are both broken in the same way.
They both invalidate the iterator they are using. I don't think it's
reasonable to assume rational behavior when you are altering the
sequence while you are iterating it. But perhaps it should be worth
noting in the docs that FOREACH doesn't perform any heroics to make this

Eric Niebler
Boost Consulting

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