前言
       我将从定义、技术实现、设问形式、实例总结来阐述我对于萃取机制的理解。
定义
       traits中文意思是特性,它通过提取不同类的共性,使得可以统一处理。
技术实现
       traits运用显式模板特殊化将代码中因为类型不同而发生变化的片段提取出来,用统一的接口来包装,并通过traits模板类公开的接口间接访问相应的类。
设问形式
       问题1:什么是显式模板特殊化呢?
       答:模板特殊化又分了一个偏特化(意思就是没有完全的特化)我们来看一段代码
| 1 2 3 4 5 6 7 8 9 10 11 12 13
 | template<class T> class NumTraits {}; template<>  class NumTraits<IntArray> { public:     typedef int resulttype;     typedef int inputargtype; };
 | 
       问题2:用实例来展示一下为什么会使用萃取机制?
       答:我会用(3步走)的代码来解释为什么需要使用萃取。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
 | #include<iostream> using namespace std; class IntArray { public:     IntArray()     {         a = new int[10];         for(int i = 0; i < 10; ++i)         {             a[i] = i + 1;         }     }          ~IntArray()     {         delete[] a;     }     int GetSum(int times)     {         int sum = 0;         for(int i = 0; i < 10; ++i)         {             sum += a[i];         }         cout << "int sum=" << sum << endl;         return sum * times;     } private:     int *a; }; class FloatArray { public:     FloatArray()     {         f = new float[10];         for(int i = 1; i <= 10; ++i)         {             f[i - 1] = 1.0f / i;         }     }          ~FloatArray()     {         delete[] f;     }          float GetSum(float times)     {         float sum = 0.0f;         for(int i = 0; i < 10; i++)         {             sum += f[i];         }         cout << "float sum=" << sum << endl;         return sum * times;     } private:     float* f; }; template<class T> class Apply { public:     float GetSum(T& t, float inarg)     {         return t.GetSum(inarg);     } }; template<class T> class NumTraits {}; template<> class NumTraits<IntArray> { public:     typedef int resulttype;     typedef int inputargtype; }; template<> class NumTraits<FloatArray> { public:     typedef float resulttype;     typedef float inputargtype; }; template<class T> class Apply2 { public:     NumTraits<T>::resulttype GetSum(T& obj, NumTraits<T>::inputargtype inputarg)     {         return obj.GetSum(inputarg);     } }; int main() {     IntArray intary;     FloatArray floatary;     Apply<IntArray> ai;       Apply<FloatArray> af;       cout << "1整型数组的和3倍:" << ai.GetSum(intary, 3) << endl;     cout << "1浮点数组的和3.2倍:" << af.GetSum(floatary, 3.2f) << endl;     cout<<endl;     cout<<endl;     Apply2<IntArray> ai2;       Apply2<FloatArray> af2;      cout << "2整型数组的和3倍:" <<ai2.GetSum(intary,3) << endl;     cout << "2浮点数组的和3.2倍:" << af2.GetSum(floatary,3.2f) << endl;     return 0; }
 | 
实例总结
       第①步:我们会发现代码冗余度很高,所以采用了第二种;
       第②步:我们会发现在运用模板后,代码量是减少了,但是其类内部函数定义出现了固定形式的类型。若遇到复杂的问题,会导致数据的错误。
       第③步:我们运用了traits机制,将根据不同类类型特化出相应的函数参数类型和返回值类型,这样就可以通过统一的接口,来实现不同的实例。
       由此,萃取机制对我们编码的复用性,帮助还是很大的!!!!!!!