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::get_deleter(3) C++ Standard Libary std::get_deleter(3)

std::get_deleter - std::get_deleter


Defined in header <memory>
template< class Deleter, class T > (since C++11)
Deleter* get_deleter( const std::shared_ptr<T>& p ) noexcept;


Access to the p's deleter. If the shared pointer p owns a deleter of type
cv-unqualified Deleter (e.g. if it was created with one of the constructors that
take a deleter as a parameter), then returns a pointer to the deleter. Otherwise,
returns a null pointer.


p - a shared pointer whose deleter needs to be accessed


A pointer to the owned deleter or nullptr. The returned pointer is valid at least as
long as there remains at least one shared_ptr instance that owns it.


The returned pointer may outlive the last shared_ptr if, for example, std::weak_ptrs
remain and the implementation doesn't destroy the deleter until the entire control
block is destroyed.


demonstrates that std::shared_ptr deleter is independent of the shared_ptr's type

// Run this code


#include <iostream>
#include <memory>


struct Foo { int i; };
void foo_deleter(Foo * p)
{
std::cout << "foo_deleter called!\n";
delete p;
}


int main()
{
std::shared_ptr<int> aptr;


{
// create a shared_ptr that owns a Foo and a deleter
auto foo_p = new Foo;
std::shared_ptr<Foo> r(foo_p, foo_deleter);
aptr = std::shared_ptr<int>(r, &r->i); // aliasing ctor
// aptr is now pointing to an int, but managing the whole Foo
} // r gets destroyed (deleter not called)


// obtain pointer to the deleter:
if(auto del_p = std::get_deleter<void(*)(Foo*)>(aptr))
{
std::cout << "shared_ptr<int> owns a deleter\n";
if(*del_p == foo_deleter)
std::cout << "...and it equals &foo_deleter\n";
} else
std::cout << "The deleter of shared_ptr<int> is null!\n";
} // deleter called here


shared_ptr<int> owns a deleter
...and it equals &foo_deleter
foo_deleter called!


constructor std::shared_ptr constructors
(public member function)
get_deleter returns the deleter that is used for destruction of the managed object
(public member function of std::unique_ptr<T,Deleter>)

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.