The distinction and function of priority, associativity, and evaluation order

First, let's take a look at the definition of the three.

Priority is the order of operations . For example, we learned in elementary school to calculate multiplication and division first, and then add and subtract . The same is true in C++, first *, /, then +, -;

Look at the following C language code:

// An highlighted block
cout << 1 + 2 * 3 << endl;

According to the priority of multiplication first and then addition, the output result is 7;

Associativity, under the same priority conditions, is calculated from left to right (left associative) or right to left (right associative) .

Code piece:

// An highlighted block
cout << 2 - 1 - 1 << endl;

Calculate from left to right, the output result is 0, not 2 from right to left;

Evaluation order, this is the key point, it refers to the operation object that is calculated first in an expression (for example: in the expression of two operation objects (A=B+C), should B be calculated first, or calculated first What about C ?)

Note: B and C are operation objects, which can be expressions or some objects;

At first glance, some friends may think:

First, according to the priority, calculate B or C first.

Secondly, according to associativity, addition is left associative, adding B and C together.

Finally, let it go and get the value of A;

So, the question arises. What is the difference between counting B first and counting C first, as if it has no effect on the result!

It is like A=(1+2)+(3+4) (where B is (1+2) and C is (3+4));

Whether we first count (1+2) or (3+4), the result is the same.

So what is the use of evaluation order?

However, we have overlooked a problem: the examples we cite are all specific numbers , and the codes are often objects (such as built-in type objects, int a, double b, etc.)

At this time, let's look at another example:

// An highlighted block
    int a = 1, b = 2, c = 3, d = 4;
	int B = a + b;
	int C = c + d;
	int A = B + C;
	cout << "A的值为:" << A << endl;

emmm...it seems to have no effect.

Let's look at another example:

// An highlighted block
    int i = 0, j;
	j = i * 2 + i++;
	cout << "j的值为:" << j << endl;

In this example, if we first count i*2, then i++, the result is 0;

And if i++ is counted first, then i*2, the result is 2;

Different results are produced, so should we count the left operand first (i * 2) or the right operand first (i++)?

Is this what the order of evaluation does? ? ? (In fact, it is not)

In fact, i++ has to wait until the semicolon ends (that is, when all side effects are executed at the end of the statement) before performing the ++ operation . Therefore, in the assignment expression of j, the value of i is always 0; there is no change.

Therefore, no matter if the left and right operation objects are counted first, the result will be 0 instead of 2 ;

So the above example is still wrong (it should be a good example of side effects and sequence points )

I also tested the following code:

// An highlighted block
#include<iostream>
using namespace std;
int f1(int a) {
    
    
	int b = a * 2;
	return b;
}
int& f2(int& a) {
    
       //这里注意是地址传递;
	a++;
	return a;  
}
int main() {
    
    
	int i = 0, j;
	cout << f1(i) + f2(i) << endl;
}

The output value of the above code VS2019 is 1; it means that f1 is calculated first and then f2 is calculated; otherwise, if f2 is first followed by f1, the output value will be 3;
I think the above is an example of the order of evaluation.

It is not difficult to find that the order of evaluation only takes effect when the two operation objects have the same object, and mutual influence occurs ( in the last example, we have the i object in both left and right operation objects )

So far, the effect of evaluation order is clear.

Listed below, there are four evaluation orders specified in c99:

// An highlighted block
    ||//与,先左后右
    &&//或,先左后右
	?: //三目运算符,先左后右
	,//逗号运算符,从左到右

Only these four evaluation orders are specified in the c99 standard (there is no change in c++11). Whether the result of the last example above is 0 or 2 depends on the compiler. The value of j in VS2019 is 0.

Everyone’s praise is the greatest support for me, thank you.

Guess you like

Origin blog.csdn.net/myf_666/article/details/112971512