C++ template function details

  • 2020-05-17 05:56:45
  • OfStack

Templates in C++ can be generally divided into two categories: template functions and template classes. This article begins with template functions, followed by template classes.

Definition: template functions are generic function descriptions, that is, they are defined using generic types that can be replaced with concrete types.

Code example:


#include <iostream>
// Template class declaration 
template<typename T>
void Swap(T& a,T& b);
int main()
{
  int i = 10;
  int j = 20;
  std::cout<<"i=" << i << "," << "j=" <<j;
  Swap(i,j);// generate  void Swap(int &,int&);
  std::cout<<"i=" << i << "," << "j=" <<j;
  double x = 11.5;
  double y = 19.5;
  std::cout<<"x=" << x << "," << "y=" <<y;
  Swap(x,y);// Compiler generation  void Swap(double &,double&);
  std::cout<<"x=" << x << "," << "y=" <<y;
  return 0;
}
// The definition of a template class 
template<typename T>
void Swap(T& a,T& b)
{
  T temp;
  temp = a;
  a = b;
  b = temp;
}

The above example is the simplest example of a function template, and the compiler generates the corresponding function according to the specific type used.

Overloaded templates:

Templates can be used when multiple types are required to use the same 1 algorithm for different types, as shown in the code above. But not all types use the same algorithm. To meet this requirement, you can override the template definition as you would overload a regular function definition. The feature table of an overloaded function must be different from that of an overloaded regular function. The code example is as follows:


#include <iostream>
// Template class declaration 
template<typename T>
void Swap(T& a,T& b);
const int iCount = 5;
template<typename T>
void Swap(T* a,T*b,int n);
int main()
{
  int i = 10;
  int j = 20;
  std::cout<<"i=" << i << "," << "j=" <<j;
  Swap(i,j);// generate  void Swap(int &,int&)
  std::cout<<"i=" << i << "," << "j=" <<j;
  double x = 11.5;
  double y = 19.5;
  std::cout<<"x=" << x << "," << "y=" <<y;
  Swap(x,y);// Compiler generation  void Swap(double &,double&);
  std::cout<<"x=" << x << "," << "y=" <<y;
  int d[iCount] = {0,1,2,3,4};
  int e[iCount] = {5,6,7,8,9};
  Swap(d,e,iCount);// Match the new template and swap the array 
  return 0;
}
// The definition of a template class 
template<typename T>
void Swap(T& a,T& b)
{
  T temp;
  temp = a;
  a = b;
  b = temp;
}
template<typename T>
void Swap(T* a,T*b,int n)
{
  for (int i=0;i<iCount;++i)
  {
    T temp;
    temp = a[i];
    a[i] = b[i];
    b[i] = temp;
  }
}

The code above adds a template to swap elements in the two arrays. The original template is marked (T&, T&), and the new template is marked (T[],T[]),int). Note that in the last template, the last parameter is of a concrete type (int), not a generic type, and not all template parameters must be template parameter types.

Display materialization:

For a given function name, there can be non-template functions, template functions, and display materialized template functions with their overloaded versions.

Display materialized stereotypes and definitions in template < > Start with, and indicate the type by name.

Materialization overrides the regular template, and non-template functions overwrite both the materialized and the regular templates.

Below are the non-template functions, template functions, and materialized prototypes used to exchange Job structures.


void Swap ( job &,job&);// Non-template functions 
template <typename T>
void Swap(T& . T& ) ;// The template function 
template <> void Swap<job>(job& . job&);// Display materialization function , Among them Swap After the job Parameter can be removed, then the function signature is template <> void Swap(job& . job&);

It was pointed out earlier that if there are multiple prototypes, the compiler will select the prototype in such a way that the non-template will take precedence over the display materialization and the template version, and the display materialization will take precedence over the version generated using the template.

The following call:

double u,v;
Swap (u v); // use common templates
job a,b;
swap (a,b)// use the display materialized version.

Instantiation and concretization:

In order to further understand templates, you must understand the terms instantiation and concretization. Remember, including a function template in your code does not itself generate a function definition, it is just a scheme for generating a function definition. When the compiler USES a template to generate a definition for a particular type, it gets an instance of the template (instantiation). For example, the function calls Swap (i, j), causing the compiler to generate an instance of Swap () that USES the int type. Templates are not function definitions, but instances of templates using int are function definitions. This type of instantiation is called implicit instantiation, because the compiler knows it needs to be defined because the program provides the int parameter when it calls Swap ().

Now the compiler can also allow display instantiation, which means you can directly command the compiler to generate specific instances, such as Swap < int > . The syntax is, declare the selected type - with < > The symbol indicates the type and is preceded by the keyword template:


template void Swap<int>(int,int ) ;// Display instantiation 

When the compiler that implements this feature sees the above declaration, it will use the Swap () template to generate an instance of type int.

Unlike display instantiation, display materialization USES one of the following two equivalent declarations:


template <> void Swap<int>(int,int);
template <> void Swap(int,int);

The difference is that these statements mean "do not use the Swap() template to generate function definitions, but instead use a separate, specialized function definition that is displayed to generate function definitions of type int.

Note: attempting to use the same type of display materialization and display instantiation in 1 programming unit will cause an error.

The above is the site for you to introduce C++ template function details, I hope to help you, if you have any questions welcome to give me a message.


Related articles: