Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-01-25 15:21:56


From: "Brey, Edward D" <EdwardDBrey_at_[hidden]>
> I don't think this will work. There is no implicit conversion between 0
> shared_ptr<FILE>, which is a good thing.

Yes, you could (should?) do this

shared_ptr<FILE> my_fopen(const char* name, const char* mode)
{
    if (FILE* p = fopen(name, mode))
        return shared_ptr<FILE>(p, fclose);
    else
        throw open_error(name, errno);
}

instead. Of course this is simply an example.

> On a larger question, what is the design rational for not making the
deleter
> a template parameter?

Both approaches have their uses. It is generally expected that the Loki
smart pointer, or some other policy-based design, will cover the template
parameterized case, where you encode the deallocation policy into the type
of the pointer.

shared_ptr<> can be used in situations where you don't want to have the
deallocation policy encoded in the type. For example, if you maintain a
library that has

shared_ptr<Object> createObject();

and you want to change the allocator used for object creation (or even make
some objects static) without breaking compatibility.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk