std::unique_ptr<T,Deleter>::release

Releases the ownership of the managed object, if any.

# Declarations

pointer release() noexcept;

(since C++11) (constexpr since C++23)

# Return value

Pointer to the managed object or nullptr if there was no managed object, i.e. the value which would be returned by get() before the call.

# Example

#include <cassert>
#include <iostream>
#include <memory>
 
struct Foo
{
    Foo() { std::cout << "Foo\n"; }
    ~Foo() { std::cout << "~Foo\n"; }
};
 
// Ownership of the Foo resource is transferred when calling this function
void legacy_api(Foo* owning_foo)
{
    std::cout << __func__ << '\n';
    // [legacy code that no one understands or dares touch anymore]
    // [...]
    delete owning_foo;
}
 
int main()
{
    std::unique_ptr<Foo> managed_foo(new Foo);
    // [code that might return or throw or some such]
    // [...]
    legacy_api(managed_foo.release());
 
    assert(managed_foo == nullptr);
}

# See also