17 Jun 2012 14:25
[Range] Range adaptor approach for temporary range lifetime issue
Michel Morin <mimomorin <at> gmail.com>
2012-06-17 12:25:36 GMT
2012-06-17 12:25:36 GMT
When an rvalue range is adapted in range-based for loop,
the lifetime of the rvalue range ends before the loop body begins.
For example, the following code produces dangling references.
for (auto x : std::string("Hello world!") | reversed) { /*...*/ }
This is the temporary range lifetime issue.
When I proposed BOOST_FOREACH extension for this issue,
( http://lists.boost.org/Archives/boost/2011/04/180591.php )
a solution with "moving an rvalue range into range adaptors"
was suggested in the thread.
I implemented this approach as a range adaptor (with the aid of
rvalue references and decltype C++11 features):
for (auto x : std::string("Hello world!") | moved | reversed) { /*...*/ }
When an rvalue range is piped to `moved` adaptor, it returns `moved_range`.
`moved_range` stores the following data:
* Moved rvalue range:
The rvalue range is moved and stored in `moved_range`.
* Applied adaptors:
When range adaptors are applied to`moved_range`, they are
not invoked immediately; they are stored in fusion vector.
The stored adaptors are invoked when `begin` or `end`
member functions of `moved_range` are called.
(To prevent repeated applications of the stored range adaptors,
(Continue reading)
I understand the rationale entirely and normally this is something I
would push for. In this case I believe we should be able to iterate upon
your original solution and take advantage of building some optimizations
into the range adaptors. I think we can then achieve a zero-cost under
normal conditions implementation with opt-in explicit moving. I'm
hacking up something to opt-in once and have the opt-in continue
left-wise through the application of the | operator.
>
> Regards,
> Michel
>
> _______________________________________________
> Unsubscribe & other changes:
RSS Feed