Re: [boost] FW: Suggestion for boost`s smart pointers

-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Tobias Schwinger Sent: Friday, December 01, 2006 12:17 PM To: boost@lists.boost.org Subject: Re: [boost] FW: Suggestion for boost`s smart pointers
Sohail Somani wrote:
Thanks for your reply but the domain of COM objects lends itself well to shared_ptr.
As to scoped_ptr. I don't quite understand the "but" in that sentence.
Agreed. But something that applies to scoped_ptr + deleter doesn't always apply to shared_ptr.
I went through the archives after the posts and I have realized that scoped_ptr is not changing! I will still disagree that the deleter shouldn't be a parameterized type.
What's wrong with:
template<typename T> void free_resource(T* ptr) { delete ptr; } void free_resource(std::FILE* f) { std::fclose(f); } void free_resource(IDispatch * o) { o->Release(); }
template<typename T> struct scoped_ptr { // [...] ~scoped_ptr() { free_resource(this->p_); } // [...] };
I don't particularly like requiring implicit function declarations. I'd rather tie them to the resource pointer type somehow. That's just my own preference which may or may not be totally wrong (but its not). I think its too easy for people to forget to do it. I prefer parameterized type + typedef.
The only other option is using a type erasure mechanism like shared_ptr which is more trouble than its worth for things like deallocating C resources.
I was referring to the file pointer, which is pretty much the only "C resource" I use. You're right when it comes to malloc/free, of course - but do we really need it?
Anytime you deal with C atleast, the answer is yes. Sohail

Sohail Somani wrote:
I don't particularly like requiring implicit function declarations. I'd rather tie them to the resource pointer type somehow. That's just my own preference which may or may not be totally wrong (but its not). I think its too easy for people to forget to do it. I prefer parameterized type + typedef.
My view on the issue is, that if a designer follows RAII (or should I say RRID) there is no need for such a thing. So I don't see why third-party issues should affect our interfaces. Anyway, I don't have too strong of an opinion about it.
The only other option is using a type erasure mechanism like shared_ptr which is more trouble than its worth for things like deallocating C resources.
I was referring to the file pointer, which is pretty much the only "C resource" I use. You're right when it comes to malloc/free, of course - but do we really need it?
Anytime you deal with C atleast, the answer is yes.
I doubt it's good practice for a C library to allocate stuff and hand it to the user who has to then free it or to free stuff that comes from the user... The other cases are: a) it's up to the user to obtain the memory, so new/delete will work b) the library provides ctor/dtor routines, so custom deleters are needed Regards, Tobias
participants (2)
-
Sohail Somani
-
Tobias Schwinger