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.