
on Sat Jun 23 2012, Nathan Ridge <zeratul976-AT-hotmail.com> wrote:
By the way, my first thought when I encountered this problem is that C++11 ought to have specified the range-based for loop in such a way that the lifetime of temporaries in the range expression is extended for the duration of the loop.
Does anyone share this opinion?
+0. I don't know enough to have an opinion yet.
Would there be any downsides to this?
Well of course there would. Extending the lifetime of a non-trivial temporary beyond what's needed creates inefficiencies (memory pressure, register pressure, worse locality of reference...)
It is also interesting to note that boost::for_each together with a lambda - a from to which any range-based for loop can easily be converted - does not suffer from this problem. I think this corroborates the fact that the range-base for loop is suboptimally specified in the standard - after all the range- based for loop was meant to replace things like boost::for_each.
You have a point... though maybe what we really need is expression-statements ;-) -- Dave Abrahams BoostPro Computing http://www.boostpro.com