<?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[Unterschiedliche Funktionsaufrufe je typedef]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich würde gerne wissen, ob es möglich ist, je nach typedef eine andere<br />
Funktion aufrufen zu lassen. Zur Verdeutlichung ein kurzes Code-Fragment:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

typedef unsigned int my_int;

void doItNow(unsigned int i){
	std::cout &lt;&lt; &quot;unsigned int&quot; &lt;&lt; std::endl;
}

void doItNow(my_int i){
	std::cout &lt;&lt; &quot;my_int&quot; &lt;&lt; std::endl;
}

template &lt;typename T&gt; void doIt(T t) {
	doItNow(t);
}

int main() {
	unsigned int i = 0;
	doIt(i);
	my_int i2 = 0;
	doIt(i2);
	return 0;
}
</code></pre>
<p>Leider kompiliert er nicht:</p>
<pre><code class="language-cpp">../src/Test.cpp: In function 'void doItNow(my_int)':
../src/Test.cpp:18: error: redefinition of 'void doItNow(my_int)'
../src/Test.cpp:14: error: 'void doItNow(unsigned int)' previously defined here
</code></pre>
<p>Eine Alternative zu dem geposteten Code, die mir bekannt ist, wäre:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

typedef unsigned int my_int;

void doItNow1(unsigned int i){
	std::cout &lt;&lt; &quot;unsigned int&quot; &lt;&lt; std::endl;
}

void doItNow2(my_int i){
	std::cout &lt;&lt; &quot;my_int&quot; &lt;&lt; std::endl;
}

template &lt;typename T&gt; void doIt(T t, void (*f)(T)) {
	f(t);
}

int main() {
	unsigned int i = 0;
	doIt(i, &amp;doItNow1);
	my_int i2 = 0;
	doIt(i2, &amp;doItNow2);
	return 0;
}
</code></pre>
<p>Geht sowas denn auch ohne den Funktionszeiger?</p>
<p>Gruß,<br />
CSpille</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/242764/unterschiedliche-funktionsaufrufe-je-typedef</link><generator>RSS for Node</generator><lastBuildDate>Sun, 20 Sep 2026 20:21:21 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/242764.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 08 Jun 2009 02:33:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Unterschiedliche Funktionsaufrufe je typedef on Mon, 08 Jun 2009 02:34:37 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich würde gerne wissen, ob es möglich ist, je nach typedef eine andere<br />
Funktion aufrufen zu lassen. Zur Verdeutlichung ein kurzes Code-Fragment:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

typedef unsigned int my_int;

void doItNow(unsigned int i){
	std::cout &lt;&lt; &quot;unsigned int&quot; &lt;&lt; std::endl;
}

void doItNow(my_int i){
	std::cout &lt;&lt; &quot;my_int&quot; &lt;&lt; std::endl;
}

template &lt;typename T&gt; void doIt(T t) {
	doItNow(t);
}

int main() {
	unsigned int i = 0;
	doIt(i);
	my_int i2 = 0;
	doIt(i2);
	return 0;
}
</code></pre>
<p>Leider kompiliert er nicht:</p>
<pre><code class="language-cpp">../src/Test.cpp: In function 'void doItNow(my_int)':
../src/Test.cpp:18: error: redefinition of 'void doItNow(my_int)'
../src/Test.cpp:14: error: 'void doItNow(unsigned int)' previously defined here
</code></pre>
<p>Eine Alternative zu dem geposteten Code, die mir bekannt ist, wäre:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

typedef unsigned int my_int;

void doItNow1(unsigned int i){
	std::cout &lt;&lt; &quot;unsigned int&quot; &lt;&lt; std::endl;
}

void doItNow2(my_int i){
	std::cout &lt;&lt; &quot;my_int&quot; &lt;&lt; std::endl;
}

template &lt;typename T&gt; void doIt(T t, void (*f)(T)) {
	f(t);
}

int main() {
	unsigned int i = 0;
	doIt(i, &amp;doItNow1);
	my_int i2 = 0;
	doIt(i2, &amp;doItNow2);
	return 0;
}
</code></pre>
<p>Geht sowas denn auch ohne den Funktionszeiger?</p>
<p>Gruß,<br />
CSpille</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1723042</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1723042</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 08 Jun 2009 02:34:37 GMT</pubDate></item><item><title><![CDATA[Reply to Unterschiedliche Funktionsaufrufe je typedef on Mon, 08 Jun 2009 02:40:02 GMT]]></title><description><![CDATA[<p>nein. nach der typegedefte typ kann vom originaltyp nicht unterschieden werden. typedef macht sozusagen nur ein schreibvereinfachungsmakro für den programmierer.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1723043</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1723043</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Mon, 08 Jun 2009 02:40:02 GMT</pubDate></item><item><title><![CDATA[Reply to Unterschiedliche Funktionsaufrufe je typedef on Mon, 08 Jun 2009 03:02:20 GMT]]></title><description><![CDATA[<p>Danke für die kurze präzise Antwort!!!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1723044</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1723044</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 08 Jun 2009 03:02:20 GMT</pubDate></item><item><title><![CDATA[Reply to Unterschiedliche Funktionsaufrufe je typedef on Mon, 08 Jun 2009 08:49:42 GMT]]></title><description><![CDATA[<blockquote>
<p>Geht sowas denn auch ohne den Funktionszeiger?</p>
</blockquote>
<p>Klar. Da gibt es sehr viele Möglichkeiten. Du könntest z.B eine Fallunterscheidung innerhalb der Funktion machen und dann einfach z.B einen Integer übergeben, anstatt des Funktionszeigers.<br />
Du könntest aber auch einen Wrapper für einen der Typen schreiben, dann kannst du das wieder zur Kompilierzeit machen.<br />
Oder du benutzt dennoch ein template, aber anstatt den Typen implizit zu übernehmen, gibst du da einen Integer für das template an, damit er weiss, welche spezialisierte Funktion er nehmen soll.</p>
<pre><code class="language-cpp">template &lt;int which&gt;
void doIt ( int i )
{
 //...
}

template &lt;&gt;
void doIt&lt;0&gt; ( int i )
{
 //...
}

template &lt;&gt;
void doIt&lt;1&gt; ( int i )
{
 //...
}

int main ()
{
    unsigned int i = 0;
    doIt&lt;0&gt;(i);
    my_int i2 = 0;
    doIt&lt;1&gt;(i2);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1723122</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1723122</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Mon, 08 Jun 2009 08:49:42 GMT</pubDate></item></channel></rss>