
Frank Mori Hess:
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.
Interesting idea. I'm not sure why do you need the two pointers though. How would you cope with the following scenarios: class X: enable_shared_from_this<X> {}; int main() { { X x; } { std::auto_ptr<X> px( new X ); } } ?