|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2004-05-07 20:04:20
"Eric Niebler" <eric_at_[hidden]> writes:
> David Abrahams wrote:
>> "Eric Niebler" <eric_at_[hidden]> writes:
>>
>>>For all but the most trivial loops, the cost of reading and writing a
>>>bool is lost in the noise. IMO, being able have reference loop
>>>variables is worth the very small price.
>> "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.
-- 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