Reputation: 481
Is there anyway I can send arguments to the deleter of std::shared_ptr
?
something that would feel like:
std::shared_ptr<A> myA( a, myDeleter(a, 5) );
where myDeleter
has this signature:
void myDeleter(A* a, int i)
(Obviously the syntax above is wrong, but just to emphasize that I need my deleter to take extra arguments.)
Upvotes: 14
Views: 4954
Reputation: 110658
You could std::bind
your deleter's second argument before passing it as the deleter:
auto deleter = std::bind(myDeleter, std::placeholders::_1, 5);
std::shared_ptr<A> myA(a, deleter);
Alternatively, your deleter could be a functor that takes the int
through its constructor:
struct myDeleter
{
myDeleter(int);
void operator()(A*);
};
myDeleter deleter(5);
std::shared_ptr<A> myA(a, deleter);
Alternatively you could use a lambda expression:
std::shared_ptr<A> myA(a, [](A* a){ myDeleter(a, 5); });
Upvotes: 26