• 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
  • 相关阅读:
    《第一行代码》核心知识点:Android简介
    力扣刷题记录163.1-----684.冗余连接
    我的DW个人网站设计——安徽宣城6页HTML+CSS+JavaScript
    A Survey on Bias and Fairness in Machine Learning 阅读笔记
    【初阶数据结构】——堆排序和TopK问题
    无人驾驶(二)---室外导航之RTK配置与接入及GPS与UTM坐标转换
    佘太地纯的前世今生
    Dinky上路之旅
    【Call for papers】DSN-2023(CCF-B/截稿日期: 2022年12月7日)
    Ansys Zemax|在设计抬头显示器(HUD)时需要使用哪些工具?
  • 原文地址:https://blog.csdn.net/weixin_50183638/article/details/126519581