C++ function template (2) The difference between ordinary functions and function templates, the calling rules of ordinary functions and function templates, and the limitations of templates

C++ function template (2)

Yesterday I just sorted out the C++ function template (1), and you must stick to it today. Today, I will continue to organize the notes of the C++ function template. Today, I will mainly organize-the difference between ordinary functions and function templates, the calling rules of ordinary functions and function templates, and the limitations of templates. Don't talk nonsense, rush!

05 The difference between ordinary function and function template
1. Automatic type conversion (implicit type conversion) can occur when ordinary function is called.
2. Function template, automatic type inference, and implicit type conversion cannot occur.
3. Function template, use display to specify type, implicit type conversion can occur.
The specific code implementation is as follows:

#include<iostream>
using namespace std;
//普通函数 减法
int  Sub(int a,int b)
{
    
    
	return(a-b);

}

//写一个模板函数
template<class T>
T Sub01(T a,T b)
{
    
    
     return (a-b);
}

//写一个输出函数
void test01()
{
    
    
    int a=100;
	int b=200;
	char c= 'c';

	cout<<Sub(a,c)<<endl;
	//自动类型推导输出
	//cout<<Sub01(a,c)<<endl; 自动类型推导不可以发生隐式类型转换
	//显示指定类型
	cout<<Sub01<int>(a,c)<<endl;
}

int main()
{
    
    
	test01();

system("pause");
return 0;
}

Note: It is recommended to use the method of displaying the specified type and calling the function template, because you determine the general type T yourself.

06 Rules for calling common functions and function templates
1. If both function templates and common functions can be implemented, call common functions first.
2. The function template can be forced to be called through the empty template parameter list.
3. Function templates can also be overloaded.
4. If the function template can produce a better match, call the function template first.

#include<iostream>
using namespace std;
//1、如果函数模板和普通函数同时调用,则优先调用普通函数

//先来一个普通函数
void myfunc(int a,int b)
{
    
    
cout<<"This is ordinary func"<<endl;
}

//再来一个模板函数
template<typename T>
void myfunc(T a,T b)
{
    
    
cout<<"This is 模板函数"<<endl;

}

//再来一个函数重载
template<typename T>
void myfunc(T a,T b,T c)
{
    
    
cout<<"This is 重载模板函数"<<endl;

}

//写一个输出函数
void test01()
{
    
    
int a=10;
int b=20;
//myfunc(a,b);

//通过空模板参数列表来,强制调用函数模板
//myfunc<>(a,b);

//函数重载的调用
//myfunc<>(a,b,0);

//如果函数模板可以产生更好的匹配,优先调用函数模板
char c1='a';
char c2='b';

myfunc<>(c1,c2);//这是因为,输入的是字符,如果是调用普通函数需要进行隐式转换,即把字符转换为int数字,而调用模板的只用推导出T即可不用进行转换。
}**加粗样式**
//模板的话自动类型推导,不可以发生隐式类型转换。显示指定类型的话可以发生隐式转换。

int main()
{
    
    

	test01();

system("pause");
return 0;
}

Note: Provide function templates, do not write ordinary functions to avoid ambiguity.

07 Limitations of
Templates The versatility of templates is not a panacea. Some specific data types require special implementations such as bool data type calls and some custom data types such as Person.
The function case test is as follows:

#include<iostream>
using namespace std;
#include<string>
//函数的局限性,函数的通用性不是万能的
//有些特定的数据类型需要用具体话的方式来实现
//整一个自定义数据类型的对比
class Person
{
    
    
public:
	Person(string name,int age)
	{
    
    
		this->m_name=name;
		this->m_age=age;
	}

//姓名
string m_name;
//年龄
int m_age;
};

//1、比较两个数据的大小
template<class T>
bool Contrast(T &a,T &b)
{
    
    
if(a==b)
{
    
    
return true;
}
else
{
    
    
return false;
}

}

//利用具体化Person的版本实现代码,具体化优先调用
template<> bool Contrast(Person &p1,Person &p2)
{
    
    
	if(p1.m_name == p2.m_name && p1.m_age==p2.m_age)
	{
    
    
	return true;
	}
	else
	{
    
    
	return false;
	}

}

//写一个测试输出函数
void test01()
{
    
    
int a=10;
int b=20;

bool ret=Contrast(a,b);
if(ret)
{
    
    
cout<<"a==b"<<endl;
}
else
{
    
    
cout<<"a!=b"<<endl;
}
}

//再写一个自定义类的输出函数
void test02()
{
    
    
Person p1("wjx",20);
Person p2("wjx",20);

bool ret=Contrast(p1,p2);
if(ret)
{
    
    
cout<<"p1==p2"<<endl;
}
else
{
    
    
cout<<"p1!=p2"<<endl;
}
}
int main()
{
    
    
	test01();
	test02();

system("pause");
return 0;
}

Note:
1. How the character is defined char c1='a'; char c2='b'.
2. Classes such as Person\Class must be added at the end; if not, there will be unexpected errors.
3. The use of specific templates can solve the generalization of custom types.
4. Learning templates is not to write templates, but to use the templates provided by the system in STL to meet our needs.

Guess you like

Origin blog.csdn.net/qq_45252077/article/details/107895214