0

Test:

bool last_test = false;

int func1(int& i) {
    if (last_test == true) //for last test
        cout << "func1" << endl;
    i = 1000;
    return 0;
}

int func2(int& i) {
    if (last_test == true) //for last test
        cout << "func2" << endl;
    i = 2000;
    return 0;
}

int main() {
    int x = 1;
    int y = 2;

    int r1;
    int r2;
    int r3;
    int r4;
    int r5;

    r1 = x + func1(x);
    cout << "r1 = " << r1 << endl; // (x + (func1))
    r2 = func2(y) + y;
    cout << "r2 = " << r2 << endl; // ((func2) + y)

    x = 1;
    y = 2;
    r3 = func1(x) + x + y + func2(y); // ((((func1) + x) + y) + func2)
    cout << "r3 = " << r3 << endl;
    x = 1;
    y = 2;
    r4 = x + func1(x) + func2(y) + y; // (((x + (func1)) + func2) + y)
    cout << "r4 = " << r4 << endl;

    last_test = true;
    r5 = func1(x) + func2(y); // ((func1) + func2)
    }

Output:

r1 = 1000
r2 = 2000
r3 = 1002
r4 = 3000
func1
func2

I am trying to figure out the operand evaluation order of C++ in Visual Studio. I created a few small tests and it looks from the results it looks as though:

Given two operands, one being a function call, and the other being a variable, the arithmetic operator '+' always evaluates the function call operand first.

and

Given two operands, both being function calls, the arithmetic operator '+' always evaluates the left operand first.

Can someone help me confirm these assumptions? I want to make sure I have not made an error with my test.

  • I don't think this behavior is guaranteed even looking at a single `c++` implementation. – drescherjm Apr 03 '20 at 19:15
  • About the dupe, `a + b` is `operator +(a, b)`, so it is a function call and the dupe has the answer to this. – NathanOliver Apr 03 '20 at 19:15
  • It should be noted that C++17 [made changes to this](https://stackoverflow.com/questions/38501587/what-are-the-evaluation-order-guarantees-introduced-by-c17), but those changes don't apply here. (`operator +` was not an operator that was changed) – NathanOliver Apr 03 '20 at 19:16
  • Evaluation order is unspecified in C++ - see [here](https://en.cppreference.com/w/cpp/language/eval_order). With some restrictions, compilers are free to choose how to evaluate the arguments of arithmetic operators. – Hristo 'away' Iliev Apr 03 '20 at 19:19
  • @Hristo lliev Do you know if my assumption is correct for how Visual Studio handles this evaluation order? – ChildOfHonor Apr 03 '20 at 21:24
  • I would say your assumption is incorrect. You can not guarantee the order even though it looks like the order is always the same through testing. – drescherjm Apr 03 '20 at 21:37
  • 1
    I think that whatever assumption you make is valid only for the very narrow set of conditions that you base your observations on and can be invalidated by a fluke in the optimiser logic or a change in the code generator in the next version. On the other hand, if your goal is to understand what your particular version of VS compiler does, that is a completely different story. – Hristo 'away' Iliev Apr 03 '20 at 22:18

0 Answers0