Why can you indirectly bind an rvalue to an lvalue reference but not directly?

后端 未结 3 1738
天涯浪人
天涯浪人 2021-02-06 00:06

From what I\'ve read and seen you cannot bind an expression that is an rvalue to an lvalue reference. What I have seen however is that you can bind an rvalue to an rvalue refere

3条回答
  •  孤街浪徒
    2021-02-06 00:18

    When you call foo(1), a temporary int equal to 1 is created, and int&& a is bound to it. You can freely change it. The same way you can write int&& a = 1;. a itself is an lvalue, so you can bind lvalue reference to it.

    You may read a great article Universal References by Scott Meyers which explains rvalue and lvalue references in detail.

提交回复
热议问题