Boost logo

Boost Users :

Subject: [Boost-users] Detect generic base class
From: Francesco Biscani (bluescarni_at_[hidden])
Date: 2010-04-28 22:31:29


Hi list,

how can I detect if a class inherits from a generic class? E.g.,

template <class Derived>
class base {};

class final: public base<final> {};

I would like to specialise a type trait for all classes deriving from
base<whatever>, possibly using enable_if and is_base_of:

template <class T, class Enable = void>
class trait: boost::false_type {};

// What syntax here? Something like this (which does not work):
template <class T, class Derived>
class trait<T,typename
boost::enable_if<boost::is_base_of<base<Derived>,T> >::type>:
boost::true_type {};

The best I've come up with has been putting a typedef for Derived in
base, and then using SFINAE:

template <class Derived>
class base {
  public:
    typename Derived derived;
};

template <class T>
class trait<T,typename
boost::enable_if<boost::is_base_of<base<typename T::derived>,T>
>::type>: boost::true_type {};

But this is ugly and I'm sure there are much better ways of doing this :)

Thanks in advance,

  Francesco


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net