函数模版、重载和特化的匹配规则

函数匹配规则:

1.对于一个调用,其候选函数包括所有模版实参推断成功的函数模版实例(顶层const可忽略;对于非引用的形参,数组或函数指针转化到到指针)

2.可行函数(模版和非模版),按类型转换排序

3.如果恰有一个函数比其它都更匹配,则选择此函数,否则

a.如果只有一个非模版函数,选择他,

b.没有非模版函数,选择更特例化的模版

c.否则,有歧义,编译错误

注意:
函数特例化的本质是实例化一个模板,而非重载它,所以特例化的版本不影响函数匹配。也就是说函数匹配时只比较模版函数,模版函数重载,普通函数,不考虑特例化。只有选择了匹配了该函数模板后才会考虑该模板下的特化版本。
#include<iostream>

using namespace std;
template<class t>
inline bool LESS(const t& t1, const t& t2)
{
	cout<<"general   ";
	return t1<t2?1:0;
}
template<class t>
inline bool LESS( t* const t1,  t* const t2)
{
	cout<<"overload1  ";
	return *t1<*t2?1:0;
}
template<class t>
inline bool LESS(const t* const t1, const t* const t2)
{
	cout<<"overload2  ";
	return *t1<*t2?1:0;
}
int main()
{
	int int1=10;
	int int2=20;
	int* pint1 = &int1;
	int* pint2 = &int2;
	const int* cpint1 = &int1;
	const int* cpint2 = &int2;
	cout<< LESS(int1,int2) <<endl;//general 
	cout<< LESS(pint1,pint2) <<endl;//overload1
	cout<< LESS(cpint1,cpint2) <<endl;//overload2
}
对于第二个LESS,三个版本都是可行的。对于第一个版本,t绑定到int*,生成的可行的实例是LESS(const int* & t1, const int* & t2),需要一次普通指针到const指针的转换;对于第二个版本是精确匹配;对于第三个版本,t绑定到int,生成的可行的实例是LESS(const int*  t1, const int*  t2),需要一次普通指针到const指针的转换。规则3,所以选择第二个版本。
对于第三个LESS,三个版本都是可行的。对于第一个版本,t绑定到int*,生成的可行的实例是LESS(const int* & t1, const int* & t2);t绑定到const int*,生成的可行的实例是LESS(const int*  t1, const int*  t2);对于第三个版本,t绑定到int,生成的可行的实例是LESS(const int*  t1, const int*  t2)。规则3.b,三个版本中,他们的通用程度依次降低,所以选择第三个版本。

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

//函数模版只能全特化,不能偏特化,但是可以重载呀
template<class t>
inline bool LESS(const t& t1, const t& t2)//本质上可用于任何类型,包括指针类型。 
{
	cout<<"general   ";
	return t1<t2?1:0;
}
template<class t>//指针类型的重载版 ,只能用于指针类型,所以比上一个更特例化 。 
inline bool LESS(t* const t1, t* const t2)
{
	cout<<"overload1  ";
	return *t1<*t2?1:0;
}
template<class t>//const指针类型的重载版 ,只能用于const指针类型,所以比上一个更特例化 。 
inline bool LESS(const t* const t1, const t* const t2)
{
	cout<<"overload2  ";
	return *t1<*t2?1:0;
}

bool LESS(const char* const t1, const char* const t2)//普通函数版本,优先级更高 
{
	cout<<"function  ";
	return strcmp(t1,t2);
}

template<>
inline bool LESS<char>(const char &t1, const char &t2)//特化 template<class t> t LESS(const t& t1, const t& t2)
{
	cout<<"special_one   ";
	return t1<t2?1:0;
}
template<>
inline bool LESS<const int*>(const int* const &t1, const int* const &t2)//特化 template<class t> t LESS(const t& t1, const t& t2)
{
	cout<<"special_two   ";
	return *t1<*t2?1:0;
}

int main()
{
	int int1=10;
	int int2=20;
	int* pint1 = &int1;
	int* pint2 = &int2;
	char c1 = 'a';
	char c2 = 'z';
	const char* cc1 = "abc";
	const char* cc2 = "abc";

	cout<< LESS(pint1,pint2) <<endl;//overload1
	cout<< LESS(c1,c2) <<endl;//special_one 
	cout<< LESS(cc1,cc2) <<endl;//function
	cout<< LESS("hi","zzzz") <<endl;//function
}
第一个LESS,可行的函数有general,overload1,overload2;其中overload1版本精确匹配,其它两个需要const转换;虽然general也有一个完全匹配的特化版本special_two,但是“只有选择了匹配了该函数模板后才会考虑该模板下的特化版本”,所以special_two根本就没有参与到匹配中。
第二个LESS,可行的只有general版本,所以选择它,同时发现有char类型的全特化版本,所以选择special_one。
第三个LESS,可行的有general,overload2和普通函数。对于general,t绑定到char*,生成的可行的实例是LESS(const char* & t1, const int* & t2),精确匹配;对于overload2,t绑定到chat,生成的可行的实例是LESS(const char*  t1, const char*  t2),精确匹配;对于普通函数,也是精确匹配。规则3.a,所以选择普通函数版本。

所以总结一下,在候选的函数中,优先在精确匹配中选择,优先选择普通函数,特例性更强的模版函数次之,然后是模版函数的特化版本,最后才是泛化版本。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
函数模板的重载是指在同一作用域内定义多个同名函数模板,但这些函数模板的参数列表或参数类型不同,从而实现了函数模板的重载函数模板重载的方法有两种: 1. 普通函数函数模板的重载 可以在函数模板的定义前或后定义一个普通函数,这个普通函数函数模板同名但参数列表或参数类型不同,就可以实现重载。例如: ```cpp // 普通函数 void swap(int& a, int& b) { int temp = a; a = b; b = temp; } // 函数模板 template<typename T> void swap(T& a, T& b) { T temp = a; a = b; b = temp; } ``` 2. 函数模板之间的重载 可以在同一作用域内定义多个函数模板,这些函数模板同名但参数列表或参数类型不同,就可以实现重载。例如: ```cpp // 函数模板1 template<typename T> void print(T data) { cout << data << endl; } // 函数模板2 template<typename T1, typename T2> void print(T1 data1, T2 data2) { cout << data1 << " " << data2 << endl; } ``` 需要注意的是,函数模板重载的时候,需要注意避免歧义的情况。例如: ```cpp // 函数模板1 template<typename T> void foo(T data) { // ... } // 函数模板2 template<typename T> void foo(T& data) { // ... } ``` 这里的函数模板1和函数模板2都是同名的foo,参数列表也都是一样的,只是第二个参数加了一个引用符号。在这种情况下,编译器无法确定应该调用哪个函数模板,会报错。为避免这种情况,可以采用或默认参数的方式来实现函数模板的重载

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值