Can I use rvalue reference to temporary? Is it undefined behavior or not?

后端 未结 3 1580
感情败类
感情败类 2021-02-13 20:55

Updating the question Why this two rvalue references examples have different behavior?:

Source code:

int a = 0;
auto && b = a++;
++a;
cout <&l         


        
3条回答
  •  -上瘾入骨i
    2021-02-13 21:41

    The code is fine. b refers to a lifetime-extended object that is the result of the expression a++, which is a different object from a. (Binding a temporary object to a reference extends the lifetime of the object to that of the reference.) You can use and modify both objects.

提交回复
热议问题