<?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[Problem mit templates]]></title><description><![CDATA[<p>Folgende Situation</p>
<p>Ich habe zwei Funktionen f, g mit derselben Signatur. Ich will nun bewerkstelligen, dass ich mit templates die eine oder die andere aufrufen kann:</p>
<pre><code class="language-cpp">template &lt;????&gt;
void foo()
{
  // hier soll abhängig von ???? f oder g aufgerufen werden
}
</code></pre>
<p>Wie mache ich das?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/275904/problem-mit-templates</link><generator>RSS for Node</generator><lastBuildDate>Wed, 26 Aug 2026 14:51:34 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/275904.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 22 Oct 2010 20:12:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit templates on Fri, 22 Oct 2010 20:12:33 GMT]]></title><description><![CDATA[<p>Folgende Situation</p>
<p>Ich habe zwei Funktionen f, g mit derselben Signatur. Ich will nun bewerkstelligen, dass ich mit templates die eine oder die andere aufrufen kann:</p>
<pre><code class="language-cpp">template &lt;????&gt;
void foo()
{
  // hier soll abhängig von ???? f oder g aufgerufen werden
}
</code></pre>
<p>Wie mache ich das?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1969290</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1969290</guid><dc:creator><![CDATA[guuuuuuuuuu]]></dc:creator><pubDate>Fri, 22 Oct 2010 20:12:33 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit templates on Fri, 22 Oct 2010 20:21:00 GMT]]></title><description><![CDATA[<p>Eine mögliche Variante:</p>
<pre><code class="language-cpp">struct call_f_tag {};
struct call_g_tag {};
template&lt;&gt; void foo&lt;call_f_tag&gt;() { f(); }
template&lt;&gt; void foo&lt;call_g_tag&gt;() { g(); }

// Aufruf:
foo&lt;call_f_tag&gt;();
foo&lt;call_g_tag&gt;();
</code></pre>
<p>(ungetestet)</p>
<p>Die <a href="http://www.sgi.com/tech/stl/iterator_tags.html" rel="nofollow">iterator_tags</a> funktionieren auf ähnliche Art und Weise.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1969292</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1969292</guid><dc:creator><![CDATA[templater]]></dc:creator><pubDate>Fri, 22 Oct 2010 20:21:00 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit templates on Fri, 22 Oct 2010 20:24:52 GMT]]></title><description><![CDATA[<p>Geht es auch so, dass ich die Funktion foo nur einmal definiere?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1969295</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1969295</guid><dc:creator><![CDATA[guuuuuuuuuu]]></dc:creator><pubDate>Fri, 22 Oct 2010 20:24:52 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit templates on Fri, 22 Oct 2010 20:58:59 GMT]]></title><description><![CDATA[<p>Ich würde die Funktionen als Funktoren anlegen, dann ist das recht einfach:</p>
<pre><code class="language-cpp">class f
{
    void operator() (void)    
    {
    /*
     *
     */
    }
};

class g
{
    void operator() (void)
    {
    /*
     *
     */
    }
};

template&lt;typename T&gt;
void foo()
{
    T funk;
    funk();
}

// Aufruf:
foo&lt;g&gt;();
foo&lt;f&gt;();
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1969296</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1969296</guid><dc:creator><![CDATA[NEO.PIXEL]]></dc:creator><pubDate>Fri, 22 Oct 2010 20:58:59 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit templates on Fri, 22 Oct 2010 20:39:23 GMT]]></title><description><![CDATA[<p>Warum soll es denn ein template sein? Übergib doch einen Funktionszeiger. Oder einen bool &quot;call_f&quot;.</p>
<pre><code class="language-cpp">void foo(bool call_f) {
  if(call_f) f();
  else g();
}
</code></pre>
<p>Das ganze geht auch als template <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_tongue"
      title=":P"
      alt="😛"
    /></p>
<pre><code class="language-cpp">template &lt;bool call_f&gt;
void foo() {
  if(call_f) f();
  else g();
}
</code></pre>
<p>Ansonsten ist Spezialisierung (wie von templater gezeigt) ein üblicher Weg.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1969300</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1969300</guid><dc:creator><![CDATA[l&#x27;abra d&#x27;or]]></dc:creator><pubDate>Fri, 22 Oct 2010 20:39:23 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit templates on Fri, 22 Oct 2010 22:34:26 GMT]]></title><description><![CDATA[<p>Man kann auch Funktionszeiger oder -referenzen als Template-Parameter verwenden.<br />
Ich ziehe allerdings die Funktor-Variante vor, wenn sonst nichts dagegen spricht.</p>
<p>p.S.:<br />
Die &quot;tag&quot; Variante von &quot;templater&quot; macht IMO Sinn, wenn man das Template &quot;zumachen&quot; will, also dem User die Möglichkeit nehmen jeden beliebigen Funktor/Funktionszeiger zu übergeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1969324</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1969324</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Fri, 22 Oct 2010 22:34:26 GMT</pubDate></item></channel></rss>