vendredi 24 septembre 2021

Undefined Behavior when using Comma Operator in C++

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 here. So i guess since they have used it, this must not be UB. But i have my doubts.

#include <iostream>
int main()
{
    int n = 1;
    //std::cout << n << " " << ++n << std::endl;//this is undefined behavior i am sure
    int m = (++n, std::cout << "n = " << n << '\n', ++n, 2*n);//will this also produce UB because here also we have cout in the same manner as above?
    std::cout << "m = " << (++m, m) << '\n';
}

As you can see in the above code, i am sure that the statement:

cout << n << " " << ++n << endl;

produces undefined behavior. My questions are:

  1. 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.
int m = (++n, std::cout << "n = " << n << '\n', ++n, 2*n);
  1. How can we explain what is going on in terms of sequence-before, unsequenced etc the behavior of the above mentioned statement.

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. I want to know when we use std::cout << n << " " << ++n; outside comma operator it is UB but when we use it inside the comma operator how will it become defined behavior.

Aucun commentaire:

Enregistrer un commentaire