View Single Post
  #10 (permalink)  
Old 04-05-2012, 06:10 PM
Gene Bushuyev
Guest
 
Posts: n/a
Default Re: Implicit move of an lvalue

On Apr 4, 10:21 am, "Alf P. Steinbach" <alf.p.steinbach
+use...@gmail.com> wrote:
> However, I would suggest the in my view simpler and more direct
>
> template< class Type >
> void f( Type&& that)
> {
> A b(std::move(that));
> }
>
> template< class Type >
> void f( Type& that ); // No way (lvalue => ambiguous).
>
> which simply catches any lvalue actual argument with the second
> signature, which (in that case) produces an ambiguity error, and just
> for good measure is left unimplemented.


You can be even more direct about it and make function deleted:

template< class Type > void f( Type& that ) = delete;

Though I must say, the original issue sounds too contrived, and the
example demonstrates incorrect use, not the language problem. When the
perfect forwarding syntax is used it's always the intention to
forward, so the original example becomes:

class A
{
public:
A() {}
A(const A& that) {}
A(A&& that) {}
};

template <typename Type>
void f(Type&& that)
{
A b(std::forward<Type>(that));
}

In other situations, when forwarding is not needed, but one wants to
use an opportunity to move from rvalues, instead of copying them, the
signature simply changes to:

template <typename Type>
void f(Type that)
{
std::move(that);
}


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Reply With Quote