Order of evaluation vs. sequence points

Jens Mueller jens.k.mueller at gmx.de
Mon Aug 8 07:48:37 PDT 2011


Hi,

at http://www.d-programming-language.org/expression.html the following
expressions are said to be illegal:

i = i++;
c = a + (a = b);

The documentation says they are illegal because the order of evaluation
is implementation-defined. That is possible because the order of
evaluation of the AssignExpression is implementation-defined.
I'm having trouble understanding the difference between order of
evaluation and sequence points.
Sequence points are the points in execution where all side effects of
previous expressions must have been performed.
An analysis for the second example:

int a = 1;
int b = 2;
int c;
c = a + (a = b);
-> c = 1 + (a = b) (AddExpression is evaluated left-to-right http://www.d-programming-language.org/expression.html#AddExpression)
-> c = 1 + 2 (with side effect a = 2)
-> c = 3

It seems that D follows C/C++'s sequence points. I.e. the side effect 'a
= 2' has to be performed until specific execution points. I believe the
above expression is illegal because the side effect may be executed
before the subexpression 'a' is evaluated. In this reading evaluating an
expression does not say anything about sequence points. I.e. in the
above case it is not the order of evaluation that makes the expression
illegal. It is C/C++'s inherited sequence points. This complicates
matters in contrast to Java where each (sub)expression is a sequence
point.
Leaving behavior (implementation-)undefined usually allows the compiler
to better optimize. What are the usual/optimal gains for less
restrictive sequence points (i.e. more flexible operation reordering)?
Why did D choose to follow C/C++'s defined sequence points?

When is an expression considered to be fully evaluated? Does it include
performing its side effects? I believe it does not, because then the
above expressions would be legal.

Jens


More information about the Digitalmars-d mailing list