<?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[VS 2010 Template Spezialisierung]]></title><description><![CDATA[<pre><code>template&lt;bool b, typename T&gt; int GetElementSize2()
{
	return -1;
}
template&lt;&gt; int GetElementSize2&lt;true, int&gt;()
{
	return sizeof(int);
}
template&lt;typename T&gt; int GetElementSize2&lt;true,T&gt;()
{
	return sizeof(T);
}
</code></pre>
<p>Bin nach einer Stunde googlen eigentlich der Meinung, dass es so (oder zumindest ganz aehnlich), funktionieren muesste. Zum Beispiel wie hier beim Dictionary&lt;int, Value&gt; <a href="http://msdn.microsoft.com/de-de/library/vstudio/3967w96f.aspx" rel="nofollow">http://msdn.microsoft.com/de-de/library/vstudio/3967w96f.aspx</a></p>
<p>Beim dritten wird trotzdem immer der Fehler &quot;error C2768: 'GetElementSize2' : illegal use of explicit template arguments&quot; geworfen. Hat jemand einen Tipp, was ich aendern koennte um die gewuenschte Funktionalitaet zu erreichen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/315698/vs-2010-template-spezialisierung</link><generator>RSS for Node</generator><lastBuildDate>Fri, 31 Jul 2026 03:31:29 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/315698.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 09 Apr 2013 16:35:54 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to VS 2010 Template Spezialisierung on Tue, 09 Apr 2013 16:35:54 GMT]]></title><description><![CDATA[<pre><code>template&lt;bool b, typename T&gt; int GetElementSize2()
{
	return -1;
}
template&lt;&gt; int GetElementSize2&lt;true, int&gt;()
{
	return sizeof(int);
}
template&lt;typename T&gt; int GetElementSize2&lt;true,T&gt;()
{
	return sizeof(T);
}
</code></pre>
<p>Bin nach einer Stunde googlen eigentlich der Meinung, dass es so (oder zumindest ganz aehnlich), funktionieren muesste. Zum Beispiel wie hier beim Dictionary&lt;int, Value&gt; <a href="http://msdn.microsoft.com/de-de/library/vstudio/3967w96f.aspx" rel="nofollow">http://msdn.microsoft.com/de-de/library/vstudio/3967w96f.aspx</a></p>
<p>Beim dritten wird trotzdem immer der Fehler &quot;error C2768: 'GetElementSize2' : illegal use of explicit template arguments&quot; geworfen. Hat jemand einen Tipp, was ich aendern koennte um die gewuenschte Funktionalitaet zu erreichen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2314170</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2314170</guid><dc:creator><![CDATA[TGGC]]></dc:creator><pubDate>Tue, 09 Apr 2013 16:35:54 GMT</pubDate></item><item><title><![CDATA[Reply to VS 2010 Template Spezialisierung on Tue, 09 Apr 2013 16:53:51 GMT]]></title><description><![CDATA[<p>Es gibt keine partielle Spezialisierung von Funktionen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2314179</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2314179</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Tue, 09 Apr 2013 16:53:51 GMT</pubDate></item><item><title><![CDATA[Reply to VS 2010 Template Spezialisierung on Tue, 09 Apr 2013 17:30:54 GMT]]></title><description><![CDATA[<p>Es gibt aber Workarounds. Im Speziellen</p>
<pre><code class="language-cpp">template&lt;bool b, typename T&gt; int GetElementSize2()
{
    return b ? sizeof(T) : -1;
}
</code></pre>
<p>etwas allgemeiner (C++11)</p>
<pre><code class="language-cpp">template&lt;bool b, typename T&gt; int GetElementSize2()
{
    return GetElementSize2&lt;T&gt;(std::integral_constant&lt;b&gt;{});
}
template&lt;typename T&gt; int GetElementSize2(std::true_type)
{
    return sizeof(T);
}
template&lt;typename T&gt; int GetElementSize2(std::false_type)
{
    return -1;
}
</code></pre>
<p>oder ganz die Funktion in eine &quot;Partielle Spezialisierung von einer Klassenvorlage&quot; umwandeln.</p>
<pre><code class="language-cpp">template&lt;bool b, typename T&gt; int GetElementSize2()
{
    return GetElementSize2_klassenvorlage&lt;b, T&gt;::get();
}
template&lt;bool b, typename T&gt;
struct GetElementSize2_klassenvorlage {
  int get() { return -1; }
};
template&lt;&gt; struct GetElementSize2_klassenvorlage&lt;true, int&gt; {
  int get() { return sizeof(int); }
};
template&lt;typename T&gt; struct GetElementSize2_klassenvorlage&lt;true,T&gt;() {
  int get() { return sizeof(T); }
};
</code></pre>
<p>(alle drei ungetestet)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2314194</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2314194</guid><dc:creator><![CDATA[wörki]]></dc:creator><pubDate>Tue, 09 Apr 2013 17:30:54 GMT</pubDate></item><item><title><![CDATA[Reply to VS 2010 Template Spezialisierung on Tue, 09 Apr 2013 17:31:57 GMT]]></title><description><![CDATA[<p>Edit: muss natürlich <code>integral_constant&lt;bool, b&gt;</code> heissen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2314195</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2314195</guid><dc:creator><![CDATA[wörki]]></dc:creator><pubDate>Tue, 09 Apr 2013 17:31:57 GMT</pubDate></item><item><title><![CDATA[Reply to VS 2010 Template Spezialisierung on Tue, 09 Apr 2013 17:45:08 GMT]]></title><description><![CDATA[<p>Danke fuer die Informationen und die Ideen. Ich denke mit mindestens einem davon werde ich es hinbekommen!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2314208</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2314208</guid><dc:creator><![CDATA[TGGC]]></dc:creator><pubDate>Tue, 09 Apr 2013 17:45:08 GMT</pubDate></item><item><title><![CDATA[Reply to VS 2010 Template Spezialisierung on Tue, 09 Apr 2013 18:04:15 GMT]]></title><description><![CDATA[<p>wörki schrieb:</p>
<blockquote>
<pre><code class="language-cpp">template&lt;bool b, typename T&gt; int GetElementSize2()
{
    return GetElementSize2_klassenvorlage&lt;b, T&gt;::get();
}
template&lt;bool b, typename T&gt;
struct GetElementSize2_klassenvorlage {
  int get() { return -1; }
};
template&lt;&gt; struct GetElementSize2_klassenvorlage&lt;true, int&gt; {
  int get() { return sizeof(int); }
};
template&lt;typename T&gt; struct GetElementSize2_klassenvorlage&lt;true,T&gt;() {
  int get() { return sizeof(T); }
};
</code></pre>
</blockquote>
<p>Besser:</p>
<pre><code>template&lt;bool b, typename T&gt;
struct GetElementSize2_klassenvorlage {
  static constexpr int value = -1;
};
</code></pre>
<p>Warum statische Ausdrücke dynamisch machen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2314227</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2314227</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Tue, 09 Apr 2013 18:04:15 GMT</pubDate></item><item><title><![CDATA[Reply to VS 2010 Template Spezialisierung on Tue, 09 Apr 2013 18:15:46 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>Warum statische Ausdrücke dynamisch machen?</p>
</blockquote>
<p>Das static habe ich vergessen (das <code>::get()</code> verrät das).</p>
<p>Der Code ist so, wie er ist, weil es hier um den allgemeinen Fall geht. Wenn das Resultat <code>constexpr</code> sein soll, kann man das (IMHO immer) schon mit einem Ansatz ähnlich zu <code>b ? sizeof(T) : -1;</code> erschlagen und braucht keine zusätzlichen Funktionen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2314233</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2314233</guid><dc:creator><![CDATA[wörki]]></dc:creator><pubDate>Tue, 09 Apr 2013 18:15:46 GMT</pubDate></item></channel></rss>