Boost logo

Boost :

From: Neil Groves (neil_at_[hidden])
Date: 2008-01-03 10:07:07


I don't think this is optimized by any C++ compiler. The body of the
for-loop might modify the sequence and invalidate the end() iterator of the
previous iteration.

Therefore I don't think it's possible for the compiler or boost to optimize
this.

You could write:
for (Iterator i = boost::begin(x), last = boost::end(x); i != last; ++i) ...

for_each(boost::begin(x), boost::end(x), ... )

Or use boost::range_ex (see range_ex.zip in the root of boost vault) to use:
for_each(x, ... )

Or use the BOOST_FOR_EACH macro.

The intent to not modify the iterator needs to be more explicitly stated to
allow the optimization while ensuring correctness.

HTH,
Neil Groves

On Jan 3, 2008 2:42 PM, Neal Becker <ndbecker2_at_[hidden]> wrote:

> The common idiom is:
>
> for (; i != boost::end (vector); ++i) ...
>
> The compiler had damn well better realize that boost::end is a constant
> function that can be lifted from the loop, or performance can be severely
> impacted.
>
> Does anyone have any experience with this? Is there a portable way to
> give
> hints to the compiler? Should boost provide such a facility?
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk