Custom deleters for std::shared_ptrs

Posted by Kristian D'Amato on Stack Overflow See other posts from Stack Overflow or by Kristian D'Amato
Published on 2013-10-25T21:31:49Z Indexed on 2013/10/25 21:54 UTC
Read the original article Hit count: 176

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about shared-ptr