Boost logo

Boost Users :

From: Frank Birbacher (bloodymir.crap_at_[hidden])
Date: 2008-05-18 18:04:11


Hi!

Kevin Martin schrieb:
> I have just discovered to my delight that you can use shared_ptr with
> a null deleter to pass around stack based objects.

I got this idea recently, too. In short:

struct Bar;
struct Foo{
        vector<shared_ptr<Bar> > bars;

        void addBar(shared_ptr<Bar> bar)
        { bars.push_back(bar); }

        void addBar(Bar& bar)
        {
                bars.push_back(shared_ptr<Bar>(&bar, null_deleter());
                bar.onDestructionUnregisterAt(this);
        }
};

I pass objects allocated on the heap or objects with static storage
duration to addBar. (Of course construction/destruction order of globals
is another issue) Pairing this with a registration service for Bars
makes them deregister with Foo upon their destruction. I think this can
be safe to use. Here, the usage of the null_deleter is an implementation
detail of Foo.

Regards, Frank


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net