|
Boost : |
From: Sean Parent (sparent_at_[hidden])
Date: 2001-11-02 17:42:34
I'd like to extend auto_ptr<> to parameterize how the pointer is deleted -
The basic form for this would be:
--- template<class T, class DelFun> class auto_ptr { public: explicit auto_ptr(T* x, DelFun del = DelFun ()) : fDelMem(del), fVal(x) { } ~auto_ptr() { fDelMem(fVal); } ... private: DelFun fDelMem; T* fVal; }; --- Ignoring empty member opts and such. The problem with this is that it is fairly rare that you have a class to do the delete (usually you have a function) - and if you use the fun_ptr adaptor then you don't have a default constructor and you have to explicitly pass one each time you construct an auto_ptr. Not what I want. That is, instead of this: ---- auto_ptr<int, pointer_to_unary_function<void, int*> > my_ptr(p, fun_ptr(&MyDelete)); ---- I want to be able to do this: ----- auto_ptr<int, function_object<&MyDelete> > my_ptr(p); ----- The challenge is coming up with a definition of function_object<> that works statically by value. Here is what I've managed so far (slightly abbreviated to just the interesting bits): ----- template <typename T, T V> class function_object { }; template <typename Result, typename Argument, Result (*CallValue)(Argument)> struct function_object<Result (*)(Argument), CallValue> { Result operator () (Argument a) { return CallValue(a); } }; ----- Using this you can do the following: ----- auto_ptr<int, function_object<void (*)(int*), &MyDelete> > my_ptr(p); ----- Not bad, but specifying the function type seems unnecessary. I know I can derive the type in a function template but how do I do it in a class template? And yes I know I can always write the adapter for every instance (it is small) but that gets to be a pain and I would like to use this technique for more complicated operations. Sean -- Sean Parent Sr. Computer Scientist II Advanced Technology Group Adobe Systems Incorporated sparent_at_[hidden]
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk