Boost logo

Boost :

Subject: Re: [boost] [Foreach] Supporting range adaptors for temporary containers
From: Michel MORIN (mimomorin_at_[hidden])
Date: 2011-04-22 07:26:04


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

Right.

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

I don't intend to fix C++11 range-based for.
To fix this, we need to modify the Range library.

I saw a few approaches to solve the dangling reference problem in the
foreach statement. One approach is, the same as yours, that range adaptors
have a copy (resource) if necessary. Now that we have rvalue references and
move semantics, this approach seems realizable.

Another approach, which is currently available, is to use `oven::shared`:

    BOOST_FOREACH(
        char ch
      , pstade::oven::shared(new std::string("Hello, world!")) | reversed)
    {
        ...
    }

See
http://p-stade.sourceforge.net/oven/doc/html/oven/ranges.html#oven.ranges.shared

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

Regards,
Michel


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