title: Undefined Behavior when using Comma Operator in C++
body: <p>I am trying to learn how expression are evaluated in C++. So trying out and reading different examples. Below is the code about which i am unable to understand whether it will produce undefined behavior or not. The code is from <a href="https://en.cppreference.com/w/cpp/language/operator_other" rel="nofollow noreferrer">here</a>. So i guess since they have used it, this must not be UB. But i have my doubts.</p>
  <pre><code>#include &lt;iostream&gt;
  int main()
  {
      int n = 1;
      //std::cout &lt;&lt; n &lt;&lt; &quot; &quot; &lt;&lt; ++n &lt;&lt; std::endl;//this is undefined behavior i am sure
      int m = (++n, std::cout &lt;&lt; &quot;n = &quot; &lt;&lt; n &lt;&lt; '\n', ++n, 2*n);//will this also produce UB because here also we have cout in the same manner as above?
      std::cout &lt;&lt; &quot;m = &quot; &lt;&lt; (++m, m) &lt;&lt; '\n';
  }
  </code></pre>
  <p>As you can see in the above code, i am sure that the statement:</p>
  <pre><code>cout &lt;&lt; n &lt;&lt; &quot; &quot; &lt;&lt; ++n &lt;&lt; endl;
  </code></pre>
  <p>produces undefined behavior.
  My questions are:</p>
  <ol>
  <li>But will the same statement used inside the comma operator produce UB(as shown in the code above)? That is, will the below given statement produce UB.</li>
  </ol>
  <pre><code>int m = (++n, std::cout &lt;&lt; &quot;n = &quot; &lt;&lt; n &lt;&lt; '\n', ++n, 2*n);
  </code></pre>
  <ol start="2">
  <li>How can we explain what is going on in terms of sequence-before, unsequenced etc the behavior of the above mentioned statement.</li>
  </ol>
  <p>PS: I know since C++11 we use sequence-before etc. instead of sequence point so that why i asked the explanation in terms of current standard.</p>