C++ class template and template class in depth

  • 2020-04-02 02:26:25
  • OfStack

1. The keywords typename and class are used in many places in the Template of c++, and sometimes they can be replaced, so are the two keywords exactly the same?
In fact, class is used to define classes. After the template was introduced in c++, the initial method to define the template was: template < The class T > In this case, the class keyword indicates that T is a type. Later, in order to avoid the possible confusion caused by the use of class in these two places, the keyword typename is introduced. Its function is the same as that of class to indicate that the following symbol is a type.           The template < Typename T > The keyword class does exactly the same thing as typename in the template definition syntax.

2. Class template and the concept of template class

(1) What is a class template

A class template (also known as a generic class or class generation class) allows the user to define a pattern for a class that allows certain data members of the class, parameters of the default member function, and return values of certain member functions to be of any type (both system-predefined and user-defined).
If the data type of a data member in a class cannot be determined, or the type of a parameter or return value of a member function cannot be determined, such a class must be declared as a template, and its existence does not represent a concrete, actual class, but a class.

(2) Class template definition

Define a class template, there are generally two aspects of the content:

A. First define the class in the following format:


template <class T>
class foo
{
 ... 
}

Foo is the name of the class. In the definition body of the class, if a member of the generic data type is adopted, T should be added before the function parameter. The generic type T can be used as the type of the ordinary member variable, const and static member variable, as well as the parameter and return type of the member function. Such as:


template<class T>
class Test{
private:
T n;
const T i;
static T cnt;
public:
Test() : i(0){}
Test(T k);
~Test(){}
void print();
T operator+(T x);
};

B. When defining a member function outside the class definition, if there are template parameters in the member function, the definition should be the same as that of the member function outside the class definition, and the template declaration should be made outside the function

Such as:


template<classT>
voidTest<T>::print(){
std::cout<<"n="<<n<<std::endl;
std::cout<<"i="<<i<<std::endl;
std::cout<<"cnt="<<cnt<<std::endl;
}

If the function returns a generic type, suffix the class name before the function name." < T > ". Such as:


template<class T>
Test<T>::Test(T k) : i(k){n=k;cnt++;}
template<class T>
T Test<T>::operator+(T x){
return n + x;
}

C. The method of initializing const member and static member variable outside the class definition is basically the same as that of initializing const member and static member variable outside the class definition. The only difference is that the template needs to be declared, for example


template<class T>
int Test<T>::cnt=0;
template<class T>
Test<T>::Test(T k):i(k){n=k;cnt++;}

(3) The use of class templates . The use of a class template actually instantiates the class template into a concrete class in the form of: class name < Actual type > .
A template class is an instantiation of a class template. Let me give you a visual example. We compare class template to a mold making cookies, and template class is to use the mold made of biscuits, as for the biscuits is what taste depends on your own in the instantiation is what material, you can do chocolate cookies, can also do cookies, red bean paste these biscuits in addition to the material is different, other things are all the same.

3. Function template and template function

(1) function template
Function templates can be used to create a generic function that supports many different parameters and avoids repetitive design of the body of an overloaded function. It is characterized by the data type used by the function as an argument.
The declaration form of the function template is:


template<typename( or class) T>
< The return type >< The function name >( Parameter table )
{
 The body of the function 
}

Where, template is the keyword that defines the template function. The Angle bracket after the template cannot be omitted; Typename (or class) is a keyword that declares a datatype parameter identifier to indicate that the identifier after it is a datatype identifier. In this way, in the function defined later, any variable that wishes to determine the data type based on the real data type can be described by the data type parameter identifier, so that the variable can be adapted to different data types. Such as:


template<typename( or class) T>
T fuc(T x, T y)
{
T x;
// ... 
}

A function template simply declares a description of a function called a template, not a function that can be executed directly. Only when the type parameter identifier is replaced by the argument's data type can a real function be generated.

(2) template function:
The generation of template function is the process of instantiating the type parameter of function template.
Such as:


double d;
int a;
fuc(d,a);

Then the system will replace the T generating function in the function template with the data type double of argument d:


double fuc(double x,int y)
{
double x;
// ... 
}

Related articles: