Reputation: 1047
I wonder whether it is possible to specify custom deleter for std::unique_ptr with more than one argument (standard deleter signature). I know that with std::shared_ptr exists workaround with std::bind which makes it possible but is some trick for std::unique_ptr exists?
For me seems like it's not because according to http://en.cppreference.com/w/cpp/memory/unique_ptr:
Type requirements -Deleter must be FunctionObject or lvalue reference to a FunctionObject or lvalue reference to function, callable with an argument of type unique_ptr::pointer
Upvotes: 4
Views: 2761
Reputation: 6016
void my_free(int* p, int x, int y){
std:: cout << x << " " << y << "\n";
}
int main()
{
auto my_deleter = std::bind(my_free, std::placeholders::_1, 1, 2) ;
auto my_lambda = [](int* t) { my_free(t, 3, 4); };
std::unique_ptr<int, decltype(my_deleter)> ptr(new int, my_deleter);
std::unique_ptr<int, decltype(my_lambda)> ptr2(new int, my_lambda);
return 0;
}
Upvotes: 5