C ++ learning note 36: the explicit and explicit instantiation of the template, and the learning note 36

Source: Internet
Author: User

C ++ learning note 36: the explicit and explicit instantiation of the template, and the learning note 36

C ++ templates may not be able to process 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 are template version "<<t1<<" and "<<t2<<" swap successed!"<<endl;};int main(){mSwap(88,66);man m1("guang",99);man m2("jing",10);mSwap(m1,m2);}
Compilation result:


You can see many errors. (Although this is not the focus .)

Suppose I want mSwap to only exchange the data of two man, and the name remains the same as the original one. What should we do?

At this time, we need to provide specific template definitions for specific types.

Concrete includes explicit and implicit instantiation and explicit instantiation.


1. The explicit prototype and definition should start with template <> and indicate the type by name.

template<>void mSwap(man &m1,man &m2)
Or

template<>void mSwap<man>(man &m1,man &m2)

EXAMPLE After modification:

#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 are 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 are the man version,successed!"<<endl;}int main(){int i=88,j=66;mSwap(i,j);man m1("guang",99);man m2("jing",10);mSwap(m1,m2);m1.show();m2.show();}
Run:


This is the role of an explicit template.

It should be noted that the normalization takes precedence over the general template, while the non-template function takes precedence over the embodiment and the general template.


2. instantiation

To learn more about templates, you must understand the instantiation and embodiment of terms. Remember, the function template itself does not generate function definitions in the Code. It is just a solution for generating function definitions. When the compiler uses a template to generate a function definition for a specific type, the template instance is obtained. For example, the above mSwap (I, j), the function call mSwap (I, j) this causes the compiler to generate an instance of mSwap (), which uses the int type.

A template is not a function definition, but an int template instance is a function definition. This instantiation method is called implicit instantiation.

C ++ can also be explicitly instantiated.

Syntax: declare the required category-use the <> symbol to indicate the type, and add the keyword template before the declaration:

Templata void mSwap <man> (man &, man &)

This statement indicates "using the mSwap () template to generate man-type function definitions ".





C ++ primer plus (Chinese Version 5th) is not clear about the specifics of the Instance

This is a bit painful. After a long summary and analysis. The following is a simple example:
First, display the details:
Declaration: template <class T>
Void test (T t); // This is a general template function in the display embodiment
Template <> void test <int> (int a); // [display specific functions]

Definition: template <class T>
Void test (T t ){.....}
Template <> void test <int> (int ){......}

Call: Use the function name directly.
For example, test (3); // If the parameter is int, the call is specific.
Test (3.3); // If the parameter is not int, a general template function is called.

Let's take a look at the instance details:
Declaration: template <class T>
Void test (T t );
Void test (float f );

Definition: template <class T>
Void test (T t) {...} // [instance specific function]
Void test (float t) {...} // General function for comparison

Call: [This is very different]
Template void test <int> (int t); // you need to describe the type before calling the instance
Test (3); // The parameter is int, And the called instance is specific.
Test (3.3); // If the parameter is not int, a general function is called.

As can be seen from the above, the actual display is [the specific function to be called has been determined at the time of function declaration ], the concrete type of an instance is [Determining a type when a function is called to make it call a specific function]

From the two examples, we can see that the two instantiations are [to take care of some types that you want to handle, so that they are not executed according to the template]. To put it bluntly, it is special, programming also requires special treatment.
The above is what I have summarized and hope to help you. If you think something is wrong during your learning, please point it out. Learn and improve together.

Since implicit instantiation of a function template applies all types of real parameters to one function template, why do I need to display them?

Under normal circumstances, you can use the call process of the function template to export the data type corresponding to the type parameter (called implicit instantiation), according to the statement of C ++ founder Bjarne Stroustrup, one of the reasons is that it is treated like a class template. The more important reason is that in some special cases, the corresponding types of template parameters cannot be pushed or exported. In this case, the type can only be explicitly instantiated, for detailed description and examples, see section 15.6.2 of The book "The Design and Evolution of C ++" in Bjarne Stroustrup. The Chinese title is: Design and Evolution of C ++.

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.