What happens when you assign a literal constant to an rvalue reference?

后端 未结 2 1016
一个人的身影
一个人的身影 2020-12-10 04:36

This is admittedly a nit-picky question that is mainly driven by curiosity. Suppose we have the following:

int x = 5;
int&& xref = std::move(x);
std         


        
2条回答
  •  伪装坚强ぢ
    2020-12-10 05:18

    int&& xref = 5;
    

    ... creates a temporary, initialized with 5, whose lifetime is extended to the end of the block.

    The assignment

    xref = 10;
    

    changes the value of the still living temporary.

提交回复
热议问题