|
Boost Users : |
Subject: [Boost-users] [SmartPtr] enable_shared_from_raw
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2014-07-24 00:53:33
Hi all,
Firstly, any chance this could get some documentation? Is the author
still around?
I'm thinking about using this at the moment for two separate reasons
(and ways in which enable_shared_from_this seems to fall short):
1. shared_from_this can't be used from the constructor (requiring
factory-method-init shenanigans); reportedly
(https://groups.google.com/forum/#!topic/boost-list/8JWFZ56ry6k)
shared_from_raw can, although I can't find specific details on the
caveats mentioned.
2. What I *really* want to be able to do is to create a child object
(most commonly a callback functor) that contains a weak_ptr, and it
seems silly (and badly performant) to use the usual factory-init pattern
to call shared_from_this just to degrade it back to a weak_ptr.
And it should be possible to obtain that weak_ptr before any shared_ptr
exists, and "fix" it later to point at the real shared_ptr, especially
if it is known that the weak_ptr will never actually be used until that
real shared_ptr exists.
enable_shared_from_raw almost seems to fit the bill (according to
http://stackoverflow.com/questions/22184772/why-is-boostenable-shared-from-raw-so-undocumented),
but I encountered the same problem as in this post
(http://lists.boost.org/boost-users/2013/05/78833.php); it works with
shared_from_raw but not weak_from_raw.
However this change:
https://github.com/boostorg/smart_ptr/pull/8
fixes that. It looks like the two calls went down different code paths
for some reason I can't fathom. Making this change (or the
similar-but-better-style one mentioned in the PR discussion) makes the
above scenario work as expected. I'm not sure if there was some
specific reason to not do this before or if it was just an oversight.
Any chance this can make it into 1.56? Or is it too late for that?
Regards,
Gavin Lambert
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