jeudi 21 mai 2015

C++ template for numeric types

I want to have a template to select on numeric types, but I want to also have a global type template too. I tried to apply the solution for this question, but it didn't work:

template<typename T, typename ... Types>
void myFct(T arg1, Types ... rest) { /*do stuff*/ }

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type, 
                     typename ... Types>
void myFct(T arg1, Types ... rest) { /* do stuff */ }

because now I have two functions with the same header. What's the right way to do something like:

template<typename T, typename ... Types>
void myFct(T arg1, Types ... rest) 
{ 
    if (isNumeric(T))
        doNumericStuff();
    else
        doStuff();
}

Aucun commentaire:

Enregistrer un commentaire