"a[++i] = i" vs "a[i] = ++i"

bearophile bearophileHUGS at lycos.com
Fri Dec 20 15:17:27 PST 2013


Ali Çehreli:

> Although D is less vocal on these topics it is the same as C 
> and C++: The evaluation order is unspecified. I've read before 
> that Walter wants to eventually define such evaluation orders 
> but it is not specified yet.

Right, currently the outcome of that kind of code is not 
specified in D. So writing it is a error, and the D compiler 
doesn't warn you. Do not write that kind of code in D.

Walter wants to eventually (but when? Five years from now?) make 
that code specified, this means all the D compilers will know 
what that kind of code produce and will all give the same result 
reliably.

But in my opinion making the code unambiguous and specified for 
the compiler is not enough. You have also to take in account 
people that write and read the code, it needs to be unambiguous 
for them too. For them that kind of code can become tricky, even 
when you know the rules used by the compiler. So I'd like that 
kind of D code just to be disallowed, to be an error (and this 
doesn't break compatibility with C, because equivalent C code is 
not defined).

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list