Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 5
    Please edit your question with an minimal reproducible example or SSCCE (Short, Self Contained, Correct Example) Commented Sep 14, 2017 at 12:34
  • Also, this is probably relevant if you are returning a reference. Commented Sep 14, 2017 at 12:37
  • You would have similar issue if it returns /*const*/T& BTW. Commented Sep 14, 2017 at 12:39
  • Returning a T would do one extra move. Commented Sep 14, 2017 at 12:41
  • In your old code, you assign a temporary T to a T&. I would never do that. Even if it somehow works, it's confusing and doesn't make sense and is just begging for problems. Wouldn't that seem like a dangling reference to you? Even if there's some obscure aspect of the standard which allows it to work, it doesn't make sense. If you want to keep the value around, assign it to a value, not to a reference. I would say the design flaw, if any, in C++ is that your old code works at all. Commented Dec 25, 2019 at 15:11