www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Metaprogramming, generate argument list.

reply ciechowoj <keepitsimplesirius gmail.com> writes:
Is it possible to generate an argument list that contains 
pointers to local variables at compile time?

For example, consider following code:

template Repeat(alias int N, alias variable)
{
     // Magic

     alias Repeat = /* Even more magic */;
}

void foo(int* x, int* y, int* z)
{
     // [...]
}

void fun()
{
     int bar = 42;

     foo(Repeat!(3, bar)); // want to replace it with &bar, &bar, 
&bar
}
Aug 22 2016
next sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Monday, 22 August 2016 at 22:01:51 UTC, ciechowoj wrote:
 Is it possible to generate an argument list that contains 
 pointers to local variables at compile time?

 For example, consider following code:

 template Repeat(alias int N, alias variable)
 {
     // Magic

     alias Repeat = /* Even more magic */;
 }

 void foo(int* x, int* y, int* z)
 {
     // [...]
 }

 void fun()
 {
     int bar = 42;

     foo(Repeat!(3, bar)); // want to replace it with &bar, 
 &bar, &bar
 }
template Repeat( int N, alias variable) { static if (N == 1) alias Repeat = variable; else alias Repeat = AliasSeq!(variable,Repeat!(N-1,variable)); } Not sure if it will work with addresses of variables.
Aug 22 2016
prev sibling parent reply Jack Applegame <japplegame gmail.com> writes:
On Monday, 22 August 2016 at 22:01:51 UTC, ciechowoj wrote:
 Is it possible to generate an argument list that contains 
 pointers to local variables at compile time?

 For example, consider following code:

 template Repeat(alias int N, alias variable)
 {
     // Magic

     alias Repeat = /* Even more magic */;
 }

 void foo(int* x, int* y, int* z)
 {
     // [...]
 }

 void fun()
 {
     int bar = 42;

     foo(Repeat!(3, bar)); // want to replace it with &bar, 
 &bar, &bar
 }
This is impossible since pointers to local variables are unknown at compile time. 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)); }
Aug 23 2016
parent reply ciechowoj <keepitsimplesirius gmail.com> writes:
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.
Aug 23 2016
parent Jack Applegame <japplegame gmail.com> writes:
On Tuesday, 23 August 2016 at 21:14:01 UTC, ciechowoj wrote:
 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.
D doesn't accept aliases to expressions, only symbols and literals. Spec: https://dlang.org/spec/template.html#TemplateAliasParameter
 Alias parameters enable templates to be parameterized with 
 almost any kind of D symbol, including user-defined type names, 
 global names, local names, module names, template names, and 
 template instance names. Literals can also be used as arguments 
 to alias parameters.
Aug 24 2016