<?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[Template-template problem -&amp;gt; &amp;quot;teufelskreis&amp;quot;]]></title><description><![CDATA[<p>ich habe eine klasse die folgendermaßen aussieht:</p>
<pre><code>template&lt;class TId/*identifier*/, class TData/*datatype*/, class TPCreation&gt; class MyClass{ /*code*/ };
</code></pre>
<p>jetzt will ich einen solchen datentyp erstellen:</p>
<pre><code>typedef MyClass&lt;int, float,PCreateUsingNew&lt;MyClass&lt;int, float, ...&gt; &gt; my_type;
</code></pre>
<p>und da ist auch schon das problem, 'PCreateUsingNew&lt;class T&gt;' benötigt den zu erstellenden typ für den allocater, da dieser typ aber wiederum CCreateUsingNew enthällt, entsteht eine art teufelskreis und ich habe keine möglichkeit die klasse zu erstellen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/312116/template-template-problem-gt-quot-teufelskreis-quot</link><generator>RSS for Node</generator><lastBuildDate>Mon, 03 Aug 2026 02:54:18 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/312116.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 24 Dec 2012 21:30:57 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Template-template problem -&amp;gt; &amp;quot;teufelskreis&amp;quot; on Mon, 24 Dec 2012 21:31:23 GMT]]></title><description><![CDATA[<p>ich habe eine klasse die folgendermaßen aussieht:</p>
<pre><code>template&lt;class TId/*identifier*/, class TData/*datatype*/, class TPCreation&gt; class MyClass{ /*code*/ };
</code></pre>
<p>jetzt will ich einen solchen datentyp erstellen:</p>
<pre><code>typedef MyClass&lt;int, float,PCreateUsingNew&lt;MyClass&lt;int, float, ...&gt; &gt; my_type;
</code></pre>
<p>und da ist auch schon das problem, 'PCreateUsingNew&lt;class T&gt;' benötigt den zu erstellenden typ für den allocater, da dieser typ aber wiederum CCreateUsingNew enthällt, entsteht eine art teufelskreis und ich habe keine möglichkeit die klasse zu erstellen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2283041</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2283041</guid><dc:creator><![CDATA[gamer8o4]]></dc:creator><pubDate>Mon, 24 Dec 2012 21:31:23 GMT</pubDate></item><item><title><![CDATA[Reply to Template-template problem -&amp;gt; &amp;quot;teufelskreis&amp;quot; on Mon, 24 Dec 2012 22:56:02 GMT]]></title><description><![CDATA[<p>Falls das ein normaler Allokator sein soll, wie ihn die Standardbibliothek kennt, ist es nicht zwingend erforderlich, den Typ gleich korrekt anzugeben.</p>
<pre><code class="language-cpp">template&lt;class T, class Alloc&gt;
class Foo
{
    typedef typename Alloc::template rebind&lt;Foo&gt;::other real_alloc; // C++03
    using real_alloc = typename std::allocator_traits&lt;Alloc&gt;::template rebind_alloc&lt;Foo&gt;; // C++11
};

typedef Foo&lt;int, std::allocator&lt;void&gt; &gt; my_foo;
</code></pre>
<p>Alternativ könnte man auch einen Templatetemplateparamter verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2283042</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2283042</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 24 Dec 2012 22:56:02 GMT</pubDate></item><item><title><![CDATA[Reply to Template-template problem -&amp;gt; &amp;quot;teufelskreis&amp;quot; on Mon, 24 Dec 2012 22:58:37 GMT]]></title><description><![CDATA[<p>also, hier mal der code zweier meiner allocatoren:</p>
<pre><code>// implementation of the CreationPolicy
	// -&gt; creates objects using a straight call to the new operator 
	template &lt;class T&gt; class PCreateUsingNew{
	public:
		static T* create(){
			return new T;
		}
        static void destroy(T* p){ 
			delete p;
		}
    };// c: PCreateUsingNew

	// implementation of the CreationPolicy
	// -&gt; creates objects using a call to std::malloc, 
	//    followed by call to the placement new operator
	template &lt;class T&gt; class PCreateUsingMalloc{
	public:
        static T* create(){
            void* p = std::malloc(sizeof(T));
            if (!p) return 0;
            return new(p) T;
        }

        static void destroy(T* p){
            p-&gt;~T();
            std::free(p);
        }
    };// c: PCreateUsingMalloc
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2283043</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2283043</guid><dc:creator><![CDATA[gamer8o4]]></dc:creator><pubDate>Mon, 24 Dec 2012 22:58:37 GMT</pubDate></item><item><title><![CDATA[Reply to Template-template problem -&amp;gt; &amp;quot;teufelskreis&amp;quot; on Mon, 24 Dec 2012 23:12:34 GMT]]></title><description><![CDATA[<p>Das hier:</p>
<blockquote>
<pre><code>static T* create(){ 
            void* p = std::malloc(sizeof(T)); 
            if (!p) return 0; 
            return new(p) T; 
        }
</code></pre>
</blockquote>
<p>Ist doch nicht sicher, oder?<br />
Sollte es nicht</p>
<pre><code>static T* create()
        { 
            void* p = std::malloc(sizeof(T)); 
            if (!p) return 0; 

            try {
                 return new(p) T;
            } catch( ... ) {
                std::free( p );
                throw;
           } 
        }
</code></pre>
<p>sein?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2283046</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2283046</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Mon, 24 Dec 2012 23:12:34 GMT</pubDate></item><item><title><![CDATA[Reply to Template-template problem -&amp;gt; &amp;quot;teufelskreis&amp;quot; on Mon, 24 Dec 2012 23:16:57 GMT]]></title><description><![CDATA[<p>Jede beliebige Form von Indirektion hilft. Ein zusätzliches typedef-/alias-template wie die Standardallokatoren. Oder aber, falls die Allokatoren immer zustandslos sind, den Typparemeter aus der Klasse entfernen und statt dessen Templatefunktionen verwenden.<br />
Oder eben, wie schon angesprochen, die Allokationspolicy durch enen Templatetemplateparameter spezifizieren (im Stile von Alexandrescus PB-Designs).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2283047</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2283047</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 24 Dec 2012 23:16:57 GMT</pubDate></item><item><title><![CDATA[Reply to Template-template problem -&amp;gt; &amp;quot;teufelskreis&amp;quot; on Mon, 24 Dec 2012 23:22:57 GMT]]></title><description><![CDATA[<blockquote>
<p>Ist doch nicht sicher, oder?<br />
Sollte es nicht</p>
</blockquote>
<p>sicher ist immer relativ, deins ist auf jeden fall sicherer, aber in den meisten fällen reicht meine variante vollkommen (zumindest bis jetzt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> )</p>
<blockquote>
<p>Ist doch nicht sicher, oder?<br />
Sollte es nicht</p>
</blockquote>
<p>ich hab 'modern c++ design by andrei alexandrescu' hier liegen, weiß einer die seitenzahl? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2283048</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2283048</guid><dc:creator><![CDATA[gamer8o4]]></dc:creator><pubDate>Mon, 24 Dec 2012 23:22:57 GMT</pubDate></item><item><title><![CDATA[Reply to Template-template problem -&amp;gt; &amp;quot;teufelskreis&amp;quot; on Tue, 25 Dec 2012 10:31:56 GMT]]></title><description><![CDATA[<p>gamer8o4 schrieb:</p>
<blockquote>
<p>sicher ist immer relativ</p>
</blockquote>
<p>Nein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2283084</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2283084</guid><dc:creator><![CDATA[Dobi]]></dc:creator><pubDate>Tue, 25 Dec 2012 10:31:56 GMT</pubDate></item><item><title><![CDATA[Reply to Template-template problem -&amp;gt; &amp;quot;teufelskreis&amp;quot; on Tue, 25 Dec 2012 10:41:28 GMT]]></title><description><![CDATA[<p>gamer8o4 schrieb:</p>
<blockquote>
<p>sicher ist immer relativ, deins ist auf jeden fall sicherer, aber in den meisten fällen reicht meine variante vollkommen (zumindest bis jetzt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> )</p>
</blockquote>
<p>Du meinst, du hast noch nie einen Konstruktor gesehen, der eine Exception wirft? Auch wenns ein Default-Konstruktor ist, sowas sollte man nicht im Vornherein ausschliessen.</p>
<p>&lt;:xmas2:&gt;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2283091</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2283091</guid><dc:creator><![CDATA[glühnase]]></dc:creator><pubDate>Tue, 25 Dec 2012 10:41:28 GMT</pubDate></item><item><title><![CDATA[Reply to Template-template problem -&amp;gt; &amp;quot;teufelskreis&amp;quot; on Tue, 25 Dec 2012 13:15:39 GMT]]></title><description><![CDATA[<p>habs <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> andrei alexandrescu hat mich (mal wieder) gerettet!<br />
danke für den tipp und ich habe meine allocatoren übrigens überarbeitet wie ihr das vorgeschlagen habt (try-catch blöcke)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2283112</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2283112</guid><dc:creator><![CDATA[gamer8o4]]></dc:creator><pubDate>Tue, 25 Dec 2012 13:15:39 GMT</pubDate></item><item><title><![CDATA[Reply to Template-template problem -&amp;gt; &amp;quot;teufelskreis&amp;quot; on Tue, 25 Dec 2012 14:34:13 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<pre><code>static T* create()
        { 
            void* p = std::malloc(sizeof(T)); 
            if (!p) return 0; 

            try {
                 return new(p) T;
            } catch( ... ) {
                std::free( p );
                throw;
           } 
        }
</code></pre>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /> Das ist korrekt. Ausserdem absolut super, dass du gelernt hast, dass das bei destroy nicht nötig ist, weil Destruktoren eh nie werfen dürfen.</p>
<p>Es ist immer eine nette Übung, Code mit try/catch umzuschreiben, dass kein try/catch nötig ist. Führt oft zu lesbarem Code.</p>
<pre><code>template &lt;typename T&gt;
class malloc_guard {
  T *p;
public:
  malloc_guard() { p = std::malloc(sizeof(T)); }
 ~malloc_guard() { if (p) std::free(p); } // if (p) fürs Herausoptimieren

  T* get()     { return p; }
  T* release() { T *ptr=p; p=0; return ptr; }
};

static T* create()
{ 
  malloc_guard&lt;T&gt; memory;
  if (memory.get())
    new (memory.get()) T;
  return memory.release();
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2283124</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2283124</guid><dc:creator><![CDATA[wintersonne]]></dc:creator><pubDate>Tue, 25 Dec 2012 14:34:13 GMT</pubDate></item></channel></rss>