zoukankan      html  css  js  c++  java
  • 第二十三模板 4普通函数,函数模板与具体化函数模板的优先级 简单

    //第二十三模板 4普通函数,函数模板与具体化函数模板的优先级
    //我们定义了个普通函数,然后又定义了一个该函数的模板,接着又重载了这个函数的模板,那么这里就存在一个优先级的问题,即先调用哪里个函数
    //1  普通函数和函数模板的执行次序
    /*#include <iostream>
    using namespace std;
    template <class T>
    void show(T a){cout<<"模板函数"<<endl;}
    void show(int a){cout<<"普通函数"<<endl;}
    int main()
    {
    	int a=5;
    	show(a);//普通函数调用在先,模板函数调用在后
    	//不过这个前提是执行调用函数的必须与普通函数的参数类型
    	show(3.14);
        return 0;
    }*/
    
    // 2 函数模板与具体化函数模板的执行次序
    /*#include <iostream>
    using namespace std;
    template <class T>
    void show(T &a){cout<<"模板函数"<<a<<endl;}
    template<> void show<int>(int&a){ cout<<"具体化模板函数"<<a<<endl;}
    int main()
    {
    	int a=9;
        show(a);
    	float b =2.5;
    	show(b);
    	return 0;
    }*/
    
    //3 具体化函数模板与普通函数的优先级
    /*#include <iostream>
    using namespace std;
    template<class T>
    void show(T a){ cout<<"模板函数"<<endl;}
    template<> void show<int>(int a){ cout<<"具体化模板函数"<<a<<endl;}
    void show(int a){ cout<<"普通函数"<<a<<endl;}
    int main()
    {
    	int a=5;
    	show(a);
        return 0;
    }*/
    //我们看到输出了”普通函数“这说明在函数名,参数类型和参数个数相同的情况,普通函数优先级高于具体化函数模板,而具体化函数模板又高于函数模板
    

      

  • 相关阅读:
    Codeforces 1265A Beautiful String
    1039 Course List for Student (25)
    1038 Recover the Smallest Number (30)
    1037 Magic Coupon (25)
    1024 Palindromic Number (25)
    1051 Pop Sequence (25)
    1019 General Palindromic Number (20)
    1031 Hello World for U (20)
    1012 The Best Rank (25)
    1011 World Cup Betting (20)
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2711679.html
Copyright © 2011-2022 走看看