User Tools

Site Tools


misc:partialtemplatespecialisation

Little example for partial template specialisation. Inspired by http://www.glenmccl.com/tip_027.htm

#include <cstdio>
 
template <class T>
struct detector {
  static void magic(T t) {
    printf("is element\n");
  }
};
 
template <class T>
struct detector<T*> {
  static void magic(T *t) {
    printf("is pointer\n");
  }
};
 
template <class T>
void detect(T t) {
  detector<T>::magic(t);
}
 
int main() {
  int a;
  int *b = &a;
  int &r = a;
  detect(a);
  detect(b);
  detect(r);
}
misc/partialtemplatespecialisation.txt · Last modified: 2008/10/18 17:56 (external edit)