Order of evaluation of a += a++;

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 30 11:30:04 PDT 2015

My understanding is that, as per spec, a += k is rewriten as :

a = cast(typeof(a)) (a + k);

Which make our example looks like :

a = cast(typeof(a)) (a + a++);
a = cast(typeof(a)) (a + { auto olda = a; a++; return olda; }());

The whole damn thing should end up with newa == 2 * olda . This 
is what SDC does. DMD tells me newa == 2 * olda + 1, as it 
evaluate a++ before that the value of a for the sum.

I though we decided to do everything LTR, but discussing with 
Andrei, it seems that Walter oppose this in some special cases. I 
think we should keep it consistent and do LTR all the way down.

Relevant bug report: 

More information about the Digitalmars-d mailing list