"C + +" template Brief (b): function template

Source: Internet
Author: User

As we mentioned above, the introduction of templates, we found that in some special circumstances, we have to pass the template in order to solve the problem perfectly.

This article is to briefly describe the basic use of function templates.

First, function template format
Template<typename Param1, TypeName Param2,..., class paramn> return value type function name (parameter list) {      ...}
Ii. Examples of function templates
T represents the type, specifically what does not know, the instantiation of the time only know//typename can be replaced by class, but recommended to use typename//Note: TypeName can not be used to replace the struct!!! Template<typename t>t Add (const T l,const t r) {return l+r;} The template function can also be defined as inline//note the position of the inline, which must be preceded by the return value of the template Template<typename t>inline t Add (const T l,const t r) {return L+r;}

Note: The template itself is not a function or a class, it is just a generic code.

The process by which the compiler generates a specific type of code through a template is called: instantiation of the template.

Therefore, the template was compiled two times:

Before the first compilation is instantiated, the work done at compile time is mainly: Check the template code itself for grammatical problems (such as a semicolon or something)

At the second compile time, during instantiation, the template code is checked again, and all calls are valid under a specific type.

Iii. formal parameters of a function template

First, the template has two parameters, and the type participates in the non-type parameter.

T is a type parameter, N is a non-type parameter template<typename t,int n>t Add (const T L,const t r) {return l+r;}

Template parameter Summary:

1. template parameter names can be used only after template parameters to the end of a template declaration or definition, following the name masking rule.

2. The name of the template parameter can only be used once in the same template parameter list.

3. All type parameters must be preceded by a class or typename keyword modifier.

4. You cannot specify a default parameter inside a function template.

Iv. overloading of template functions
Template<typename t>int Add (const int A,const int b) {    return a+b;} T Add (const T l,const t r) {return l+r;} Template<typename t>t Add (const t a,const t b,const t c) {return a+b+c;} Template<typename t1,typename t2>t1 Add (const T1 l,const T2 r) {return l+r;} Template<typename t,int n>void Print (const T (&arr) [N]) {for (int i=0; i<n; ++i) {cout<<arr[i]<< " ";} Cout<<endl;} ... cout<<add (<<endl;//int) Add (const int,const int) cout<<add<> (<<endl;//t) Add (const T l,const t R) cout<<add (<<endl;//t) Add (const T a,const t b,const T C) cout<<add (1,2.2) &L T;<ENDL;//T1 Add (const T1 l,const T2 R) cout<<add (1.1,2) <<endl;//t1 Add (const T1 l,const T2 R)

Description of template function overloading:

1. A non-template function can exist with a function template of the same name, and the function template can also be instantiated as a non-template function.

2. For non-template functions and function templates with the same name, if the other conditions are the same, the non-template function will be transferred with priority and no one instance will be generated from the template. If the template can produce a function with a better match, the template is selected.

3. Explicitly specify an empty template argument list, which tells the compiler that only the template can match the call, and that all template parameters should be interpreted according to the arguments.

4. Template functions do not allow automatic type conversions, but normal functions can perform automatic type conversions.

The specificity of template function

In some cases, a generic template definition might be completely wrong for a type, or not compile, or do something wrong:

Template<typename t>t Max (const T l,const t r) {return l>r?l:r;} .... Char *str1= "123456789"; char *str2= "Abcdefhij"; Cout<<max <<endl;//correct Cout<<max (str1, STR2) <<endl;//error, which compares the size of the pointer to the ASCII code

Therefore, we need to use the template's specificity:

Template<>const char* max<const char*> (const char* l,const char* r) {int ret = strcmp (l,r); return ret>0?l:r ;}

Attention:

1. In a template-specific version of the call, the argument type must exactly match the formal parameter type of the special version function, and if it does not, the compiler instantiates an instance for the argument template definition.

2. After a call to a template instance is not available, the template-specific declaration should be included in the header file, and then each source file that uses that specific version will contain the header file.

Parameter deduction of template function

The process of determining template parameter types and values from function arguments is called template argument inference.

When a type parameter is converted, the arguments are typically not converted to match an existing instantiation, instead creating a new instance.

Instead, the compiler performs only two types of conversions:

1. Const conversion: A function that receives a const reference or a const pointer can be called by a reference or a pointer to a non-const object, respectively

2. Array or function-to-pointer conversions: If the template parameter is not a reference type, the arguments to the array or function type are applied to the regular pointer conversions. The arguments is a pointer to its first element, and the function argument as a pointer to the function type.

"C + +" template Brief (b): function template

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.