mardi 10 août 2021

With variadic function templates can we design a function that can be called with at lease 2 arguments

I am just beginning to understand variadic function templates. Is it possible to have a definition where the generic case (handling recursion) be written 2 or more arguments. That is, I am wondering can I write something as follows :

template <class T>
void min(T val1, T val2) { return val1>val2 ? val2 : val1 ; }

template <class T, class... Others>
void min(T a1, T a2, Others ... others) {
    // implementation
    T a = a1 > a2 ? a2 : a1 ;
    // min(a, first-element-in-others, all-elems-in-others-except-first) ;
}

If this is possible, can someone show me how

Aucun commentaire:

Enregistrer un commentaire