I am looking for a functor that deletes its argument:
template<class T>
struct delete_functor
{
void operator()(T* p)
{
delete p;
}
};
Is there something like this in std
, tr1
or boost
?
I am looking for a functor that deletes its argument:
template<class T>
struct delete_functor
{
void operator()(T* p)
{
delete p;
}
};
Is there something like this in std
, tr1
or boost
?
C++0x will add std::default_delete
to the standard library to support std::unique_ptr
.
It has effectively the same functionality as your delete_functor
, but is also specialized to call delete[]
for array type objects.
std::default_delete<void>()
do? –
Chloral operator()
is instantiated, the program would be ill-formed. The type with which default_delete
is instantiated may be incomplete at the time the class template is instantiated, but it must be complete when the operator()
is instantiated. –
Oakie std::unique_ptr<void, void(*)(void*)> p{ 0, operator delete };
to delete void*
correctly? –
Chloral delete
may not be a void*
. If you attempt to delete
a void*
, the program is ill-formed. –
Oakie Boost.Lambda has delete_ptr and delete_array
#include <boost/lambda/construct.hpp>
–
Plummer We are not allowed to use boost in my company, and we're not using C++11 either, so I use this:
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>();
};
};
I think that's what you're looking for.
You can also recreate it as dtor_fun_t and replace "delete _ptr;" by "_ptr->~_Type();" to only call the dtor. This would be the case where you used a memory manager and placement new for example.
© 2022 - 2024 — McMap. All rights reserved.
struct delete_functor { template <typename T> void operator()(T* p) { delete p; } };
, you don't have to specify T when you create one. – Microseism