<?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[C++ Funktions-Templates - andere Konstante in Implementierung möglich?]]></title><description><![CDATA[<p>Hallo,</p>
<p>kann ich für folgendes &quot;Problem&quot; C++ Templates verwenden?</p>
<p>Ich habe eine Funktion Ai und eine Funktion As, die beide sehr ähnlich sind. Sie unterscheiden sich nur in Datentypen und einer Konstante voneinander. Der Code von As ist also quasi ein Copy&amp;Paste von Ai mit leichten Modifikationen.</p>
<p>Beispiel (fiktiv):</p>
<pre><code class="language-cpp">unsigned int Ai(unsigned int x, unsigned int y)
{
  if(x &lt; 100)
    return x;
  else
    return y;
}
</code></pre>
<pre><code class="language-cpp">unsigned short As(unsigned short x, unsigned short y)
{
  if(x &lt; 50)
    return x;
  else
    return y;
}
</code></pre>
<p>Kann man das mit Templates lösen? Wie bekomme ich die Konstante 100 bzw. 50 in abhägigkeit des Datentyps?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/270782/c-funktions-templates-andere-konstante-in-implementierung-möglich</link><generator>RSS for Node</generator><lastBuildDate>Sat, 29 Aug 2026 23:10:11 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/270782.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 18 Jul 2010 09:22:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C++ Funktions-Templates - andere Konstante in Implementierung möglich? on Sun, 18 Jul 2010 09:22:49 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>kann ich für folgendes &quot;Problem&quot; C++ Templates verwenden?</p>
<p>Ich habe eine Funktion Ai und eine Funktion As, die beide sehr ähnlich sind. Sie unterscheiden sich nur in Datentypen und einer Konstante voneinander. Der Code von As ist also quasi ein Copy&amp;Paste von Ai mit leichten Modifikationen.</p>
<p>Beispiel (fiktiv):</p>
<pre><code class="language-cpp">unsigned int Ai(unsigned int x, unsigned int y)
{
  if(x &lt; 100)
    return x;
  else
    return y;
}
</code></pre>
<pre><code class="language-cpp">unsigned short As(unsigned short x, unsigned short y)
{
  if(x &lt; 50)
    return x;
  else
    return y;
}
</code></pre>
<p>Kann man das mit Templates lösen? Wie bekomme ich die Konstante 100 bzw. 50 in abhägigkeit des Datentyps?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1927967</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1927967</guid><dc:creator><![CDATA[stefan-tiger]]></dc:creator><pubDate>Sun, 18 Jul 2010 09:22:49 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Funktions-Templates - andere Konstante in Implementierung möglich? on Sun, 18 Jul 2010 09:37:01 GMT]]></title><description><![CDATA[<p>Das ist z.B. über Hilfsstrukturen möglich (wenn die Konstante wirklich nur vom Datentyp abhängt):</p>
<pre><code class="language-cpp">template&lt;class T&gt;
struct constholder  // oder irgendein anderer name
{
};

// Spezialisierung für unsigned int
template&lt;&gt;
struct constholder&lt;unsigned int&gt;
{
    static const unsigned int value = 100;
};

// Spezialisierung für unsigned short
template&lt;&gt;
struct constholder&lt;unsigned short&gt;
{
    static const unsigned short value = 50;
};

// weitere Spezialisierungen

template&lt;class T&gt;
T A(T x, T y)
{
  if(x &lt; constholder&lt;T&gt;::value)
    return x;
  else
    return y;
}
</code></pre>
<p>Das hat noch den hübschen Nebeneffekt, dass die Funktion nur mit Typen benutzt werden kann, für die eine Spezialisierung von <code>constholder&lt;T&gt;</code> existiert (weil es <code>value</code> in der allgemeinen Struktur nicht gibt).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1927972</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1927972</guid><dc:creator><![CDATA[ipsec]]></dc:creator><pubDate>Sun, 18 Jul 2010 09:37:01 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Funktions-Templates - andere Konstante in Implementierung möglich? on Sun, 18 Jul 2010 09:37:02 GMT]]></title><description><![CDATA[<p>Vielleicht so?</p>
<pre><code class="language-cpp">template &lt;typename Ty_, std::size_t constant&gt;
Ty_ AT(Ty_ x, Ty_ y) {
   if(x &lt; constant)
      return x;
   else
      return y;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1927973</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1927973</guid><dc:creator><![CDATA[Ad aCTa]]></dc:creator><pubDate>Sun, 18 Jul 2010 09:37:02 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Funktions-Templates - andere Konstante in Implementierung möglich? on Sun, 18 Jul 2010 09:38:11 GMT]]></title><description><![CDATA[<p>hi</p>
<pre><code class="language-cpp">template&lt;int x, class T&gt;
T Ax(T val1, T val2)
{
   return (val1 &lt; x) ? val1 : va2;
}

Ax&lt;50,unsigned short&gt;(30,10);
</code></pre>
<p>ungetestet</p>
<p>Meep Meep</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1927974</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1927974</guid><dc:creator><![CDATA[Meep Meep]]></dc:creator><pubDate>Sun, 18 Jul 2010 09:38:11 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Funktions-Templates - andere Konstante in Implementierung möglich? on Sun, 18 Jul 2010 09:39:27 GMT]]></title><description><![CDATA[<p>Das könntest du mit Spezialisierung und einer Hilfsfunktion lösen.</p>
<p>(nicht getestet)</p>
<pre><code class="language-cpp">template&lt;class T, int count&gt;
T A_ ( T x, T y)
{
  if(x &lt; count)
    return x;
  else
    return y;
}

template&lt;class T&gt;
T A (T x, T y) {}

template&lt;&gt;
unsigned int A&lt;unsigned int&gt; (unsigned int x, unsigned int y) { A_&lt;unsigned int,100&gt; (x, y);}

template&lt;&gt;
unsigned short A&lt;unsigned shor&gt; (unsigned short x, unsigned short y) { A_&lt;unsigned short&gt;, 50&gt; (x, y);}
</code></pre>
<p>Wenn du natürlich den Wert angeben kannst/willst dann gehts auch einfacher. Oder man übergibt den Wert auch gleich dynamisch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1927975</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1927975</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sun, 18 Jul 2010 09:39:27 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Funktions-Templates - andere Konstante in Implementierung möglich? on Sun, 18 Jul 2010 09:45:57 GMT]]></title><description><![CDATA[<p>Danke für eure Vorschläge.</p>
<p>Wichtig ist, dass diese Kosntante nur innerhalb der Implementierung bleibt.</p>
<p>Sprich ein Benutzer ruft Ai oder As auf und weiss garnichts davon dass darin eine 100 oder 50 gebraucht wird. Das soll also beim Aufruf also auch nicht &quot;übergeben&quot; werden müssen.</p>
<p>Der Vorschlag von ipsec ist dann warscheinlich das zutreffendste.<br />
In der Tat soll es nur die Implementirungen geben für dessen Datentypen auch eine Konstante angegeben wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1927979</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1927979</guid><dc:creator><![CDATA[stefan-tiger]]></dc:creator><pubDate>Sun, 18 Jul 2010 09:45:57 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Funktions-Templates - andere Konstante in Implementierung möglich? on Sun, 18 Jul 2010 09:58:37 GMT]]></title><description><![CDATA[<p>stefan-tiger  schrieb:</p>
<blockquote>
<p>Kann man das mit Templates lösen? Wie bekomme ich die Konstante 100 bzw. 50 in abhägigkeit des Datentyps?</p>
</blockquote>
<p>hattest ja auch noch geschrieben gehabt.<br />
tjo wer lesen kann is klar im vorteil <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
<p>wuerde also mit meinem beispiel so aussehen:</p>
<pre><code class="language-cpp">template&lt;class T&gt; struct constant { };
template&lt;&gt; struct constant&lt;unsigned short&gt; { static const unsigned short val = 50; };
template&lt;&gt; struct constant&lt;unsigned int&gt; { static const unsigned short val = 100; };

template&lt;class T&gt;
T Ax(T val1, T val2)
{
   return (val1 &lt; constant&lt;T&gt;::val) ? val1 : val2;
}
</code></pre>
<p>Meep Meep</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1927982</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1927982</guid><dc:creator><![CDATA[Meep Meep]]></dc:creator><pubDate>Sun, 18 Jul 2010 09:58:37 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Funktions-Templates - andere Konstante in Implementierung möglich? on Sun, 18 Jul 2010 14:17:57 GMT]]></title><description><![CDATA[<p>Ich glaube mit Templates geht das doch nicht so wie gewünscht.</p>
<p>So weit ich weiss muss ich dann die ganze Implementierung im Quellecode mitgeben und diese wird dann inkludiert. Es werden keine dedizierten Objekt-Dateien erzeugt.</p>
<p>Templates scheinen mir auch etwas zu unflexibel.<br />
Beispielsweise will ich für alle integer Datentypen für unsigned und signed sowie für float/double eine andere Implementierung. Auch bestimmte Kombinationen haben bestimmte Implementierungen.</p>
<p>Ich denke wenn ich mir selbst einen Code-Generator schreibe, der dann cpp Dateien mit ausformulierten Implementierungen ohne Templates erzeugt, ist das für mich besser.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1928101</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1928101</guid><dc:creator><![CDATA[stefan-tiger]]></dc:creator><pubDate>Sun, 18 Jul 2010 14:17:57 GMT</pubDate></item></channel></rss>