Boost logo

Boost :

Subject: Re: [boost] [Range] Range adaptor approach for temporary range lifetime issue
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2012-06-23 16:22:36


On Sat, Jun 23, 2012 at 2:13 AM, Nathan Ridge <zeratul976_at_[hidden]>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? Would there be any downsides
> to this?
>

I, too, was thinking along the lines of Dave:

"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...)"

So, I'm not sure, either.

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

Maybe, like Dave suggested, there should be a language construct to
preserve temporaries within an expression "beyond the semicolon" within a
code block:

some-block-keyword( x = expr ) { /*...no dangling references within x...*/ }

- Jeff


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