c ++ --- initial templates and template

  • What generic programming, template
  • Function template
  • Class Template
  1. What is a generic programming
    how to use a function to exchange data int, you might say this is so so easy, you see me
void swap(int &a,int &b){
	int c = a;
	a = b;
	b = a;
}

This is not written yet, but if you write to make a double function type of exchange it, you might also say simple, but to achieve a switching function custom type it, no way, right! ! Because there are numerous kinds of custom types, it is impossible to achieve a myriad of functions bar, where we need to use generic programming, the same functional capabilities of this exchange, but not the same type of slowing down, we can not pass a generic type so that the compiler themselves to identify the type of device it? Generic programming is like this, just give the compiler a template that allows the compiler to identify their own type. This is the template is generic and type-independent code to write, is a means of code reuse. Template that we conducted on the basis of the generic programming.

- function template

  1. What is the function template
    name suggests is a function template is to provide a template, represents a family of functions, has nothing to do with the type of the function template, parameterized in use, according to the characteristics of the type produced version argument types

  2. Function template format

    template<class T1,class T2…>

Return type function name (parameter list)
e.g.

template<class T>
void Swap(T& a,T& b){
	T c = a;
	a = b;
	b = c;
}

This enables all the switching functions.
This class can be typename replaced, but when we call a function template called it?

  1. The principle function template
    function template when we call instead of calling, but the compiler will generate a corresponding function according to our argument when we call the type function template, you can find a function generated according to our function overloading we in line with the type of function

Here Insert Picture Description
Template is a blueprint, not a function of his own, is the compiler of use to produce specific types of mold-specific function, the compiler when we call the function to deduce template to generate a corresponding function according to the type of argument types passed for transfer. For example, we use the picture above is called when a double function template, compiled by deducing the corresponding argument types.
To see whether the code can be compiled to run below

#include <iostream>
using namespace std;
template <class T>
void Swap(T &a, T& b){
	T c = a;
	a = b
	b = c;
}
int main(){

	system("pause");
	return EXIT_SUCCESS;
}

The answer is compiles and runs, containing function templates in our code, we will only use the time into our function template check for errors, but some of the template function block can not be missed, {is} It is indispensable.

  1. Use function template
  • Implicit instantiation
    does not specify the type when using function templates, such as our code above Swap(4,5);. Is implicitly instantiated
template<class T>
void Swap(T& a,T& b){
	T c = a;
	a = b;
	b = c;
}
int main(){
	Swap(4,5);
	return 0;
}
  • It shows an example of
    the actual type of template function specified in the function name <> in
    suchSwap<int>(4,5);
template<class T>
void Swap(T& a,T& b){
	T c = a;
	a = b;
	b = c;
}
int main(){
	Swap<int>(4,5);
	return 0;
}

Matching principle template parameters

  • A non-template functions and template functions with the same name exist, and that the function template that can be instantiated into the non-template functions when selecting a non-template function.
  • For non-template functions and function template of the same name, if other conditions are the same, when the mobilization of non-priority call the template function without generating an instance from the template. If the template function can produce a better match, then select the template
  • Template function does not allow automatic type conversion, but common functions can be automated type conversion

Class template
similar class templates and function templates are designed to achieve a certain number of classes, these classes are the same functionality, but with different data parameters
such as our string class and an int array

  1. The definition of a class template
template <class T1,class T2....>
class 类模板名{
	//类成员
};

Such as our order form, the order table can store any data type, but different types of data storage.

template <class T>
class Vector{
public:
		Vector(){};
		~Vector(){};
		void Push_back(){const T& data};
		....
private:
		T* _p;
		size_t _size;
		size_t _capacity;
};

Here we are not a Vector class, and it is not our template function as a function, Vector is a container, but a template for storing data.

  1. Examples of the class template
    class template instantiation need to add after the class template <>, together with the data type in <>,
    e.g.std::vector<int>
#include <iostream>
using std::cout;
using std::endl;
int main(){
	std::vector<int>v;
	v.push_back(1);
	v.push_back(2);
	return 0;
}

We operate after class template can be initialized using this v.
Our understanding of Vectorand Vector<int>relationship between the
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/boke_fengwei/article/details/90404941