Tuesday, May 15, 2012

Determine if a class has a template specialization



#include<iostream>

template<typename T>
struct A{
  typedef int NotSpecialized;
};


template<typename T>
struct isSpecialized{
  typedef char yes;
  typedef char no[2];
  template<typename F>
  static no& test(typename F::NotSpecialized);
  template<typename F>
  static yes& test(double);
  static const bool value=(sizeof(test<T>(0))==sizeof(yes));
};


template<>
struct A<bool>{
};


int main(){
 
  std::cout<< isSpecialized<A<char> >::value<<" "<<isSpecialized<A<bool> >::value<<std::endl;

}


===output====

0 1

No comments:

Post a Comment