Using Smart Pointers with Custom Deleters

Can I use smart pointers with my own custom deleters?

Yes, std::unique_ptr allows you to specify a custom deleter. This can be useful if you need to do something more than just call delete when the pointer is destroyed.

Here's an example where we use a custom deleter to print a message when the object is destroyed:

#include <memory>
#include <iostream>

class Character {
public:
  std::string Name;
  Character(std::string Name) : Name{Name} {
    std::cout << "Creating " << Name << '\n';
  }
  ~Character() {
    std::cout << "Destroying " << Name << '\n';
  }
};

struct CharacterDeleter {
  void operator()(Character* c) const {
    std::cout << "Custom deleting "
      << c->Name << '\n';
    delete c;
  }
};

int main() {
  std::unique_ptr<Character, CharacterDeleter>
    Gandalf{new Character{"Gandalf"}};
}
Creating Gandalf
Custom deleting Gandalf
Destroying Gandalf

Here, we define a CharacterDeleter struct that overloads the () operator. This operator will be called when the unique_ptr needs to delete the Character.

We then specify CharacterDeleter as the second template argument to std::unique_ptr. This tells the unique_ptr to use CharacterDeleter instead of the default deleter.

You can also use a lambda as a custom deleter:

#include <memory>
#include <iostream>

class Character {
public:
  std::string Name;
  Character(std::string Name) : Name{Name} {
    std::cout << "Creating " << Name << '\n';
  }
  ~Character() {
    std::cout << "Destroying " << Name << '\n';
  }
};

int main() {
  std::unique_ptr<Character,
    void(*)(Character*)> Gandalf{
    new Character{"Gandalf"},
    [](Character* c) {
      std::cout << "Custom deleting "
        << c->Name << '\n';
      delete c;
    }
  };
}
Creating Gandalf
Custom deleting Gandalf
Destroying Gandalf

Here, we specify the type of the deleter as void(*)(Character*), which is a function pointer that takes a Character* and returns void. We then pass a lambda with this signature as the second argument to the unique_ptr constructor.

Custom deleters can be useful for managing resources that aren't directly heap-allocated with new, such as file handles, network sockets, or graphics resources.

Smart Pointers and std::unique_ptr

An introduction to memory ownership using smart pointers and std::unique_ptr in C++

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Mixing Smart and Raw Pointers
Is it okay to mix smart pointers and raw pointers in the same program?
Should I Pass Smart Pointers by Reference?
Should I pass smart pointers by value or reference?
Dynamically Allocating Arrays with Smart Pointers
How do I dynamically allocate an array with smart pointers?
std::make_unique vs new Keyword
What are the advantages of using std::make_unique over the new keyword?
Using std::move with std::unique_ptr
What does std::move do when used with std::unique_ptr?
Using std::unique_ptr as a Class Member
How do I use std::unique_ptr as a member of a class?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant