Explicit materialization and explicit instantiation of the C + + Learning Note 36 Template

Source: Internet
Author: User

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".




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.