Home > database >  Check if class is derived from templated class
Check if class is derived from templated class

Time:07-19

I'm trying to check if a class that I'm templating is inheriting from another templated class, but I can't find the correct way to do it.

Right now I have the following:

#include <iostream>

template <typename MemberType, typename InterfaceType>
class Combination : public InterfaceType
{
public:
    Combination();
    virtual ~Combination();

private:

    MemberType*   pointer;
};


class MyInterface {};
class MyMember {};

class MyCombination : public Combination<MyMember, MyInterface>
{

};


int main()
{
    static_assert(std::is_base_of_v<Combination<MyMember, MyInterface>, MyCombination>);
    std::cout << "hello";
}

Which is working fine, but in the place this template is placed, I don't have access to the template parameters definitions, and this should be quite generic. What I'd like is to test for the MyCombination class to inherit from a generic Combination without taking into account the template arguments, something like this:

static_assert(std::is_base_of_v<Combination, MyCombination>);
or
static_assert(std::is_base_of_v<Combination<whatever, whatever>, MyCombination>);

Would you know how to check for this? In this case I can't use boost libraries or other external libraries.

Thanks!

EDIT: I don't have access to modify the Combination class, and what I can change is the assert and the My* classes.

CodePudding user response:

This can easily be done with the C 20 concepts. Note that it requires a derived class to have exactly one public instantiated base class.

template <typename, typename InterfaceType>
class Combination : public InterfaceType {};

class MyInterface {};
class MyMember {};

class MyCombination : public Combination<MyMember, MyInterface> {};

template<class Derived, template<class...> class Base>
concept derived_from_template = requires (Derived& d) {
  []<typename... Ts>(Base<Ts...>&) {}(d);
};

static_assert(derived_from_template<MyCombination, Combination>);

Demo

The equivalent C 17 alternative would be

#include <type_traits>

template<template<class...> class Base, typename... Ts>
void test(Base<Ts...>&);

template<template<class...> class, class, class = void>
constexpr bool is_template_base_of = false;

template<template<class...> class Base, class Derived>
constexpr bool is_template_base_of<Base, Derived, 
  std::void_t<decltype(test<Base>(std::declval<Derived&>()))>> = true;

static_assert(is_template_base_of<Combination, MyCombination>);
  • Related