
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