Function templates and template functions, function template Functions

Source: Internet
Author: User

Function templates and template functions, function template Functions

1. function pointer -- pointer Function

Function pointers focus on pointers. A pointer pointing to a function. For example:

Int (* pf )();

Pointer functions focus on functions. Indicates a function, and its return value is a pointer. Example:

Int * fun ();

2. array Pointer -- pointer Array

Array pointers focus on pointers. A pointer pointing to an array. For example:

Int (* pa) [8];

The focus of pointer arrays is arrays. Indicates an array, which contains a pointer. Example;

Int * ap [8];

3. class template-template class (class template-template class)

Class templates focus on templates. A template is used to generate a class model.Example:

Template <typename T>

Class Vector

{

...

};

This Vector Template can generate many classes), Vector <int>, Vector <char>, Vector <int>, and Vector <Shape *> .......

The focus of the template class is the class. Indicates the class generated by a template. Example:

The preceding Vector <int>, Vector <char> ,...... All are template classes.

These two words are easy to confuse. I have seen many articles that make the mistake, even some English articles. It is important to separate them, so you can understand whyIn the header file of the template definition. h, the template member function implementation must also be written in the header file. h, instead of writing the class declaration in. in the H file, the definition of class is written in. cpp file.

Array is a template, and array <int, 50> is a template instance-a type. The process of creating an array <int, 50> from an array is the instantiation process. The instantiation elements are embodied in the main. cpp file. If, in the traditional way, the compiler sees the template declaration in the array. h file, but there is no template definition, the compiler cannot create an array of Type <int, 50>. But there is no error at this time, because the compiler considers that the template is defined in other files and leaves the problem to the linked program for processing.

Now, what will happen when compiling array. cpp? The compiler can parse the template definition and check the syntax, but cannot generate code for member functions. It cannot generate code, because to generate code, you need to know the template parameters, that is, you need a type, not the template itself.

In this way, the definition of array <int, 50> is not found in main. cpp or array. cpp, so the error of non-defined members is reported.


Example of a default template parameter:

Template <typename T = int>

Class Array

{

...

};

This is the first time I defined this template and used it:

Array books; // I think there are default template parameters, which is equivalent to Array <int> books

The above usage is incorrect and compilation fails because Array is not a class. The correct usage is Array <> books;

Here, Array <> is a specific class generated by a class template used for default template parameters.

4. function template-template function (function template-template function)

Function templates focus on templates. A template is used to produce functions. Example:

Template <typename T>

Void fun (T)

{

...

}

When using this function, you can explicitly produce template functions, such as fun <int>, fun <double>, and fun <Shape *> .......

You can also use the compiler to deduce template parameters during use to help you generate them implicitly (implicitly.

Fun (6); // implicitly generates fun <int>

Fun (8.9); // implicitly generates fun <double>

Fun ('A'); // implicitly generates fun <char>

Shape * ps = new Cirlcle;

Fun (ps); // implicitly generates fun <Shape *>

The focus of template functions is on functions. Indicates a function generated by a template. Example:

Fun <int> and fun <Shape *> generated by explicit or implicit (implicitly) above ...... All are template functions.

The use of templates is very limited. Generally, they are just a model for generating classes and functions. In addition, there are very few fields used, so it is impossible to have any template pointer, that is, the pointer to the template, because in C ++,A template is a code production tool. In the final code, no template exists at all, and only the code of the specific classes and functions that the template has come up with exists.

Note: In the terms in this article, the center of gravity of the language is behind, and the previous words are used as adjectives.

 

2. Function template Exception Handling

The template parameters in the function template can be instantiated into various types, but an error may occur when the actual parameters of the instantiated template parameters are inconsistent. For example:

template<typename T>       void min(T &x, T &y){  return (x<y)?x:y;  }void func(int i, char j){   min(i, i);   min(j, j);   min(i, j);      min(j, i);}

The last two calls in this example are incorrect because the compiler implicitly generates a template function based on the type of the first real parameter, use it to check the consistency of all template functions, such as statements

Min (I, j );

The real parameter I encountered first is an integer type, and the compiler interprets the template parameter as an integer type. Therefore, the template real parameter j cannot be interpreted as an integer type, resulting in an error,Template Function isNo implicit type Conversion Function. There are two ways to solve this exception:

(1) Use forced type conversion. For example, rewrite the statement min (I, j) to min (I, int (j ));

(2) Use non-template functions to overload function templates

There are two methods:

① Borrow the function body of the function Template

In this case, only the prototype of the non-template function is declared. Its function body borrows the function body of the function template. For example, the preceding example is rewritten as follows:

template<typename T>       void min(T &x, T &y){  return (x<y)?x:y;  }int min(int,int);void func(int i, char j){   min(i, i);   min(j, j);   min(i, j);   min(j, i);}

The execution of this program will not cause errors, becauseOverload Functions Support implicit type conversion between data.

② Redefine the function body

Just like a general overload function, you can redefine a complete non-template function, and its parameters can be random.

In C ++, when function templates and non-template function with the same name are overloaded, the following call principles should be observed:

• Find a function with exactly matched parameters and call it if it is found. If the parameter matches more than one function, this call is an incorrect call.

• Find a function template and instantiate it to generate a matching template function and call it.

• If both of the above two fail, the function overload method is used to generate Parameter Matching through type conversion. If it is found, it is called.

• If none of the above three failed and no matching functions have been found, this call is an incorrect call.

 

3. What is the difference between a function template and a class template?

A: The instantiation of a function template is automatically completed by the compiler when processing the function call. The instantiation of a class template must be explicitly specified by the programmer in the program.

That is, the function template allows implicit and explicit calls, while the class template can only display calls

 

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.