Use of function templates in C + + detailed parsing _c language

Source: Internet
Author: User
Tags define function

Defined

We know that the overload of a function can be used to implement a multiple function name, with the same functions or similar functions defined with the same name. This simplifies the invocation of a function, but in a program, you still need to define each function separately.

The function templates provided by C + + can simplify this process more.

The so-called function template is actually to establish a general function, the type of the element of the culvert types is not specified, with a virtual type to represent, this universal function is called function template.

All functions of the same function can be replaced with this template, do not have to define more than one function, only to be defined in the template once. When calling a function, the system replaces the virtual type in the template based on the type of the argument, thus enabling the functions of the different functions.

The general form of defining a function template is:

Copy Code code as follows:

Template <typename t>
Common function definitions

Or
Copy Code code as follows:

Template <class t>
Common function definitions

Below, let's compare the use of normal functions, the use of overloads of functions, and the use of templates for functions:

This piece of code is used to solve the problem of adding unused type data

Using multiple common functions
============= Sample Code 1.1==============

Copy Code code as follows:

#include <iostream>
using namespace Std;
int int_add (int a,int b)//defining function Int_add for int type data addition
{
int C;
C=a+b;
return C;
}
Double Dou_add (double a,double b)//define function Dou_add for double function add
{
Double C;
C=a+b;
return C;
}
int main ()
{
Cout<<int_add (5,3) <<endl; Calling the Int_add function
Cout<<dou_add (5.35,5.5) <<endl; Calling the Dou_add function
return 0;
}

using constructors
=============== Sample Code 1.2===============
Copy Code code as follows:

#include <iostream>
using namespace Std;
int n_add (int a,int b)//defining function N_add for int type data addition
{
int C;
C=a+b;
return C;
}
Double N_add (double a,double b)//define function N_add for double function add
{
Double C;
C=a+b;
return C;
}
int main ()
{
Cout<<n_add (5,3) <<endl; Calling the N_add function
Cout<<n_add (5.35,5.5) <<endl; Calling the N_add function
return 0;
}

Working with Function templates
=============== Sample Code 1.3===================
Copy Code code as follows:

#include <iostream>
using namespace Std;
Template<typename t>
T N_add (t a,t b)
{
T C;
C=a+b;
return C;
}
int main ()
{
Cout<<n_add (5,3) <<endl;
Cout<<n_add (5.35,5.5) <<endl;
return 0;
}

Analysis:First, we analyze example code 1.1, which is based on the addition of different data (int and double two), defines two different functions Int_add and dou_add, and when different types of data are added, we manually set the corresponding function to operate.

Example code 1.2 compared to 1.1, in the function of the invocation form is simplified, using the function of the overload technology, for all data, unified use of function N_add to operate, the compilation system will automatically call the corresponding function according to the type of data.

Example code 1.3 compared to 1.2, there is a simplification in the function body, if we use the function template, we do not need to go to one by one write the corresponding function, we only need to construct the appropriate template, and then the system will automatically determine the type of data, and then replace the corresponding virtual type,

For example, when manipulating N_add (5.35,5.5), the system automatically determines that the data is a DOUBL type, and then replaces T in the function template with a double:

It is equivalent to constructing a function:

Copy Code code as follows:

int n_add (int a,int b)
{
int C;
C=a+b;
return 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.