Boost logo

Boost :

Subject: Re: [boost] [Foreach] Supporting range adaptors for temporary containers
From: Ivan Le Lann (ivan.lelann_at_[hidden])
Date: 2011-04-22 05:18:16


----- "Michel MORIN" <mimomorin_at_[hidden]> a écrit :

> Sometimes it is convenient to apply range adaptors to a temporary
> container
> and iterate over it:
>
> // `using namespace boost::adaptors;` is assumed
> BOOST_FOREACH(auto x, std::string("Hello, world!") | reversed)
> {...}
>
> However, the lifetime of the temporary container ends before the loop
> body
> begins. This problem also exists in C++0x range-based for.
> To solve this, I'd like to propose an extension of BOOST_FOREACH
> macro:
>

IIUC, the line below is broken:
auto const & broken_range = std::string("Hello, world!") | reversed;

So it seems you're proposing to adapt BOOST_FOREACH to solve a problem in adaptors.
How do you intend to fix C++11 builtin foreach?

In C++11, I think this could be fixed with rvalue refs detection.
Shouldn't any function relying on the fact that its arguments still exist
after the call do that check?

So either delete the &&-function or ... [disclaimer: ugly but funny] make it move
the range inside the adaptor somehow.

Ivan


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