第二十三模板 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;
}*/
//我们看到输出了”普通函数“这说明在函数名,参数类型和参数个数相同的情况,普通函数优先级高于具体化函数模板,而具体化函数模板又高于函数模板

  

posted @ 2012-10-04 19:55  简单--生活  阅读(379)  评论(0编辑  收藏  举报
简单--生活(CSDN)