<?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[Funktionsüberladung mit Templates]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>Ich habe 3 Funktionen:</p>
<pre><code>template &lt;typename ostream&gt;
ostream&amp; operator &lt;&lt; (ostream &amp;os, std::exception &amp;ex);

message_box &amp; operator &lt;&lt; (message_box &amp; messagebox, const std::exception &amp;exception);

message_box &amp; operator &lt;&lt; (message_box &amp; messagebox, const stl::file_exception &amp;exception); // file_exception ist eine von runtime_error abgleitete Klasse
</code></pre>
<p>In der main passiert folgendes:</p>
<pre><code>int main()
{
    try
    {
        throw stl::file_exception(&quot;test.txt&quot;);
    }
    catch(std::exception &amp;ex)
    {
        win::message_box::error &lt;&lt; ex;
    }
}
</code></pre>
<p>Ich ging davon aus, dass der &lt;&lt;-Operator für file_exceptions aufgerufen wird, das ist aber nicht so. Es wird der für std::exception ohne Template aufgerufen.<br />
Liegt das daran, dass bei der Ausgabe nicht mehr klar ist, dass die exception eigentlich eine file_exception ist? Wenn ja, kann man da irgendetwas gegen machen? Ich will nicht tausende catch-Blöcke haben.<br />
lg<br />
Nathan</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/313513/funktionsüberladung-mit-templates</link><generator>RSS for Node</generator><lastBuildDate>Sun, 02 Aug 2026 03:55:08 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/313513.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 02 Feb 2013 15:12:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 15:12:22 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>Ich habe 3 Funktionen:</p>
<pre><code>template &lt;typename ostream&gt;
ostream&amp; operator &lt;&lt; (ostream &amp;os, std::exception &amp;ex);

message_box &amp; operator &lt;&lt; (message_box &amp; messagebox, const std::exception &amp;exception);

