Boost logo

Boost :

Subject: Re: [boost] [range][Foreach] Supporting range adaptors for temporary containers
From: Eric Niebler (eric_at_[hidden])
Date: 2011-04-22 05:31:07

On 4/22/2011 4:18 PM, Ivan Le Lann wrote:
> ----- "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.

Agreed. If this is fixed at all, it should be fixed in Boost.Range, and
moving an rvalue container into the range adapter makes perfect sense to me.

Eric Niebler
BoostPro Computing

Boost list run by bdawes at, gregod at, cpdaniel at, john at