Boost logo

Boost Users :

From: Craig Henderson (yg-boost-users_at_[hidden])
Date: 2002-09-17 16:48:04


I need to free memory from a container with elements of pointers, where the
container value_type is a pointer type. I cannot use the checked_delete
template as it is, as the template parameter is defined without the '*'
pointer.

For example, the following fails (obviously):

typedef std::vector<char *> X;

X x;
...
std::for_each(x.begin(),
              x.end(),
              boost::checked_delete<X::value_type>);

I'm using VC7, so have no partial specialization available, so using the
type traits library remove_pointer<> does not work:

typedef boost::remove_pointer<typename X::value_type>::type type;
std::for_each(x.begin(),
              x.end(),
              boost::checked_delete<type>);

My existing solution uses a unary functor, as below, but this met with
critism on the Boost Developers' list. What would be the 'boost' solution to
this problem?

template<typename T>
class free_heap_ptr : public std::unary_function<T, void>
{
    public:
    free_heap_ptr() { }
    ~free_heap_ptr() { }
    void operator() (T p)
    {
        delete p;
    }
};

...

std::for_each(changes_.begin(),
              changes_.end(),
              free_heap_ptr<typename change_list::value_type>());

TIA
-- Craig


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net