<?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[SmartPointer is not a template]]></title><description><![CDATA[<p>Ich versuche gerade eine Klasse zu schreiben die sich um einen Vector mit Smartpointer kümmert:</p>
<pre><code>#ifndef SMARTVECTOR_H_INCLUDED
#define SMARTVECTOR_H_INCLUDED

#include &lt;memory&gt;
#include &lt;vector&gt;

template &lt; typename SmartPointer, typename Type &gt;
class SmartVector
{
    public:
        void push_back( Type* type );

        std::vector &lt; SmartPointer&lt; Type &gt; &gt; data;
};

template &lt; typename SmartPointer, typename Type &gt;
void SmartVector&lt; SmartPointer, Type &gt;::push_back( Type* type )
{
    SmartPointer&lt; Type* &gt; pointer( new Type(type) );
    data.push_back( pointer );
}

#endif // SMARTVECTOR_H_INCLUDED
</code></pre>
<p>Ich bekomme die Fehlermeldung die ich als Titel geschrieben haben.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/310779/smartpointer-is-not-a-template</link><generator>RSS for Node</generator><lastBuildDate>Tue, 04 Aug 2026 07:51:59 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/310779.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 19 Nov 2012 19:44:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to SmartPointer is not a template on Mon, 19 Nov 2012 19:44:01 GMT]]></title><description><![CDATA[<p>Ich versuche gerade eine Klasse zu schreiben die sich um einen Vector mit Smartpointer kümmert:</p>
<pre><code>#ifndef SMARTVECTOR_H_INCLUDED
#define SMARTVECTOR_H_INCLUDED

#include &lt;memory&gt;
#include &lt;vector&gt;

template &lt; typename SmartPointer, typename Type &gt;
class SmartVector
{
    public:
        void push_back( Type* type );

        std::vector &lt; SmartPointer&lt; Type &gt; &gt; data;
};

template &lt; typename SmartPointer, typename Type &gt;
void SmartVector&lt; SmartPointer, Type &gt;::push_back( Type* type )
{
    SmartPointer&lt; Type* &gt; pointer( new Type(type) );
    data.push_back( pointer );
}

#endif // SMARTVECTOR_H_INCLUDED
</code></pre>
<p>Ich bekomme die Fehlermeldung die ich als Titel geschrieben haben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2272484</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272484</guid><dc:creator><![CDATA[SmartClass]]></dc:creator><pubDate>Mon, 19 Nov 2012 19:44:01 GMT</pubDate></item><item><title><![CDATA[Reply to SmartPointer is not a template on Mon, 19 Nov 2012 19:54:44 GMT]]></title><description><![CDATA[<p>Laut Zeile 13 und 19 ist SmartPointer ein Klassentemplate. Laut Zeile 7 jedoch nur ein &quot;einfacher Typ&quot;, da muss statt <strong>typename SmartPointer</strong> ein</p>
<pre><code>typename &lt;typename, typename&gt; class SmartPointer
</code></pre>
<p>hin.</p>
<p>Das hilft dir zwar für jetzt, aber vielleicht schaffst du den Rest dann selbst <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2272489</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272489</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 19 Nov 2012 19:54:44 GMT</pubDate></item><item><title><![CDATA[Reply to SmartPointer is not a template on Mon, 19 Nov 2012 20:05:34 GMT]]></title><description><![CDATA[<blockquote>
<p>Laut Zeile 13 und 19 ist SmartPointer ein Klassentemplate. Laut Zeile 7 jedoch nur ein &quot;einfacher Typ&quot;, da muss statt typename SmartPointer ein</p>
<pre><code>typename &lt;typename, typename&gt; class SmartPointer
</code></pre>
</blockquote>
<p>Meinst du das so ?</p>
<pre><code>template &lt; typename Type, typename &lt;typename, typename&gt; class SmartPointer &gt;
</code></pre>
<p>Da bekomme ich ein error:</p>
<pre><code>expected nested-name-specifier before ‘&lt;’ token|
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2272491</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272491</guid><dc:creator><![CDATA[SmartClass]]></dc:creator><pubDate>Mon, 19 Nov 2012 20:05:34 GMT</pubDate></item><item><title><![CDATA[Reply to SmartPointer is not a template on Mon, 19 Nov 2012 20:12:54 GMT]]></title><description><![CDATA[<p>Sorry, vertippsel und keine Ahnung wieso ich auf zwei Templateparameter komme. Wohl nen Einfluss eines anderen Threads <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> (cooky ist schuld)</p>
<pre><code>template &lt; template &lt;typename&gt; class SmartPointer, typename Type &gt;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2272494</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272494</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 19 Nov 2012 20:12:54 GMT</pubDate></item><item><title><![CDATA[Reply to SmartPointer is not a template on Mon, 19 Nov 2012 20:42:18 GMT]]></title><description><![CDATA[<p>Du musst überlegen, was du willst.</p>
<p><strong>1. Typ-Template-Parameter</strong></p>
<pre><code>template &lt;class SmartPtr, typename T&gt;
class SmartVector;
</code></pre>
<p><code>SmartPtr</code> bezeichnet hier einen vollständigen Typen wie <code>std::unique_ptr&lt;int&gt;</code> . Hat den Vorteil, dass du die Anzahl Template-Parameter des Smart-Pointers nicht vorschreibst. Im <code>unique_ptr</code> -Beispiel könnte man einen Deleter angeben.</p>
<p><strong>2. Template-Template-Parameter</strong></p>
<pre><code>template &lt;template &lt;typename&gt; class SmartPtr, typename T&gt;
class SmartVector;
</code></pre>
<p>Hier ist <code>SmartPtr</code> kein Typ, sondern ein Template, das mit einem Typ-Parameter instanziiert werden kann. Zum Beispiel könnte man <code>std::unique_ptr</code> übergeben. Du vermeidest auf diese Weise die doppelte Angabe des Typs, jedoch schränkst du die Wahl des Smart-Pointers ein.</p>
<p>Weitere Anmerkungen:</p>
<ul>
<li>Du kannst auch nur den Smart-Pointer als Typ-Parameter entgegen nehmen. Den Typen extrahierst du dir mit einer Metafunktion, einem Member-Typedef wie <code>value_type</code> , oder implizit durch Templates bzw. C++11-Typinferenz.</li>
<li>Den Header <code>&lt;memory&gt;</code> brauchst du nicht. Wenn Benutzer ihn brauchen, sollen sie ihn selbst inkludieren.</li>
<li>Die Signatur von <code>push_back()</code> ist äusserst unintuitiv. Wenn der Parameter ein Zeiger auf nicht-konstante Objekte ist, gehe ich davon aus, dass er direkt in den Smart-Pointer eingefügt wird. Was du aber machst, ist eine Kopie davon -- angebrachter wäre in diesem Fall eine Referenz auf <code>const Type</code> .</li>
<li>Möglicherweise liegt es am Minimalbeispiel, aber <code>data</code> sollte <code>private</code> sein.</li>
</ul>
]]></description><link>https://www.c-plusplus.net/forum/post/2272501</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272501</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 19 Nov 2012 20:42:18 GMT</pubDate></item><item><title><![CDATA[Reply to SmartPointer is not a template on Mon, 19 Nov 2012 21:00:40 GMT]]></title><description><![CDATA[<p>Ich hatte eigentlich die Idee eine Klasse zu schreiben die mir mir so etwas ersparrt:</p>
<pre><code>#include &lt;memory&gt;
#include &lt;vector&gt;

using namespace std;

int main()
{
    std::vector &lt; std::unique_ptr&lt; int &gt; &gt; foo;
    //Das soll die Klasse selber machen und ich muss nur push_back und denn Datentype eingeben
    std::unique_ptr&lt; int &gt; bar( new int(12) );
    foo.push_back( std::move( bar ) );
}
</code></pre>
<p>Warum sollte ich denn vector private machen? ich will ja nur eine push_back funktion ohne std::unique_ptr. So sollte es aussehen:</p>
<pre><code>SmartVector&lt; std::unique_ptr&lt; int &gt; &gt; foo;
foo.push_back( new int( 12 ) );
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2272504</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272504</guid><dc:creator><![CDATA[SmartClass]]></dc:creator><pubDate>Mon, 19 Nov 2012 21:00:40 GMT</pubDate></item><item><title><![CDATA[Reply to SmartPointer is not a template on Mon, 19 Nov 2012 21:18:52 GMT]]></title><description><![CDATA[<p>Dann brauchst du gar keine Klasse. Eine freie Funktion genügt völlig.</p>
<pre><code>template &lt;typename T&gt;
void push_back_smartptr(std::vector&lt;T&gt;&amp; container, T* newValue)
{
    container.push_back( std::unique_ptr&lt;T&gt;(newValue) );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2272509</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272509</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 19 Nov 2012 21:18:52 GMT</pubDate></item><item><title><![CDATA[Reply to SmartPointer is not a template on Mon, 19 Nov 2012 21:28:44 GMT]]></title><description><![CDATA[<p>Vielleicht sind <a href="http://www.boost.org/doc/libs/1_52_0/libs/ptr_container/doc/ptr_container.html" rel="nofollow">Boosts Pointer Container</a> für dich ganz interessant.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2272513</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272513</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Mon, 19 Nov 2012 21:28:44 GMT</pubDate></item><item><title><![CDATA[Reply to SmartPointer is not a template on Mon, 19 Nov 2012 21:37:11 GMT]]></title><description><![CDATA[<p>Äh...wo du doch schon C++11 verwendest...</p>
<pre><code class="language-cpp">#include &lt;memory&gt;
#include &lt;vector&gt;

int main() {
  std::vector&lt;std::unique_ptr&lt;int&gt;&gt; foo;

  foo.emplace_back(new int(12));
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2272515</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272515</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Mon, 19 Nov 2012 21:37:11 GMT</pubDate></item><item><title><![CDATA[Reply to SmartPointer is not a template on Mon, 19 Nov 2012 21:52:35 GMT]]></title><description><![CDATA[<p>seldon schrieb:</p>
<blockquote>
<p>Äh...wo du doch schon C++11 verwendest...</p>
<pre><code class="language-cpp">#include &lt;memory&gt;
#include &lt;vector&gt;
 
int main() {
  std::vector&lt;std::unique_ptr&lt;int&gt;&gt; foo;

  foo.emplace_back(new int(12));
}
</code></pre>
</blockquote>
<p>Habe ich auch daran gedacht zu schreiben, macht aber wohl in diesem Fall kein Unterschied. Oder siehst du das anders? Abgesehen davon, dass wir uns trotzdem nur noch die emplace-Funktionen angewöhnen sollten, weil sie alles können was push_back auch kann* und dazu auch perfect forwarding beherrschen.</p>
<p>* bis auf aggregat initialisierung</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2272524</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272524</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 19 Nov 2012 21:52:35 GMT</pubDate></item><item><title><![CDATA[Reply to SmartPointer is not a template on Mon, 19 Nov 2012 21:55:30 GMT]]></title><description><![CDATA[<p>OK, sorry. Hier macht das natürlich einen wesentlichen Unterschied. Meine Gedanken waren noch bei Nexus's Funktion. Da ist es unwesentlich. Aiaiaia, heute denke ich mehr an die Vergangenheit als ordentlich hinzuschauen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2272526</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272526</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 19 Nov 2012 21:55:30 GMT</pubDate></item><item><title><![CDATA[Reply to SmartPointer is not a template on Tue, 20 Nov 2012 11:13:42 GMT]]></title><description><![CDATA[<p>seldon schrieb:</p>
<blockquote>
<p>Äh...wo du doch schon C++11 verwendest...</p>
<pre><code class="language-cpp">#include &lt;memory&gt;
#include &lt;vector&gt;
 
int main() {
  std::vector&lt;std::unique_ptr&lt;int&gt;&gt; foo;

  foo.emplace_back(new int(12));
}
</code></pre>
</blockquote>
<p>Ich bin quasi ein Fan von emplace. Aber in diesem Fall ist die Verwendung von emplace -- meiner Meinung nach -- nicht ausnahmesicher. Das Problem ist hier, dass im Fehlerfall von emplace_back gar kein unique_ptr erzeugt wird. Die Verantwortung zur Verwaltung einer Resource sollte frühstmöglich an ein schlaues Objekt abgegeben werden. Das passiert hier relativ spät bis gar nicht.</p>
<p>Die Sache mit der Ausnahmesicherheit macht make_unique dann auch so interessant, dass wir es wohl noch für C++14 bekommen *wunschdenk*.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2272678</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272678</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Tue, 20 Nov 2012 11:13:42 GMT</pubDate></item><item><title><![CDATA[Reply to SmartPointer is not a template on Tue, 20 Nov 2012 12:52:04 GMT]]></title><description><![CDATA[<p>krümelkacker schrieb:</p>
<blockquote>
<p>Die Verantwortung zur Verwaltung einer Resource sollte frühstmöglich an ein schlaues Objekt abgegeben werden.</p>
</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="👍"
    /></p>
<p><a href="http://herbsutter.com/gotw/_102/" rel="nofollow">http://herbsutter.com/gotw/_102/</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2272728</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2272728</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Tue, 20 Nov 2012 12:52:04 GMT</pubDate></item></channel></rss>