message_box &amp; operator &lt;&lt; (message_box &amp; messagebox, const stl::file_exception &amp;exception); // file_exception ist eine von runtime_error abgleitete Klasse
</code></pre>
<p>In der main passiert folgendes:</p>
<pre><code>int main()
{
    try
    {
        throw stl::file_exception(&quot;test.txt&quot;);
    }
    catch(std::exception &amp;ex)
    {
        win::message_box::error &lt;&lt; ex;
    }
}
</code></pre>
<p>Ich ging davon aus, dass der &lt;&lt;-Operator für file_exceptions aufgerufen wird, das ist aber nicht so. Es wird der für std::exception ohne Template aufgerufen.<br />
Liegt das daran, dass bei der Ausgabe nicht mehr klar ist, dass die exception eigentlich eine file_exception ist? Wenn ja, kann man da irgendetwas gegen machen? Ich will nicht tausende catch-Blöcke haben.<br />
lg<br />
Nathan</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295593</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295593</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 02 Feb 2013 15:12:22 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 15:33:39 GMT]]></title><description><![CDATA[<p>Wenn du eine file_exception haben willst, dann musst du auch eine file_exception fangen. (MessageBox mit operator &lt;&lt;? Wirklich? Ist das nicht eher ein Text der einmal feststeht und kein Stream?)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295594</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295594</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Sat, 02 Feb 2013 15:33:39 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 15:45:16 GMT]]></title><description><![CDATA[<p>Der operator &lt;&lt; dient lediglich der Optik.<br />
Ich nutze &quot;Streams&quot; überall aus optischen Gründen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295597</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295597</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 02 Feb 2013 15:45:16 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 15:42:22 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>Der operator &lt;&lt; dient lediglich der Optik.<br />
Ich nutze Streams überall aus optischen Gründen.</p>
</blockquote>
<p>Seltsame Leute gibts ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295599</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295599</guid><dc:creator><![CDATA[optiker]]></dc:creator><pubDate>Sat, 02 Feb 2013 15:42:22 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 15:45:40 GMT]]></title><description><![CDATA[<p>Tja.<br />
Ich finde das schön.^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295600</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295600</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 02 Feb 2013 15:45:40 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 16:01:44 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>Tja.<br />
Ich finde das schön.^^</p>
</blockquote>
<p>Solange der Code nur für dich selber ist, ist es ja relativ egal. Wenn du dann aber mal mit mehreren Leuten programmierst, dann sollte man sich an gewisse &quot;Normen&quot; halten. Ansonsten sind die Klasse, usw. nutztlos, weil sie sich einfach nur falsch verhalten. Und wenn du jetzt immer so weiter programmierst, dann wirst du immer wieder in diesen Stil zurückfallen und ihn nur schwer verbessern können...</p>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_down"
      title=":-1:"
      alt="👎"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295601</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295601</guid><dc:creator><![CDATA[klöklö]]></dc:creator><pubDate>Sat, 02 Feb 2013 16:01:44 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 16:12:12 GMT]]></title><description><![CDATA[<p>Was spricht gegen</p>
<pre><code>win::message_box::error(e.what());
</code></pre>
<p>? Da brauchst du dann auch nur noch std::exception zu fangen..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295603</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295603</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Sat, 02 Feb 2013 16:12:12 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 16:17:21 GMT]]></title><description><![CDATA[<p>OK, das ist ein Argument.<br />
Ich finde aber trotzdem, dass das gut zu meinen Image-Streams und Packet-Streams passen würde...<br />
In welchen Situationen machen Streams/operator &lt;&lt; denn wirklich Sinn?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295606</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295606</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 02 Feb 2013 16:17:21 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 16:24:41 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>In welchen Situationen machen Streams/operator &lt;&lt; denn wirklich Sinn?</p>
</blockquote>
<p>Wenn du tatsächlich Stream-Semantik hast. D.h. du kannst mehrere Objekte hintereinander irgendwo reinschieben. Z.B. in ein Packet, das übers Netzwerk versendet wird. Sinnvoll ist dabei, wenn der <code>operator&lt;&lt;</code> verkettet werden kann, und wenn es möglicherweise auch einen <code>operator&gt;&gt;</code> mit umgekehrter Semantik gibt.</p>
<p>Aber verwende <code>&lt;&lt;</code> und <code>&gt;&gt;</code> <strong>nicht</strong>, wenn benannte Funktionen viel aussagekräftiger sind und man erst mal überlegen muss, was der Operator in deinem Kontext bedeuten könnte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295607</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295607</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 02 Feb 2013 16:24:41 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 16:26:05 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>In welchen Situationen machen Streams/operator &lt;&lt; denn wirklich Sinn?</p>
</blockquote>
<p>Streams=Datenfluss<br />
<a href="http://de.wikipedia.org/wiki/Datenstrom" rel="nofollow">http://de.wikipedia.org/wiki/Datenstrom</a><br />
<a href="http://en.wikipedia.org/wiki/Stream_%28computing%29" rel="nofollow">http://en.wikipedia.org/wiki/Stream_%28computing%29</a></p>
<p>&lt;&lt; = formatierte Ausgabe</p>
<blockquote>
<p>&gt; = formatierte Eingabe</p>
</blockquote>
<p>=&gt; Wenn man einen Datenfluss mit formatiertem Text hat.</p>
<p>Für unformatierte Sachen wie Images oder Packets sind Streambuffer zuständig.</p>
<p>Wobei das alles relativ ist, weil sich die Standardbibliothek nicht einmal selber daran hält. Ist ein riesiger Murks, den man am liebsten umgeht (nicht grundlos haben std::exceptions ein what() und kein operator&gt;&gt;).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295608</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295608</guid><dc:creator><![CDATA[optiker]]></dc:creator><pubDate>Sat, 02 Feb 2013 16:26:05 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 16:36:05 GMT]]></title><description><![CDATA[<p>Für mich ist &lt;&lt; = Ausgabe und &gt;&gt; = Eingabe.<br />
Deswegen hatte ich dort den Operator überladen denn Text auf einer Message Box auszugeben ist eine Ausgabe.<br />
Für mich ist der Kontext klar und die Operatoren sind lediglich Alternativen zu benannten Funktionen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295612</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295612</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 02 Feb 2013 16:36:05 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 16:50:22 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>Für mich ist der Kontext klar und die Operatoren sind lediglich Alternativen zu benannten Funktionen.</p>
</blockquote>
<p>Es gibt aber einen grossen semantischen Unterschied.</p>
<pre><code class="language-cpp">win::message_box::error &lt;&lt; &quot;1&quot;;
// Erwartung: error enthält &quot;1&quot;
win::message_box::error &lt;&lt; 2 &lt;&lt; '3';
// Erwartung: error enthält &quot;123&quot;
</code></pre>
<pre><code class="language-cpp">win::message_box::error.set_msg(&quot;1&quot;);
// Erwartung: error enthält &quot;1&quot;
win::message_box::error.set_msg(&quot;2&quot;); // nur strings erlaubt
win::message_box::error.set_msg(&quot;3&quot;);
// Erwartung: error enthält &quot;3&quot;
</code></pre>
<p>Wenn du das richtig umsetzen willst, müsste jede Klasse einen stringstream halten und das ist Overkill.</p>
<p>Ausserdem führt dein Ansatz zu Inkonsistenzen</p>
<pre><code class="language-cpp">std::deque&lt;int&gt; d;
d &lt;&lt; 4 &lt;&lt; 5 &lt;&lt; 6; // ich bin ganz schlau und habe operator&gt;&gt; überladen
int a, b;
d &gt;&gt; a &gt;&gt; b; // a=4 und b=5 und d.size()=1

d.push_front(3); // leider kein Operator mehr frei
int c = d.back(); d.pop(); // dito
</code></pre>
<p>Daher: Im Zweifelsfall lieber benannte Funktionen, die haben Namen und jedem ist klar, was sie machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295613</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295613</guid><dc:creator><![CDATA[optiker]]></dc:creator><pubDate>Sat, 02 Feb 2013 16:50:22 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 17:05:21 GMT]]></title><description><![CDATA[<p>OK, mein Bild von operator &lt;&lt;/operator&gt;&gt; war wohl nicht ganz klar definiert.<br />
Allerdings käme ich nie auf die Idee für deque etc. sie zu überladen, da das für mich keine Aus-/Eingabe ist. Anderseits ist es das schon...<br />
Und eigentlich ist es bei message_box schon so, dass 123 ausgegeben wird, nur in verschiedenen Boxen (Ausgabe ist bei mir ungepuffert, wenn man das so sagen darf).<br />
Trotzdem werde ich hier darauf verzichten.<br />
Wie sieht es mit Bildausgabe aus?</p>
<pre><code>main_display &lt;&lt; sprite1 &lt;&lt; sprite2 &lt;&lt; depth_test(false) &lt;&lt; rectangle &lt;&lt; flush;
</code></pre>
<p>Ist es da in Ordnung die Operatoren zu verwenden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295616</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295616</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 02 Feb 2013 17:05:21 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 17:24:06 GMT]]></title><description><![CDATA[<p>Ein- und Ausgabe sind sehr weit gefasste Begriffe. Sie sind daher nicht unbedingt ein geeignetes Kriterium für <code>operator&lt;&lt;</code> und <code>operator&gt;&gt;</code> .</p>
<p>Ich würde zum Zeichnen ebenfalls nicht diese Operatoren verwenden. Was stört dich an</p>
<pre><code>window.draw(sprite1);
window.draw(sprite2);
window.display();
</code></pre>
<p>? Hat neben dem besser verständlichen Code auch den Vorteil, dass du mehrere Parameter angeben kannst. Zum Beispiel, wenn du etwas mit einem Shader rendern willst:</p>
<pre><code>window.draw(sprite, shader);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2295628</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295628</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 02 Feb 2013 17:24:06 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 17:33:39 GMT]]></title><description><![CDATA[<p>Ich weiß es nicht, mir gefällt das optisch nicht so...<br />
Warum gibt es dann bei o/istream die Operatoren? Das ist doch ähnlich wie hier oder irre ich mich da?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295632</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295632</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 02 Feb 2013 17:33:39 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 17:40:43 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>Ich weiß es nicht, mir gefällt das optisch nicht so...</p>
</blockquote>
<p>Das ist aber die Syntax für Methodenaufrufe, daran solltest du dich in modernen Sprachen besser gewöhnen.</p>
<p>Davon abgesehen: <code>window.draw(sprite);</code> kannst du lesen als &quot;window draws sprite&quot;, expressiver geht gar nicht. Bei <code>&lt;&lt;</code> ist es hingegen ein Rätselraten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295633</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295633</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 02 Feb 2013 17:40:43 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Thu, 12 Jun 2014 11:07:35 GMT]]></title><description><![CDATA[<p>...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295634</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295634</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Thu, 12 Jun 2014 11:07:35 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 18:05:20 GMT]]></title><description><![CDATA[<p>Ich weiß es nicht, ich fand das es schöner aussieht.<br />
Und klar, dass &lt;&lt; und &gt;&gt; eigentlich Bitshiftoperatoren sind, ich redete natürlich vom Kontext aus.<br />
Ich werde &lt;&lt; und &gt;&gt; nur noch verwenden, wenn der Stream von i/ostream abgeleitet ist/werden kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295638</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295638</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 02 Feb 2013 18:05:20 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 18:13:20 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>Ich werde &lt;&lt; und &gt;&gt; nur noch verwenden, wenn der Stream von i/ostream abgeleitet ist/werden kann.</p>
</blockquote>
<p>Es gibt schon auch andere Fälle, wo es sinnvoll sein kann. Aber die Semantik ist üblicherweise sehr ähnlich zu Streams.</p>
<p>Beispiel ist die Klasse<a href="http://www.sfml-dev.org/documentation/2.0/classsf_1_1Packet.php" rel="nofollow"> <code>sf::Packet</code> </a>von SFML. Diese repräsentiert ein Netzwerkpaket, wobei <code>&lt;&lt;</code> und <code>&gt;&gt;</code> Werte ein- und auslesen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295639</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295639</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 02 Feb 2013 18:13:20 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sat, 02 Feb 2013 18:20:37 GMT]]></title><description><![CDATA[<p>Ja, die kenne ich.<br />
Man könnte sie aber auch so implementieren, dass sie von iostream abgeleitet ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295641</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295641</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 02 Feb 2013 18:20:37 GMT</pubDate></item><item><title><![CDATA[Reply to Funktionsüberladung mit Templates on Sun, 03 Feb 2013 01:34:41 GMT]]></title><description><![CDATA[<p>Und wieder einen Abweichler erfolgreich auf Linie gebracht!<br />
Ein voller Erfolg für das Kollektiv!<br />
Ach, kommt schon …</p>
<p>optiker schrieb:</p>
<blockquote>
<p>Ausserdem führt dein Ansatz zu Inkonsistenzen</p>
</blockquote>
<p>Bei Streams gibt es aber keine? Deswegen kann ich auch so toll Binärdaten mittels operator&gt;&gt; einlesen?<br />
Ich habe noch nie die vorparsenden Einlesefunktionen benutzt, denn das haut nur einigermaßen hin, wenn man die Eingabe spezifisch auf sie abstimmt …</p>
<p>Nexus schrieb:</p>
<blockquote>
<p>Davon abgesehen: window.draw(sprite); kannst du lesen als &quot;window draws sprite&quot;</p>
</blockquote>
<p>Ist es nicht eher eine Anweisung, also ein Befehl, keine Aussage? (Vgl.: <em>window, draw sprite</em>)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2295706</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2295706</guid><dc:creator><![CDATA[&amp;amp;#8239;]]></dc:creator><pubDate>Sun, 03 Feb 2013 01:34:41 GMT</pubDate></item></channel></rss>