<?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[[Solved] Funktion die sowohl int als auch string zurückgeben kann]]></title><description><![CDATA[<p>Meine Funktion sieht im Moment ungefähr so aus: (Unwichtige Teile habe ich ausgelassen)</p>
<pre><code>template &lt;class T&gt;
T funktion()
{
	T ret;
	string strret;
	bool str = typeid(T) == typeid(string);

	//...

	return (str ? strret : ret);
}

funktion&lt;int&gt;();
</code></pre>
<p>Der Fehler ist, dass er &quot;strret&quot; nicht in int convertieren kann.</p>
<p>Warum der compiler fehler macht, ist mir klar, da er zur compile-zeit nicht wissen kann, welchen Wert str hat.</p>
<p>Wie kann ich das machen, ohne das der compiler rummurrt?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/296204/solved-funktion-die-sowohl-int-als-auch-string-zurückgeben-kann</link><generator>RSS for Node</generator><lastBuildDate>Sat, 15 Aug 2026 02:53:29 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/296204.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 29 Nov 2011 23:56:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Solved] Funktion die sowohl int als auch string zurückgeben kann on Wed, 30 Nov 2011 12:26:17 GMT]]></title><description><![CDATA[<p>Meine Funktion sieht im Moment ungefähr so aus: (Unwichtige Teile habe ich ausgelassen)</p>
<pre><code>template &lt;class T&gt;
T funktion()
{
	T ret;
	string strret;
	bool str = typeid(T) == typeid(string);

	//...

	return (str ? strret : ret);
}

funktion&lt;int&gt;();
</code></pre>
<p>Der Fehler ist, dass er &quot;strret&quot; nicht in int convertieren kann.</p>
<p>Warum der compiler fehler macht, ist mir klar, da er zur compile-zeit nicht wissen kann, welchen Wert str hat.</p>
<p>Wie kann ich das machen, ohne das der compiler rummurrt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150788</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150788</guid><dc:creator><![CDATA[Xandaros]]></dc:creator><pubDate>Wed, 30 Nov 2011 12:26:17 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Funktion die sowohl int als auch string zurückgeben kann on Wed, 30 Nov 2011 00:01:16 GMT]]></title><description><![CDATA[<p>Spezialisierung für String?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150789</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150789</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 30 Nov 2011 00:01:16 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Funktion die sowohl int als auch string zurückgeben kann on Wed, 30 Nov 2011 00:58:36 GMT]]></title><description><![CDATA[<p>Wieso nicht</p>
<pre><code class="language-cpp">template &lt;class T&gt;
T funktion()
{
  return (typeid(T) == typeid(string));
}

funktion&lt;int&gt;();
</code></pre>
<p>und in der Überfunktion das machen was du willst?</p>
<p>Ansonsten müsstest du string zu T casten können müssen dürfen... :xmas2:</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150811</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150811</guid><dc:creator><![CDATA[itwasntpete]]></dc:creator><pubDate>Wed, 30 Nov 2011 00:58:36 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Funktion die sowohl int als auch string zurückgeben kann on Wed, 30 Nov 2011 01:51:21 GMT]]></title><description><![CDATA[<p>Als direkt reinpatchbare Lösung fällt mir Folgendes ein:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;

template&lt;typename T&gt; T select_return_value(T ret, std::string const &amp;) { return ret; }
inline std::string const &amp;select_return_value(std::string const &amp;, std::string const &amp;strret) { return strret; }

template&lt;typename T&gt;
T funktionsvorlage() {
  T ret = T();
  std::string strret = &quot;foo&quot;;

  return select_return_value(ret, strret);
}

int main() {
  std::cout &lt;&lt; funktionsvorlage&lt;int&gt;()         &lt;&lt; '\n'
            &lt;&lt; funktionsvorlage&lt;std::string&gt;() &lt;&lt; '\n';
}
</code></pre>
<p>...aber du solltest SeppJs Vorschlag ernst nehmen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150814</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150814</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Wed, 30 Nov 2011 01:51:21 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Funktion die sowohl int als auch string zurückgeben kann on Wed, 30 Nov 2011 05:25:29 GMT]]></title><description><![CDATA[<p>Xandaros schrieb:</p>
<blockquote>
<p>Meine Funktion sieht im Moment ungefähr so aus: (Unwichtige Teile habe ich ausgelassen)</p>
<pre><code>template &lt;class T&gt;
T funktion()
{
	T ret;
	string strret;
	bool str = typeid(T) == typeid(string);

	//...

	return (str ? strret : ret);
}

funktion&lt;int&gt;();
</code></pre>
<p>Der Fehler ist, dass er &quot;strret&quot; nicht in int convertieren kann.</p>
<p>Warum der compiler fehler macht, ist mir klar, da er zur compile-zeit nicht wissen kann, welchen Wert str hat.</p>
<p>Wie kann ich das machen, ohne das der compiler rummurrt?</p>
</blockquote>
<p>Kann mir einer erklären, was das machen soll und wieso ihr Lösungen dazu habt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150823</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150823</guid><dc:creator><![CDATA[was???]]></dc:creator><pubDate>Wed, 30 Nov 2011 05:25:29 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Funktion die sowohl int als auch string zurückgeben kann on Wed, 30 Nov 2011 08:13:17 GMT]]></title><description><![CDATA[<blockquote>
<p>Funktion die sowohl int als auch string zurückgeben kann</p>
</blockquote>
<p>Klar gibt es Loesungen. Dein eigentliches Problem liegt aber wahrscheinlich im Design.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150859</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150859</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Wed, 30 Nov 2011 08:13:17 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Funktion die sowohl int als auch string zurückgeben kann on Wed, 30 Nov 2011 09:36:24 GMT]]></title><description><![CDATA[<p>Das ist nichts soooo ungewöhnliches. Er programmiert Templatespezialisierung von Hand. Daher, wie schon gesagt: Benutz Templatespezialisierung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150900</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150900</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 30 Nov 2011 09:36:24 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Funktion die sowohl int als auch string zurückgeben kann on Wed, 30 Nov 2011 10:44:03 GMT]]></title><description><![CDATA[<p>funktion überladen wäre auch möglich, oder versuch eine auto funktion <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
auto func(void){</p>
<p>}<br />
wird wahrscheinlich nicht funktionieren die auto funktion.</p>
<pre><code class="language-cpp">string func(void);
int func(void);

string func(void){
 return &quot;String&quot;;
}

int func(void){
return 10;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2150946</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150946</guid><dc:creator><![CDATA[buntehaare]]></dc:creator><pubDate>Wed, 30 Nov 2011 10:44:03 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Funktion die sowohl int als auch string zurückgeben kann on Wed, 30 Nov 2011 10:53:19 GMT]]></title><description><![CDATA[<p>buntehaare schrieb:</p>
<blockquote>
<p>funktion überladen wäre auch möglich</p>
</blockquote>
<p>Der return-Typ gehört nicht zur Signatur, demnach kann man eine Funktion nicht über den return-Typ überladen. Dein Beispiel kompiliert folglich nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150956</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150956</guid><dc:creator><![CDATA[grauehaare]]></dc:creator><pubDate>Wed, 30 Nov 2011 10:53:19 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Funktion die sowohl int als auch string zurückgeben kann on Wed, 30 Nov 2011 11:14:11 GMT]]></title><description><![CDATA[<p>Templatespezialisierung hört sich an, als könnte ich es gebrauchen.<br />
Was mir daran allerdings missfällt: Ich hab dann haufenweise Code doppelt.</p>
<p>Ich hab zwar einige Code-abschnitte speziell für strings, der Rest gilt allerdings für alle erdenklichen typen...</p>
<p>Mein Problem ist somit zwar gelöst, gibt es aber noch eine Möglichkeit, die &quot;hübscher&quot; ist?</p>
<p>(Ich werde das mit der Spezialisierung jetzt erst einmal implementieren, davon wusste ich nichts)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150960</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150960</guid><dc:creator><![CDATA[Xandaros]]></dc:creator><pubDate>Wed, 30 Nov 2011 11:14:11 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Funktion die sowohl int als auch string zurückgeben kann on Wed, 30 Nov 2011 11:46:18 GMT]]></title><description><![CDATA[<p>Xandaros schrieb:</p>
<blockquote>
<p>Was mir daran allerdings missfällt: Ich hab dann haufenweise Code doppelt.</p>
<p>Ich hab zwar einige Code-abschnitte speziell für strings, der Rest gilt allerdings für alle erdenklichen typen...</p>
</blockquote>
<p>Dann musst du halt deinen Code aufteilen (in Klassen/andere Funktionen), so dass du in der Spezialisierung allgemeinen Code nutzen kannst. Wie du das am besten machst, kann man ohne genauerer Kenntnisse deines Codes nicht sagen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150974</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150974</guid><dc:creator><![CDATA[grauehaare]]></dc:creator><pubDate>Wed, 30 Nov 2011 11:46:18 GMT</pubDate></item></channel></rss>