Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-05-07 22:45:02


David Abrahams <dave_at_[hidden]> writes:

> "Eric Niebler" <eric_at_[hidden]> writes:
>
>> David Abrahams wrote:

>>> "There's gotta be a better way" ;-)
>>>
>>
>> There isn't, and it's easy to see why. If you want to support
>> reference loop variables, you need to be able to rebind the reference
>> at each iteration. References aren't rebindable, so you need a
>> declaration. In C++, there is only one way to declare a varible,
>> inject it into a subsequent scope and *not* evaluate it in boolean
>> context: the for loop. Hence, the inner for loop is necessary, as is a
>> mechanism for executing the loop only once and for handling break
>> statements.
>
> That isn't enough to convince me that a boolean is needed, yet. Maybe
> something could be done with "goto"? I haven't looked at
> BOOST_FOREACH carefully, but I'm imagining something like:
>
> for (C::iterator s = c.begin(), f = c.end(); s != f; ++s) // outer
> {
> for(C::reference r = *s;;) // inner
> {
> // Body goes here
> goto continue_;
> }
> break; // if there's a break in the body, break out all the way
> continue_:
> }
>
> Not sure if you can generate those labels reliably, but it seems
> possible as long as nobody wants to put two FOREACHes on a single
> line.

Err, this could only work if you were willing to pass the loop block
to the macro. Might be worth the pain, though (?)

-- 
Dave Abrahams
Boost Consulting
http://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