std::unique_ptr::reset(3)
hey bestie π welcome to std::unique_ptr::reset(3), straight outta the C++ Standard Libary. this is the reset glow-up page β it replaces the managed object, and yes it's giving main character energy. no cap.
π NAME π
std::unique_ptr::reset β std::unique_ptr::reset
β¨ Synopsis β¨
okay so here's the overload lineup, bestie β three of them, all noexcept, all constexpr since C++23:
members of the primary template, unique_ptr<T>
void reset( pointer ptr = pointer() ) noexcept; (1) (constexpr since C++23)
members of the specialization unique_ptr<T[]>
template< class U > (2) (constexpr since C++23)
void reset( U ptr ) noexcept;
void reset( std::nullptr_t = nullptr ) noexcept; (3) (constexpr since C++23)
Replaces the managed object. periodt.
- Given
current_ptr, the pointer that was managed by*this, performs the following actions, in this order:- Saves a copy of the current pointer:
old_ptr = current_ptr. - Overwrites the current pointer with the argument:
current_ptr = ptr. - If the old pointer was non-empty, deletes the previously managed object if
(old_ptr) get_deleter()(old_ptr).
- Saves a copy of the current pointer:
- Behaves the same as the
resetmember of the primary template, except that it will only participate in overload resolution if either:Uis the same type aspointer, orpointeris the same type aselement_type*andUis a pointer typeV*such thatV(*)[]is convertible toelement_type(*)[].
- Equivalent to
reset(pointer()).
π«Ά Parameters π«Ά
ptrβ pointer to a new object to manage
π Return value π
(none) β nothing comes back, bestie. it's giving silence.
π§ Notes π§
- To replace the managed object while supplying a new deleter as well, the move assignment operator may be used.
- A test for self-reset β i.e. whether
ptrpoints to an object already managed by*thisβ is not performed, except where provided as a compiler extension or as a debugging assert. - Note that code such as
p.reset(p.release())does not involve self-reset, only code likep.reset(p.get())does. π
πΈ Example πΈ
run this code, bestie:
// Run this code
#include <iostream>
#include <memory>
struct Foo // object to manage
{
Foo() { std::cout << "Foo...\n"; }
~Foo() { std::cout << "~Foo...\n"; }
};
struct D // deleter
{
void operator() (Foo* p)
{
std::cout << "Calling delete for Foo object... \n";
delete p;
}
};
int main()
{
std::cout << "Creating new Foo...\n";
std::unique_ptr<Foo, D> up(new Foo(), D()); // up owns the Foo pointer (deleter D)
std::cout << "Replace owned Foo with a new Foo...\n";
up.reset(new Foo()); // calls deleter for the old one
std::cout << "Release and delete the owned Foo...\n";
up.reset(nullptr);
}
π Output π
Creating new Foo...
Foo...
Replace owned Foo with a new Foo...
Foo...
Calling delete for Foo object...
~Foo...
Release and delete the owned Foo...
Calling delete for Foo object...
~Foo...
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. slay for retroactive fixes β¨
- DR
LWG 2118β Applied to:C++11β Behavior as published:unique_ptr<T[]>::resetrejected qualification conversions β Correct behavior: accepts.
π See also π
releaseβ returns a pointer to the managed object and releases the ownership (public member function)
source: http://cppreference.com β version/date: 2024.06.10 β page: std::unique_ptr::reset(3) β mwah π
π¦ 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