Boost logo

Boost :

Subject: Re: [boost] Is there a way to specialize how a smart_ptr<T> releases the T?
From: David Hagood (david.hagood_at_[hidden])
Date: 2012-10-24 07:21:01


On 10/23/2012 09:16 PM, TONGARI wrote:
> Just provide it with your custom Deleter in the ctor.
>
> template<class Y, class D> shared_ptr(Y * p, D d);
OK, thanks.

However - this does require every construction of a shared pointer to be
passed that destructor - that seems error prone if it is known that all
T must be released by a given call.

If shared_ptr defined a release function:

template <typename T> class shared_ptr
{
   ...
   void Release(T *t) { delete(t);}
}
and used Release where it currently deletes the object, then in a case
like mine, all you'd have do to is specialize it:

template <> shared_ptr<xmlDoc>::Release(xmlDoc *t) { xmlFreeDoc(t);}

and then all shared_ptr<xmlDoc> would correctly release the object.

I guess what I can do to avoid the risk would be to derive from
shared_ptr and provide the destructor in my ctor.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk