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.
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>{}, "");
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