Boost logo

Boost :

Subject: [boost] [smart_ptr] boost::auto_ptr?
From: Preston A. Elder (prez_at_[hidden])
Date: 2009-05-06 13:15:52


I was wondering if the maintainer of the smart pointers could create an
analogue to auto_ptr in the boost namespace. Specificlly to allow the
addition of a deallocator like that which exists in shared_ptr?

Right now, none of std::auto_ptr, shared_ptr or scoped_ptr will quite fit
this niche.
  - std::auto_ptr - always uses delete ptr; There not only no standard
way to supply an allocator, but also no way to specify a one-off deleter.
  - scoped_ptr - is noncopyable (by design), I want to use it like an
auto_ptr.
  - shared_ptr - has more overhead for thread safety and multiple usage
concerns than is desired in this circumstance. Not to mention that it
doesn't function like auto_ptr ('nullifying' after operator=).

Quite simply, I'd love to do: boost::auto_ptr<MyClass> obj(ptr, deleter);
Just like I can with boost::shared_ptr, and use it like regular
std::auto_ptr. Can someone add this to boost? Pretty please? :)

Incidentlly, I have another question, about enable_shared_from_this.
Will this play well with a regular shared_ptr? For example - say I have
MyClass and MyOtherClass. MyClass is going to invoke a function on
MyOtherClass that will cause MyOtherClass to store the shared_ptr. For
example:

class MyClass
{
public:
    void foo(MyOtherClass &moc) const { moc.foo(shared_from_this()); }
};

class MyOtherClass
{
    shared_ptr<MyClass> mc;
public:
    void foo(const boost::shared_ptr<MyClass> &in) { mc = in; }
};

If I did:

shared_ptr<MyClass> mc(new MyClass);
MyOtherClass moc;
mc->foo(moc);

Ignoring the abserdity of the example (obviously real-life situations
would be much more complex), would the mc and moc.mc shared ptr's be in
sync? would they play nice together? or would there be chaos and
destruction?

What if I instead did:
MyClass *tmp = new MyClass;
shared_ptr<MyClass> mc(tmp->shared_from_this());

Would that fix the above if the above is a problem? I am curious,
because right now, I have several instances where I am making
constructors private, adding a weak_ptr to a class and adding a static
create() function that takes the constructor arguments. Then my create()
function will construct the object, putting it in a shared_ptr, then
initialize the weak_ptr from the newly created shared_ptr, and then
return the shared_ptr - all of this is quite cumbersome.

Thanks,

PreZ :)


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