mercredi 9 juin 2021

How to define a template function with the help of std::enable_if

What I'm trying to do is to define a template function, which can only be specializd by the class, which inherits some classes.

For example, I have already had two class Base1 and Base2. I'm trying to define such a template function:

template<typename T>  // if (std::is_base_of<Base1, T>::value || std::is_base_of<Base2, T>::value)
std::ostream & operator<<(std::ostream &os, const T& t)
{
    // os << t.member1 << t.member2...;
    return os;
}

It seems that std::enable_if can help but I don't know how.

Aucun commentaire:

Enregistrer un commentaire