Template is mainly for generic programming, to do with type-independent template has a function template and class template, this article is mainly organized by the function template
1. function template definitionTemplate<typename type parameter 1,typename type parameter 2,... > return type function template name (call formal parameter list) {function body} In return type, call formal parameter list and function body, the type of the template parameter list can be referenced in the type parameter
Template<typename a,typenamen b,typename _c>A function (B arg) { var; ...}
2. Usefunction template name < type parameter 1, type parameter 2,... > (invoke Argument table)
Replace the contents of template<> in <>
Eg:int i = function<int,double,string> (1.23); char C = function<char,string,studnet> ("Hello");
3. Type parametersType parameter: Must precede with the TypeName or class keyword. type arguments: type arguments passed to a function template can be either a base type (char/int/double, etc.) or a class type (Student/teacher). It can even be a type (string) that is instantiated by another template, and the type arguments passed to the function template must meet the specific requirements of the template implementation, or a compilation error will be thrown.
#include <iostream>using namespacestd;template<typename t>t Add (t X, t y) {returnX +y;}classInteger { Public: Integer (intarg =0): M_var (Arg) {} friend Ostream&operator<< (ostream& Os,integerConst&i) {returnOS <<I.m_var; } IntegerConst operator+ (IntegerConst& RHS)Const { returnM_var +Rhs.m_var; }Private: intM_var;};intMain (void) {cout<< add<int> (123,456) <<Endl; cout<< add<Double> (1.3,4.6) <<Endl; cout<< add<string> ("Hello,","World !") <<Endl;//cout << Add<char const*> ("Hello,", "World!") << Endl;cout << add<integer> (123,456) <<Endl; return 0;}
4. Deferred compilationEach function template is actually compiled
Two plays:(1) The first time the compiler sees the function template being defined, the compiler only makes a general syntax check for that function template (
regardless of type), and then generate an internal representation of the function template (2) The second time is when the compiler sees that the function template is called, using the supplied type arguments, combining the type parameters in the previously generated internal representations, to do type-related syntax checking, and to generate the binary instruction code for the function template.
5. Implicit inference(1) If the function template invocation parameter (the parameter in the park brackets) is related to the parameter of the template < The argument in angle brackets, then when the function template is called, the compiler has the ability to implicitly infer the correct template parameter type based on the type of the calling parameter, even if the specified template parameter is not displayed. This is called implicit inference of template parameters. (2) However note: If the compiler implicitly infers a type that is inconsistent with the type expected by the program designer, it is possible to cause a compilation error.
#include <iostream>#include<typeinfo>using namespacestd;template<typename t>voidfoo (t x, t y) {cout<<"foo:"<< typeid (x). Name () <<' '<< typeid (y). Name () <<Endl;} Template<typename t>voidBar (TConst& X, TConst&y) {cout<<"Bar:"<< typeid (x). Name () <<' '<< typeid (y). Name () <<Endl;} Template<typename R, TypeName t>R Hum (T x) {R y; cout<<"Hum:"<< typeid (x). Name () <<' '<< typeid (y). Name () <<Endl; returny;}voidF1 (intXinty) {}voidF2 (DoubleXDoubley) {}intMain (void) { intA, B; Foo (a, b); //I i DoubleC, D; Bar (c, D); //D D Chare[ the], f[ the]; Foo (e, F); //pc PCBar (E, f);//A256_c A256_c//cout << sizeof (e) << Endl;// the//char (*P) [up] = &e;//e[0] = ' C ';//* (e+0) = ' C 'Foo ("Hello","Tarena");//PKC PKC//bar ("Hello", "Tarena");//A6_c A7_cbar<string> ("Hello","Tarena");//SS SSBar (string("Hello"),string("Tarena")); F1 (A, c); //c:double-intF2 (A, c);//A:int Double//int i = 1.2;//cout << i << Endl; // implicit inference is not implicitly convertible at the same time//Foo (A, c);//c:double, int, t=int//A:int, double, t=doubleFoo ((Double) A, c);//D DFoo (A, (int) c);//I ifoo<Double> (A, c);//D Dfoo<int> (A, c);//I i//a = Hum (c); //the type of the return value does not participate in implicit inferenceA = hum<int> (c);//d i return 0;}
6. OverloadingLike normal functions, function templates can also form overloaded relationships, and the more restrictive versions of types are, the more limited the selection
#include <iostream>#include<typeinfo>#include<cstring>using namespacestd;//maximum value of two arbitrary type valuesTemplate<typename t>TConst& Max (TConst& X, TConst&y) {cout<<"<1"<< typeid (x). Name () <<'>'<<Flush; returnx < y?y:x;}//two The maximum value of the target to which any kind of pointer is directedTemplate<typename t>T*Const& Max (t*Const& X, t*Const&y) {cout<<"<2"<< typeid (x). Name () <<'>'<<Flush; return*x < *y?y:x;}//the maximum value of the string that the two-character pointer points toChar Const*Const& Max (Char Const*Const& X,Char Const*Const&y) {cout<<"<3"<< typeid (x). Name () <<'>'<<Flush; returnstrcmp (x, Y) <0?y:x;}/*Char const* Max (char const* x,char const* y) {cout << "<3" << typeid (x). Name () << ' > ' &L t;< Flush; return strcmp (x, y) < 0? y:x;}*///maximum value of three arbitrary type valuesTemplate<typename t>TConst& Max (TConst& X, TConst&y, TConst&z) {cout<<"<4"<< typeid (x). Name () <<'>'<<Flush; return:: Max (:: Max (x, y), z);}/*//Two character pointer to the maximum value of the string char const* const& max (char const* const& x,char const* const& y) {cout << <3 "<< typeid (x). Name () << ' > ' << flush; return strcmp (x, y) < 0? y:x;}*/intMain (void) { intA =123, B =456; cout<<:: Max (A, b) <<Endl; cout<< *::max (&a, &b) <<Endl; Char Const* C ="AB", *d ="ABC"; cout<<:: Max (c, D) <<Endl; cout<<::max<> (c, D) <<Endl; cout<<::max<Char Const*> (c, D) <<Endl; Char Const* E ="ABCD"; Char Const*Const& f =:: Max (c, D, E); cout<< F <<Endl; Char Const* g =" A", *h ="123", *i ="1234"; :: Max (g, H, I); cout<< F <<Endl; return 0;}
That's all for the time being ...
C++stl-Function templates