Function templates in C ++

Source: Internet
Author: User
Tags float max

Understand or be familiar with C ++ and have a certain understanding of function overloading. Function overloading in C ++ refers to declaring several function-like functions, but the parameter types or sequences of these functions must be different, that is, different computing functions of the same function. For example, to compare the size of A and B, we can define a max function, which has the following versions for different types:

char max( char a, char b){return (a>b)?a:b;}int max ( int a, int b){return (a>b)?a:b;}float max( float a, float b){return (a>b)?a:b;}

In the preceding three functions, if you enter parameters of the corresponding type, the C ++ compiler can correctly select the function to be executed. However, if we enter two double-type parameters, the compiler will report an error because we do not define double-type function overloading. If there are other types, such as long and unsigned char, We need to reload them, which will result in code explosion and the problem of incomplete reload printing.

To solve the above problem, C ++ introduces the template mechanism template. Templates can realize code reuse and generalized programming. Templates are divided into function templates and class templates. Next we will introduce the function template.

1) general definition of function templates

Template <class or typename _ T> Return Value Type Function Name (form parameter list) {function body ;}

Template is the keyword of a declarative template. Class or typename is the parameter type. If there are multiple types, you need to add multiple classes or typename. You can use the following example to further understand the template function:

template <typename _T> _T const& max( const _T& a, const _T& b){return (a>b)?a:b;}int main( int argc, char*argv[]){//int ans = gSum(4,5,6,7,1);//cout<<"gSum(4,5,6,7,1)="<<ans<<endl;cout<<"int typename "<<max<int>(6,7)<<endl;cout<<"double typename "<<max<double>(12.0,11.0)<<endl;return 0;}


The biggest benefit of a function template is that you can determine the parameter type of a function as needed. by defining a template function, we can pass the parameter type when the template is specific, returns the maximum value of both. In addition, function templates, like other functions, support overloading. Through the above example, we can find the two biggest advantages of the function template:

1) In terms of macros, the template adds type security detection.

2) in terms of inheritance, the template solves the class and function explosion problem.

Function templates are a major mechanism of C ++. flexible use of templates can greatly improve programming efficiency, code reuse rate, and reduce code maintenance.

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.