Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2008-04-08 12:12:41


Howard, Andrei,

There is some confusion on the Boost list about the behavior of rvalue
references, caused in part by Andrei's ACCU keynote. Can one of you
clear up the issue of the proper way to define the "identity" function
in C++0x? Thanks.

Anthony Williams wrote:
> "Peter Dimov" <pdimov_at_[hidden]> writes:
>> Eric Niebler:
>>
>>> Can you write what the identity function object would look like in C++0x?
>>>
>> struct identity
>> {
>> template<class A> A operator()( A && a ) const
>> {
>> return std::forward<A>( a );
>> }
>> };
>>
>> I guess.
>
> No. That will convert lvalues to rvalues. You also need the lvalue overload in
> order to preserve the reference-ness of lvalues. IIRC, in Andrei
> Alexandrescu's talk at ACCU 2008 he also claimed you need a const-reference
> overload:
>
> struct identity
> {
> template<class A> A operator()( A && a ) const
> {
> return std::move( a );
> }
> template<class A> A& operator()( A & a ) const
> {
> return a;
> }
> template<class A> A const& operator()( A const & a ) const
> {
> return a;
> }
> };
>
> I'm not sure we need the third overload, as the second will deduce A to be
> "const A". Unfortunately, the only compiler I have which handles rvalue
> references says that the first two are ambiguous when given an lvalue.

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

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