I will define, technology, asked the question form, summary examples to illustrate my understanding of the extraction mechanism.
1. Definitions: a traits Chinese characteristics mean that by extracting different types of common, so that the processing can be unified.
2. technology: traits using explicit template specialization be extracted code segments varies because of the different types, with a uniform interface to the package, and by the class disclosed traits template indirect access interface corresponding class.
3. asked the question in the form:
Question 1: What is explicit template specialization it?
A: template specialization is divided into a partial specialization (meaning that there is no complete specialization) we look at a piece of code,
Template < class T, class the U->
// foundation template class class NumTraits {}; // specialized format template Template < class T> // partial specialization class NumTraits <IntArray> { public : typedef int resultType; typedef int inputargtype ; }; template < class T> class NumTraits {}; // specialized format template template <> // specialization class NumTraits <IntArray> { public: typedef int resulttype; typedef int inputargtype; };
Question 2: use examples to show you why using an extraction mechanism?
A: I would use (3-step) code to explain why you need to use extraction.
#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 << " a float SUM = " << SUM << endl; return SUM * Times; } Private : a float * F; }; // ② template wording Template < class T> class the Apply { public : a float GetSum (T & T, a float inarg ) { return t.GetSum (inarg); } }; // the above method can not completely solve our problem (function return value is fixed, it will result in an exception) // ③ extracted with mechanisms: template specialization 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; the Apply <IntArray> AI; / / template using the Apply <FloatArray> AF; // use template COUT << " . 1 and 3 times the integer array: " << ai.GetSum (intary, 3 ) << endl; COUT << " . 1 floating-point array and 3.2 times: " << af.GetSum (floatary, 3.2f) << endl; the cout<< endl; COUT << endl; Apply2 <IntArray> AI2; // extracted with Apply2 <FloatArray> AF2; / / was extracted COUT << " and three times the integer array 2: " << ai2.GetSum (intary , . 3 ) << endl; COUT << " and 3.2 times the floating-point array 2: " << af2.GetSum (floatary, 3.2F ) << endl; return 0 ; }
4. Examples Summary:
The first step ①: we will find a high degree of code redundancy, so use the second;
The first step ②: We will find that after using the template, the amount of code is reduced, but its function inside the class definition appears in the form of a fixed type. If they are complex issues, it can lead to erroneous data.
Step ③: We use the mechanism traits, depending on the specialization class type corresponding type of function parameters and return value type, so that through a unified interface to achieve different instances.
Thus, the extraction mechanism of our coding reusability, help is still great! ! ! ! ! ! !
Reference books: "C ++ STL Fundamentals and Applications"