The right way would presumably be to return by value and only do so if the instance is movable-from:
class Foo
{
std::unique_ptr<Bar> bar_;
public:
std::unique_ptr<Bar> get_bar() &&
{
return std::move(bar_);
}
};
That way you can only move from the member if the entire instance is itself expiring. There's no need to return an rvalue reference in general, since that gains you very little, and comes at a big cost of not being generally safe. For example, suppose you have Foo f();
, then consider:
auto && p = f().get_bar();
This is safe if you return by value, but would be a dangling reference if you returned by reference.