13 -- function Template

Source: Internet
Author: User
I. Generic programming in C ++

--- Function Template

--- Provides a special function for calling different types

--- Type can be parameterized

template<typename T>void Swap(T& a,T& b){T t=a;a = b;b = t;} 

Note: The-template keyword is used to declare the start of generic programming.

-The typename keyword is used to declare the generic type.

Function template Application

-- Automatic type deduction call

-- Display call for specific types

Int A = 1; int B = 2; swap (a, B); // call float FA for automatic type derivation; float Fb = 4; Swap <float> (FA, FB); // call the display type

Understanding of generic programming

--- The compiler does not process the function template as a function that can process any type of function.

--- The compiler generates different functions from the function template through specific types.

--- The compiler will compile the function template process twice.

--- Compile the code of the function template on the declared party 

--- Compile the code after the parameter is replaced at the place of the call

Ii. function templates and overloading

Function templates can be reloaded like normal functions.

*: The C ++ compiler gives priority to common functions.

*: If the function template can produce a better match, select the template.

*: You can use the syntax of the null template parameter class table to limit the compiler to compile only the template.

Int max (int A, int B) {return A> B? A: B;} template <typename T> T max (t a, t B) {cout <"template <typename T>" <Endl; Return A> B? A: B;} template <typename T> T max (t a, t B, T c) {return max (a, B), c );} int main () {int A = 1; int B = 2; cout <max (a, B) <Endl; // select the normal function cout <max <> (a, B) <Endl; // only the template matching return 0 is considered ;}

Note:

Cout <max (a, B) <Endl; // The normal function cout <max <> (a, B) <Endl; // only template matching is considered

Iii. multi-parameter function templates

Function templates can define any number of different types of templates.

template<typename T1,typename T2,typename RT>RT Add(T1 a,T2 b){return static_cast<RT>(a+b);}cout<<Add<char,float,double>('a',100)<<endl;

Declare the return value type parameter to the first parameter position. You only need to display the declared return type parameter when calling the parameter.

template<typename RT,typename T1,typename T2>RT Add(T1 a,T2 b){return static_cast<RT>(a+b);}cout<<Add<double>('a',100)<<endl;

Summary:

(1) function templates are actually a family of functions with the same behavior

(2) function templates can be used to derive and call functions based on type real parameters.

(3) parameters of the specified type that can be displayed in the function Template

(4) function templates can be overloaded.













13 -- function Template

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.