3

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.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
ollo
  • 926
  • 1
  • 14
  • 33

1 Answers1

7

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.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Can you explain the danger in being able to edit the return value from a non-const reference? Is it just the fact that people may try to return actual function scoped references to it, instead of aliasing to the temporary? – ollo Aug 31 '15 at 14:01
  • MSVC issues a warning indicating that it's a non-standard extension. – Adrian McCarthy Nov 15 '17 at 22:44