Fun surprising things

Patrick Schluter Patrick.Schluter at bbox.fr
Fri Nov 2 09:27:50 UTC 2018


On Friday, 2 November 2018 at 08:15:28 UTC, Kagamin wrote:
> On Wednesday, 31 October 2018 at 14:54:48 UTC, Patrick Schluter 
> wrote:
>> 01 is the other possible result. It all depends in what order 
>> a and b are called. ~ is not a sequence point (does D even 
>> have that typical C notion of sequence points?) so the order 
>> of evaluation is at the discretion of the compiler.
>
> In C function call is a sequence point, undefined order applies 
> only to expressions without sequence points.

Yes, but irrelevant here. f(g(x), h(y)) doesn't mandate in which 
order g(x) and h(y) are evaluated even though x will be evaluated 
before g is called and y before h is called. That's all the 
sequence point of function call can do (btw, I wouldn't rely, in 
C, on that sequence point as inlining often breaks it).

>BTW to!string
> allocates memory from GC which involves a mutex lock, which 
> can't possibly have undefined order.

Same remark, doesn't change anything.

f(g(x), h(y))
only garantees these 2 orders

push x
sequence point
call g
push g(x)
push y
sequence point
call h
push h(y)
sequence point
call f

or

push y
sequence point
call h
push h(y)
push x
sequence point
call g
push g(x)
sequence point
call f

theoretically also parallel evaluation of g(x) and h(y) but in 
practice I've never seen it.


But all this is moot as D is not C and the specs of D mandate the 
order of evaluation for ~. dmd and gdc clearly violate the specs 
and either the specs are loosened or the compiler is corrected.











More information about the Digitalmars-d mailing list