mercredi 7 juillet 2021

why custom deleter for std::unique_ptr needs a template parameter while for std::shared_ptr it doesn't?

Why it is needed to declare a template parameter for unique pointer while in shared pointer it isn't?

For a shared pointer we can just give a lambda

std::shared_ptr<Foo> sp  std::shared_ptr<Foo>(new Foo, [](Foo* p){delete p;});

But for unique pointer we need the declaration to have a template parameter for the deleter

std::unique_ptr<Foo, void (*)(Foo*)> up = std::unique_ptr<Foo, void (*)(Foo*)>(new Foo, [](Foo* p){delete p;});

Aucun commentaire:

Enregistrer un commentaire