mardi 5 juillet 2016

Selecting one of 2 template functions for 4 types

Is there a cleaner ("less c++11") way of selecting which template function to use in this example, based on multiple types. I only want to have 2 functions, I could overload or call implementations with an extra 2 or 4 functions.

struct A{}; struct B{}; struct C{}; struct D{};

template <typename T>
typename std::enable_if<std::is_same<T, A>::value or std::is_same<T, B>::value, void>::type
foo(T& t)
{ 
    printf("first\n"); 
}

template <typename T>
typename std::enable_if<std::is_same<T, C>::value or std::is_same<T, D>::value, void>::type
foo(T& t)
{ 
    printf("second\n"); 
}

int main()
{
  A a; B b; C c; D d;
  foo(a); foo(b); foo(c); foo(d);
}

Aucun commentaire:

Enregistrer un commentaire