How to unpack template parameters,Or how to forward parameters?

vit vit at vit.vit
Fri May 6 05:44:39 UTC 2022


On Friday, 6 May 2022 at 00:41:18 UTC, zjh wrote:
> Hello everyone,I have following function:
> ```d
> import core.stdc.stdio;
> void f(int i,int j){
>     printf("%i,%i",i,j);
> }
> ```
> I want to forward `int[N]` to `(int i,int j)` etc. How can I 
> write a forwarding function?
> ```d
> void ff(alias g,int...I)(int[2]k){
>     g(k[I]);
> }//How to unpack template variable parameters?
>
> void f(int[2]k){
>     ff!(f,0,1)(k);//how to forward?
> }
>
> ```
>
> main function:
> ```d
> extern(C):void main()
> {
>     int[2] a=[2,4];
>     f(a);//calling like this
> }
> ```

Try this:

```d
import core.stdc.stdio;

void f(int i,int j){
     printf("%i,%i",i,j);
}


import std.range :  iota;
import std.traits :  isStaticArray;
import std.meta :  AliasSeq;


template expand(alias arr)
if(isStaticArray!(typeof(arr))){
     auto get(size_t I)(){
         return arr[I];
     }

     alias Result = AliasSeq!();

     static foreach(I; iota(0, arr.length))
         Result = AliasSeq!(Result, get!I);

	alias expand = Result;
}


extern(C):void main(){
     int[2] a=[2,4];
     f(expand!a);
}
```


More information about the Digitalmars-d-learn mailing list