C + + template learning

1. C + + template

Template definition: template is a tool to realize the code reuse mechanism. It can realize type parameterization, that is, define types as parameters, so as to realize the real code reusability.

Template classification: function template and class template. Function templates are for functions with different parameter types; Class templates are only for classes with different types of data members and member functions.

Purpose of using templates: let programmers write type independent code.

Note: the declaration or definition of a template can only be made in the global, namespace or class scope. That is, it cannot be carried out within the local scope or function. For example, a template cannot be declared or defined in the main function.

2. Function templates

Function template in C + +
https://blog.csdn.net/lms1008611/article/details/81985815

Templates enable us to generate general functions. These functions can accept parameters of any data type and return values of any type without overloading all possible data types. This realizes the function of macro to a certain extent. Their prototype definition can be any of the following two:

template <class type> ret-type func-name(parameter list)
{
   // Body of function
}

template <typename type> ret-type func-name(parameter list)
{
   // Body of function
}

Here, type is the placeholder name of the data type used by the function. This name can be used in the function definition.

The function template instance returns the maximum of the two numbers:

#include <iostream>
#include <string>
using namespace std;

template <typename T>
T Max (T const& a, T const& b)
{ 
    return a < b ? b:a; 
} 
int main()
{
	int i = 29;
	int j = 20;
	cout << "Max(i, j):" << Max(i, j) << endl; 

	double f1 = 13.5; 
    double f2 = 20.7; 
    cout << "Max(f1, f2): " << Max(f1, f2) << endl; 
    return 0;
}

Function templates can also define any number of different type parameters, but for multi parameter function templates:

  • The compiler cannot automatically deduce the return value type
  • Type parameters can be specified from left to right
#include <iostream>
using namespace std;
template <typename T1, typename T2, typename T3>
T1 add(T2 a, T3 b)
{
	T1 ret;
	ret
}
void main()
{
	int c = 12;
	float d = 23.4;
	//cout << add(c, d) << endl; 		// error, the return value of the function cannot be derived automatically
	cout << add<float>(c, d) << endl;	//The return value is specified in the first type parameter
	cout << add<int, int, float>(c, d) << endl;
}

Question: so far, the argument types passed by the above debugging are all basic types. Can you pass relatively complex parameters such as structures and classes? Yes, it needs to be designed according to the expected results and data characteristics.

2.1 function template classification

According to different function types, they can be divided into ordinary function templates and member function templates.
According to the number of parameters, it can be divided into single parameter type function template and multi parameter type function template.

A previous article: https://blog.csdn.net/mayue_web/article/details/83755761

3. Class templates

Class templates in C + +
https://blog.csdn.net/lms1008611/article/details/82013901

The general form of generic class declaration is as follows:

template< class Formal parameter name, class Formal parameter name, ......> class Class name {...};

//example
template <class T>
class test 
{
private:
    T m_value;
public:
    test(T value)
    {
		m_value = value;
    }
    T get()
    {
		return m_value;
    }
};

In this way, we define a simple class template, where T represents any type and can appear anywhere in the class template. Unlike the function template, the compiler cannot automatically deduce the specified data type that must be displayed when using the class template, such as test < int > t; The specified data type that needs to be displayed.

The compiler handles class templates the same way as function templates

  • Generate different classes from class templates by specific types
  • Compile the class template code itself where it is declared
  • Compile the code after parameter replacement where it is used

example:

#include <iostream>
#include <string>
using namespace std;
 
template <typename T>
class test 
{
private:
    T m_value;
public:
    test(T value)
    {
	m_value = value;
    }
    T get()
    {
	return m_value;
    }
};

void main()
{
    //test t(12.3); 	// error, type cannot be derived automatically
    test<int> t1(12);	//Displays the specified type as int
    test<string> t2("hello C++");	//Displays a string of the specified type
 
    cout << t1.get() << endl;
    cout << t2.get() << endl;
}

When defining class templates, the general rule is

  • Define the class template in the header file
  • The member functions in the class template must be implemented in the same file and cannot be implemented separately in different files
  • Member functions defined outside the class template need to be declared like template

As follows, we implement the above test class template in the test.h header file and the member function outside

#ifndef __TEST_H__
#define __TEST_H__
 
template <typename T>
class test 
{
private:
    T m_value;
public:
    test(T value)
    {
	m_value = value;
    }
    T get();
};
 
template <typename T>	//Member functions implemented outside the class need to add similar template declarations
T test<T>::get()
{
    return m_value;
}
 
#endif	//__TEST_H__

Like function templates, class templates can define multiple parameters, such as

template <typename T1, typename T2>
class test
{
private:
    T1 m_value1;
    T2 m_value2;
public:
    test(T1 value1, T2 value2)
    {
	m_value1 = value1;
	m_value2 = value2;
    }
};

summary
-C + + class templates, like function templates, handle different types in the same way to achieve code reuse
-Class templates are ideal for writing code related to data structures
-C + + class templates can only display the specified types when used

4. References

C + + template summary
https://blog.csdn.net/yzhang6_10/article/details/50839516

C + + template
http://yige.org/cpp/templates.php

C + + templates and generic programming
http://c.biancheng.net/cplus/70/

Keywords: C++

Added by UrbanCondor on Fri, 03 Dec 2021 09:53:02 +0200