mercredi 29 juin 2016

One template specialization for PODs, one for class hierarchy and error in other cases?

I'm trying to create a template which would work one way for all fundamental types, other way for all classes deriving from A and fail for everything else. Reading about SFINAE, I created such thing:

struct A {};
struct B : A
{
    int a;
};
template <typename T, typename = typename std::enable_if<std::is_base_of<A, T>::value>::type>
void foo(const T& derived)
{
    std::cout << "stuff with derived";
}

template <typename T, typename = typename std::enable_if<std::is_fundamental<T>::value>::type>
void foo(const T& pod)
{
    std::cout << "stuff with fundamental";
}

int main()
{
    foo(7);
    B instance;
    foo(instance);
}

From what I understood, matching template types for 7 (int) fails for first case and works for second, and derived class works for first. What mistake am I doing?

Aucun commentaire:

Enregistrer un commentaire