7. Function Templates and Class Templates

   C++ provides function templates. The so-called function template is actually to create a general function whose function type and formal parameter type are not specified, but represented by a virtual type. This generic function is called a function template. All functions with the same function body can be replaced by this template, you don't need to define multiple functions, you only need to define them once in the template. When calling a function, the system will replace the virtual type in the template according to the type of the actual parameter, thus realizing the functions of different functions.
 
1) C++ provides two template mechanisms: function template, class template
2) Generic - type parameterization, also known as parameter template

 

function template syntax

transfer

 myswap<float>(a, b); // explicit call
 myswap(a, b); // automatic data type deduction (implicit call)

 

Function Templates and Function Overloading

   1 Function templates can be overloaded like normal functions
   2 C++ compilers prefer normal functions
   3 If the function template can produce a better match, then choose the template
   4 The syntax of an empty template argument list can restrict the compiler to only pass the template match

function template mechanism

The compiler does not treat the function template as a function that can handle any class. The
compiler generates different functions from the function template through the concrete type. The
compiler compiles the function template twice
. The template code itself is compiled at the place of declaration; where the code after parameter substitution is compiled.

 

class template

grammar

template <typename T>
class A
{
protected:
    T m_a;
public:
    A(T a)
{
    m_a = a;
}
};

Class template syntax in inherited classes

Template class derived from ordinary class

class B : public A<int>
{
private:
    int m_b;
public:
  B(int a=10, int b=20) : A<int>(a)    //显式调用
  {
     m_b = b;
  }

};

template class derived template class

template <typename T>
class C : public A<T>
{
protected:
  T c;
public:
  C(T c, T a) : A<T>(a)
  {
    this->c = c;
  }
};
 

A few notes about class templates:
1) Class templates can have one or more type parameters, and each type must be preceded by a typename, such as:
    template <typename T1, typename T2>
    class someclass {
    …};
Substitute the actual type name respectively, such as:
    someclass<int,double> obj;
2) Like using a class, pay attention to its scope when using a class template, and can only use it to define objects within its effective scope.
3) Templates can have hierarchies, and a class template can be used as a base class to derive derived template classes.
 

Implementation of friend function in template class

Take overloading << as an example

template <typename T>
class  Array
{
/*...*/
    template <typename U>
	friend ostream &operator <<(ostream &out, const Array<U> &a);
};
template <typename T>
ostream &operator <<(ostream &out, const Array<T> &a)
{
	/*...*/
	return out;
}

 

static keyword in class template

  • Each template class instantiated from a class template has its own class template data member, and all objects of that template class share a static data member
  •  Like static data members of non-template classes, static data members of template classes should also be defined and initialized at file scope
  •  Each template class has its own copy of the static data members of the implemented class template
#include <iostream>

using namespace std;

template <typename T>
class A
{
private:
	T m_a;
public:
	static int count;
	A()
	{
		count++;
	}
};

template <typename T>
int A<T>:: count = 0;

int main()
{
	A<int> a1;
	A<int> a2;
	A<int> a3;
	
	A<double> a4;
	A<double> a5;
	
	cout << a1.count << endl;			//相同类型模板类公用一个静态成员变量
	cout << a4.count << endl;
	cout << A<int>::count << endl;
	cout << A<double>::count << endl;
	
	return 0;
}

 

Summarize

  • Templates are a polymorphic tool for C++ type parameterization. C++ provides function templates and class templates.
  •  A template definition begins with a template description. Generic parameters must appear at least once in the template definition.
  •  The same generic parameter can be used in multiple templates.
  •  Generic parameters can be used on function parameter types, return types, and variables in declared functions.
  •  Templates are instantiated by the compiler based on actual data types, resulting in executable code. An instantiated function template is called a template function; an instantiated class template is called a template class.
  •  Function templates can be overloaded in several ways.
  •  Class templates can be used in class hierarchies.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324358421&siteId=291194637