My code is as follows:
#include <iostream>
using namespace std;
class A{
public:
void sendByRvalue(string&& str){
cout << str << endl;
}
};
class B{
private:
A a;
void send(string&& str){
a.sendByRvalue(str);
}
public:
void run(const string& str){
send("run " + str + "\n");
}
};
int main(void){
string str("hello world");
B b;
b.run(str);
return 0;
}
When I compile the code shown above, I got some compile errors:
It seems that the str
in B::send
function has been changed to lvalue. Then I change the implement of B::send
, like:
class B{
private:
A a;
void send(string&& str){
cout << boolalpha << is_rvalue_reference<decltype(str)>::value << endl;
a.sendByRvalue(std::move(str));
}
public:
void run(const string& str){
send("run " + str + "\n");
}
};
Everything goes well, but the output of this program made me confuse more. The output is as follows:
Why the parameter str is a rvalue reference but I cannot pass it to the function A::sendByRvalue
directly without std::move
?