简体   繁体   中英

std::unique_ptr using custom deleter with few arguments

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM