Check if the class is a Specialization?
How can I check if a given type is a specialization of a particular class template? For example, given
template <class T>
struct A {};
How to check if CompareT
A<*>
for some type is *
as follows:
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.
source to share
Here's where you can provide a pattern to match 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>{}, "");
source to share
CompareT
is the type and A
is the pattern. Likewise, a class cannot be a template. The class can be a specialization of the template, so I'll assume whatever you want.
Partial specialization can do pattern matching:
template<class T>
struct is_an_A // Default case, no pattern match
: std::false_type {};
template<class T>
struct is_an_A< A< T > > // For types matching the pattern A<T>
: std::true_type {};
template< class CompareT >
void compare() {
std::cout << is_an_A< CompareT >::value;
}
source to share