Why do ints defined in template mixins have garbage values?

Steven Schveighoffer schveiguy at gmail.com
Tue Dec 11 21:19:59 UTC 2018


On 12/11/18 4:09 PM, 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.

Looks like a bug to me in static foreach.

Using -vcg-ast, it appears that the int definitions do not show up in main.

If I add an int using a regular declaration or a straight mixin("int i0 
= 5;"); then the variable shows up.

-Steve


More information about the Digitalmars-d-learn mailing list