<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[enable_shared_from_this mit CRTP?]]></title><description><![CDATA[<p>Hi,</p>
<p>kann man <code>enable_shared_from_this</code> mit CRTP &quot;polymorph&quot; machen?</p>
<pre><code>template&lt;typename connection&gt;
struct TCPConnection :
    public std::enable_shared_from_this&lt;connection&gt;
{
    static_assert( std::is_base_of&lt;TCPConnection&lt;connection&gt;, connection&gt;::value, &quot;connection has to be subclass of TCPConnection&lt;connection&gt;!&quot; );

    typedef std::shared_ptr&lt;connection&gt; pointer;

    static pointer create(boost::asio::io_service&amp; io_service)
    {
        return pointer(new connection(io_service));
    }

    /* ......... */
};
</code></pre>
<p>Ich habe das Beispiel <a href="http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/tutorial/tutdaytime3.html" rel="nofollow">hier</a> stark abgeändert, und bin so darauf gekommen.<br />
Geht das so? Oder geht es auch schöner?</p>
<p>Es geht darum, dass <code>TCPConnection</code> eine abstrakte Basisklasse ist, von der (eine) Klasse(n) ableitet(n).<br />
Jetzt soll TCPServer also als Template-Parameter nur connection bekommen, und kann dann damit wie im Beispiel gezeigt, arbeiten.</p>
<p>MfG</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/314462/enable_shared_from_this-mit-crtp</link><generator>RSS for Node</generator><lastBuildDate>Sat, 01 Aug 2026 04:41:44 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/314462.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 03 Mar 2013 09:23:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to enable_shared_from_this mit CRTP? on Sun, 03 Mar 2013 09:23:52 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>kann man <code>enable_shared_from_this</code> mit CRTP &quot;polymorph&quot; machen?</p>
<pre><code>template&lt;typename connection&gt;
struct TCPConnection :
    public std::enable_shared_from_this&lt;connection&gt;
{
    static_assert( std::is_base_of&lt;TCPConnection&lt;connection&gt;, connection&gt;::value, &quot;connection has to be subclass of TCPConnection&lt;connection&gt;!&quot; );

    typedef std::shared_ptr&lt;connection&gt; pointer;

    static pointer create(boost::asio::io_service&amp; io_service)
    {
        return pointer(new connection(io_service));
    }

    /* ......... */
};
</code></pre>
<p>Ich habe das Beispiel <a href="http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/tutorial/tutdaytime3.html" rel="nofollow">hier</a> stark abgeändert, und bin so darauf gekommen.<br />
Geht das so? Oder geht es auch schöner?</p>
<p>Es geht darum, dass <code>TCPConnection</code> eine abstrakte Basisklasse ist, von der (eine) Klasse(n) ableitet(n).<br />
Jetzt soll TCPServer also als Template-Parameter nur connection bekommen, und kann dann damit wie im Beispiel gezeigt, arbeiten.</p>
<p>MfG</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2303783</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2303783</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 03 Mar 2013 09:23:52 GMT</pubDate></item><item><title><![CDATA[Reply to enable_shared_from_this mit CRTP? on Sun, 03 Mar 2013 10:54:25 GMT]]></title><description><![CDATA[<p>Ich hab auch mal was in der Richtung gebraucht und es genauso gelöst:</p>
<p><a href="http://www.c-plusplus.net/forum/309922-2" rel="nofollow">http://www.c-plusplus.net/forum/309922-2</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2303818</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2303818</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Sun, 03 Mar 2013 10:54:25 GMT</pubDate></item><item><title><![CDATA[Reply to enable_shared_from_this mit CRTP? on Sun, 03 Mar 2013 19:51:26 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Oder geht es auch schöner?</p>
</blockquote>
<p>Das kommt darauf an was man als &quot;schöner&quot; empfindet.<br />
Wenn die <code>TCPConnection</code> ordentlich gross ist und so richtig viel Code hat, könnte es sich auszahlen sie als einfache Klasse zu implementieren, und erst danach ein Template drüberzulegen.<br />
Wenn die <code>TCPConnection</code> selbst dabei schon <code>shared_from_this</code> braucht, muss man dann natürlich casten.</p>
<pre><code class="language-cpp">struct TCPConnectionBase :
    public std::enable_shared_from_this&lt;TCPConnectionBase&gt;
{ 
    /* ......... */
};

template &lt;class connection&gt;
struct TCPConnection : TCPConnectionBase
{ 
    typedef std::shared_ptr&lt;connection&gt; pointer;

    static pointer create(boost::asio::io_service&amp; io_service)
    {
        return pointer(new connection(io_service));
    }

    pointer shared_from_this()
    {
        return static_pointer_cast&lt;connection&gt;(TCPConnectionBase::shared_from_this());
    }

};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2303969</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2303969</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 03 Mar 2013 19:51:26 GMT</pubDate></item><item><title><![CDATA[Reply to enable_shared_from_this mit CRTP? on Sun, 03 Mar 2013 20:17:16 GMT]]></title><description><![CDATA[<blockquote>
<p>Wenn die TCPConnection ordentlich gross ist und so richtig viel Code hat, könnte es sich auszahlen sie als einfache Klasse zu implementieren, und erst danach ein Template drüberzulegen.</p>
</blockquote>
<p>Lol nein, das ... Kommentar ersetzt praktisch fast nichts. ^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2303975</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2303975</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 03 Mar 2013 20:17:16 GMT</pubDate></item></channel></rss>