I'm having trouble understanding how rvalue references work. I have the following code:
void f(int&& i)
{}
int main()
{
int i = 42;
int&& r = std::move(i);
f(r);
return 0;
}
Here, I'm getting the following compiler error:
prog.cpp: In function ‘int main()’:
prog.cpp:46:7: error: cannot bind rvalue reference of type ‘int&&’ to lvalue of type ‘int’
f(r);
^
prog.cpp:38:6: note: initializing argument 1 of ‘void f(int&&)’
void f(int&& i)
^
Why does the compiler think I'm passing an lvalue, when I explicitly declared r
to be an rvalue reference? Doing f(std::move(r))
seems to work, but I still don't understand what's different there.
Is this because, in the f(r)
call, r
is taken to be an lvalue? If so, what's the type of std::move(r)
?