Search code examples
c++clanguage-designassignment-operatorsequence-points

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.


Solution

  • 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.