• C++_模板函数重载


    C++_模板函数重载

    1、先列出候选函数,包括普通函数、参数推到成功的模板函数
    2、根据“类型转换”来排序
    3、选择更匹配的函数
    4、匹配度相同时,优先选择普通函数
    5、对于多个模板函数选择更特化的
    ①列出推导函数:
    第一个模板函数:mymax(const int& a, const int& b);
    第二个模板函数:mymax(int& a, int& b);
    第三个模板函数:mymax(int& a, int& b);
    ②确定咱们调用的参数
    mymax(int, int);
    ③根据参数排序
    第一个模板函数:int转换成const int
    第二个模板函数:int装换成int
    第三个模板函数:int装换成int

    #include 
    #include 
    #include 
    
    using namespace std;
    
    template<typename T>
    const T& mymax(const T& a, const T& b)  //排在第二位
    {
    	cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    template<typename T>
    const T& mymax(T& a, T& b)  //并列第一位
    {
    	cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    const int& mymax(int& a, int& b)  //并列第一位,因为优先选择普通函数,所以不会有二义性
    {
    	cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    int main(int argc, char **argv)
    {
    	int ia = 1;
    	int ib = 2;
    
    	cout<<mymax(ia, ib)<<endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    2、复现二义性出现

    #include 
    #include 
    #include 
    
    using namespace std;
    
    template<typename T>
    const T& mymax(const T& a, const T& b)  //排在第二位
    {
    	cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    template<typename T>
    const T& mymax(T& a, T& b)  //并列第一位
    {
    	cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    const int& mymax(int& a, int& b)  //并列第一位,因为优先选择普通函数,所以不会有二义性
    {
    	cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    template<typename T>
    const T mymax(T a, T b)  //并列第一位
    {
    	cout<<"4: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    int main(int argc, char **argv)
    {
    	int ia = 1;
    	int ib = 2;
    
    	cout<<mymax(ia, ib)<<endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    #include 
    #include 
    #include 
    
    using namespace std;
    
    template<typename T>
    const T& mymax(const T& a, const T& b)
    {
    	cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    template<typename T>
    const T& mymax(T& a, T& b)
    {
    	cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    #if 0
    const int& mymax(int& a, int& b)   //普通函数注释掉后剩下两个并列第一位的模板函数就会因为二义性而报错
    	cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    #endif
    
    template<typename T>
    const T mymax(T a, T b)
    {
    	cout<<"4: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    
    int main(int argc, char **argv)
    {
    	int ia = 1;
    	int ib = 2;
    
    	cout<<mymax(ia, ib)<<endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    3、传入指针变量
    ①列出推导函数:
    第一个模板函数:mymax(const int*, const int*);
    第二个模板函数:mymax(int*, int*);
    第三个模板函数:不匹配
    ②根据参数排序:
    第一个模板函数:int *转换成const int *
    第二个模板函数:int *装换成int *

    #include 
    #include 
    #include 
    
    using namespace std;
    
    template<typename T>
    const T& mymax(const T& a, const T& b)  //第二位
    {
    	cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    template<typename T>
    const T& mymax(T& a, T& b)  //第一位
    {
    	cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    const int& mymax(int& a, int& b)
    {
    	cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    int main(int argc, char **argv)
    {
    	int ia = 1;
    	int ib = 2;
    
    	cout<<mymax(ia, ib)<<endl;
    
    	int *p1=&ia;
    	int *p2=&ib;
    
    	cout<<mymax(p1, p2)<<endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    4、改进例程3
    ①列出推导函数:
    第一个模板函数:mymax(const int*, const int*);
    第二个模板函数:mymax(int*, int*);
    第三个模板函数:不匹配
    第四个模板函数:mymax(int*, int*);
    ②根据参数排序:
    第一个模板函数:int 转换成const int *
    第二个模板函数:int 装换成int *
    第四个模板函数:int 装换成int *
    ③找出最特化,对比第二个模板函数和第四个模板函数
    第二个模板函数可以推导出:mymax(int
    , int
    );和mymax(int, int);
    第四个模板函数只能推导出:mymax(int
    , int*);更加具体化,所以选择第四个模板

    #include 
    #include 
    #include 
    
    using namespace std;
    
    template<typename T>
    const T& mymax(const T& a, const T& b)
    {
    	cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    template<typename T>
    const T& mymax(T& a, T& b)
    {
    	cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    template<typename T>
    const T mymax(T * a, T* b)
    {
    	cout<<"4: "<<__PRETTY_FUNCTION__<<endl;
    	return (*a < *b)? *b : *a;
    }
    
    const int& mymax(int& a, int& b)
    {
    	cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
    	return (a < b)? b : a;
    }
    
    int main(int argc, char **argv)
    {
    	int ia = 1;
    	int ib = 2;
    
    	cout<<mymax(ia, ib)<<endl;
    
    	int *p1=&ia;
    	int *p2=&ib;
    
    	cout<<mymax(p1, p2)<<endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
  • 相关阅读:
    视频理解学习笔记(三)
    网络安全高级工具软件100套
    别把对象当Map
    Excel-lookup函数核对两个表格的数据匹配
    在WPF应用中使用FastReport.WPF报表模块
    【面试题】在循环 for、for-in、forEach、for-of 、map中改变item的值,会发生什么?
    Day17:图
    Matlab:数据分析与多项式计算
    算法小讲堂之关键路径
    详解Redis之Lettuce实战
  • 原文地址:https://blog.csdn.net/weixin_50183638/article/details/126519581