First we can write a tag to check if it is obtained Tfrom S, but not S:
template <class Base, class Derived>
using is_strict_base =
std::integral_constant<bool,
std::is_base_of<Base,Derived>::value &&
!std::is_same<Base,typename std::remove_cv<Derived>::type>::value>;
You can use std::enable_ifto use this feature:
template<class T>
typename std::enable_if<is_strict_base<S,T>::value>::type
foo(T* instance)
{}
++ 14 std::enable_if_t, :
template<class T>
std::enable_if_t<is_strict_base<S,T>::value>
foo(T* instance)
{}
- static_assert:
template<class T>
void foo(T* instance)
{
static_assert(is_strict_base<S,T>::value,
"T must be derived from S, but not S");
}
, , .