Code compilation Run Environment: Vs2012+debug+win32
Instantiation of a template refers to the process of generating a template function (a template class) from a function template (class template). For a function template, a real function is generated after the template is instantiated. After the class template is instantiated, only the class definition is completed, and the member function of the template class needs to be initialized when it is called. Instantiation of a template is divided into implicit instantiation and display instantiation.
For the use of function templates, there are two kinds of invocation methods, one is to display the template argument call (display call) and one is an implicit template argument invocation (implicit invocation). For the use of class templates, there is no implicit template arguments and explicit template arguments to use, because the use of class templates must display the specified template arguments. Do not confuse each concept.
1. Implicitly instantiate the definition of an implicit instantiation of a 1.1 template
This is relative to the template display instantiation. When using template functions and template classes, there is no entity for the specified type of template function and template class, and the compiler implicitly generates the template function or the entity of the template class as an implicit instantiation of the template based on the specified type parameter.
1.2 Implicit instantiation of function templates
Implicit instantiation of a function template refers to a function template that is instantiated when a function call is made, if no matching function is found, and if the argument type deduction can be successful.
There is also an introduction to calling a function, or the instantiation of a function template. The so-called introduction call refers to passing the function entry address to a function pointer and completing the function call through the function pointer. If the pass to the function pointer is not a real function, then the compiler will look for a function template with the same name for argument deduction, thus completing the instantiation of the function template. Refer to the example below.
#include <iostream>usingnamespacestd;template <typenamevoid func(T t){ cout<<t<<endl;}void invoke(void (*p)(int)){ int num=10; p(num);}int main(){ invoke(func);}
The program runs successfully and outputs 10.
Implicit instantiation of Class 1.3 templates
Implicit instantiation of a class template refers to the instantiation of a template when the template class is used, as opposed to a class template display instantiation. Review the following procedures.
#include <iostream>usingnamespacestd;template<typename T>class A{ T num;public: A(){ num=T(6.6); } void print(){ cout<<"A‘num:"<<num<<endl; }};int main(){ A<int//显示模板实参的隐式实例化 a.print();}
Program output Result: A ' num:6.
2. Display Instantiation 2.1 Template shows the definition of instantiation
Display instantiation is also known as external instantiation. A function template is instantiated when no function calls occur, or instantiation of a class template is called a template display instantiation when the class template is not applicable.
2.2 Display instantiation of a function template
For a function template, a function template can be instantiated by displaying an instantiation declaration, regardless of whether a function call occurs, in the form:
template[函数返回类型][函数模板名]<实际类型列表>(函数参数列表)
For example:
templatevoid func<int>(constint&);
Display instantiation of Class 2.3 templates
For a class template, the class template can be instantiated directly from the display instantiation declaration, regardless of whether or not an object of the template class is generated, in the form:
class [类模板名]<实际类型列表>
For example:
class theclass<int>;
3. Function template Invocation Method 3.1 Implicit template argument invocation
In the event of a call to a function template, a parameter deduction, called a function template, is not shown for the template argument, and is called implicitly by the template arguments. Such as:
template <typenamevoid func(T t){ cout<<t<<endl;} func(5);//隐式模板实参调用
3.2 Display template argument calls
In the event of a call to a function template, a display template argument call (which displays the call) is displayed that gives the template parameter without argument deduction.
The display template argument invocation is necessary if the argument deduction is unsuccessful. Review the following procedures.
#include <iostream> using namespace STD ; template <typename t> T Max (const t& t1,const t& T2) { return (T1>T2)? T1:t2;} int Main () {int i=5 ; //cout<<max (i, ' a ') <<endl;//Cannot compile cout <<max<int > (I, ' a ' ) <<endl; //display call by compiling }
A direct function call to Max (I, ' a ') produces a compilation error because I and ' a ' have different data types and cannot be inferred from these two parameters. With max< int> (i, ' a ') invocation, the instantiation of the function template does not need to be parameterized, and the second argument of the function can be converted from char to int, thus successfully completing the function call.
During the programming process, it is recommended to invoke the function template in the way of displaying template arguments, which improves the readability of the code and facilitates the understanding and maintenance of the code.
4. Template specificity 4.1 definition of template specificity
Template specificity differs from the instantiation of a template, where 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.
4.2 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 example below.
#include <iostream>using namespace STD;Template<TypeNameT> 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;}intMain () {//Call instance: int max<int> (int,int) intI=max (Ten,5);//Call 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:10
P:very
In the function template display special definition (Explicit specialization definition), the keyword template and a pair of angle brackets <> are displayed, followed by 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 you do not give the function template max< t> when T is const char*, then when comparing the size of two strings, the size of the starting address of the string is compared, rather than the contents of the string in the dictionary order.
4.2.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.
typedefconstchar* 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 overloaded functions in each source file, otherwise you will use template functions instead of overloaded functions in some source files.
Class 4.3 Template Special
Class template specificity is similar to the specificity of a function template, which is a specific implementation of a class template parameter under a specific type. Examine the following code.
#include <iostream>using namespace STD;Template<TypeNameT>classa{T num; Public: A () {num=t (6.6); }voidPrint () {cout<<"A ' num:"<<num<<endl; }};Template<>classa<Char*>{Char* STR; Public: A () {str="A ' special definition"; }voidPrint () {cout<<str<<endl; }};intMain () {a<int> A1;//Show implicit instantiation of template argumentsA1.print (); a<Char*> A2;//using a special class templateA2.print ();}
The program output results are as follows:
A ' Num:6
A ' Special definition
Reference documents
[1] Chen Gang. Advanced Step-by-step tutorials for C + + [M]. Wuhan: Wuhan University Press, 2008[6.2 (p215-p227)]
Implicit instantiation of C + + templates, display instantiation, implicit invocation, display invocation, and template-specific elaboration