std::get_deleter(3)
πΈ NAME πΈ
Okay so this is std::get_deleter(3) from the C++ Standard Libary manual β yes, that spelling is literally what the page says, bestie β and the NAME line is std::get_deleter - std::get_deleter π
β¨
β¨ Synopsis β¨
Defined in header <memory>:
template< class Deleter, class T >
Deleter* get_deleter( const std::shared_ptr<T>& p ) noexcept;
This one is since C++11, no cap. It gives you access to 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 it returns a pointer to the deleter. Otherwise, it returns a null pointer. π¦
π Parameters π
pβ a shared pointer whose deleter needs to be accessed π«Ά
π Return value π
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. π
π¦ Notes π¦
The returned pointer may outlive the lastshared_ptrif, for example,std::weak_ptrsremain and the implementation doesn't destroy the deleter until the entire control block is destroyed. Keep that in mind, bestie π
π Example π
Demonstrates that std::shared_ptr deleter is independent of the shared_ptr's type. The page says // Run this code, so here it is:
#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
Output:
shared_ptr<int> owns a deleter
...and it equals &foo_deleter
foo_deleter called!
π See also π
- constructor β
std::shared_ptrconstructors (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>) π₯
Source/version and date, bestie: http://cppreference.com, 2024.06.10, page std::get_deleter(3). Bye, go slay your shared pointers πβ¨
π¦ 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