The special and partial localization of C + + templates
Recently in the process of looking at the STL, found that they are unfamiliar with the knowledge of the template here, so today this part of the content to be collated, in case of forgetting.
The concept of C + + templates is not mentioned here, the default reader already has the basic knowledge.
The specificity of the template
Look at the code first:
#include <iostream>using namespace std; Template <class t>class tclass{public:bool Equal (const t& ARG, const t& arg1);}; Template <class t>bool tclass<t>::equal (const t& ARG, const t& arg1) {return (arg = = arg1);} int M Ain () {tclass<int> obj; Cout<<obj. Equal (2, 2) <<endl; Cout<<obj. Equal (2, 4) <<endl;}
#include <iostream>using namespace std; template <class t>class compare{public: bool isequal (Const t& arg, const &NBSP;T&&NBSP;ARG1);}; // already does not have the meaning of template, has been clear for the float template <>class compare<float>{public: bool isequal (CONST&NBSP;FLOAT&&NBSP;ARG,&NBSP;CONST&NBSP;FLOAT&&NBSP;ARG1 );}; // already does not have the meaning of template, has been explicitly double the template <>class compare<double>{public: bool isequal (const double& arg, const double& &NBSP;ARG1);}; template <class t>bool compare<t>::isequal (const T& arg, CONST&NBSP;T&&NBSP;ARG1) { cout<< "Call compare<t>::isequal" <<endl; return (ARG&NBSP;==&NBSP;ARG1);} bool&nbSp Compare<float>::isequal (CONST&NBSP;FLOAT&&NBSP;ARG,&NBSP;CONST&NBSP;FLOAT&&NBSP;ARG1) { cout<< "Call compare<float>::isequal" <<endl; return (ABS (ARG&NBSP;-&NBSP;ARG1) < 10e-3);} bool compare<double>::isequal (const double& arg, const double& &NBSP;ARG1) { cout<< "Call compare<double>::isequal" <<endl; return (ABS (ARG&NBSP;-&NBSP;ARG1) < 10e-6);} int main () { compare<int> obj; compare<float> obj1; compare<double> obj2; cout<<obj. IsEqual (2, 2) <<endl; cout<<obj1. IsEqual (2.003, 2.002) <<endl; cout≪<obj2. IsEqual (3.000002, 3.0000021) <<endl;}
This enables the template to be special, for the special version of the float and double, and even do something different from the nonspecific, which is the original intention of the template, so that the special template with the non-template with the behavior.
Template partial localization
Template <class iterator>struct iterator_traits { typedef typename iterator::iterator_category iterator_category; typedef typename iterator:: value_type value_type; typedef typename Iterator::d ifference_type difference_type; typedef typename iterator:: pointer pointer; typedef typename iterator::reference reference;}; Template <class t>struct iterator_traits<t*> { typedef random_ access_iterator_tag iterator_category; typedef t value_type; typedef ptrdiff_t difference_type; typedef T* pointer; typedef T& reference;}; Template <class t>struct iterator_traits<const t*> { typedef random_access_iterator_tag iterator_category; typedef T value_type; typedef ptrdiff_t difference_type; typedef const t* pointer; typedef const T& reference;};
#include <iostream>using namespace std; // generalization design template <class t1 , class t2>class testclass{public:testclass () {cout << "T1, T2" << endl;}};/ / design of template <class t1, class t2>class testclass<t1*, t2 for ordinary pointers *>{public:testclass () {cout << "t1*, t2*" << endl;}};/ / design of a const pointer template <class t1, class t2>class testclass<const t1*, t2*>{public:testclass () {cout << "const t1*, t2*" << endl;}}; Int main () { testclass<int, char> obj;//t1,t2 TestClass<int *, char *> obj1;//T1*,T2* TestClass< Const int *, char *> obj2;//const t1*,t2* return 0;}
The code above implements the template's partial specificity.
Sequence of calls for special and partial localization
How does the compiler choose to match a template, a template's specificity, and a template's bias when it is matched in the compile phase? From the philosophical point of view, should take care of the most special, then the second special, the last is the most common. The compiler makes the choice is also the respect from this principle. From the above example, we can also see that this is no longer an example of the explanation.
This article is from the "11408774" blog, please be sure to keep this source http://11418774.blog.51cto.com/11408774/1794683
The special and partial localization of C + + templates