<?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[Partielle Template-Spezialisierung]]></title><description><![CDATA[<p>Hallo zusammen, ich hab ein Problem mit partieller Template-Spezialisierung, und zwar will ich einen Konstruktor haben, der entweder einen Parameter an eine Parent-Klasse weitergibt od. halt void ist.</p>
<p>Folgendes Beispiel funktioniert:</p>
<pre><code class="language-cpp">template&lt;typename _param&gt;
class PTest
{
public:
	PTest(_param);
};

template&lt;&gt;
PTest&lt;void&gt;::PTest(void)
{
	std::cout &lt;&lt; &quot;void param&quot; &lt;&lt; std::endl;
}

template&lt;typename _T&gt;
PTest&lt;_T&gt;::PTest(_T p)
{
	std::cout &lt;&lt; typeid(_T).name() &lt;&lt; &quot; param &quot; &lt;&lt; p &lt;&lt; std::endl;
}

void mtest()
{
	PTest&lt;void&gt;	vPtest;
	PTest&lt;int&gt;	iPtest(3);

}
</code></pre>
<p>In dem Beispiel ist der Konstruktor des Template PTest für void vollständig definiert (totale Spezialisierung).</p>
<p>Was ich erreichen will ist folgendes:</p>
<pre><code class="language-cpp">template&lt;class _Base, typename _P&gt;
class AnotherTest : public _Base
{
public:
	typedef _Base Parent;
	AnotherTest(_P);
};

template&lt;class _B&gt;
AnotherTest&lt;_B, void&gt;::AnotherTest(void):
	Parent()
{
	std::cout &lt;&lt; &quot;base class: &quot; &lt;&lt; typeid(_B).name() &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;param: void&quot; &lt;&lt; std::endl;
}

template&lt;class _B, typename _X&gt;
AnotherTest&lt;_B, _X&gt;::AnotherTest(_X x):
	Parent(x)
{
	std::cout &lt;&lt; &quot;base class: &quot; &lt;&lt; typeid(_B).name() &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;param: &quot; &lt;&lt; typeid(_X).name() &lt;&lt; x &lt;&lt; std::endl;
}

class Foo
{
public:
	Foo(){}
};

class Bar
{
	int i;
public:
	Bar(int _i):i(_i){}
};

void atest()
{
	AnotherTest&lt;Foo, void&gt;	vATest;
	AnotherTest&lt;Bar, int&gt;	vBTest(4);
}
</code></pre>
<p>Das Template AnotherTest&lt;&gt; ist partiell für den Parameter void spezialisiert.<br />
Leider kompiliert dieser Code-Teil nicht, daher meine Fragen:<br />
- Warum?<br />
- Welche Alternativen habe ich?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/227576/partielle-template-spezialisierung</link><generator>RSS for Node</generator><lastBuildDate>Wed, 30 Sep 2026 00:08:06 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/227576.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 17 Nov 2008 12:16:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Partielle Template-Spezialisierung on Mon, 17 Nov 2008 12:16:08 GMT]]></title><description><![CDATA[<p>Hallo zusammen, ich hab ein Problem mit partieller Template-Spezialisierung, und zwar will ich einen Konstruktor haben, der entweder einen Parameter an eine Parent-Klasse weitergibt od. halt void ist.</p>
<p>Folgendes Beispiel funktioniert:</p>
<pre><code class="language-cpp">template&lt;typename _param&gt;
class PTest
{
public:
	PTest(_param);
};

template&lt;&gt;
PTest&lt;void&gt;::PTest(void)
{
	std::cout &lt;&lt; &quot;void param&quot; &lt;&lt; std::endl;
}

template&lt;typename _T&gt;
PTest&lt;_T&gt;::PTest(_T p)
{
	std::cout &lt;&lt; typeid(_T).name() &lt;&lt; &quot; param &quot; &lt;&lt; p &lt;&lt; std::endl;
}

void mtest()
{
	PTest&lt;void&gt;	vPtest;
	PTest&lt;int&gt;	iPtest(3);

}
</code></pre>
<p>In dem Beispiel ist der Konstruktor des Template PTest für void vollständig definiert (totale Spezialisierung).</p>
<p>Was ich erreichen will ist folgendes:</p>
<pre><code class="language-cpp">template&lt;class _Base, typename _P&gt;
class AnotherTest : public _Base
{
public:
	typedef _Base Parent;
	AnotherTest(_P);
};

template&lt;class _B&gt;
AnotherTest&lt;_B, void&gt;::AnotherTest(void):
	Parent()
{
	std::cout &lt;&lt; &quot;base class: &quot; &lt;&lt; typeid(_B).name() &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;param: void&quot; &lt;&lt; std::endl;
}

template&lt;class _B, typename _X&gt;
AnotherTest&lt;_B, _X&gt;::AnotherTest(_X x):
	Parent(x)
{
	std::cout &lt;&lt; &quot;base class: &quot; &lt;&lt; typeid(_B).name() &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;param: &quot; &lt;&lt; typeid(_X).name() &lt;&lt; x &lt;&lt; std::endl;
}

class Foo
{
public:
	Foo(){}
};

class Bar
{
	int i;
public:
	Bar(int _i):i(_i){}
};

void atest()
{
	AnotherTest&lt;Foo, void&gt;	vATest;
	AnotherTest&lt;Bar, int&gt;	vBTest(4);
}
</code></pre>
<p>Das Template AnotherTest&lt;&gt; ist partiell für den Parameter void spezialisiert.<br />
Leider kompiliert dieser Code-Teil nicht, daher meine Fragen:<br />
- Warum?<br />
- Welche Alternativen habe ich?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1615918</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1615918</guid><dc:creator><![CDATA[Kurisu]]></dc:creator><pubDate>Mon, 17 Nov 2008 12:16:08 GMT</pubDate></item><item><title><![CDATA[Reply to Partielle Template-Spezialisierung on Mon, 17 Nov 2008 12:42:29 GMT]]></title><description><![CDATA[<p>Kurisu schrieb:</p>
<blockquote>
<p>Leider kompiliert dieser Code-Teil nicht, ...</p>
</blockquote>
<p>Das ist keine Fehlerbeschreibung!</p>
<p>Aber wenn ich raten müsste, würde ich wohl sagen, liegt es an deinem Konstruktor. Diese Templatespezialisierung nur des Konstruktors funktioniert wahrscheinlich nicht, weil es nicht die gleiche Funktionssignatur ist. Du müsstest für sowas die ganze Klasse spezialisieren:</p>
<pre><code class="language-cpp">template&lt;typename T&gt;
class Test
{
public:
  Test(T const&amp; t); // &lt;- Normal
};

template&lt;&gt;
class Test&lt;void&gt;
{
public:
  Test(); // &lt;- Spezialisierung
};
</code></pre>
<p>Templates sind schliesslich nicht nur eine dumme Textersetzung. Bei deiner Spezialisierung erwartet der Kompiler dann immer noch einen Parameter, einfach vom Typ <code>void</code> , was natürlich nicht geht. Das gibt aber wahrscheinlich noch keinen Fehler, aber er meckert dann rum, dass es keinen Standardkonstruktor gibt.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1615926</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1615926</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Mon, 17 Nov 2008 12:42:29 GMT</pubDate></item><item><title><![CDATA[Reply to Partielle Template-Spezialisierung on Mon, 17 Nov 2008 13:31:05 GMT]]></title><description><![CDATA[<p>Hier der Compiler-Fehler, sorry, vergessen.</p>
<blockquote>
<p>c:\dokumente und einstellungen\chris\eigene dateien\visual studio 2005\projects\template_testing\template_testing\template_testing.cpp(77) : error C2244: 'AnotherTest&lt;_Base,_P&gt;::AnotherTest': Keine Übereinstimmung für Funktionsdefinition mit vorhandener Deklaration gefunden<br />
c:\dokumente und einstellungen\chris\eigene dateien\visual studio 2005\projects\template_testing\template_testing\template_testing.cpp(68): Siehe Deklaration von 'AnotherTest&lt;_Base,_P&gt;::AnotherTest'<br />
Definition<br />
'AnotherTest&lt;_B,void&gt;::AnotherTest(void)'<br />
Vorhandene Deklarationen<br />
'AnotherTest&lt;_Base,_P&gt;::AnotherTest(_P)'</p>
</blockquote>
<p>Mit vollständiger Spezialisierung funktioniert das ganze, nur halt mit der partiellen nicht.</p>
<p>Vollständige Spezialisierung folgt:</p>
<pre><code class="language-cpp">template&lt;class _Base, typename _P&gt;
class AnotherTest : public _Base
{
public:
	typedef _Base Parent;
	AnotherTest(_P);
};

/*
template&lt;class _B&gt;
AnotherTest&lt;_B, void&gt;::AnotherTest(void):
	Parent()
{
	std::cout &lt;&lt; &quot;base class: &quot; &lt;&lt; typeid(_B).name() &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;param: void&quot; &lt;&lt; std::endl;
}*/

template&lt;class _B, typename _X&gt;
AnotherTest&lt;_B, _X&gt;::AnotherTest(_X x):
	Parent(x)
{
	std::cout &lt;&lt; &quot;base class: &quot; &lt;&lt; typeid(_B).name() &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;param: &quot; &lt;&lt; typeid(_X).name() &lt;&lt; x &lt;&lt; std::endl;
}

class Foo
{
public:
	Foo(){}
};

template&lt;&gt;
AnotherTest&lt;Foo, void&gt;::AnotherTest(void):
	Parent()
{
	std::cout &lt;&lt; &quot;base class: &quot; &lt;&lt; typeid(Foo).name() &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;param: void&quot; &lt;&lt; std::endl;
}

class Bar
{
	int i;
public:
	Bar(int _i):i(_i){}
};

void atest()
{
	AnotherTest&lt;Foo, void&gt;	vATest;
	AnotherTest&lt;Bar, int&gt;	vBTest(4);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1615946</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1615946</guid><dc:creator><![CDATA[Kurisu]]></dc:creator><pubDate>Mon, 17 Nov 2008 13:31:05 GMT</pubDate></item><item><title><![CDATA[Reply to Partielle Template-Spezialisierung on Mon, 17 Nov 2008 13:31:31 GMT]]></title><description><![CDATA[<p>Kurisu schrieb:</p>
<blockquote>
<p>Hier der Compiler-Fehler, sorry, vergessen.</p>
<blockquote>
<p>c:\dokumente und einstellungen\chris\eigene dateien\visual studio 2005\projects\template_testing\template_testing\template_testing.cpp(77) : error C2244: 'AnotherTest&lt;_Base,_P&gt;::AnotherTest': Keine Übereinstimmung für Funktionsdefinition mit vorhandener Deklaration gefunden<br />
c:\dokumente und einstellungen\chris\eigene dateien\visual studio 2005\projects\template_testing\template_testing\template_testing.cpp(68): Siehe Deklaration von 'AnotherTest&lt;_Base,_P&gt;::AnotherTest'<br />
Definition<br />
'AnotherTest&lt;_B,void&gt;::AnotherTest(void)'<br />
Vorhandene Deklarationen<br />
'AnotherTest&lt;_Base,_P&gt;::AnotherTest(_P)'</p>
</blockquote>
<p>Mit vollständiger Spezialisierung funktioniert das ganze, nur halt mit der partiellen nicht.</p>
</blockquote>
<p>Das ist es ja.. Das geht einfach nicht. <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>
<p><a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-128361.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-t-is-128361.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1615948</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1615948</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Mon, 17 Nov 2008 13:31:31 GMT</pubDate></item><item><title><![CDATA[Reply to Partielle Template-Spezialisierung on Mon, 17 Nov 2008 14:00:23 GMT]]></title><description><![CDATA[<p>Ich hab nicht wirklich Plan, will aber meinen Senf dazugeben. Lautet die Antwort Element-Spezialisierung?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1615971</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1615971</guid><dc:creator><![CDATA[Planlos1]]></dc:creator><pubDate>Mon, 17 Nov 2008 14:00:23 GMT</pubDate></item></channel></rss>