From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Sohail Somani Sent: Sunday, December 10, 2006 9:17 PM To: boost-users@lists.boost.org; boost-users@lists.boost.org Subject: Re: [Boost-users] shared_ptr with custom free function From: boost-users-bounces@lists.boost.org on behalf of Nat Goodspeed
[Nat] It might be worth binding that knowledge into a specific shared-pointer-to-blob type so that the required deleter function is implicitly captured EVERY time you construct such a pointer.
-------------
How do you encode that knowledge into the type?
[Nat] I admit I haven't tried coding this. My thought is to derive your "blob pointer" type from shared_ptr<blob>, implementing a subset of the constructor variants that will be useful to you, and ensuring that the constructor that accepts a dumb blob* pointer implicitly passes free_blob to the base-class constructor. Something like this: class blob; void free_blob(blob*); class blob_ptr: public shared_ptr<blob> { typedef shared_ptr<blob> super; public: // should only need to implement (a subset of) constructors // ... // This constructor is the reason blob_ptr is a class rather // than a typedef. Since we MUST use free_blob() to free a // heap blob instance, always bind free_blob() as shared_ptr's // custom deleter. blob_ptr(blob* ptr): super(ptr, free_blob) {} // ... };