Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2004-05-07 12:37:16


David Abrahams wrote:
>
> It surprises me that BOOST_FOREACH is ever measurably different from
> a hand-coded loop. Can anyone explain that?
>

I could hazard a guess. BOOST_FOREACH uses two nested for loops. The
sole purpose of the inner loop is to rebind the loop variable in case it
is a reference. There is some fancy footwork to make sure the inner loop
executes exactly once for each iteration of the outer loop. In addition,
if the inner loop is exited with a "break," that information must be
relayed to the outer loop. This is all accomplished with the help of a
hidden bool, which is set, checked and reset at each iteration. It's the
only overhead.

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.

-- 
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