More from C++ Now 2014

Namespace via Digitalmars-d digitalmars-d at puremagic.com
Mon May 19 01:20:52 PDT 2014


On Sunday, 18 May 2014 at 22:29:04 UTC, bearophile wrote:
>> "Undefined Behavior in C++; what is it, and why should I care":
>> https://github.com/boostcon/cppnow_presentations_2014/blob/master/files/Undefined-Behavior.pdf?raw=true
>> This reminds us to remove as much undefined behavior as 
>> possible from D.
>
> Fixing some of those problems will break some D code. An 
> example from the slides pack:
>
> void main() {
>     import std.stdio;
>     auto arr = [0, 2, 4, 6, 8];
>     int i = 1;
>     writeln(i + arr[++i] + arr[i++]);
> }
>
>
> It gives different results in dmd and ldc2:
>
> ...>dmd -wi -run temp.d
> 9
>
> ...>ldmd2 -wi -run temp.d
> 10
>
> Hopefully no one writes code like that, but this situation 
> can't be accepted in a language that tries to be safer and 
> better than C/C++.
>
> Bye,
> bearophile

dmd is right, so ldc has probably a bug.
i is 1
++i is 2 -> arr[2] is 4
i++ is still 2 -> arr[2] is 4
1 + 4 + 4 = 9


More information about the Digitalmars-d mailing list