Custom deleters for std::shared_ptrs
- by Kristian D'Amato
Is it possible to use a custom deleter after creating a std::shared_ptr without using new?
My problem is that object creation is handled by a factory class and its constructors & destructors are protected, which gives a compile error, and I don't want to use new because of its drawbacks.
To elaborate: I prefer to create shared pointers like this, which doesn't let you set a custom deleter (I think):
auto sp1 = make_shared<Song>(L"The Beatles", L"Im Happy Just to Dance With You");
Or I can create them like this, which does let met set a deleter through an argument:
auto sp2(new Song, MyDeleterFunc);
But the second one uses new, which AFAIK isn't as efficient as the top sort of allocation.
Maybe this is clearer: is it possible to get the benefits of make_shared<> as well as a custom deleter? Would that mean having to write an allocator?