mercredi 21 février 2018

Evaluation order in parenthesis and comma expression in C++ of the type (x0 = y0, x1 = y1, x2 = x0, ...)

In C++, it is possible to do the following:

int f(int& a, int& b, int& c, int& d) { 
    return (a, b, c, d); // here
}

First, what is the name in the language (and link on cppreference) to this "parenthesis" expression where only the last term is returned;

Second, let's say the code becomes:

int f(int& a, int& b, int& c, int& d) { 
    return (a += 2, d += 5, b -= 3, c = a + b + d, d); // here
}

Is the order of evaluation of theses "parenthesis" expressions fixed? If so do I have the guarantee that f will change the values of a, b, c and d from the left to the right, and return the correct updated value of d as if it was:

int f(int& a, int& b, int& c, int& d) { 
    a += 2;
    d += 5;
    b -= 3;
    c = a + b + d;
    return d;
}

Finally, purely in C++11 (since the problem don't exist in C++14 anymore because constexpr don't have such strong requirements), can this parentheses expressions be used to write several computation in a single constexpr function?

Aucun commentaire:

Enregistrer un commentaire