www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to unpack template parameters,Or how to forward parameters?

reply zjh <fqbqrr 163.com> writes:
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
}
```
May 05 2022
parent reply vit <vit vit.vit> writes:
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); } ```
May 05 2022
parent zjh <fqbqrr 163.com> writes:
On Friday, 6 May 2022 at 05:44:39 UTC, vit wrote:

 Try this:
Very Good,Thank you very much!
May 05 2022