<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aditya Agarwal</title>
    <description>The latest articles on DEV Community by Aditya Agarwal (@naveenadi).</description>
    <link>https://dev.to/naveenadi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F212243%2Ffb1aa874-4f95-474b-8b7e-596dc9239449.png</url>
      <title>DEV Community: Aditya Agarwal</title>
      <link>https://dev.to/naveenadi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/naveenadi"/>
    <language>en</language>
    <item>
      <title>Argument Evaluation Order in C++</title>
      <dc:creator>Aditya Agarwal</dc:creator>
      <pubDate>Mon, 18 May 2020 12:03:07 +0000</pubDate>
      <link>https://dev.to/naveenadi/argument-evaluation-order-in-c-31e5</link>
      <guid>https://dev.to/naveenadi/argument-evaluation-order-in-c-31e5</guid>
      <description>&lt;p&gt;I am here discussing about "Don’t depend on order of evaluation of function arguments". Now I think you may worry, what i am talking about because many of Developers assumed that the order of the evaluation of function arguments is left to right. Wrong! You have no guarantees!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reason&lt;/strong&gt; Because that order is unspecified.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; C++17 tightens up the rules for the order of evaluation, but the order of evaluation of function arguments is still unspecified.&lt;/p&gt;

&lt;p&gt;Now take simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;//test.cpp&lt;/span&gt;

&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"a: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"b: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// (1, 0) | (0, 1) | (0, 0)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you understand what I am saying why this comment then you actually on the way. If not then get move to the explanation.&lt;/p&gt;

&lt;p&gt;Here is my proof. The output from gcc and clang differs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;gcc&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;g++ -std=c++2a -O3 -Wall test.cpp -o test &amp;amp;&amp;amp; ./test&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test.cpp: In function ‘int main()’:
test.cpp:9:15: warning: operation on ‘i’ may be undefined [-Wsequence-point]
    9 |     foo(i++, i++); // (1, 0) | (0, 1) | (0, 0)
      |              ~^~
test.cpp:9:15: warning: operation on ‘i’ may be undefined [-Wsequence-point]
a: 1 b: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;clang&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;clang++ -std=c++2a -O3 -Wall test.cpp -o test &amp;amp;&amp;amp; ./test&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test.cpp:9:10: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
    foo(i++, i++); // (1, 0) | (0, 1) | (0, 0)
         ^    ~~
1 warning generated.
a: 0 b: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The call will most likely be foo(0, 1) or foo(1, 0), but you don’t know which. Technically, the behavior is undefined. In C++17, this code does not have undefined behavior, but it is still not specified which argument is evaluated first.&lt;/p&gt;

&lt;p&gt;I compile in C++20 or give best possible error in compilation with optimization with compiler flag.&lt;/p&gt;

&lt;p&gt;If you want to learn more about this then follow these link:&lt;br&gt;
&lt;a href="https://isocpp.org/blog/2016/08/quick-q-why-doesnt-cpp-have-a-specified-order-for-evaluating-function-argum" rel="noopener noreferrer"&gt;https://isocpp.org/blog/2016/08/quick-q-why-doesnt-cpp-have-a-specified-order-for-evaluating-function-argum&lt;/a&gt;&lt;br&gt;
&lt;a href="https://en.cppreference.com/w/cpp/language/eval_order" rel="noopener noreferrer"&gt;https://en.cppreference.com/w/cpp/language/eval_order&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/questions/621542/compilers-and-argument-order-of-evaluation-in-c" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/621542/compilers-and-argument-order-of-evaluation-in-c&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>developer</category>
    </item>
  </channel>
</rss>
