After replying on question about returning temporaries I've noticed there was a second reply which is slightly different.
Instead of returning by value, it returned by rvalue reference. Can you explain what the difference is between these approaches and where the risks are for them?
struct Bar
{
Bar& doThings() & {return *this;}
// Returning rvalue reference
Bar&& doThings() && {return std::move(*this);}
// Alternative: Returning by value
//Bar doThings() && {return std::move(*this);}
std::unique_ptr<int> m_content; // A non-copyable type
};
One major difference is that, if rvalue reference is returned, the lifetime of the temporary will not be extended when the return value is bound to a reference.
For example,