我正在寻找一个删除其参数的函子:
template<class T>
struct delete_functor
{
void operator()(T* p)
{
delete p;
}
};
有这样的std
东西tr1
吗boost
?
我正在寻找一个删除其参数的函子:
template<class T>
struct delete_functor
{
void operator()(T* p)
{
delete p;
}
};
有这样的std
东西tr1
吗boost
?
C++0x 将添加std::default_delete
到标准库以支持std::unique_ptr
.
它实际上与您的 具有相同的功能delete_functor
,但也专门用于调用delete[]
数组类型的对象。
Boost.Lambda 有delete_ptr 和 delete_array
我们公司不允许使用 boost,我们也没有使用 C++11,所以我使用这个:
namespace
{
// - for use to deletion:
// std::vector<int*> foobar;
// std::for_each(foobar.begin(), fooabr.end(), del_fun<T>());
template<class _Type>
struct del_fun_t:
public unary_function<_Type*, void>
{
void operator()(_Type* __ptr) {
delete __ptr;
}
};
template<class _Type>
del_fun_t<_Type> del_fun()
{
return del_fun_t<_Type>();
};
};
我想这就是你要找的。
您也可以将其重新创建为 dtor_fun_t 并替换“delete_ptr ;” 通过“ _ptr->~_Type();” 只打电话给dtor。例如,您使用内存管理器和放置新的情况就是这种情况。