std::unique_ptr::release(3)
π NAME π
okay bestie, welcome to std::unique_ptr::release(3) from the C++ Standard Libary manual π
(yes, thatβs the manual name, no cap). The name is literally:
std::unique_ptr::release β std::unique_ptr::release
like, thatβs it. itβs giving βthe function is the name, the name is the functionβ energy πβ¨
π¦ Synopsis π¦
pointer release() noexcept; (since C++11)
(constexpr since C++23)
okay so what does it do? It releases the ownership of the managed object, if any. π«Ά
After the call, get() returns nullptr. periodt.
β οΈ bestie, the caller is responsible for cleaning up the object (e.g. by use ofget_deleter()). thatβs on you, not theunique_ptr. π
πΈ Parameters πΈ
(none) β literally zero parameters, not a single one, we love a no-arg icon π
β¨ Return value β¨
A 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. iykyk π
π Example π
hereβs the example, bestie β run this code:
// 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
π See also π
getβ returns a pointer to the managed object (public member function)get_deleterβ returns the deleter that is used for destruction of the managed object (public member function)resetβ replaces the managed object (public member function)
and thatβs the whole vibe check, bestie π source/version: http://cppreference.com Β· date: 2024.06.10 Β· title: std::unique_ptr::release(3) π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms