I have read this and this, but I still do not understand why the following code compiles in XCode:
void func2(string &s) {
s = "yyyyy";
}
void func(string &&s) {
func2(s);
}
int main() {
func("xxxxx");
return 0;
}
I think an rvalue reference shouldn't be converted to a non-const lvalue reference, right? In general, what's the rule of conversion between lvalue references and rvalue references? I already know that const lvalue reference can bind to rvalues, but what about rvalue references (rather than rvalues)? Thanks a lot!
The r-value reference is a reference to the original object, so converting it to a l-value reference will just make a reference to the original object.
Once a move constructor is called upon the reference, the original object should be reset to the origin state, and so does any reference to it.
This example might clarify it:
#include <iostream>
using namespace std;
int main()
{
string s = "my string";
string &&rval = move(s);
cout << '"' << rval << '"' << endl; // "my string"
cout << '"' << rval << '"' << endl; // "my string"
cout << '"' << s << '"' << endl; // "my string"
string &lval = rval;
cout << '"' << lval << '"' << endl; // "my string"
string s2(move(rval));
cout << '"' << rval << '"' << endl; // ""
cout << '"' << lval << '"' << endl; // ""
cout << '"' << s << '"' << endl; // ""
cout << '"' << s2 << '"' << endl; // "my string"
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With