mercredi 1 novembre 2017

Is it ok to subclass std::unique_ptr to transparently apply a custom deleter?

I have a certain type called A and I want to create multiple instances of std::unique_ptr<A, void(*)(A *ptr)> without explicitly passing the custom deleter every time. This is what I came up with:

void ADeleter(A *ptr) {
  // custom deletion logic
}

class APtr : public std::unique_ptr<A, void(*)(A *ptr)> {
  APtr() : APtr(nullptr) {}
  APtr(A *ptr) : std::unique_ptr<A, void(*)(A *ptr)>(ptr, ADeleter) {}
};

Is it safe (or even a good practice) to inherit from std::unique_ptr (or other smart pointer classes) in order to abstract construction with a custom deleter?

Aucun commentaire:

Enregistrer un commentaire