Order of evaluation of post-increment operator

Joakim via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 28 07:01:28 PST 2014


On Sunday, 28 December 2014 at 14:51:22 UTC, Gary Willoughby 
wrote:
> I was just taking a look at the following poll[1] about the 
> order of evaluation when using the post-increment operator. The 
> following D snippet shows an example.
>
> 	import std.stdio;
>
> 	void main(string[] args)
> 	{
> 		auto foo = [0, 0];
> 		int i = 0;
>
> 		foo[i++] = i++; // Woah!
>
> 		writefln("%s", foo);
> 	}
>
> Apparently the C++ equivalent is undefined behaviour but when 
> run using D the following result is output:
>
> 	[1, 0]
>
> 1. Can someone please explain this output?
> 2. Is there anywhere this order of evaluation is documented?
> 3. Do you agree this is right?
>
> [1]: 
> http://herbsutter.com/2014/12/01/a-quick-poll-about-order-of-evaluation/

It has been pointed before that this behavior even varies by D 
compiler:

http://forum.dlang.org/post/swczuwclttmoakpvebut@forum.dlang.org

One of those many small details that will have to be tightened up 
in the spec someday.


More information about the Digitalmars-d-learn mailing list