参考
    https://www.internalpointers.com/post/c-rvalue-references-and-move-semantics-beginners

    精简例子

    1. std::string s1 = "Hello ";
    2. std::string s2 = "world";
    3. std::string&& s_rref = s1 + s2; // the result of s1 + s2 is an rvalue
    4. s_rref += ", my friend"; // I can change the temporary string!
    5. std::cout << s_rref << '\n'; // prints "Hello world, my friend"

    修改上面的例子,发现左值不能赋值给右值引用
    Screen Shot 2019-11-08 at 11.01.18 PM.png

    所谓“右值”,其实就是临时值(或字面值),它们不能取地址,没有对应变量名。
    “右值引用”是专门指向这种值的引用,相当于给这种值添加了名称,然后我们可以对其修改。

    universal reference & forward

    1. class Monster {
    2. };
    3. Monster createMonster() {
    4. return Monster();
    5. }
    6. void f1(int num) {
    7. std::cout << num << std::endl;
    8. std::this_thread::sleep_for(std::chrono::seconds(2));
    9. }
    10. //void f2(Monster&& m) {
    11. // std::cout << "monster rvalue reference" << std::endl;
    12. //}
    13. //
    14. //void f2(Monster& m) {
    15. // std::cout << "monster lvalue reference" << std::endl;
    16. //}
    17. void foo(Monster&& m) {
    18. std::cout << "foo rvalue refer" << std::endl;
    19. }
    20. void foo(Monster& m) {
    21. std::cout << "foo lvalue refer " << std::endl;
    22. }
    23. template <typename T>
    24. void f2(T&& m){
    25. std::cout << "universal reference" << std::endl;
    26. // forward as lvalue or as rvalue, depending on T
    27. //foo(std::forward<T>(m));
    28. foo(m);
    29. }
    30. auto inc(int num) -> int {
    31. return num++;
    32. }
    33. int main(int argc, char* argv[]) {
    34. //Monster m = createMonster();
    35. f2(createMonster());
    36. }