0

There is a good post in [1] about move semantics. I'm fairly comfortable with them, but there are still some edge cases that I'm not sure about -- specifically, about returning rvalue references. For example, the link says that this creates a dangling reference:

unique_ptr<Shape>&& flawed_attempt()   // DO NOT DO THIS!
{
    unique_ptr<Shape> very_bad_idea(new Square);
    return std::move(very_bad_idea);   // WRONG!
}

I don't quite understand the error. The function is declared to return an rvalue ref, and it's doing that with the std::move().

How is the dangling reference created?

[1] What is move semantics?

Dess
  • 2,064
  • 19
  • 35

0 Answers0