Let's say I have a two classes: Serializable
and Printable
.
So a simple template function which accepts all derived classes of Printable
could look like:
template <class T, class B = Printable, class = typename std::enable_if<std::is_base_of<B, T>::value>::type>
void print(T value) {
cout << value << endl;
}
However, if I want it to accept also all derived classes of Serializable
while I still have control over the function body, this would obviously not work:
template <class T, class B = Printable, class = typename std::enable_if<std::is_base_of<B, T>::value>::type>
void print(T value) {
cout << value << endl;
}
template <class T, class B = Serializable, class = typename std::enable_if<std::is_base_of<B, T>::value>::type>
void print(T value) {
cout << value << endl;
}
// Error: Redefinition of ...
So I figured the remaining solutions for this problem are template specializations.
But I just can't figure out, how I can specialize a template in combination with std::is_base_of
and std::enable_if
.
I hope someone is willing to help me!