Re: Implicit move of an lvalue
On 3 Apr., 20:36, Kaba wrote:
Have a look at the following code:
class A
{
public:
A() {}
A(const A& that) {}
A(A&& that) {}
};
template <typename Type>
void f(Type&& that)
{
A b(std::move(that));
}
int main()
{
A a;
f(a); // Compiles.
return 0;
}
[big snip]
I am feeling unease about the template moving from an lvalue as
something that might cause potential traps. What do you think of it?
Well, this template argument deduction rule does feel like a language
design hack to allow perfect forwarding. I don't doubt that some of
those who are unaware of this rule expect 'that' only to bind to
rvalues and may move from it accidentally. So, it's important to
teach that a parameter of type T&& where T is a template parameter
deduced by the compiler will bind to _anything_ and that in this case
the original valueness can be restored with std::forward<T>(that).
So, what I'm saying is that it's a quirk of the language one should be
aware of that otherwise may make you trip and fall. I remember
Johannes Schaub proposing (some time ago) to let the user explicitly
enable this deduction rule with the following syntax:
template<class && T> void foo(T && ref);
^^
I really like his idea. Unfortunately, it's a bit late in the game to
change any of that in the standard (I think).
Cheers!
SG
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]