digitalmars.D.learn - Metaprogramming, generate argument list.
- ciechowoj (18/18) Aug 22 2016 Is it possible to generate an argument list that contains
- Nicholas Wilson (9/27) Aug 22 2016 template Repeat( int N, alias variable)
- Jack Applegame (16/34) Aug 23 2016 This is impossible since pointers to local variables are unknown
- ciechowoj (5/21) Aug 23 2016 This is a bit strange, as the local variables aren't known either
- Jack Applegame (4/12) Aug 24 2016 D doesn't accept aliases to expressions, only symbols and
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
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
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
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
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#TemplateAliasParameterAlias 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