C++ Memory Library get_deleter



Description

It returns a pointer to the deleter owned by sp.

Declaration

Following is the declaration for std::get_deleter.

template <class D, class T>
  D* get_deleter (const shared_ptr<T>& sp) noexcept;

C++11

template <class D, class T>
  D* get_deleter (const shared_ptr<T>& sp) noexcept;

Parameters

sp − Its a shared pointer.

Return Value

It returns a pointer to the deleter owned by sp.

Exceptions

noexcep − It doesn't throw any exceptions.

Example

In below example explains about std::get_deleter.

#include <iostream>
#include <memory>

struct D {
   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];
   (*std::get_deleter<D>(foo))(bar);
   return 0;
}

Let us compile and run the above program, this will produce the following result −

[deleter called]
[deleter called]
memory.htm
Advertisements