Подтвердить что ты не робот

Проверьте, унаследован ли аргумент шаблона от класса

Я хочу проверить, унаследован ли тип, заданный шаблону, из базового класса в моем проекте.

Он должен работать, как и следовало ожидать в следующем примере:

template< class T : public CBaseClass >
  • Можно ли сделать это с помощью шаблонов, если нет, как еще я могу это сделать?
4b9b3361

Ответ 1

Вы можете использовать boost::is_base_and_derived из Boost, в сочетании с BOOST_STATIC_ASSERT. Если вы используете компилятор с поддержкой TR1 или С++ 0x, в стандартной библиотеке есть эквиваленты этих конструкций (std:: is_base_of, и static_assert в С++ 0x).

Ответ 2

Следуя примеру от Stroustrup:

template<class Test, class Base>
struct AssertSameOrDerivedFrom {
  AssertSameOrDerivedFrom() { &constraints; }
public:
  static void constraints() {
    Test *pd = 0;
    Base *pb = pd;
  }
};

template<class T>
struct YourClass {
  YourClass() {
    AssertSameOrDerivedFrom<T, CBaseClass>();
  }
};

В С++ 0x это будет:

template<class T>
struct YourClass {
  static_assert(std::is_base_of<CBaseClass, T>::value);
};

Ответ 3

Если вы хотите утверждать, сделайте это Nurk. Если вы хотите проверить, используйте is_base_of из boost или С++ 0x. Если вы не можете использовать любой из них, используйте SFINAE:

template < typename Base, typename PotentialDerived >
struct is_base
{
  typedef char (&no)  [1];
  typedef char (&yes) [2];

  static yes check(Base*);
  static no  check(...);

  enum { value = sizeof(check(static_cast<PotentialDerived*>(0))) == sizeof(yes) };
};

Ответ 5

Короче лучше

template <typename Base, typename Derived>
struct is_base {
    constexpr static bool check(Base*)   { return true; }
    constexpr static bool check(...)     { return false; }
    enum { value = check(static_cast<Derived*>(0)) };
};

Пример 1:

struct A {};
struct B  : A { };

int main(void) {
    static_assert(is_base<A,B>::value, "If Error: A is not base of B");
}

Пример 2:

template <bool, typename T=void>
struct Use {
    static std::string info() { return "Implementation that consider that A is not base of B"; }
};

template <typename T>
struct Use<true,T>  {
    static std::string info() { return "Implementation that consider that A is the base of B"; }
};


int main(void) {
    std::cout << Use<is_base<A,B>::value>::info(); //Implementation that consider that A is the base of B
}