Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if class is a template specialization? [duplicate]

Tags:

c++

templates

How can I check if a given type is a specialization of a particular class template? For instance, given

template <class T>
struct A {};

How can I check if CompareT is an A<*> for some type * in the following:

template<class CompareT>
void compare(){
   // is this A ?
   cout << is_same< A<*> , CompareT >::value;     // A<*> ????
}

int main(){
  compare< A<int> >();
}

For example, here A<int> should match A<*> and print 1.

like image 948
tower120 Avatar asked Aug 01 '15 14:08

tower120


1 Answers

Here's one where you can provide the template to be matched against :

template <class T, template <class...> class Template>
struct is_specialization : std::false_type {};

template <template <class...> class Template, class... Args>
struct is_specialization<Template<Args...>, Template> : std::true_type {};

static_assert(is_specialization<std::vector<int>, std::vector>{}, "");
static_assert(!is_specialization<std::vector<int>, std::list>{}, "");
like image 121
Quentin Avatar answered Oct 20 '22 21:10

Quentin