Templates for C + + are sometimes more likely to fail to handle certain types.
For example:
#include <iostream>using namespace Std;class man{private:string name;int data;public:man (String s,int i): Name (s) , data (i) {}void show () const{cout<< "This name is" <<name<< ", Data=" <<data<<endl;}}; Template <class t>void mswap (t t1,t T2) {T temp=t1;t1=t2;t2=temp;cout<< "here is template version" <<t1 << "and" <<t2<< "swap successed!" <<endl;}; int main () {Mswap (88,66); Man M1 ("Guang", "a"), Man m2 ("Jing"); Mswap (m1,m2);}
Compilation Result:
You can see that there are many errors. (Although this is not the point.) )
Let's say I want Mswap to just exchange data for two man, and name remains the same as it used to be. What should I do?
At this point, we need to provide a concrete template definition for a particular type .
Materialization includes explicit materialization as well as implicit instantiation and explicit instantiation.
1. The prototype and definition of an explicit materialization should begin with template<> and indicate the type by name .
Template<>void Mswap (man &m1,man &m2)
Or
Template<>void mswap<man> (man &m1,man &m2)
Modified Example:
#include <iostream>using namespace Std;class man{private:string name;int data;public:man (String s,int i): Name (s) , data (i) {}void show () const{cout<< "This name is" <<name<< ", Data=" <<DATA<<ENDL;} void SetData (int d) {data=d;} int GetData () const{return data;}; Template <class t>void mswap (t &t1,t &t2) {T temp=t1;t1=t2;t2=temp;cout<< "here is template version" <<t1<< "and" <<t2<< "swap successed!" <<endl;}; Template<>void Mswap (man &m1,man &m2) {int temp=m1.getdata (); M1.setdata (M2.getdata ()); M2.setData (temp );cout<< "Here is the man version,successed!" <<endl;} int main () {int I=88,j=66;mswap (I,J), Man M1 ("Guang", "Max"), Man m2 ("Jing"), Mswap (M1,M2); M1.show (); M2.show ();}
Run:
This is the template explicit embodiment of the role.
It is important to note that materialization takes precedence over regular templates, while non-template functions take precedence over materialization and regular templates.
2. Instantiation
To learn more about the template, you must understand the terminology instantiation and materialization. Remember that the inclusion of a function template in your code does not inherently generate a function definition, it is just a scenario for generating a function definition . When the compiler uses a template to generate a function definition for a particular type, the result is a template instance, such as Mswap (I,J) above, where the function call Mswap (I,J) causes the compiler to generate an instance of Mswap () that uses the int type.
A template is not a function definition, but a template instance using int is a function definition. This instantiation is known as an implicit instantiation .
C + + can also be explicitly instantiated.
The syntax is, declare the kind you want--use the <> symbol to indicate the type, and precede the declaration with the keyword template:
Templata void Mswap<man> (Man &,man &)
The declaration means "using the Mswap () template to generate a function definition for the man type".