1. Definition of template specificity
Template specificity in C + + is different from the instantiation of templates, and the specific implementation of template parameters under a particular type is called template specificity. Template specialization is sometimes referred to as template materialization, with function template specialization and class template specialization, respectively.
1.1 Function template Special function template specificity is when a unified function template does not work on all types of instances, you need to define the specific implementation version of the function template when the type parameter is instantiated to a particular type. See the following example:
#include <iostream>using namespace Std;template<typename t> t Max (t t1,t T2) { return (T1>T2)? T1:t2;} typedef const CHAR* ccp;template<> CCP max<ccp> (CCP s1,ccp s2) { return (strcmp (S1,S2) >0)? s1:s2 ;} int main () {//Call instance: int max<int> (int,int) int I=max (10,5); Invoke display specificity: const char* max<const char*> (const char*,const char*) const char* P=max<const char*> ("very", " Good "); cout<< "I:" <<i<<endl; cout<< "P:" <<P<<ENDL;}
The program compiles the result of the operation correctly:
I:10p:very Displays the keyword template and a pair of angle brackets <> in the function template display special definition (Explicit specialization definition), and then the definition of the function template specificity. The definition indicates the template name, template arguments used to customize the template, and function parameter tables and function bodies. In the above program, if the function template is not given the max<t> version when T is const char*, then when comparing the size of two strings, the size of the starting address of the string is compared, instead of the contents of the string in the dictionary order. 1.1.1 using function overloading instead of function template specificity in addition to defining a function template special version, you can also directly give the template function under a specific type of overloaded form (normal function). function overloading can be used to implement function template functionality, or to avoid the invalidation of specific instances of a function template. For example, the above template can be changed to the following overloaded function:
typedef const CHAR* CCP; CCP Max (CCP s1,ccp s2) { return (strcmp (S1,S2) >0) s1:s2;}
The program runs the same result as using a function template. However, the use of normal function overloading and the use of template specificity is different, mainly in the following two aspects:
(1) If a normal overloaded function is used, the binary code of the function is generated in the target file regardless of whether the actual function call occurs. If you use a special version of the template, the binary code of the special template function will not be included in the destination file unless a function call occurs. This conforms to the "lazy instantiation" guideline of the function template. (2) If you use normal overloaded functions, in the separate compilation mode, you should include the declaration of the overloaded function in each source file, otherwise you will use template instantiation in some of the original files instead of overloading the function. Class 1.3 Template Special class template specificity is similar to the specificity of a function template, that is, the specific implementation of class template parameters under a certain type. Examine the following code:
#include <iostream>using namespace std; Template<typename t>class a{ T num;public: A () { num=t (6.6); } void print () { cout<< "A ' num:" <<num<<endl; }}; Template<>class a<char*>{ char* str;public: A () { str= "a ' special definition"; } void print () { cout<<str<<endl; }}; int main () { a<int> A1;//Show implicit instantiation of template arguments a1.print (); A<char*> a2;//use a special class template A2.print ();}
The program output results are as follows:
A ' num:6a ' special definition
C + + template specificity