Side-effects of expressions explain "references"

Source: Internet
Author: User

Expressions in the C + + language can be categorized into arithmetic expressions, relational expressions, and logical expressions, but they can be divided into two types from the point of view of the variables in the expression: expressions with side effects and expressions without side effects.

1 The concept of side effects of expressions generally say that the value of an expression needs to refer to some variables,During the evaluation of an expression, the values of these variables need to be extracted, but the values of these variables are not changed, and the expression is called a non-side-effect expression. In the traditional sense, the function of an expression is to calculate that it should not change the value of any variable participating in the calculation process or produce other effects in addition to producing a calculation result. In other words, expressions in the traditional sense should not have side effects. As a result, expressions in most high-level languages are expressions that have no side effects. The C + + language, which features high-level and low-level languages, is a typical language that supports object-oriented features, and references expressions with side effects in order to improve operational efficiency. An expression in the evaluation process,The variables used are not only references, but also changes to their values, such expressions are called side-effects expressions, or the expression has side effects.


2 analysis and understanding of the side effects of the expression, there are four expressions: 1, 5*x;2, x+y;3, X++;4, y-=18*2. Where expression 1 and expression 2 are non-side-effects, because in the expression evaluation process, the value of the referenced variable x and Y does not change, for example: Known x=5,y=10, the value of expression 1 is 25, the value of expression 2 is 15, but the value of x is still 5,y value is still 10, Expression 3 and expression 4 are expressions with side-effects, because the values of the referenced variables x and y are changed during the evaluation of an expression value. For example: The values for x and Y are initially 5 and 10 are calculated, the value of expression 3 is 5,x to 6, the value of expression 4 is 126, and the value of Y becomes 126.


For the given expression, how can we correctly determine whether it is a side-effect expression or a non-side-effect expression? The main reason depends on the operator that appears in the expression. Operators in the C + + language can be divided into two categories: operators with no side effects and operators with side effects. If an expression has a reference to an operator with a side effect, the expression is an expression that has side effects. The C + + language introduces the actions with side effects and the corresponding operators include: (1) Assignment (=). (2) compound assignment (+ =, one:, *=,/=,%=, "=," =,!=,&=, I.). (3) Increase in front 1, minus 1 (+ +, one by one). (4) Increase by 1, minus 1 (+ +, one by one). The objects that these operations act on must be variables, so these operations can also be referred to as actions for variables. For actions that act on a variable, in addition to 1 and minus 1, there is a feature: the value of the expression acting as the result of the action is the value obtained by the variable being manipulated. For example, execute a statement sequence: int i=5;count<< (i+=3), Count<<i, and the latter two outputs are 8. This means that the value of the expression is stored in the variable that is being played. For the post-increment 1 and minus 1 operators, after the operation, although the value of the variable is different from the value of the expression (the value of the expression cannot be represented by a variable), by calculating the value of the expression, the value of the variable is changed, such as executing the statement sequence: int i=5;cout<<i++;count <<i; The two output results are 5 and 6, respectively, so from the concept of expressions with side effects, 1 and minus 1 operators are still side-effects operators. An expression that distinguishes whether an expression is a side effect can also be compared from the calculation order. The values of several expressions without side effects are not different from the order of calculation, for example: Set x=3,y=2, the value of 3*x and expression x+y must be 3*2=6 and 3+2=5, but the order in which the expressions with side-effects and other expressions will be evaluated is very important and the order of the different results is different, for example: X =3,y=2, then (++x) +y and x+y++ after the calculation, the value of the expression is 6 and 6, and two expressions change the order of calculation, first calculate, x+y++, after the calculation (++x) +y, the expression value of 5 and 7 respectively. Therefore, when evaluating multiple expressions with side effects, be sure to pay attention to the order in which expressions are evaluated.


3The side effects of pointer expressions also have side effects, and the cause of side effects is the use of operators with side effects. In the case of pointers, + + (1), one by one (1), + =,-=, and = 5 operators are used for variables and change their values, and the new value obtained by the variable is the value of the pointer expression, so the value of the expression is indirectly provided as a variable. That is, the result of the operation of these 5 operators (that is, the value of the pointer expression) is considered the same variable, you can continue to do only the variables can be implemented operations (including the above 5 operations and address, 1, minus 1), so like + + (++PK), pk+=&k such expressions are legitimate ( Where PK is a pointer variable). For + + (1) and one by one (minus 1) operations, although it is also used for variables and changes its value, the value of the pointer expression differs from the new value obtained by the variable, so the value of the expression cannot be supplied as a variable, but only in this more direct form of the value. That is, the result of an operation that increases by 1 (or minus 1) is treated as a numeric address value of a constant, so an expression like (pk++) ++,PK 110 =&k is illegal (PK is a pointer variable). * (Indirect access) The result of the operation is considered to be the same variable, which can continue to operate only on the variable, while the & (address) acts on the variable, but does not change the value of the variable, and the result of its operation is treated as a numeric address value of the constant, and cannot be manipulated only for variables. Both operators are operators that do not have side effects. In summary, conclusions related to the side effects of numeric expressions apply equally to pointer expressions. Operators with side effects in pointer expressions are the same as those with side effects in numeric expressions.


4 application of expression side-effects
4. 1 The comma expression, which is the comma operator (that is,) that joins the individual expressions, constitutes the comma expression, expression 1, expression 2,?? , the expression N, the order of evaluation of the comma expression is: The value of the expression 1 is evaluated first, then the value of the expression 2 is computed, and so on, the expression n is finally evaluated, the last calculated expression N is the value of the entire comma expression. For example: W--,j+=3,k++,p=w+j+k is a comma expression, if the initial state of w=5,j=6,k=7, then the value of the comma expression is 21 (5+9+7). For each expression in a comma expression, except the last one, it must be a side-effect expression that makes sense, as in the previous example. The expressions in the comma expression like x=5,6+18,72*9,y=10 6+18 and 72*9 are expressions without side effects and are meaningless when evaluated.


4. 2 expression Statements in C + +, any expression can be used as a statement, called an expression statement, by adding a statement terminator (i.e.;) after the expression. C + + expressions can be used as statements because C + + extends the concept of expressions, and the actions with side effects such as assignment are grouped into the category of expressions. All input and output operations are implemented through functions, and there are no specialized input and output statements, and function calls are also a form of expressions. Therefore, in addition to the process control statements, other such as assignment, input, output, etc. can only be implemented through expressions. When an expression is used as a statement, its value is discarded, its statement function is manifested by its side effects, and it is meaningless to use the expression without side effects as a statement, such as expression statement k+19; The value of the expression (K and 19 sum) is discarded no, it is meaningless statement It is only meaningful to use expressions with side-effects as statements, like expression statements x+=5; The value of an expression is discarded, but since + = is an operator with side effects, the value of the evaluated expression is actually saved in the variable x, so the expression statement makes sense. In short, the side-effects of the expression in the C + + language is not necessarily bad, understand its concept, grasp the operator with side effects, so as to deal with it, to design a program to bring high efficiency.

Side-effects of expressions explain "references"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.