Thread overview
How to unpack template parameters,Or how to forward parameters?
May 06, 2022
zjh
May 06, 2022
vit
May 06, 2022
zjh
May 06, 2022

Hello everyone,I have following function:

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?

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:

extern(C):void main()
{
    int[2] a=[2,4];
    f(a);//calling like this
}
May 06, 2022

On Friday, 6 May 2022 at 00:41:18 UTC, zjh wrote:

>

Hello everyone,I have following function:

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?

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:

extern(C):void main()
{
    int[2] a=[2,4];
    f(a);//calling like this
}

Try this:

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 06, 2022

On Friday, 6 May 2022 at 05:44:39 UTC, vit wrote:

>

Try this:

Very Good,Thank you very much!