"C + +" Template---parameterized polymorphism

Source: Internet
Author: User

---restore content starts---

Off Topic:

The design and details of the template mechanism were disclosed by Bjarne Stroustrup in its October 1988 article entitled "Parameterized Types for C + +".

Introduced:

Suppose there are two classes of

  

 Their structure is exactly the same, the difference is the data type int and double, so we can introduce the structure representation:

When an object is created with an argument int or a double into the parameter T, it becomes parameterized, that is, the data type is passed as a parameter, and the argument that replaces the parameter T can be a predefined data type or a user-defined data type (primarily a class object), which is parameterized polymorphism.

Detailed

A function template

  

A function template is defined, and the main function establishes three template functions, where sq (i) instantiates the type parameter T with the int type, and sq (l) instantiates the parameter T with a long type, and sq (5.55) is a double type.

Thus, using the type parameter T as a formal parameter in a function template is not a concrete function but an abstraction of a function. Wait until the instantiation, that is, to substitute concrete type arguments such as int, double, and so on, to establish a specific function, called the template function.

Second, overloading of function templates

 

It is apparent from the results that Max (I,J), Max (d,f), Max (CH,HC) overloads the overloaded function called for the template function, Max (D,J), Max (i,d), Max (ch,f), note that Max (I,J), I,j is the same as int, Max ( D,F), D,f is double, CH,HC is the same char, we return to the view function did not find the overloaded function with it, and Max (D,J), Max (i,d), we found a match with the overloaded function, which happens to be the corresponding overloaded function of the call, but for Max (ch,f), ch is a char type, F is a type of double, first the program does not match the overloaded function, there is no matching function template, swollen? The program shows that it called void Max (int A, double b), and we cannot help wondering why it does not call void Max (double b, int a), but instead calls the previous function to fine-see the formal parameters of another function, it is not difficult to find void Max (int A, do Uble b), the second form parameter is a double type, and F is the same type, which is closer than void Max (double b, int a), why is this? Looking at the rules of the call, we find that ambiguity should be avoided when using overloaded functions and templates in a mix. At this point they are called in the following order:

1. Call the matching overloaded function first

2. If there are no matching overloaded functions, the matching function template is called. If it is found, it is instantiated, creating a template function

3. If you do not match again, the similar overloaded function is called , and the precision may be lost at this time.

Therefore, can be roughly summed up as : first Heavy, then template, close.

the three-overloaded functions and function templates are polymorphic mechanisms, with the following differences:

(1) The main function calls the overloaded function and the template function are in exactly the same format.

The advantage of overloaded functions is the ability to use different program logic for similar operations on different data types. For example, program logic and operations that are used to calculate the maximum value of an int or double differ greatly from the program logic and operations that take the maximum value of char or char*. The function template cannot be processed.

If the program logic and operations of each data type are the same, then using the function template will be more concise and convenient.

A template is a code generation mechanism.

(2) for overloaded functions, regardless of whether they are used or not, all are stored in the code area, all occupying space.

For a function template, a template function is created only when a specific parameter type is used, and another template function is established when another specific parameter type is used. Easy programming, flexible use, high efficiency.

---restore content ends---

"C + +" Template---parameterized polymorphism

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.