mardi 21 février 2017

Array Elements to Variadic Template arguments

I have a question on how to accomplish the following behavior, or if it is even possible in C++. Say I have this variadic function

template<typename T, typename...Pack>
T sum(T a, Pack... pack) {
    return a + sum(pack...);
}

template<typename T>
T sum(T a) {
    return a;
}

and then I have an array

double array[5];

is there a way I could go about doing something like

sum(array[0], array[1], array[2], array[3], array[4]);

without hard coding in each element explicitly? The reason, I am trying to implement something such as,

double array[N];
sum(array[0], array[1], array[2], ..., array[N-1]);

where N is set at compile time.

Aucun commentaire:

Enregistrer un commentaire