From: Eric Niebler (eric_at_[hidden])
Date: 2004-05-07 17:56:39
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.
If the compiler had excellent flow analysis *and* the loop body did not
contain a break statement, it should be possible for the compiler to
optimize the boolean away. But it looks like compiler technology isn't
quite there yet.
-- 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