Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if two types are a specialization of a same class template?

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?

like image 335
Vincent Avatar asked Apr 21 '13 01:04

Vincent


1 Answers

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
{ };
like image 173
Jonathan Wakely Avatar answered Dec 04 '22 02:12

Jonathan Wakely