Boost C++ Libraries: Ticket #2911: shared_from_this free function https://svn.boost.org/trac10/ticket/2911 <p> Attached is an implementation of a free shared_from_this function, plus a non-template enable_shared_from_this_base class which can only be used with the free shared_from_this function. The free function will also work with the usual enable_shared_from_this template. It is implemented on top of the existing enable_shared_from_this class, and so requires no modification of the existing smart_ptr code. </p> <p> This scheme has the advantage of simplicity, since the "curiously recurring template pattern" is not needed. It also "just works" if you have a class hierarchy that looks like "X is derived from Y is derived from enable_shared_from_this_base" and want to use shared_from_this with both X and Y. </p> <p> I've only tried it with gcc 4.3. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2911 Trac 1.4.3 fhess Fri, 03 Apr 2009 21:28:16 GMT attachment set https://svn.boost.org/trac10/ticket/2911 https://svn.boost.org/trac10/ticket/2911 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">shared_from_this.hpp</span> </li> </ul> <p> free boost::shared_from_this template function </p> Ticket Jonathan Wakely <jwakely.boost@…> Mon, 08 Jun 2009 17:06:33 GMT cc set https://svn.boost.org/trac10/ticket/2911#comment:1 https://svn.boost.org/trac10/ticket/2911#comment:1 <ul> <li><strong>cc</strong> <span class="trac-author">jwakely.boost@…</span> added </li> </ul> <p> This idea is interesting, but I don't like the name because there is no <code>this</code> pointer in the context of a non-member function. Maybe I'm wrong, but I regard the "this" in <code>shared_from_this</code> as referring to <code>this</code>. How about <code>shared_from</code> instead? </p> <pre class="wiki">X* p = ...; shared_ptr&lt;X&gt; sp = shared_from(p); </pre><p> It also seems odd to have a class called <code>enable_shared_from_this_base</code> which has a base class called <code>enable_shared_from_this</code>! I understand the reasons, but it seems backwards at first glance. </p> <p> Renaming the free function to <code>shared_from</code> means your new class could be renamed <code>enable_shared_from</code>. Users who only want to use the free function can derive their class X from <code>enable_shared_from</code> (and don't need to care that calling <code>p-&gt;enable_shared_from_this()</code> returns a <code>shared_ptr&lt;enable_shared_from&gt;</code> not a <code>shared_ptr&lt;X&gt;</code>) while users who want to use either the free function or the existing member function can continue to derive from <code>enable_shared_from_this&lt;X&gt;</code>. </p> Ticket Peter Dimov Tue, 01 Dec 2009 01:49:22 GMT owner, milestone changed https://svn.boost.org/trac10/ticket/2911#comment:2 https://svn.boost.org/trac10/ticket/2911#comment:2 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Peter Dimov</span> to <span class="trac-author">Frank Mori Hess</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.39.0</span> → <span class="trac-field-new">To Be Determined</span> </li> </ul> Ticket