<?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[Operation mit Seiteneffekt in einem return-Ausdruck]]></title><description><![CDATA[<p>Guten Abend,<br />
ich bin mir gerade bei folgendem nicht ganz sicher:</p>
<pre><code class="language-cpp">int fkt(int&amp; i)
{
    return i++;
}

int main()
{
    int i=0;

    int val = fkt(i) /*Ist ab hier der Seiteneffekt garantiert eingetreten?*/ + i; // val erhält also garantiert den Wert 1, und nicht 0 oder 1?
}
</code></pre>
<p>Danke schonmal <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="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/296424/operation-mit-seiteneffekt-in-einem-return-ausdruck</link><generator>RSS for Node</generator><lastBuildDate>Fri, 14 Aug 2026 21:43:01 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/296424.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 04 Dec 2011 17:58:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Sun, 04 Dec 2011 18:00:59 GMT]]></title><description><![CDATA[<p>Guten Abend,<br />
ich bin mir gerade bei folgendem nicht ganz sicher:</p>
<pre><code class="language-cpp">int fkt(int&amp; i)
{
    return i++;
}

int main()
{
    int i=0;

    int val = fkt(i) /*Ist ab hier der Seiteneffekt garantiert eingetreten?*/ + i; // val erhält also garantiert den Wert 1, und nicht 0 oder 1?
}
</code></pre>
<p>Danke schonmal <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="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2152932</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2152932</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 04 Dec 2011 18:00:59 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Sun, 04 Dec 2011 18:02:23 GMT]]></title><description><![CDATA[<p>1...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2152934</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2152934</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Sun, 04 Dec 2011 18:02:23 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Sun, 04 Dec 2011 18:06:18 GMT]]></title><description><![CDATA[<p>Der Wert <code>val</code> ist undefiniert, sofern C++ keine strikte Auswertungsreihenfolge von Links nach Rechts vorschreibt. Vermeide solchen Code!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2152936</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2152936</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Sun, 04 Dec 2011 18:06:18 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Sun, 04 Dec 2011 18:07:41 GMT]]></title><description><![CDATA[<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>1...</p>
</blockquote>
<p>Nein. Undefiniert.</p>
<p>edit: Man sollte noch dazu sagen: Das Ergebnis ist nicht aus dem Grund undefiniert, den Gugelmoser meint. Bei Verlassen einer Funktion sind sämtliche Seiteneffekte garantiert ausgewertet. Was hier undefiniert ist, ist ob zuerst i oder zuerst fkt(i) ausgewertet wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2152937</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2152937</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 04 Dec 2011 18:07:41 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Sun, 04 Dec 2011 18:26:37 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Bei Verlassen einer Funktion sind sämtliche Seiteneffekte garantiert ausgewertet.</p>
</blockquote>
<p>Darauf wollte ich hinaus, und damit ist die Frage beantwortet. <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="🙂"
    /></p>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Was hier undefiniert ist, ist ob zuerst i oder zuerst fkt(i) ausgewertet wird.</p>
</blockquote>
<p>Hmm, ich dachte fkt(i) würde garantiert zuerst ausgewertet werden, weil ein Funktionsaufruf in der Prioritätenliste weiter oben steht, als die Addition. Wieso wird die Priorität hierbei nicht beachtet?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2152950</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2152950</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 04 Dec 2011 18:26:37 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Sun, 04 Dec 2011 18:29:46 GMT]]></title><description><![CDATA[<p>Gugelmoser schrieb:</p>
<blockquote>
<p>Wieso wird die Priorität hierbei nicht beachtet?</p>
</blockquote>
<p>Priorität ist nur, wierum unsichtbare Klammen stehen.<br />
Das beeinflußt NICHT die zeitliche Auswertungsreihenfolge.<br />
x=a()+b()*c();<br />
Hier wird vielleicht zuerst a, dann c und dann erst b aufgerufen und die Ergebnisse zwischengepeichert. Zeitliche Reihenfolge undefiniert.<br />
Und dann wird das * vor dem + ausgeführt. Prioritäten definiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2152952</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2152952</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 04 Dec 2011 18:29:46 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Sun, 04 Dec 2011 22:18:27 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Gugelmoser schrieb:</p>
<blockquote>
<p>Wieso wird die Priorität hierbei nicht beachtet?</p>
</blockquote>
<p>Priorität ist nur, wierum unsichtbare Klammen stehen.<br />
Das beeinflußt NICHT die zeitliche Auswertungsreihenfolge.<br />
x=a()+b()*c();<br />
Hier wird vielleicht zuerst a, dann c und dann erst b aufgerufen und die Ergebnisse zwischengepeichert. Zeitliche Reihenfolge undefiniert.<br />
Und dann wird das * vor dem + ausgeführt. Prioritäten definiert.</p>
</blockquote>
<p>Ich verstehe es immer noch nicht ganz. Wo kommt z.B. die Priorität des Funktionsaufrufes jemals zum Tragen?</p>
<p>Gibt es dazu eine allgemeine Regel, dass man nicht in eine Falle tappt (bzgl. Seiteneffekt und Auswertungsreihenfolge)?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153094</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153094</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 04 Dec 2011 22:18:27 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Sun, 04 Dec 2011 22:22:06 GMT]]></title><description><![CDATA[<p>Gugelmoser schrieb:</p>
<blockquote>
<p>Gibt es dazu eine allgemeine Regel, dass man nicht in eine Falle tappt (bzgl. Seiteneffekt und Auswertungsreihenfolge)?</p>
</blockquote>
<p>Ja. Sequenzpunkte/sequence points. C++ Standard Kapitel 1.9 wird das sogar recht verständlich erklärt, was das genau bedeutet und wo die sind.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153096</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153096</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 04 Dec 2011 22:22:06 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Sun, 04 Dec 2011 22:42:23 GMT]]></title><description><![CDATA[<p>Gugelmoser schrieb:</p>
<blockquote>
<p>Wo kommt z.B. die Priorität des Funktionsaufrufes jemals zum Tragen?</p>
</blockquote>
<p>cout&lt;&lt;sin(5);<br />
ist<br />
cout&lt;&lt;(sin(5))<br />
und nicht<br />
(cout&lt;&lt;sin)(5)</p>
<p>oder</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cmath&gt;
using namespace std;

double hello()
{
    cout&lt;&lt;&quot;hello&quot;;
    return 5;
}
double world()
{
    cout&lt;&lt;&quot;world&quot;;
    return 3;
}

int main()
{
    typedef double funcType();
    funcType* h=&amp;hello;
    funcType* w=&amp;world;
    //cout&lt;&lt;*h()&lt;&lt;'\n';//Klappt nicht, denn () bindet stärker als *.
    cout&lt;&lt;(*h)()&lt;&lt;'\n';//Klappt.
    cout&lt;&lt;(*h)()+(*w)()&lt;&lt;'\n';//Zufällig wird h vor w aufgerufen, Glück gehabt.
    cout&lt;&lt;pow((*h)(),(*w)())&lt;&lt;'\n';//Und jetzt andersrum.
    return 0;
}
</code></pre>
<p>Ausgabe bei mir:</p>
<pre><code>hello5
helloworld8
worldhello125
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2153104</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153104</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 04 Dec 2011 22:42:23 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 14:03:19 GMT]]></title><description><![CDATA[<p>Noch kurz eine off-topic Frage:</p>
<p>Bedeutet undefined behavior, dass wirklich alles passieren könnte, dass ich bei folgendem</p>
<pre><code class="language-cpp">i = i++ - 1;
</code></pre>
<p>auch ein Bluescreen bekommen könnte?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153324</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153324</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 05 Dec 2011 14:03:19 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 14:10:15 GMT]]></title><description><![CDATA[<p>Bei Undefined Behaviour darf alles passieren. Hier hast du aber einen Fall von Unspecified Behaviour.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153332</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153332</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Mon, 05 Dec 2011 14:10:15 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 14:20:39 GMT]]></title><description><![CDATA[<p>Michael E. schrieb:</p>
<blockquote>
<p>Bei Undefined Behaviour darf alles passieren. Hier hast du aber einen Fall von Unspecified Behaviour.</p>
</blockquote>
<p>Um ehrlich zu sein, das hab ich auch gedacht. Aber dann hab ich gesehen, dass es im Draft (n3242) als Undefined Behaviour bezeichnet wird. [in <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#351" rel="nofollow">früheren</a> Drafts stand noch Unspecified Behaviour dran.]</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153338</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153338</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 05 Dec 2011 14:20:39 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 14:34:52 GMT]]></title><description><![CDATA[<p>Gugelmoser schrieb:</p>
<blockquote>
<p>Michael E. schrieb:</p>
<blockquote>
<p>Bei Undefined Behaviour darf alles passieren. Hier hast du aber einen Fall von Unspecified Behaviour.</p>
</blockquote>
<p>Um ehrlich zu sein, das hab ich auch gedacht. Aber dann hab ich gesehen, dass es im Draft (n3242) als Undefined Behaviour bezeichnet wird.</p>
</blockquote>
<p>Kapitelnummer? n3242 ist ja nicht der letzte Draft gewesen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153347</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153347</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 05 Dec 2011 14:34:52 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 14:36:31 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>Gugelmoser schrieb:</p>
<blockquote>
<p>Michael E. schrieb:</p>
<blockquote>
<p>Bei Undefined Behaviour darf alles passieren. Hier hast du aber einen Fall von Unspecified Behaviour.</p>
</blockquote>
<p>Um ehrlich zu sein, das hab ich auch gedacht. Aber dann hab ich gesehen, dass es im Draft (n3242) als Undefined Behaviour bezeichnet wird.</p>
</blockquote>
<p>Kapitelnummer? n3242 ist ja nicht der letzte Draft gewesen...</p>
</blockquote>
<p>Kapitel 1.9, Absatz 15.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153351</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153351</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 05 Dec 2011 14:36:31 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 14:45:58 GMT]]></title><description><![CDATA[<p>Gugelmoser schrieb:</p>
<blockquote>
<p>Kapitel 1.9, Absatz 15.</p>
</blockquote>
<p>Undefined auch in n3291</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153356</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153356</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 05 Dec 2011 14:45:58 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 14:46:16 GMT]]></title><description><![CDATA[<p>Beispiele sind bekanntlich nicht normativ, der relevante Satz steht aber direkt davor:</p>
<blockquote>
<p>If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.</p>
</blockquote>
<p>&quot;sequenced after&quot; ist transitiv.<br />
Der Seiteneffekt der Zuweisung wird nach der Wertberechung des Ausdrucks i++ - 1 sequenziert, welche wiederum nach der Wertberechnung von i++ sequenziert wird. Es besteht aber keine Sequenzierung zwischen der Wertberechnung von i++ und dem Seiteneffekt, der Erhöhung des Wertes, der in i gespeichert ist. Eine Sequenzierung der Seiteneffekte findet auch nicht aus anderen Gründen statt. Damit resultiert UB.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153357</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153357</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 05 Dec 2011 14:46:16 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 19:03:54 GMT]]></title><description><![CDATA[<p>Hier ein kleiner Quiz: bestimme den Wert des Ausdruckes und den der Variable i (unabhängig voneinander, i sei also immer 0 vor der Auswertung jedes einzelnen Ausdruckes). Im Falle von unspezifiziertem Verhalten sind alle möglichen Ergebnisse gefragt.</p>
<pre><code class="language-cpp">int&amp; foo(int&amp; v) { return ++v; }

int i = 0;
i + i;              // (1)
++i + i;            // (2)
i++ + i;            // (3)
i = ++i;            // (4)
foo(i) + i;         // (5)
foo(i) + i++;       // (6)
foo(++i) + i;       // (7)
foo(++i) + i++;     // (8)
foo(foo(i)) + i;    // (9)
foo(++i) + foo(++i);// (10)
</code></pre>
<p>Lösung gibts später (bei Interesse).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153384</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153384</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 05 Dec 2011 19:03:54 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 15:39:00 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>Lösung gibts später (bei Interesse).</p>
</blockquote>
<p>Also ich habe natürlich Interesse. <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="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153388</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153388</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 05 Dec 2011 15:39:00 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 15:46:46 GMT]]></title><description><![CDATA[<p>Frage: zählt die Auswertung des Ausdrucks <code>i</code> als &quot;value computation&quot;?<br />
(Falls ja, hab ich 9x UB)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153394</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153394</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 05 Dec 2011 15:46:46 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 16:03:52 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>Frage: zählt die Auswertung des Ausdrucks <code>i</code> als &quot;value computation&quot;?<br />
(Falls ja, hab ich 9x UB)</p>
</blockquote>
<p>Die Bestimmung des Wertes einer Variablen in einem primären Ausdruck (als Folge einer l-zu-rvalue-Konvertierung) ist auch eine &quot;value computation&quot;, allerdings sind das ein bisschen zu viele UBs. Ich denke wir können 1-4 abhacken, (1) ist trivial, 2-4 UB (4 ist evtl. bloß unspezifiziert).<br />
Bei 5-10 sind nicht alle undefiniert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153397</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153397</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 05 Dec 2011 16:03:52 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 16:29:51 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>Bei 5-10 sind nicht alle undefiniert.</p>
</blockquote>
<p>Aber man kann sich doch gar nicht sicher sein, ob zuerst der rechte oder der linke Ausdruck ausgewertet wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153414</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153414</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 05 Dec 2011 16:29:51 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 16:33:57 GMT]]></title><description><![CDATA[<p>Gugelmoser schrieb:</p>
<blockquote>
<p>Aber man kann sich doch gar nicht sicher sein, ob zuerst der rechte oder der linke Ausdruck ausgewertet wird.</p>
</blockquote>
<p>Ja, aber z.B. (5) ist nicht undefiniert, sondern nicht spezifiziert (hoffe ich mal ;)), weil ein Sequenzpunkt zwischen ++i und i liegt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153415</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153415</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Mon, 05 Dec 2011 16:33:57 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 16:35:10 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>Bei 5-10 sind nicht alle undefiniert.</p>
</blockquote>
<p>Gnarf, got it. 11, 12 und 14 sind garkein gültiges C++, weil das temporäre objekt aus i++ garnicht an int&amp; binden kann <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>
<p>Ich nehme mal an, du meinst jeweils ++i.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153417</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153417</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 05 Dec 2011 16:35:10 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 19:03:36 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>Bei 5-10 sind nicht alle undefiniert.</p>
</blockquote>
<p>Gnarf, got it. 11, 12 und 14 sind garkein gültiges C++, weil das temporäre objekt aus i++ garnicht an int&amp; binden kann <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>
<p>Ich nehme mal an, du meinst jeweils ++i.</p>
</blockquote>
<p>oh... ja, korrigiert</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153476</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153476</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 05 Dec 2011 19:03:36 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 21:16:31 GMT]]></title><description><![CDATA[<p>Habe mich doch ein bisschen selbst reingelegt, allerdings gibt es auch Unterschiede zu C++03</p>
<ol>
<li>i + i</li>
</ol>
<p>C++11 schrieb:</p>
<blockquote>
<p>1.9/15 [...] The value computations of the operands of an operator are sequenced before the value computation of the result of the operator.</p>
</blockquote>
<p>Die Summe wird also erst berechnet, nachdem der Wert der Summanden ermittelt wurde. Eine bestimmte Reihenfolge bei der Auswert der Summanden besteht nicht, da allerdings auch keine Seiteneffekte im Spiel sind, die diese Auswertung beinflussen könnten, ist klar, dass hier nur 0 herauskommen kann, der Wert von i ändert sich nicht.</p>
<ol start="2">
<li>++i + i</li>
</ol>
<p>C+11 schrieb:</p>
<blockquote>
<p>1.9/15 [...] If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.</p>
</blockquote>
<p>Die Auswertung des rechten Summanden und der Seiteneffekt der Inkrementierung sind nicht sequenziert -&gt; UB</p>
<ol start="3">
<li>
<p>i++ + i<br />
wie 2)</p>
</li>
<li>
<p>i = ++i<br />
Dieser Fall ist interessant, und C++11 gibt hier eine andere Antwort als C++03 oder C99!</p>
</li>
</ol>
<p>C++03 schrieb:</p>
<blockquote>
<p>5/4 Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified.53) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined. [Example:<br />
i = v[i++]; // the behavior is unspecified<br />
i = 7, i++, i++; // i becomes 9<br />
i = ++i + 1; // the behavior is unspecified<br />
i = i + 1; // the value of i is incremented<br />
—end example]</p>
</blockquote>
<p>Abgesehen davon, dass das Beispiel fehlerhaft ist: ganz klar wird hier i zweimal modifiziert, einen Sequenzpunkt gibt es erst am Ende des Ausdruckes, also resultiert UB. Der zitierte Absatz ist in C++11 ersatzlos weggefallen.<br />
Man könnte noch darüber nachdenken, ob das Speichern des gleichen Wertes in einem Objekt eine Modifikation darstellt:</p>
<pre><code class="language-cpp">x = x = 0
</code></pre>
<p>Falls nicht, besteht kein Problem, sofern irgendwie sichergestellt werden kann, dass die Modifikation von i vor der Ermittelung des Wertes des Inkrementierens stattfindet. Das Problem dabei ist, dass die Formulierung in C++03 offensichtlich von der im C-Standard abstammt:</p>
<p>C++03 schrieb:</p>
<blockquote>
<p>5.3.2 Increment and decrement [expr.pre.incr]<br />
1 The operand of prefix ++ is modified by adding 1, or set to true if it is bool (this use is deprecated). The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer to a completely-defined object type. The value is the new value of the operand; it is an lvalue. If x is not of type bool, the expression ++x is equivalent to x+=1. [Note: see the discussions of addition (5.7) and assignment operators (5.17) for information on conversions. ]</p>
</blockquote>
<p>Das ergibt keinen Sinn. Der Wert eines skalaren Objektes ist <em>kein</em> Objekt. lvalues aber verweisen auf Objekte (3.10/2). Hier passt etwas nicht. Obwohl dieser Unfall unproblematisch ist, wenn es nur darum geht, zu verstehen, was Inkrementieren macht, hilft es leider nicht, wenn es um die Reihenfolge geht. Die Formulierung bei der Zuweisung ist leider genauso fehlerhaft.<br />
Zusammenfassend kann man also sagen, dass der Ausdruck i = ++i möglicherweise definiert ist, sicher geht man aber, wenn man annimmt, es wäre undefiniert.</p>
<p>In C++11 ist diese Sache dagegen klar:<br />
Falls demonstriert werden kann, dass eine klare Reihenfolge der einzelnen Elemente der Auswertung des Ausdrucks (Ermittelung des Wertes von i (1), Speichern des Erhöhten Wertes von i (2), Ermittelung des inkrementierten Wertes (3) und Speichern dieses Wertes in i (wegen der Zuweisung) (4)) besteht, dann ist das Ergebnis wohldefiniert.<br />
Klar ist, dass der ursprüngliche Wert von i vor Ermittelung des erhöhten Wertes und dieser wiederum vor der Speichern aufgrund der Zuweisung stattfindet, also (1)-&gt;(3)-&gt;(4); (x)-&gt;(y) soll hier &quot;x is sequenced before y&quot; heißen.<br />
Fraglich ist nur, ob eine (möglicherweise unbestimmte) Reihenfolge zwischen (2) und (4) besteht.</p>
<p>C++11 schrieb:</p>
<blockquote>
<p>5.3.2 Increment and decrement [expr.pre.incr]<br />
1 The operand of prefix ++ is modified by adding 1, or set to true if it is bool (this use is deprecated). The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer to<br />
a completely-defined object type. The result is the updated operand; it is an lvalue, and it is a bit-field if the operand is a bit-field. If x is not of type bool, the expression ++x is equivalent to x+=1 [ Note: See the<br />
discussions of addition (5.7) and assignment operators (5.17) for information on conversions. —end note ]</p>
</blockquote>
<p>Vergleiche</p>
<p>C++03 schrieb:</p>
<blockquote>
<p>The value is the new value of the operand; it is an lvalue.</p>
</blockquote>
<p>C++11 schrieb:</p>
<blockquote>
<p>The result is the updated operand; it is an lvalue, and it is a bit-field if the operand is a bit-field.</p>
</blockquote>
<p>Der Unterschied ist subtil aber wichtig. In</p>
<pre><code class="language-cpp">int i = 0, j = ++i;
</code></pre>
<p>ist ganz klar, dass j mit 1 initialisiert wird. Der Ausdruck ++i verweist aber auf ein Objekt. Erst die folgende Umwandlung in ein rvalue ermittelt den <em>gespeichterten</em> Wert. Der gespeicherte Wert ist aber der erhöhte Werte. Das kann nur gerantiert sein, wenn die Modifikation von i <em>vor</em> der Ermittelung des &quot;Wertes&quot; (der Erzeugung des lvalues) von ++i erfolgt. Folglich gilt (3)-&gt;(2)<br />
&quot;sequenced after&quot; ist eine transitive Beziehung, wegen (3)-&gt;(2) und (2)-&gt;(4) gilt auch (3)-&gt;(4). Es besteht also eine klare Reihenfolge der Modifikation. Das Ergebnis ist wohldefiniert.</p>
<p>Es sollte nicht zu sehr überraschen, dass C++11 einige Konstrukte definiert, die in C++03 noch undefiniert waren. &quot;between sequence points&quot; ist schließlich eine viel gröbere Gliederung als &quot;sequenced before&quot;.<br />
Zu 5 ff. schreibe ich morgen etwas.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153531</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153531</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 05 Dec 2011 21:16:31 GMT</pubDate></item><item><title><![CDATA[Reply to Operation mit Seiteneffekt in einem return-Ausdruck on Mon, 05 Dec 2011 21:30:01 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>C++03 schrieb:</p>
<blockquote>
<p>5/4 Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified.53) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined. [Example:<br />
i = v[i++]; // the behavior is unspecified<br />
i = 7, i++, i++; // i becomes 9<br />
i = ++i + 1; // the behavior is unspecified<br />
i = i + 1; // the value of i is incremented<br />
—end example]</p>
</blockquote>
<p>Abgesehen davon, dass das Beispiel fehlerhaft ist: ganz klar wird hier i zweimal modifiziert, einen Sequenzpunkt gibt es erst am Ende des Ausdruckes, also resultiert UB.</p>
</blockquote>
<p>Warum ist dann <code>i = ++i +1</code> nicht undefiniert?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2153542</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2153542</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Mon, 05 Dec 2011 21:30:01 GMT</pubDate></item></channel></rss>