|
Boost : |
From: Brey, Edward D (EdwardDBrey_at_[hidden])
Date: 2002-01-25 14:46:05
I don't think this will work. There is no implicit conversion between 0
shared_ptr<FILE>, which is a good thing.
On a larger question, what is the design rational for not making the deleter
a template parameter? This would allow better code factoring, since you
could make a typedef:
typedef shared_ptr<FILE, cfile_deleter> cfile_ptr;
Then cfile_ptr could be used wherever fopen is used, and would eliminate the
danger of forgetting the fclose parameter (which would result in a subtle
resource leak - the file is never closed).
In my opinion, the deleter template parameter should be instantiated on
demand (in dispose()), rather than stored as a member variable, because it
will almost never be desired to change for a given shared_ptr typedef.
In this example, you would have:
struct cfile_deleter {
void operator()(FILE* f) {if (f) fclose(f);}
}
You need a null check at dispose time given this model, but it doesn't
really cost you anything since it saves you one at allocation time:
cfile_ptr shared_file (const char* name, const char* mode) {
return cfile_ptr(fopen(name, mode));
}
-----Original Message-----
From: Greg Colvin [mailto:gcolvin_at_[hidden]]
Sent: Friday, 25 January 2002 1:18 PM
To: boost_at_[hidden]
Subject: Re: [boost] new version of smart pointer class templates
[snip]
shared_ptr<FILE> shared_file (const char* name, const char* mode) {
if (FILE* p = fopen(name,mode))
return shared_ptr<FILE>(p,fclose);
return 0;
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk