Boost logo

Boost :

From: Anthony Williams (anthony_w.geo_at_[hidden])
Date: 2008-04-07 12:58:53


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

Anthony

-- 
Anthony Williams            | Just Software Solutions Ltd
Custom Software Development | http://www.justsoftwaresolutions.co.uk
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

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