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
Therefore I don't think it's possible for the compiler or boost to optimize
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.
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
> Does anyone have any experience with this? Is there a portable way to
> hints to the compiler? Should boost provide such a facility?
> Unsubscribe & other changes:
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk