vendredi 14 décembre 2018

Instantiation of function template with variadic parameter pack

Suppose say I have this code:

template<int... Us>
struct Matrix{};

template<int... U1, int... U2>
auto compute(Matrix<U1...>, Matrix<U2...>){return 0;}

Matrix<1> a; Matrix<2,3> b;
Matrix<1,2> c; Matrix<3> d;

int main(){   
    compute(a,b);
    compute(c,d);
    auto fp = &compute<1,2,3>;
    fp(a,b);
    fp(c,d);
}

Would the two compute() calls instantiate just one function template i.e. compute<1,2,3> or would there be two different instantiations depending on the arguments?

I wanted to confirm this by taking a function pointer to the particular instantiation and see if I could call the function with the 2 different sets of arguments using the same function pointer but I get the following errors at the line where I call fp(a,b):

[x86-64 gcc 8.2 #1] error: could not convert 'a' from 'Matrix<#'nontype_argument_pack' not supported by dump_expr#>' to 'Matrix<#'nontype_argument_pack' not supported by dump_expr#>'

Aucun commentaire:

Enregistrer un commentaire