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

后端 未结 3 1589
感情败类
感情败类 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条回答
  •  清酒与你
    2021-02-13 21:33

    Taking a reference to a temporary extends its lifetime to the end of lifetime of that reference.

    ISO/IEC 14882 § 12.2/5:

    The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference […]

提交回复
热议问题