C++:右值引用转换为非常量左值引用

C++: rvalue reference converted to non-const lvalue-reference

我已经阅读了this and this,但我仍然不明白为什么下面的代码在XCode中编译:

void func2(string &s) {
    s = "yyyyy";
}

void func(string &&s) {
    func2(s);
}

int main() {
    func("xxxxx");
    return 0;
}

我认为不应将右值引用转换为非常量左值引用,对吗?一般来说,左值引用和右值引用之间的转换规则是什么?我已经知道 const 左值引用可以绑定到右值,但是右值引用(而不是右值)呢?非常感谢!

右值引用是对原始对象的引用,因此将其转换为左值引用将只是对原始对象的引用。
一旦在引用上调用移动构造函数,原始对象应重置为原始状态,对它的任何引用也是如此。

这个例子可能会阐明它:

#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;
}