Check if two variable derived classes are created with the same package of parameters

In situations where you have a base and many derived classes, all of which encode a package of variables, something like the following

template<typename ... Args>
struct base
{};

template<typename ... Args>
struct derived_1: base <Args...>
{};
template<typename ... Args>
struct derived_2: base <Args...>
{};

How to check if objects of different derived classes were created using the same package. For example, given

derived_1<int,float,double> d1_obj;
derived_2<int,float,double> d2_obj;

I need a mechanism to tell me that both objects are equal, in the sense that they contain the same types in the same order.

0
source share
1 answer

You can do this using template template options and partial specialization for arguments:

template <class T, class U>
struct same_args : std::false_type{};

template <template <typename...> class T, template <typename...> class U,
          typename... Ts>
struct same_args<T<Ts...>, U<Ts...>> : std::true_type{};

Then it same_args<derived_1<int,float>, derived_2<int,float>>::valuewill be true.

Live Demo

+2
source

Source: https://habr.com/ru/post/1684948/


All Articles