I would like to know how to write a type_traits
class to detect whether two types are specializations of the same template class. The big problem is that it should work for mixed types/non-types template class like:
template <typename T, std::size_t N>
class MyClass {};
Is it possible to design a such thing?
I don't think you can do this in general for an arbitrary class template with a mix of type and non-type parameters.
You can get close for more specific sets of parameters, but I don't know any way to handle the general case:
#include <type_traits>
template <typename T, std::size_t N>
class MyClass {};
// assume not the same
template<typename T, typename U>
struct my_trait : std::false_type
{ };
// both specializations of MyClass
template<typename T1, std::size_t N1, typename T2, std::size_t N2>
struct my_trait<MyClass<T1, N1>, MyClass<T2, N2>>
: std::true_type
{ };
// both specializations of some class template Templ<typename, std::size_t>
template<template<typename, std::size_t> class Templ, typename A1, std::size_t S1, typename A2, std::size_t S2>
struct my_trait<Templ<A1, S1>, Templ<A2, S2>>
: std::true_type
{ };
// both specializations of some class template Templ<typename...>
template<template<typename...> class Templ, typename... A1, typename... A2>
struct my_trait<Templ<A1...>, Templ<A2...>>
: std::true_type
{ };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With