On 03/02/06, Loïc Joly
Guy Peleg a écrit :
Hi,
How come scoped_ptr can't work with a special 'deleter' as shared_ptr?
I was just wondering the same thing. This would be quite convenient for me, where I just want to use RAII without redefining the ressource holding class. I agree that I could just use shared_ptr anyway for this purpose, but scoped_ptr would be documenting my intent more precisely.
It's my understanding that scoped_ptr does not allow the custom deleter since shared_ptr does it by keeping a boost::function<> ( or perhaps some other similar utility ) to the deleter, which, if used for scoped_ptr, would make a scoped_ptr larger than a plain pointer ( by about 2 pointers ) and would make the destructor slower ( by about 1 call through a function pointer ), which would be unacceptable for many users of scoped_ptr. In any case, the speed difference between a shared_ptr and some scope pointer that did keep a boost::function<> to a deleter would only be in the allocation of the reference count which, especially with the quick allocator, is usually not that severe. One possibility, however, would be for a deleter_scoped_ptr ( that's not yet written ) to have a second template argument of a functor that would be default-constructed and then called on the pointer as the deleter. That would eliminate the speed and size overhead, but it means much less flexibility in what the deleter can be ( since it must be set at declaration, which means no binders or function pointers, among other things ). ~ Scott McMurray