function templates in C + +, explicit materialization, explicit instantiation:

Source: Internet
Author: User

function templates

Shaped like:

Template<typename t>//No semicolon

void func (T &a,t &b);

Called function templates, where template and TypeName are keywords, typename can be substituted with class. T (other names can be used) to represent a generic type that can represent either an int or a double or another type as a collection of data types inside C + +.

Other words:

void func (T &a,t &b) = void func (int &a,int &b)

= void Func (double &a,double &b)

= void Func (char &a,char &b)

= .....

When you call the Func () function, you can reduce the amount of code by choosing which func () function to use depending on the type of formal parameter.

Function templates can also be overloaded, such as replacing parameters with two different data types:

Template<typename T1,typename t2>

void func (T1 &a,t2 &b);

Or is this:

Template<typename t>

void func (T &a,t &b t&c);

For a template of a function, the type of formal parameter that is invoked each time the function is called may be different, but the contents of the function body are actually the same. For example, the following code:

Template<typename t>

t func (t &a,t &b)

{

Return a>b?a:b;

}

The implementation of the content is: return Ah, a large number of the.

Explicit materialization

For some special types, however, it might not be appropriate for a template implementation, and you would need to redefine the implementation at this point by using explicit materialization.

First, declare a function template, and then define the special type, as follows:

Template<typename t>

void func (T &a,t &b);//Declare Function template first

For example, a variable of struct type does not fit, then we need to make the function template more specific

struct STRC {...};//struct

Template<>void func<strc> (strc &t1,strc &t2);

Then write the implementation method after the main function.

Implementation of function templates

Template<typename t>

void func (T &a,t &b)

{......}

Implementation of explicit materialization

Template<>void func<strc> (strc &t1,strc &t2)

{......}

Instantiation of

If we were to invoke a function template in the main function, he would create an instance in the main function, without invoking it, without producing an instance, such as a, b is an int, there is this sentence in the main function: Func (A, b), then a function of void Func (Int,int) is created. This is called an implicit instantiation. After a program is used, the compiler generates an instance function based on the template.

There is also an explicit instantiation that creates an instance without passing the corresponding argument to the formal parameter, but instead displays the declaration that requires the program to create the instance, declaring in the function body:

template void Func<char> (char &,char &);

The compiler generates an instance function, whether or not the program is useful.

Explicit instantiation and explicit materialization of declarations are distinguished. A template has <>, and one does not.

function templates in C + +, explicit materialization, explicit instantiation:

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.