Boost logo

Boost Users :

Subject: Re: [Boost-users] BOOST_FOREACH with Index
From: Eric Niebler (eric_at_[hidden])
Date: 2009-12-13 17:53:00

The Dude wrote:
> Eric Niebler wrote:
>> Why not:
>> int index = 0;
>> BOOST_FOREACH(const Foo &f, foos)
>> {
>> // ... stuff ...
>> ++index;
>> }
>> ?
> Thanks for you answer. I'm not sure how to answer the "why not"? The
> code you write certainly will work, but so would the predecessor to
> BOOST_FOREACH in the first place, no? So here's my attempt:
> 1. For shorter loops, this changes 2 LOCs to 5.

So put it all on one line! Kidding. ;-)

> 2. For longer loops, the iteration code changes its meaning if it
> appears before the ++index or after.
> 3. The variable index has scope outside the loop.
> 4. Other people think so, e.g., the author's of D language
> It's true that none of these points is really a proof. Still, I'd be
> really happy to hack my own INDEX_FOREACH, but the 500+ LOCs of
> BOOST_FOREACH left me daunted.

This is true, but I'm not convinced. What value BOOST_FOREACH has comes
primarily from its ability to simplify something that's already pretty
darn simple -- a plain for(;;) loop. I strongly resist any effort to
make BOOST_FOREACH more complicated, unless the wins are truly
significant. In this case, I don't think they are.

Eric Niebler
BoostPro Computing

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at