C + + Learning path: function templates

Source: Internet
Author: User

1#include <iostream>2#include <string>3#include <vector>4 using namespacestd;5 6Template <typename t>7 T Max (t A, T B)8 {9     returna > B?a:b;Ten } One  A  -  - intMainintargcConst Char*argv[]) the { -     inti = the; -cout <<:: Max (7, i) <<Endl; -  +     DoubleF1 =3.4; -     DoubleF2 =-6.7; +cout <<:: Max (F1, F2) <<Endl; A  at     stringS1 ="Hello"; -     stringS2 =" World"; -cout <<:: Max (S1, S2) <<Endl; -  -  -     return 0; in } - //The secondary template function, is the value copy type, the input and the return value are the value copy, the cost is huge, if our input and the return value are the class type, the resource consumption is huge.  toSo the following template is commonly used
#include <iostream>#include<string>#include<vector>using namespacestd;template<typename t>ConstT &max (ConstT &a,ConstT &b) {    returna > B?a:b;}intMainintargcConst Char*argv[]) {    inti = the; cout<<:: Max (7, i) <<Endl; DoubleF1 =3.4; DoubleF2 =-6.7; cout<<:: Max (F1, F2) <<Endl; stringS1 ="Hello"; stringS2 =" World"; cout<<:: Max (S1, S2) <<Endl; return 0;}

The above template will save a lot of money.

Here's a case of a pit.

1#include <iostream>2#include <string>3#include <vector>4 using namespacestd;5 6Template <typename t>7 ConstT &max (ConstT &a,ConstT &b)8 {9     returna > B?a:b;Ten } One  A  -  - intMainintargcConst Char*argv[]) the { -     //Compile Error -cout <<:: Max (4,4.7) <<Endl; -     return 0; +}

We entered as 4-int 4.7-double, the compiler could not find a template to match, compile error.

Because our template is two parameters are of type T, not one is T1, the other is T2;

All right, let's try. Two parameter types are different templates

1#include <iostream>2#include <string>3#include <vector>4 using namespacestd;5 6Template <typename T1, TypeName t2>7 Const T1&max (Const T1&a,Const T2&b)8 {9     returna > B?a:b;Ten } One  A  -  - intMainintargcConst Char*argv[]) the { -cout <<:: Max (3,4.5) <<Endl;  -     //int Double -     //const int &max (const int &, const double &); +     //forced type conversion because the T1 is different from the T2 type -     //produces an intermediate temporary variable +     //so the last return value, which refers to a temporary variable A  at     return 0; -}

Above is a wrong example, the compilation will not pass, the error is very obscure, we see that the parameter 1 is T1, the parameter 2 is T2,

When we input parameter A is 3--int parameter B is 4.5--double, then we tell the compiler to generate an int &max (const int &, const double &) function,

The return value and the parameter are all T1==int, obviously 4.5 is greater than 3, so the return is 4.5, but we require the return value is int, obviously does not match, so the compiler will do, the cast generates a TMP = (int) 4.5;

A reference to return TMP. TMP is a temporary variable, and the stack space is freed after the function call ends, and it is meaningless to return a reference to a temporary variable.

So the compilation error is caused

Here's a look at the overload of the template

Example code:

1#include <iostream>2#include <string>3#include <vector>4 5 using namespacestd;6 7 8 9 Const int&max (Const int&a,Const int&b)Ten { One     returna > B?a:b; A } -Template <typename t> -  the ConstT &max (ConstT &a,ConstT &b) - { -     returna > B?a:b; - } +  -Template <typename t> + ConstT &max (ConstT &a,ConstT &b,ConstT &c) A { at     return:: Max (:: Max (A, B), c); - } -  -  -  -  in intMainintargcConst Char*argv[]) - { to:: Max (7, the, the) ;//Call a third +:: Max (7.0,43.5) ;//Call a second -:: Max ('a','b')  ;//Call a second the:: Max (7, the) ;//call the first one, in fact the second one can, but take the most matching *::max<> (7, the) ;//specify to match from Template 2, generate an int &max (const int &, const int &) According to the template $cout <<::max<Double> (7, the) <<endl;//called 2 for casting. Panax Notoginsengcout <<:: Max ('a',42.7) << Endl;//call 1 to cast -     return 0; the}

In short, there is no need to rote call rules, Master a principle, the compiler always chooses to call the most appropriate function.

C + + Learning path: function templates

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.