Is there a standard removal method?

I am looking for a functor that removes its argument:

template<class T> struct delete_functor { void operator()(T* p) { delete p; } }; 

Is there something similar in std , tr1 or boost ?

+6
c ++ functor delete-operator
source share
3 answers

C ++ 0x will add std::default_delete to the standard library to support std::unique_ptr .

It has the same functions as your delete_functor , but is also specialized in calling delete[] for objects of type array.

+10
source share
+2
source share

We are not allowed to use boost in my company, and we do not use C ++ 11, 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 what you are looking for.

You can also recreate it as dtor_fun_t and replace "delete _ptr;" by "_ptr-> ~ _Type ();" to call only dtoro. This will be the case when, for example, you used the memory manager and placement.

0
source share

All Articles