template<typename T> struct _type_traits { //泛化 T t; }; template<> struct _type_traits<int> { //特化 int i; }; _type_traits<Foo>::t;//使用泛化版本 _type_traits<int>::i;//使用特化版本 template <typename T,class Alloc=alloc> class vector { //模板中所有的参数类型都是泛化 }; template <typename Alloc> class vector<bool,Alloc> { //将模板中的参数一个特化,这是偏特化,类似偏倒数 }; template<class T> struct iterator_traits //泛化版本traits,如果传递的是一个迭代器 { typedef typename T::value_type value_type; }; template<class T> struct iterator_traits<T*> //特化版本traits,如果传递的不是迭代器而是一个原生的指针 { typedef T value_type; }; int main() { vector<int>vec{1,2,3}; typedef iterator_traits<int*>::value_type type1;//使用特化版本traits推倒类型 type1 t1=0;//type1类型为int typedef iterator_traits<vector<int>::iterator>::value_type type2;//使用泛化版本traits推倒类型 type2 t2=0;//type2类型为int return 0; }