function template Declaration
Template < type form parameter table > type function name (formal parameter table) { statement sequence }
A function template definition consists of a template description and a function definition
The generic parameter of the template description must appear at least once in the function definition
You can use the generic type parameter in the function parameter table, or you can use the general type parameter
#include <iostream.h>template < typename t >//function template T max (T a, T b) {return a > B? a : b; }void Main () {cout << "max (3, 5) is " << Max (3, 5) << Endl; cout << "Max (' y ', ' e ') is " << max (' y ', ' e ') << Endl; cout << "Max (9.3, 0.5) is " << max (9.3, 0.5) << Endl;}
Matching conventions
Find and use functions that best match the function name and parameter type, and invoke it if found;
Otherwise, look for a function template, instantiate it to produce a matching template function, if found, call it;
Otherwise, look for an overloaded function that can be used for parameter matching by type conversion, and if found, call it if the match function cannot be found by the above steps.
If the call has more than one matching selection, then the call match appears two semantics.
template< TypeName T >class array{public: Array (int s); Virtual ~ Array (); Virtual const t& Entry (int index) const; virtual void Enter (int index, const T & value); Protected:int size;//data member is T-type pointer T * ELEMENT;//The member function of a class template is a function template Template<typename t> Array<t>::array ( int s) {if (S > 1) size = s; else size = 1; element = new T [size]; } Template < typename T > Array < T >:: ~array () {delete [] element;} Template < typename T > Const t& Array < T >:: Entry (int index) const {return element [ind EX]; }template < typename T > void Array < T >:: Enter (int index, const t& value) {element [index] = V Alue; }
Class template as function parameter
The formal parameter type of a function can be a reference to a class template or class template, and the actual argument is a template class object instantiated by that class template, which must be a function template when a function has a class template parameter.
A function template using array<t> as a parameter templates < typename T >void tfun (const Array <T> & x, int index)
A class template can be either a base class or a derived class in a class hierarchy:
Class templates can derive from template classes
Class templates can derive from non-template classes
Template classes can derive from class templates
Non-template classes can derive from class templates
Derive a safe array class template from the class template array<t> boundarray<t>template< typename T >class array{public: Array ( int s); Virtual ~ Array (); Virtual const t& Entry (int index) const; virtual void Enter (int index, const T & value); Protected: int size; T * ELEMENT;} ; template < typename T >class boundarray:public Array < T >{public: boundarray (int low = 0, int Heig HT = 1); Virtual const t& Entry (int index) const; Virtual void Enter (int index, const t& value); Private:int
A class template derives from a generic class that instantiates an abstract class parameter of a base class when defining a derived class, deriving a template class from a normal class, meaning that the derived class adds an abstract class data member.
Derive from class template A normal class b#include<iostream.h>template< typename T >//Define class template classes a{public: A (t x) {T = x;} void Out () {cout << t << Endl;} Protected: t;}; class B:public a<int>//derived generic class {public://Instantiate base class abstract type parameter B (int A, double x): A < Int> (a) {y = x;} void out () {A <int>:: Out (); cout << y << Endl; } protected: double y;};
You can declare a variety of friend relationships in a class template
A function or function template can be a friend of a class or class template
A class or class template can be a class or class template of a friend class
It is more cumbersome to declare a friend relationship symbol between this template
Template <typename t> class x{//... friend void F1 ();} The function F1 becomes class template X instantiation of each template class of the Friend function template <typename t> class x{//... friend void F2 (X<t> &);} For a specific type (such as double), make the template function F2 (x<double>&) become X<double> 's friend template <typename t> class x{/... friend void A::f3 ();} The member function of Class A F3 the friend function of each template class that is instantiated as class template X <typename t> class x{//... friend void B<t>::f4 (X<t> &);} For a specific type (such as double), make the template class B<double> member function F4 (x<double>&) as the template class X<double> 's friend template <typename T > class x{//... Friend class Y;} Each member function of the Y class becomes a class template X instantiation of each template class of the Friend function template <typename t> class x{//... Friend class Z<t>;} For a specific type (such as double), make the template class z<double> all member functions as a friend of the template class x<double>
C + + templates