function template
<memory>
std::get_deleter
template <class D, class T>
D* get_deleter (const shared_ptr<T>& sp) noexcept;
Get deleter from shared_ptr
Returns a pointer to the deleter owned by sp.
If sp has no deleter, and thus would use ::delete to delete its managed object, the function returns a null pointer.
The returned value is valid at least as long as there exists a shared_ptr instance that owns that deleter.
Notice that the first template parameter is the return type, and thus cannot be automatically deduced by the compiler.
Return Value
A pointer to the owned deleter, if any. Or a null pointer otherwise.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
// get_deleter example
#include <iostream>
#include <memory>
struct D { // a verbose array deleter:
void operator()(int* p) {
std::cout << "[deleter called]\n";
delete[] p;
}
};
int main () {
std::shared_ptr<int> foo (new int[10],D());
int * bar = new int[20];
// use foo's deleter to delete bar (which is unmanaged):
(*std::get_deleter<D>(foo))(bar);
return 0;
// foo's deleter called automatically
}
|
Output:
[deleter called]
[deleter called]
|