<T extends Car> void startEngine(T c) {}
In C++, equivalent of the above would be this:
template<typename T, typename Unused= typename std::enable_if<std::is_base_of<Car,T>::value>::type>
void startEngine(T c) {}
Alright the syntax is ugly, but you can make it a bit nicer with alias as:
//first define a (reusuable) alias
template<typename D, typename B>
using extends = typename std::enable_if<std::is_base_of<B,D>::value>::type;
//then your code would look like this
template<typename T, typename Unused=extends<T,Car> >
void startEngine(T c)
{
}
Or you can use static_assert
, as the other answer explains. But std::enable_if
and static_assert
are not equivalent. While static_assert
gives you an opportunity to produce good error messages, std::enable_if
helps you to resolve overloads, which means the above function will be invoked only if Car
is a base of T
, else other overload, if any, will be selected/considered. With static_assert
, that is not possible: it simply fails and stop — it doesn't look further for overloads.
Likewise,
//then your code would look like this
template<typename T, typename Unused=extends<T,Comparable<T>> >
void compareWith(T c)
{
}
This technique is known as:
Hope that helps.