gpt4 book ai didi

c++ - boost::bind 和删除

转载 作者:太空狗 更新时间:2023-10-29 19:40:53 24 4
gpt4 key购买 nike

我需要将 delete ptrAddr; 映射到 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

最佳答案

您可以使用 delete_ptr 来自 Boost.Lambda:

boost::bind(boost::delete_ptr(), new_string);

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

std::bind(std::default_delete<decltype(new_string)>(), new_string);

或者只是一个 lambda:

[new_string]() { delete new_string; }

关于c++ - boost::bind 和删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8079983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com