Search code examples
c++referencemove-semanticsrvaluetemporary-objects

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.


Solution

  • 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.