模板全特化、偏特化(局部特化)

012類型模板全特化_全特化類模板_全特化類成員函數

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>

using namespace std;
template<typename T,typename U>
class TC//泛化的tc類模板
{
public:
	void FunctionTest01()
	{
		cout << "泛化版本" << endl;
	}
	//構造函數
	TC()
	{
		cout << "泛化版本構造函數" << endl;
	}
};

//必須先有泛化版本,才能存在特化版本
//當T和U都爲int類型,我們希望做一個特化版本--當兩個參數都是int,系統執行我們的特化版本函數
//全特化:所有的類型模板參數,T U都用具體的類型代表
template<>//全特化,都用int代表
class TC<int,int>
{//上面泛化版本的T U參數分別對應這裏的int   int
	//這裏可以對特化版本做操作
public:
	void FunctionTest01()
	{
		cout << "int int 特化版本版本" << endl;
	}
	
};


template<>//全特化,都用double代表
class TC<double,int>
{//上面泛化版本的T U參數分別對應這裏的int   int
 //這裏可以對特化版本做操作
public:
	void FunctionTest01()
	{
		cout << "double int 特化版本版本" << endl;
	}

};



template<>//全特化
void TC<double,double>::FunctionTest01()
{
	cout << "double doubel FunctionTest01特化版本版本" << endl;
}


int main(void)
{
	//特化版本代碼,編譯器會優先選擇
	TC<char, int>tchar;
	tchar.FunctionTest01();//調用泛化版本

	TC<int, int>tint;
	tint.FunctionTest01();//調用int,int 特化版本

	TC<double, int>tdouble;
	tdouble.FunctionTest01();//調用double int版本


	//特化成員函數,而不是類模板
	TC<double, double>tdooo;
	tdooo.FunctionTest01();//調用全特化的成員函數

	system("pause");
	return 0;
}

/*
*(1)特化---泛化
*泛化--可以隨便指定類型
*特化--對特殊的類型模板參數進行特殊對待,給它寫適合它的專用代碼
*	類模板全特化
*		1.常規全特化
*		2.特化成員函數,而不是模板
*特化版本可以任意多。
*--------------------------------------------------------
*泛化版本構造函數
泛化版本
int int 特化版本版本
double int 特化版本版本
泛化版本構造函數
double doubel FunctionTest01特化版本版本
請按任意鍵繼續. . .
*
*(3)
*
*
*/

013類型模板偏特化_局部特化_模板參數數量上

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>

using namespace std;

template<typename T, typename U,typename W>
class TC//泛化的tc類模板
{
public:
	void FunctionTest01()
	{
		cout << "泛化版本" << endl;
	}
	//構造函數
	TC()
	{
		cout << "泛化版本構造函數" << endl;
	}
};

//模板參數數量--偏特化,2個參數特化
template< typename U>//T W綁定到具體的類型
class  TC<int,U,int>
{
public:
	void FunctionTest01()
	{
		cout << "數量--偏特化版本化版本" << endl;
	}
};




int main(void)
{
	
	TC<double, double, double> tcddd;
	tcddd.FunctionTest01();//調用泛化版本
	TC<int, double, int >tcidi;
	tcidi.FunctionTest01();//調用數量--偏特化版本


	
	system("pause");
	return 0;
}

/*
*(1)類型模板偏特化--也叫局部特化
*	1.模板參數數量上
*
*(3)
*
*
*/

014局部特化_範圍上

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>

using namespace std;

template<typename T>
class TC
{
public:
	void FunctionTest()
	{
		cout << "調用泛化版本函數" << endl;
	}
};

//範圍特化版本  const
template<typename T>
class TC<const T>
{
public:
	void FunctionTest()
	{
		cout << "Const T特化版本" << endl;
	}
};

//範圍特化版本  T*
template<typename T>
class TC<const T*>
{
public:
	void FunctionTest()
	{
		cout << "T*特化版本" << endl;
	}
};


//範圍特化版本--左值引用
template<typename T>
class TC<const T&>
{
public:
	void FunctionTest()
	{
		cout << "T&特化版本" << endl;
	}
};

//範圍特化版本--右值引用
template<typename T>
class TC<const T&&>
{
public:
	void FunctionTest()
	{
		cout << "T&&特化版本" << endl;
	}
};

int main(void)
{
	TC<double>td;
	td.FunctionTest();//調用泛化版本

	TC<double*>tdp;
	tdp.FunctionTest();//調用泛化版本

	TC<const double> tcc;
	tcc.FunctionTest();//調用const特化版本
	
	TC<int&>ti;
	ti.FunctionTest();//調用泛化版本


	TC<int&&>tii;
	tii.FunctionTest();//調用泛化版本
	
	system("pause");
	return 0;
}

/*
*(1)
*模板參數範圍上  int --特化一個const int  大--小
*		從T縮小爲T*
*		從T縮小爲T& T&&
*		這種豆角範圍縮小
*(2)局部特化,特化完畢還是個類模板,因爲裏面包含類型參數T。
*全特化完畢后里面不包含類型參數T
* Sunrise於東北電力大學第二教學樓1121實驗室
* 2019年11月25日15:50:32
*
*(3)
*
*
*/

015函數模板全特化

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>

using namespace std;


template<typename T,typename U>
void TestFunction(T &t,U&u)
{
	cout << "--這是函數泛化版本--" << endl;
}

//全特化版本 t=int,u=doubel
template<>//全特化,所以爲空
void TestFunction(int &t, double&u)
{
	cout << "--這是int double類型的特化版本--" << endl;
}


void TestFunction(int &t, double&u)
{
	cout << "這是testFunction的重載函數" << endl;
}

int main(void)
{
	//函數模板全特化
	const char*p = "jisuanjiz";
	int i = 1200;
	TestFunction(p, i);//t=const char* &
	

	double d = 23.344;
	TestFunction(i, d);//調用int double 的特化版本
	//如果有重載函數,優先調用重載函數,而不去調用特例話的函數

	system("pause");
	return 0;
}

/*
*(1)函數模板全特化
*全特化函數實際山等價於實例化一個函數模板,並不是等價於一個函數重載
*
*特化函數遇上重載函數優先調用哪一個???
*
* Sunrise於東北電力大學第二教學樓1121實驗室2019年11月25日15:58:46
*
*(2)
*
*(3)
*
*
*/

016函數模板不能偏特化

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>

using namespace std;


template<typename T, typename U>
void TestFunction(T &t, U&u)
{
	cout << "--這是函數泛化版本--" << endl;
}

//全特化版本 t=int,u=doubel
template<typename T>//這種寫法是隻有一個類型參數的新的模板,不是局部偏特化
void TestFunction(T&t, double&u)
{
	cout << "--這是局部偏特化的特化版本--" << endl;
}




int main(void)
{
	//函數模板全特化
	const char*p = "jisuanjiz";
	int i = 1200;
	TestFunction(p, i);//t=const char* &


	double d = 23.344;

	TestFunction(i, d);//重新調用了新的類型模板,而不是函數偏特化
	TestFunction(p, d);

	system("pause");
	return 0;
}

/*
*(1)函數模板偏特化--
*函數模板不能偏特化

*(2)模板特化版本 放置位置建議
*模板定義、實現一般放在.h文件中,模板的特化版本和泛化版本應該在同一文件中。
*
*返還版本在前,特化版本在後
*
*(3)
*
*
*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章