Boost logo

Boost :

Subject: Re: [boost] [range] adaptors vs. rvalue to lvalue& binding
From: Arno Schödl (aschoedl_at_[hidden])
Date: 2012-03-22 09:54:43


Hello,

> > ideally I would like to use range-adapted lvalue ranges like any other
> > lvalue ranges. Writing it down naively, as below, does not compile
> > because LoadRefRange takes (and should take?) the range it modifies by
> > lvalue&, while adaptor expressions are rvalues.
> > ...
> > Any thoughts? Should adaptors receiving lvalues offer an
> > operator adaptor&()&&; // overload on rvalue-ness

> you don't want to add such operator to any class, as it would trigger automatic conversion of rvalue of given type to lvalue.
> Sometimes this is not right thing to do, but with added operator you will have no means of disabling this behaviour.

Do you have an example for range adaptors where this is bad?

> Apart from that, ref-qualifiers unfortunately are not yet widely implemented and such code won't be very portable anyway
> (I only know of 2 compilers implementing this feature).

That will hopefully change soon. I am looking for the direction we (or at least I with my project) want to go with this problem.

> What you want to do instead is to change the signature of your function to accept both lvalue and rvalue reference:
>
> template<typename Range>
> void modifies_range(Range && rng);

So do we want to change boost::sort( Rng& ) to boost::sort( Rng&& ) ? That would make boost::sort( std::vector() ) ok. C++ decided at some point to disallow binding rvalues to lvalue&. I think the reason is that rvalues are inaccessible for the caller, so modifying them is likely a bug. I am a bit hesitant to throw that out the window for ranges.

The requirement to treat certain types special w.r.t. rvalue-ness is actually not new. C++11 does the same with iostreams, see 27.7.3.9.

Regards,

Arno

--
Dr. Arno Schödl | aschoedl_at_[hidden]
Technical Director
think-cell Software GmbH | Chausseestr. 8/E | 10115 Berlin | Germany
http://www.think-cell.com | phone +49 30 666473-10 | US phone +1 800 891 8091
Amtsgericht Berlin-Charlottenburg, HRB 85229 | European Union VAT Id DE813474306
Directors: Dr. Markus Hannebauer, Dr. Arno Schoedl

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