I was wondering about a c++ behaviour when an r-value is passed among functions.
Look at this simple code:
#include <string>
void foo(std::string&& str) {
// Accept a rvalue of str
}
void bar(std::string&& str) {
// foo(str); // Does not compile. Compiler says cannot bind lvalue into rvalue.
foo(std::move(str)); // It feels like a re-casting into a r-value?
}
int main(int argc, char *argv[]) {
bar(std::string("c++_rvalue"));
return 0;
}
I know when I'm inside bar
function I need to use move
function in order to invoke foo
function. My question now is why?
When I'm inside the bar
function the variable str
should already be an r-value, but the compiler acts like it is a l-value.
Can somebody quote some reference to the standard about this behaviour? Thanks!