[Template] Mixins and foreach

Nicholas Wilson iamthewilsonator at hotmail.com
Sun Oct 1 06:27:21 UTC 2017


On Sunday, 1 October 2017 at 04:44:16 UTC, Jonathan M Davis wrote:
> I don't see any reason why the compiler would be complaining 
> about 'this' being required.

Neither do I.

> I would think that that would imply that the compiler thinks 
> that you're accessing the member rather than introspecting on 
> it.

Perhaps, but I would expect cannot access variable at compile 
time of something like that , not "need 'this'".

> The fact that you're passing raw to generateGetInfo does seem 
> off though, since it wouldn't be accessible at compile time. I 
> would have expected that to result in a compilation error, but 
> if it's complaining about 'this' when accessing foo rather than 
> complaining about raw, then that implies that raw isn't the 
> problem - or at least not that problem.

'raw' is an ellipse arg and I think that defaults to by alias, 
which is what I want. some of the structs have multiple arguments 
that i need to pass.

I tried en eponymous template:

string generateGetInfo(Info,alias func,args...)()
{
     template helper(Fields...)
     {
         static if (Fields.length == 0)
             enum helper = "";
         else
         {
             enum helper = "@property " ~ 
typeof(Field[0]).stringof ~ " " ~ Field[0].stringof ~ "()" ~
                 "{ " ~
                 "    typeof(return) ret;" ~
                 "%1$s(%2$s,"~ __traits(getAttributes, 
Field[0]).stringof ~ ",ret.sizeof,&ret,null);" ~
                 "return ret; " ~
                 "}\n" ~ helper!(Fields[1 .. $]);
         }
     }

     return 
helper!(Info.tupleof).format(func.stringof,args.stringof); // 
line 72
}

And am getting
util.d(72,12): Error: template instance helper!(foo) cannot use 
local 'foo' as parameter to non-global template helper(Fields...)



More information about the Digitalmars-d-learn mailing list