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

Replaces the managed object.

# Declarations

members of the primary template, unique_ptr<T>
void reset( pointer ptr = pointer() ) noexcept;

(constexpr since C++23)

members of the specialization unique_ptr<T[]>
template< class U >
void reset( U ptr ) noexcept;

(constexpr since C++23)

void reset( std::nullptr_t = nullptr ) noexcept;

(constexpr since C++23)

# Parameters

# Notes

To replace the managed object while supplying a new deleter as well, move assignment operator may be used.

A test for self-reset, i.e. whether ptr points 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 like p.reset(p.get()) does.

# Example

#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);      
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 2118C++11unique_ptr<T[]>::reset rejected qualification conversionsaccepts
LWG 2169C++11the overload unique_ptr<T[]>::reset(pointer) existedremoved the overload

# See also