Metaprogramming, generate argument list.

ciechowoj via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Aug 23 14:14:01 PDT 2016


On Tuesday, 23 August 2016 at 07:17:16 UTC, Jack Applegame wrote:
> This is impossible since pointers to local variables are 
> unknown at compile time.

This is a bit strange, as the local variables aren't known either 
and they seem to work. I do not want to get the address, rather 
an alias to `&variable` expression.

> But you can generate arguments list that contains functions 
> that return pointers at run-time:
>
> template Repeat(int N, alias variable) {
> 	auto ptr() @property { return &variable; }
> 	import std.meta : AliasSeq;
> 	static if(N == 1) alias Repeat = ptr;
> 	else alias Repeat = AliasSeq!(ptr, Repeat!(N-1, variable));
> }
>
> void foo(int* x, int* y, int* z) {
> }
>
> void main() {
>     int bar = 42;
>     foo(Repeat!(3, bar));
> }

Anyway, this solution works perfectly fine, thanks.





More information about the Digitalmars-d-learn mailing list