| 18 | } |
| 19 | |
| 20 | int main() |
| 21 | { |
| 22 | std::string lv1 = "string,"; // lv1 是一个左值 |
| 23 | // std::string&& r1 = s1; // 非法, 右值引用不能引用左值 |
| 24 | std::string&& rv1 = std::move(lv1); // 合法, std::move可以将左值转移为右值 |
| 25 | std::cout << rv1 << std::endl; // string, |
| 26 | |
| 27 | const std::string& lv2 = lv1 + lv1; // 合法, 常量左值引用能够延长临时变量的申明周期 |
| 28 | // lv2 += "Test"; // 非法, 引用的右值无法被修改 |
| 29 | std::cout << lv2 << std::endl; // string,string |
| 30 | |
| 31 | std::string&& rv2 = lv1 + lv2; // 合法, 右值引用延长临时对象声明周期 |
| 32 | rv2 += "string"; // 合法, 非常量引用能够修改临时变量 |
| 33 | std::cout << rv2 << std::endl; // string,string,string, |
| 34 | |
| 35 | reference(rv2); // 输出左值 |
| 36 | |
| 37 | return 0; |
| 38 | } |
nothing calls this directly
no test coverage detected