Boost logo

Boost :

From: Frank Mori Hess (fmhess_at_[hidden])
Date: 2008-03-17 01:36:34


I'm curious a patch to boost::enable_shared_from_this that makes
shared_from_this() work inside constructors would be considered?

The implementation I've got in mind (haven't actually tried it yet) would
work as follows. I'd add two shared_ptrs to enable_shared_from_this, call
them "internal_shared" and "external_shared". external_shared would be
assigned the external shared_ptr which first takes ownership of *this.
internal_shared would be initialized with the "this" pointer, and a custom
deleter which would reset external_shared. internal_shared would be reset
right after external_shared is initialized (when an external shared_ptr
takes ownership of *this). Any calls to shared_from_this() that occur
prior to an external shared_ptr taking ownership of *this (such as in the
constructor) would get a copy of internal_shared.

So the idea is, external_shared would keep the object alive as long as any
copies of internal_shared that got made in the constructor are still
around.

-- 
Frank



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