<?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[Spezialisierte template-Methode einer template-Klasse lässt sich nur innerhalb der Klasse (inline) definieren]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich würde gerne die Definitionen der Methoden meiner template-Klasse nicht innerhalb der Klassendefinition machen sondern außerhalb.<br />
Jetzt habe ich aber ein Problem mit einer spezialisierten template-Methode - die will sich nämlich nur inline vom Compiler fressen lassen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /><br />
Ich habe das Ganze auf ein kleines Bsp. übertragen. Die template-Funktion-Beispiele (bar()) habe ich eingebaut, um meine Spezialisierungssyntax zu prüfen.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;

template&lt; typename t_0 &gt;
void bar()
{
   std::cout &lt;&lt; &quot;bar&quot; &lt;&lt; &quot; &quot; &lt;&lt; sizeof( t_0 ) &lt;&lt; std::endl;
}

template&lt;&gt;
void bar&lt; std::string &gt;()
{
   std::cout &lt;&lt; &quot;bar_spec&quot; &lt;&lt; &quot; &quot; &lt;&lt; 1887 &lt;&lt; std::endl;
}

template&lt; typename t_0 &gt;
class t_foo
{
public:
   void f0() const;

   template&lt; typename t_1 &gt;
   void f1() const;

   template&lt;&gt;
   void f1&lt; std::string &gt;() const;
   //{
   //   std::cout &lt;&lt; &quot;f1_spec&quot; &lt;&lt; &quot; &quot; &lt;&lt; sizeof( t_0 ) &lt;&lt; &quot; &quot; &lt;&lt; 33 &lt;&lt; std::endl;
   //}

private:
   t_0 m_0;

};

template&lt; typename t_0 &gt;
void t_foo&lt; t_0 &gt;::f0() const
{
   std::cout &lt;&lt; &quot;f0&quot; &lt;&lt; &quot; &quot; &lt;&lt; sizeof( t_0 ) &lt;&lt; std::endl;
}

template&lt; typename t_0 &gt;
template&lt; typename t_1 &gt;
void t_foo&lt; t_0 &gt;::f1() const
{
   std::cout &lt;&lt; &quot;f1&quot; &lt;&lt; &quot; &quot; &lt;&lt; sizeof( t_0 ) &lt;&lt; &quot; &quot; &lt;&lt; sizeof( t_1 ) &lt;&lt; std::endl;
}

template&lt; typename t_0 &gt;
template&lt;&gt;
void t_foo&lt; t_0 &gt;::f1&lt; std::string &gt;() const
{
   std::cout &lt;&lt; &quot;f1_spec&quot; &lt;&lt; &quot; &quot; &lt;&lt; sizeof( t_0 ) &lt;&lt; &quot; &quot; &lt;&lt; 33 &lt;&lt; std::endl;
}

