Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-const reference to temporary works now?

With the introduction of move semantics, did the rule that you can only bind temporaries to const reference change? non-const seems to extend lifetime just as well.

 A getA() { return A();}  

 A & aref = getA(); //OK
 string & str = string("h") + string("i"); //OK again

This is with msvc, destructor for A does not get called before main exits.

like image 743
ollo Avatar asked Dec 05 '25 02:12

ollo


1 Answers

No, the rules are the same, you are not allowed to bind a rvalue to a non-const lvalue reference. MSVC is using a (dangerous) extension.

like image 195
vsoftco Avatar answered Dec 06 '25 15:12

vsoftco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!