Function template (C++01)

1. Introduction

In C++, the type of data can also be passed through parameters, and the specific data type may not be specified when the function is defined. When a function call occurs, the compiler can automatically infer the data type based on the actual parameters passed in. This is type parameterization.

A function template is a special function that can be called using different types. For functions with the same function, there is no need to write code repeatedly, and the function template looks very similar to an ordinary function. The difference is that the type can be parameterized.

2. Grammar

Syntax of template function:

template <typename 类型参数1 , typename 类型参数2 , ...> 返回值类型  函数名(形参列表){
  //在函数体中可以使用类型参数
}

There can be multiple type parameters, ,separated by commas . The type parameter list is < >enclosed, and the formal parameter list is ( )enclosed.

typenameKeywords can also be classreplaced by keywords, and there is no difference between them. C++’s early support for templates was not rigorous. No new keywords were introduced. Instead, class was used to specify type parameters. However, the class keyword was already used in class definitions. This was not very friendly, so later C++ A new keyword typename is introduced to define type parameters. However, there are still many codes using the class keyword, including the C++ standard library, some open source programs, etc.

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

#if 0 
//int类型数据交换
void MySwap(int& a, int& b){
    
    
	int temp = a;
	a = b;
	b = temp;
}
//double类型
void MySwap(double& a, double& b){
    
    
	double temp = a;
	a = b;
	b = temp;
}
#endif

//模板技术 类型参数化 编写代码可以忽略类型
//为了让编译器区分是普通函数  模板函数
template<class T1,class T2> //template<typename T>告诉编译器 ,下面写模板函数
void MySwap(T& a, T& b){
    
    
	T temp = a;
	a = b;
	b = temp;
}

void test01(){
    
    

	int a = 30;
	int b = 20;
	//1 自动类型推导,编译器根据你传的值 进行类型自动推导
	cout << "a:" << a << " b:" << b << endl;
	MySwap(a, b); 
	cout << "a:" << a << " b:" << b << endl;

	double da = 12.3;
	double db = 21.1;
	cout << "da:" << da << " db:" << db << endl;
	MySwap(da, db);
	cout << "da:" << da << " db:" << db << endl;

	//2. 显式的指定类型
	MySwap<int>(a, b);
}

int main(void){
    
    	
	test01();
	return 0;
}

Three, function templates and ordinary functions

Function templates are the same as ordinary functions and can also be overloaded

  • C++ compiler gives priority to ordinary functions
  • If the function template can produce a better match, then choose the function template
  • You can also limit the compiler to only match function templates through the empty template argument list<>
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

template<class T>
int MyAdd(T a,T b){
    
    
	return a + b;
}

//普通函数可以进行自动类型转换
//函数模板必须严格类型匹配
int MyAdd(int a,int c){
    
    
	return a + c;
}

void test01(){
    
    	
	int a = 10;
	int b = 20;
	char c1 = 'a';
	char c2 = 'b';

	MyAdd<>(a,b);//限定只使用函数模板
 
	MyAdd(a,c1);//这个调用,函数模板有更好的匹配,于是调用函数模板
	MyAdd(a, b);//普通函数int MyAdd(int a,int c)已经能完美匹配,于是调用普通函数
	MyAdd(c1,b);//这个调用,函数模板有更好的匹配,于是调用函数模板
}

//函数模板被重载
template<class T>
void Print(T a){
    
    	
    
}
template<class T>
void Print(T a , T b){
    
    
    
}

int main(void)
{
    
    
	test01();	
	return 0;
}

Fourth, the function template mechanism:

  • The compiler does not process function templates into any type of function
  • Function templates generate different functions through specific types
  • The compiler recompiles the function template, compiles the template code itself at the place of declaration, and compiles the code after parameter substitution at the place of call.

Guess you like

Origin blog.csdn.net/weixin_45341339/article/details/111828814