Suppose we have:
foo(A&& a);
if you do
A a;
foo(a);
it won’t compile and complain cannot bind a lvalue to A&&. that’s perfectly fine.
However, given the signature of std::move,
template<class T> typename remove_reference<T>::type&& std::move(T&& a);
Looks like it takes a rvalue reference, just as in foo, why the following code complies?
A a;
std::move(a);
isn’t a is a lvalue?
furthur, it is said the compile will instantiate:
typename remove_reference<A&>::type&& std::move(A& && a);
I don’t understand why it is not:
typename remove_reference<A>::type&& std::move(A && a);
it looks to me a is of type A, not A&.
Despite what others have said, the standard only talks about rvalue references.
The key to how this works for std::move is an explicit special rule in the rules for template argument deduction:
The other part are the rules for reference collapsing, which say that
Now in
template<class T> typename remove_reference<T>::type&& std::move(T&& a);the function parameter a matches above rule (“rvalue reference to cv-unqualified template parameter”), so the deduced type will be an lvalue reference to the argument type, if the argument is an lvalue. In your case that leads to T = A&.Substituting that into the declaration of move yields
Using the definition of remove_reference and the reference collapsing rule (rvalue reference to TR => TR), makes this:
Scott Meyer’s universal reference concept, as put forward in other answers, is a helpful way to remember this surprising effect of the combination of the rules for type deduction and of reference collapsing: rvalue references to a deduced type may end up being lvalue references (if the type may be deduced to be a lvalue reference). But there are no universal references int the standard. As Scott Meyers says: it is a lie – but a lie that is more helpful than the truth…
Note that std::forward is a different twist on this theme: it uses an extra indirection to prevent argument deduction (so that the type must be given explicitly), but also uses reference collapsing to forward lvalues as lvalues and rvalues as rvalues.