Angus Leeming wrote:

> Lars Gullik Bjønnes wrote:
> 
>> This is the answer I got from the boost list.
> 
> Thanks for chasing this up, Lars. Perhaps they could consider a
> scoped_ptr_custom_delete class. Ok, the name sucks ;-)

I went on to browse the boost user list on this subject. It received an 
extensive discussion there, amongst which I came across this little nugget:

Peter Dimov wrote:
> I suspect that most people just use editor inheritance or specialize
> ~scoped_ptr.

I don't know what "editor inheritance" is. Nonetheless, inheritance here is 
interesting. I note that scoped_ptr doesn't have a virtual d-tor. Would a 
derived class not simply hide it? That is exactly what we want here.

The code below compiles and seems to do the trick. 

The code could perhaps be generalised to accept a pointer to a function 
rather than a class that does the destruction. I'm a bit hazy about that.

What do you think, Lars?
Angus

ps, not that use of "free" here is an example. My interest was sparked by 
fl_free_freebrowser...


#include <cstdlib>
#include <boost/scoped_ptr.hpp>

namespace lyx {

template <typename T, typename D>
class custom_scoped_ptr : public boost::scoped_ptr<T> {
public:
        explicit custom_scoped_ptr(T * p = 0) : boost::scoped_ptr<T>(p) {}
        /// This destructor HIDES that of scoped_ptr.
        ~custom_scoped_ptr() { D()(get()); }
};

} // namespace lyx

struct Free {
        void operator()(void * ptr) { free(ptr); }
};

int main ()
{
        lyx::custom_scoped_ptr<int, Free> ptr((int *)malloc(sizeof(int)));
        return 0;
}


Reply via email to