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

来源:互联网 发布:js涂料 编辑:程序博客网 时间:2024/05/22 06:34

函数匹配规则:

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;//overload1cout<< 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;//overload1cout<< LESS(c1,c2) <<endl;//special_one cout<< LESS(cc1,cc2) <<endl;//functioncout<< 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,所以选择普通函数版本。

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