GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::unique_ptr::reset(3) C++ Standard Libary std::unique_ptr::reset(3)

std::unique_ptr::reset - std::unique_ptr::reset


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.


1) Given current_ptr, the pointer that was managed by *this, performs the following
actions, in this order:


1. Saves a copy of the current pointer old_ptr = current_ptr
2. Overwrites the current pointer with the argument current_ptr = ptr
3. If the old pointer was non-empty, deletes the previously managed object
if(old_ptr) get_deleter()(old_ptr).


2) Behaves the same as the reset member of the primary template, except that it will
only participate in overload resolution if either


1. U is the same type as pointer, or
2. pointer is the same type as element_type* and U is a pointer type V* such that
V(*)[] is convertible to element_type(*)[].


3) Equivalent to reset(pointer())


ptr - pointer to a new object to manage


(none)


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.

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


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.


DR Applied to Behavior as published Correct behavior
LWG 2118 C++11 unique_ptr<T[]>::reset rejected qualification accepts
conversions


release returns a pointer to the managed object and releases the ownership
(public member function)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.