Why do ints defined in template mixins have garbage values?
Johannes Loher
johannes.loher at fg4f.de
Wed Dec 12 09:34:50 UTC 2018
On Tuesday, 11 December 2018 at 21:09:55 UTC, Johannes Riecken
wrote:
> Code:
>
> import std.conv;
> import std.stdio;
>
> mixin template genInts()
> {
> enum arr = [0,1];
> static foreach (t; arr) {
> mixin("int i" ~ to!string(t) ~ " = 5;");
> }
> }
>
> void main() {
> mixin genInts!();
> writeln(i0);
> writeln(i1);
> }
>
>
> Expected output:
> 5
> 5
>
> Actual output is two garbage integer values.
Definitely a compiler bug.
According to
https://run.dlang.io/gist/1e6e7574a8762d7fed5a3aa22390493d?compiler=dreg
Garbage values are printed since 2.078.1.
compiling with ldc results in a segmentation fault when running
the program:
https://run.dlang.io/gist/02780bfe33a2bfd000aa5718a3fe6505?compiler=ldc
More information about the Digitalmars-d-learn
mailing list