Instantiation and embodiment of function templates and class templates

Source: Internet
Author: User
Tags types of functions

I. Function Template

1. The difference between explicit instantiation and explicit specialization:

(1) form:

Display instantiation: template void Swap <int> (int, int );

Display details: template <> void Swap <int> (int, int );

Or template <> void Swap (int, int );

We can see that the difference is that the former uses template as the header, and the latter uses template as the header <>

(2) meaning:

Display instantiation: Use the Swap () template to generate the int type function definition, that is, use the function template, and use a specific int to replace the generic type in the function template, in this way, you do not need to write function definitions.

Show the specific function: Use a dedicated and independent function instead of using the Swap () template to generate the function definition (that is, display the specific function) function definitions are used to generate function definitions for the int type. For specific functions, additional function definitions are required.

2. implicit instantiation is relatively simple. It determines the generic type in the function template based on the specific type of the parameter during function calling. For example:

Char success, ch2;

......

Swap (substring, ch2); // replace the common type in the function template with the char type.

 

3. Display instantiation, implicit instantiation, and display externalization are collectively referred to as specialization. The similarities between them are that they are all defined using specific types of functions, rather than general descriptions.

Ii. Category templates

1. Differences between display instantiation and display embodiment

(1) form:

Display instantiation: template class ArrayTp <string, 10>;

Display details: template <class T, int n>

Class ArrayTp

{

......

......

};

Template <> class ArrayTp <string, 10>

{

......

......

};

It can be seen that the difference between the two is similar to the difference between the two in the function template. One is to start with template, and the other is to start with template, but both have class keywords, because this is a class template.

(2) meaning:

Display the class definitions of specific types (such as string) generated by the general class template ArrayTp for instantiation;

Display the specific class definition, instead of using the class template definition.

2. partial specialization)

Some specific terms refer to some restrictions on the universality of the template.

The following template definitions are available:

Template <class T1, class T2>

Class Pair

{

......

};

Some implementations can be made as follows:

Template <class T2> class Pair <string, T2>

{

......

};

<> After the template, an unspecified type parameter is displayed. If both parameters are specific, the <> internal is empty, and the display is specific.

3. Implicit instantiation is relatively simple. It replaces the general type in the class template according to the specific type when declaring the object. For example, ArrayTp <double, 10> aver;

 

Related Article

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.