Use std::is_base_of along with std::enable_if :
template<typename T, typename X = std::enable_if<std::is_base_of<A, T>::value>::type> class D{...}
Note that it will accept any T if it is derived from A If you need T be either B or C , you need to change it and use std::is_same or / and std::conditional along with std::enable_if .
You can make it clean like:
template<typename T, typename Unused = extends<T,A>> class D{...}
where extends is defined as:
template<typename D, typename B> using extends = typename std::enable_if<std::is_base_of<B,D>::value>::type;
static_assert can also be used (as other answers showed) if you want it to lead to an error and compilation failure. However, if you need to choose or deselect, say, for many specializations, use the above approach.
Hope this helps.
source share