int main()
{
   bar&lt; int &gt;();
   bar&lt; std::string &gt;();

   t_foo&lt; int &gt; foo_0;

   foo_0.f0();
   foo_0.f1&lt; double &gt;();
   foo_0.f1&lt; std::string &gt;();

   std::string dummy;
   std::cin &gt;&gt; dummy;
}
</code></pre>
<p>Das Problem stellt die mit std::string spezialisierte Variante von t_foo::f1() dar.<br />
Der (Visual Studio 2010-) Compiler sagt dazu Folgendes:</p>
<blockquote>
<p>1&gt;x:\1887\main.cpp(10028): error C2768: 't_foo&lt;t_0&gt;::f1' : illegal use of explicit template arguments<br />
1&gt;x:\1887\main.cpp(10023): error C3212: 't_foo&lt;t_0&gt;::f1' : an explicit specialization of a template member must be a member of an explicit specialization<br />
1&gt; x:\1887\main.cpp(10000) : see declaration of 't_foo&lt;t_0&gt;::f1'</p>
</blockquote>
<p>Wenn ich die auskommentierte Inline-Variante verwende, geht es.<br />
Die Fehlermeldung C3212 deutet ja darauf hin, dass es nicht erlaubt ist. Aber warum? Und warum ist es inline ok?</p>
<p>Die Erklärung (<a href="http://msdn.microsoft.com/en-us/library/b11ck5wk(v=vs.90).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/b11ck5wk(v=vs.90).aspx</a>) hat mich auch nicht wirklich erleuchtet. Da steht, dass es nicht legal ist, aber warum wird auch nicht klar.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/298623/spezialisierte-template-methode-einer-template-klasse-lässt-sich-nur-innerhalb-der-klasse-inline-definieren</link><generator>RSS for Node</generator><lastBuildDate>Thu, 13 Aug 2026 18:47:45 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/298623.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 24 Jan 2012 15:56:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Spezialisierte template-Methode einer template-Klasse lässt sich nur innerhalb der Klasse (inline) definieren on Tue, 24 Jan 2012 15:56:55 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich würde gerne die Definitionen der Methoden meiner template-Klasse nicht innerhalb der Klassendefinition machen sondern außerhalb.<br />
Jetzt habe ich aber ein Problem mit einer spezialisierten template-Methode - die will sich nämlich nur inline vom Compiler fressen lassen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /><br />
Ich habe das Ganze auf ein kleines Bsp. übertragen. Die template-Funktion-Beispiele (bar()) habe ich eingebaut, um meine Spezialisierungssyntax zu prüfen.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;

template&lt; typename t_0 &gt;
void bar()
{
   std::cout &lt;&lt; &quot;bar&quot; &lt;&lt; &quot; &quot; &lt;&lt; sizeof( t_0 ) &lt;&lt; std::endl;
}

template&lt;&gt;
void bar&lt; std::string &gt;()
{
   std::cout &lt;&lt; &quot;bar_spec&quot; &lt;&lt; &quot; &quot; &lt;&lt; 1887 &lt;&lt; std::endl;
}

template&lt; typename t_0 &gt;
class t_foo
{
public:
   void f0() const;

   template&lt; typename t_1 &gt;
   void f1() const;

   template&lt;&gt;
   void f1&lt; std::string &gt;() const;
   //{
   //   std::cout &lt;&lt; &quot;f1_spec&quot; &lt;&lt; &quot; &quot; &lt;&lt; sizeof( t_0 ) &lt;&lt; &quot; &quot; &lt;&lt; 33 &lt;&lt; std::endl;
   //}

private:
   t_0 m_0;

};

template&lt; typename t_0 &gt;
void t_foo&lt; t_0 &gt;::f0() const
{
   std::cout &lt;&lt; &quot;f0&quot; &lt;&lt; &quot; &quot; &lt;&lt; sizeof( t_0 ) &lt;&lt; std::endl;
}

template&lt; typename t_0 &gt;
template&lt; typename t_1 &gt;
void t_foo&lt; t_0 &gt;::f1() const
{
   std::cout &lt;&lt; &quot;f1&quot; &lt;&lt; &quot; &quot; &lt;&lt; sizeof( t_0 ) &lt;&lt; &quot; &quot; &lt;&lt; sizeof( t_1 ) &lt;&lt; std::endl;
}

template&lt; typename t_0 &gt;
template&lt;&gt;
void t_foo&lt; t_0 &gt;::f1&lt; std::string &gt;() const
{
   std::cout &lt;&lt; &quot;f1_spec&quot; &lt;&lt; &quot; &quot; &lt;&lt; sizeof( t_0 ) &lt;&lt; &quot; &quot; &lt;&lt; 33 &lt;&lt; std::endl;
}

int main()
{
   bar&lt; int &gt;();
   bar&lt; std::string &gt;();

   t_foo&lt; int &gt; foo_0;

   foo_0.f0();
   foo_0.f1&lt; double &gt;();
   foo_0.f1&lt; std::string &gt;();

   std::string dummy;
   std::cin &gt;&gt; dummy;
}
</code></pre>
<p>Das Problem stellt die mit std::string spezialisierte Variante von t_foo::f1() dar.<br />
Der (Visual Studio 2010-) Compiler sagt dazu Folgendes:</p>
<blockquote>
<p>1&gt;x:\1887\main.cpp(10028): error C2768: 't_foo&lt;t_0&gt;::f1' : illegal use of explicit template arguments<br />
1&gt;x:\1887\main.cpp(10023): error C3212: 't_foo&lt;t_0&gt;::f1' : an explicit specialization of a template member must be a member of an explicit specialization<br />
1&gt; x:\1887\main.cpp(10000) : see declaration of 't_foo&lt;t_0&gt;::f1'</p>
</blockquote>
<p>Wenn ich die auskommentierte Inline-Variante verwende, geht es.<br />
Die Fehlermeldung C3212 deutet ja darauf hin, dass es nicht erlaubt ist. Aber warum? Und warum ist es inline ok?</p>
<p>Die Erklärung (<a href="http://msdn.microsoft.com/en-us/library/b11ck5wk(v=vs.90).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/b11ck5wk(v=vs.90).aspx</a>) hat mich auch nicht wirklich erleuchtet. Da steht, dass es nicht legal ist, aber warum wird auch nicht klar.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2171924</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2171924</guid><dc:creator><![CDATA[void*]]></dc:creator><pubDate>Tue, 24 Jan 2012 15:56:55 GMT</pubDate></item><item><title><![CDATA[Reply to Spezialisierte template-Methode einer template-Klasse lässt sich nur innerhalb der Klasse (inline) definieren on Tue, 24 Jan 2012 16:43:10 GMT]]></title><description><![CDATA[<p>n3242 14.7.3/16 schrieb:</p>
<blockquote>
<p>In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well. In such explicit specialization declaration, the keyword template followed by a template-parameter-list shall be provided instead of the template&lt;&gt; preceding the explicit specialization declaration of the member. The types of the template-parameters in the template-parameter-list shall be the same as those specified in the primary template definition. Example:</p>
<pre><code class="language-cpp">template &lt;class T1&gt; class A {
  template&lt;class T2&gt; class B {
    template&lt;class T3&gt; void mf1(T3);
    void mf2();
  };
};
template &lt;&gt; template &lt;class X&gt;
class A&lt;int&gt;::B {
  template &lt;class T&gt; void mf1(T);
};
template &lt;&gt; template &lt;&gt; template&lt;class T&gt;
void A&lt;int&gt;::B&lt;double&gt;::mf1(T t) { }
template &lt;class Y&gt; template &lt;&gt;
void A&lt;Y&gt;::B&lt;double&gt;::mf2() { } // ill-formed; B&lt;double&gt; is specialized but
                                // its enclosing class template A is not
</code></pre>
</blockquote>
<p><a href="http://ideone.com/vpBhD" rel="nofollow">http://ideone.com/vpBhD</a><br />
<a href="http://ideone.com/jWdcI" rel="nofollow">http://ideone.com/jWdcI</a></p>
<p>Meinem Verständnis nach dürfte auch Version 1 nicht kompilieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2171940</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2171940</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Tue, 24 Jan 2012 16:43:10 GMT</pubDate></item><item><title><![CDATA[Reply to Spezialisierte template-Methode einer template-Klasse lässt sich nur innerhalb der Klasse (inline) definieren on Tue, 24 Jan 2012 17:47:50 GMT]]></title><description><![CDATA[<p>cooky451 schrieb:</p>
<blockquote>
<p>Meinem Verständnis nach dürfte auch Version 1 nicht kompilieren.</p>
</blockquote>
<p>Richtig, da Spezialisierungen grundsätzlich nur auf Namensraumebene deklariert werden können.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2171974</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2171974</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Tue, 24 Jan 2012 17:47:50 GMT</pubDate></item><item><title><![CDATA[Reply to Spezialisierte template-Methode einer template-Klasse lässt sich nur innerhalb der Klasse (inline) definieren on Wed, 25 Jan 2012 10:29:56 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>vielen Dank ihr zwei für die Antworten.<br />
Gibt es auch einen Grund, warum das nicht erlaubt ist (außer, dass der Standard dies so festlegt)?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2172200</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2172200</guid><dc:creator><![CDATA[void*]]></dc:creator><pubDate>Wed, 25 Jan 2012 10:29:56 GMT</pubDate></item></channel></rss>