samedi 20 janvier 2018

Templates - check if T inherits from A, where Q is a base class of T

I'm struggling with a compile time check whether some class inherits from a template class:

template<typename T> class A { ... }

template<typename T> class B {
  T* ptr;
  bool test() {
    // shall return true, if T inherits from A<Q>, where Q is a base class of T (or T itself)
  }
}

I've tried something like this:

  template<typename Q = T> typename std::enable_if_t<std::is_base_of_v<A<Q>, Q>, bool> test(){
    return true;
  }
  template<typename Q = T> typename std::enable_if_t<!std::is_base_of_v<A<Q>, Q>, bool> test(){
    return false;
  }

But this obviously won't work since it only checks whether T inherits from A<T>.

Aucun commentaire:

Enregistrer un commentaire