boost :: bind和delete

cpr*_*mer 3 c++ boost bind

我需要将a映射delete ptrAddr;到a boost::function0但我在删除时遇到了一些麻烦.免费工作很好.问题似乎是std::ptr_fun(operator delete)但我无法想象如何在不编写助手仿函数的情况下完成这项工作.

boost::function0<void> Function;
Function = boost::bind(std::ptr_fun(free), (void*)malloc_string);  //this works
Function = boost::bind(std::ptr_fun(operator delete), (void*)new_string);  //doesn't work
Function();  //call function
Run Code Online (Sandbox Code Playgroud)

Cat*_*lus 11

您可以使用delete_ptrBoost.Lambda:

boost::bind(boost::delete_ptr(), new_string);
Run Code Online (Sandbox Code Playgroud)

在C++ 11中,您可以使用std::default_delete<T>:

std::bind(std::default_delete<decltype(new_string)>(), new_string);
Run Code Online (Sandbox Code Playgroud)

或者只是一个lambda:

[new_string]() { delete new_string; }
Run Code Online (Sandbox Code Playgroud)

  • 提到`std :: default_delete <T>`的+1. (2认同)