According to cppreference.com, move
has signature
template< class T >
typename std::remove_reference<T>::type&& move( T&& t ) noexcept;
Why does it take a rvalue reference T&& t
as its arugment?
Also when I tried the following code
void foo(int&& bar) {
cout << "baz" << endl;
}
int main(){
int a;
foo(a);
}
I got an error from the compiler "an rvalue reference cannot be bound to an lvalue"
What is going on? I'm so confused.
T&& t
is an rvalue reference. This is premature, There's no way to say whetherT&& t
is an rvlaue reference or not until you know whatT
is. WhenT
is deduced, the language rules (type deduction and reference collapsing) are deliberately crafted to makeT&& t
switch between rvalue and lvalue reference depending on the external circumstances. – Eliaseliason