std::unique_ptr<T,Deleter>::release
From cppreference.com
< cpp | memory | unique ptr
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Memory management library
(exposition only*)
(C++11)
(C++23)
(C++11)
(C++17)
(C++11)
(C++11)
(C++20)
(C++20)
(C++17)
(C++11)
(C++17)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
Uninitialized storage (until C++20)
(until C++20*)
(until C++20*)
(until C++20*)
Garbage collector support (until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)
(C++11)
(C++17)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
(until C++17*)
(C++11)
(C++17)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++11)
(C++20)
(C++11)
(C++11)
(C++20)
(C++26)
std::unique_ptr
unique_ptr::release
(C++14)(C++20)
(until C++20)(C++20)
(C++20)
pointer release() noexcept;
(since C++11) (constexpr since C++23)
Releases the ownership of the managed object, if any.
get() returns nullptr after the call.
The caller is responsible for cleaning up the object (e.g. by use of get_deleter() ).
Contents
[edit] Parameters
(none)
[edit] 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.
[edit] Example
Run this code
#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); }
Output:
Foo legacy_api ~Foo
[edit] See also
returns the deleter that is used for destruction of the managed object
(public member function) [edit]
(public member function) [edit]