In C++23 there is std::out_ptr that does exactly this and works with both unique_ptr and shared_ptr. The documentation has some great examples.
std::out_ptr
unique_ptr
shared_ptr