Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any good reason why assignment operator isn't a sequence point?

Is there any good reason for operator = not being a sequence point? Both in C and C++.

I have trouble thinking about an counter-example.

like image 998
Šimon Tóth Avatar asked Dec 06 '10 01:12

Šimon Tóth


People also ask

Is assignment a sequence point?

because there is no sequence point specified for the assignment, increment or index operators, you don't know when the effect of the increment on i occurs. The sequence points laid down in the Standard are the following: The point of calling a function, after evaluating its arguments.

What are the advantages of assignment operator?

The basic advantage of compound assignment operators is that it saves a lot of code within the Java language program.

What is the purpose of using assignment operator?

The assignment operator = assigns the value of its right-hand operand to a variable, a property, or an indexer element given by its left-hand operand. The result of an assignment expression is the value assigned to the left-hand operand.


1 Answers

By request:

In general, things need a reason to be a sequence point. They don't need a reason not to be a sequence point; that's the default.

For example, && must be a sequence point because of short-circuiting behaviour: if the left-hand side is false, the right-hand side must not be evaluated. (This is not just about optimization; the right-hand side could have side effects, and/or depend on the left-hand side being true, as in ptr && ptr->data.) Therefore the left-hand side must be evaluated first, before the right-hand side, in order to see if the right-hand side should be evaluated at all.

This reason does not exist for = because, although there is "evaluation" to do for both sides (although there are different restrictions on what can appear on both sides: the left-hand side must be an lvalue - the l doesn't stand for "left", btw; it stands for "location", as in location in memory - we can't assign to a temporary or a literal), it doesn't matter which side is evaluated first - as long as both sides are evaluated before the actual assignment.

like image 59
Karl Knechtel Avatar answered Sep 27 '22 22:09

Karl Knechtel