Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
August 22, 2016 Metaprogramming, generate argument list. | ||||
---|---|---|---|---|
| ||||
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 } |
August 23, 2016 Re: Metaprogramming, generate argument list. | ||||
---|---|---|---|---|
| ||||
Posted in reply to ciechowoj | 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. |
August 23, 2016 Re: Metaprogramming, generate argument list. | ||||
---|---|---|---|---|
| ||||
Posted in reply to ciechowoj | 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));
}
|
August 23, 2016 Re: Metaprogramming, generate argument list. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Applegame | 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. |
August 24, 2016 Re: Metaprogramming, generate argument list. | ||||
---|---|---|---|---|
| ||||
Posted in reply to ciechowoj | 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. |
Copyright © 1999-2021 by the D Language Foundation