Fun surprising things

Timon Gehr timon.gehr at gmx.ch
Wed Oct 31 22:53:34 UTC 2018


On 31.10.18 15:54, Patrick Schluter wrote:
> On Wednesday, 31 October 2018 at 14:00:14 UTC, bauss wrote:
>> What's the output of this program? (Try to figure it out without 
>> running it.)
>>
>> ```
>> int c = 0;
>>
>> int a()
>> {
>>     return c++;
>> }
>>
>> int b()
>> {
>>     return c--;
>> }
>>
>> void main()
>> {
>>     import std.conv : to;
>>     import std.stdio : writeln;
>>
>>     writeln(to!string(a) ~ to!string(b));
>> }
>> ```
>>
>> If your answer is "10" then you're wrong.
>>
>> You can get the result and answer here: https://run.dlang.io/is/idZurR
> 
> 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.
> 
> therefore -10 or 01 are both right.

No, the spec says that 01 is the only allowed result.
https://dlang.org/spec/expression.html#order-of-evaluation

Concatenation is a binary expression.


More information about the Digitalmars-d mailing list