C++ Template

Table of Contents

說明:

一. 函數模版

二. 函數模版重載

三.  類模版

四.類模版特化

五.類模版局部特化

六. 缺省模版實參



 

 

說明:

    模版的編譯分兩次:
    1. 實例化之前,先檢查模版本身代碼,查看語法是否正確,如遺漏分號等;
    2. 在實例化期間,檢查模版代碼,查看是否所有的調用都有效,如實例化類型不支持某些函數調用等;

 

一. 函數模版

#include <string>
#include <iostream>

//1. 函數模版聲明、定義
template <typename T>
inline T const & MaxFuncTemplate(T const &T1, T const &T2)
{
	return (T1 > T2) ? T1 : T2;
}

//2. 函數模版實例化
void TestTemplate()
{
	//整形比較
	int iFuncT_A = 100;
	int iFuncT_B = 200;
	std::cout << "1. Function template Max value= " << MaxFuncTemplate(iFuncT_A, iFuncT_B) << std::endl;

	//浮點比較
	double dFuncT_A = 100.109;
	double dFuncT_B = 200.208;
	std::cout << "2. Function template Max value= " << MaxFuncTemplate(dFuncT_A, dFuncT_B) << std::endl;

	//整形與浮點(涉及到強制類型轉換static_cast,否則編譯報錯)
	std::cout << "3. Function template Max value= " << MaxFuncTemplate(iFuncT_A, static_cast<int>(dFuncT_B)) << std::endl;

	//字符串比較
	string strA = "hello";
	string strB = "world!";
	std::cout << "4. Function template Max value= " << MaxFuncTemplate(strA, strB) << std::endl;
}


運行結果:

1. Function template Max value= 200
2. Function template Max value= 200.208
3. Function template Max value= 200
4. Function template Max value= world!

結論:

1. 注意強制類型轉換static_cast<類型>(值)的格式使用;
2. 關於模版函數的聲明、定義格式;
3. 關於const的使用格式,爲便於記憶,採樣"類型 const 變量"格式定義,如 int const i,表示整形常量i,int* const i,表示整形指針常量i;

二. 函數模版重載

#include <string>
#include <iostream>

//1. 求兩個值的最大者
template <typename T>
inline T const & MaxFuncTemplate(T const &T1, T const &T2)
{
	return (T1 > T2) ? T1 : T2;
}

//2. 求三個值的最大者
template <typename T>
inline T const & MaxFuncTemplate(T const &T1, T const &T2, T const &T3)
{
	return ::MaxFuncTemplate(::MaxFuncTemplate(T1, T2), T3); //遞歸嵌套
}

三.  類模版

//類模版格式
template <typename T>
class CStack
{
public:
	CStack() {};
	virtual ~CStack() {};

private:
	T elms;
};

 

四.類模版特化

       類模版特化,與函數重載一樣,只是這裏是類模版重載,必須在起始處聲明一個template <>,然後聲明用來特化類模版的類型,這個類型被用做模板實參,且必須在類名的後面指定。類模版特化是爲了滿足一些比較固定的格式

#include "stdafx.h"
#include <string>
#include <iostream>

//類模版
template <typename T>
class CStack
{
public:
	CStack() {
		std::cout << "template base" << std::endl;
	};
	virtual ~CStack() {};

private:
	T elms;
};

//類模版特化
template<>
class CStack<int>
{
public:
	CStack() {
		std::cout << "Quan te hua" << std::endl;
	};
	virtual ~CStack() {};

private:
	int elms;
};


int main()
{
	//類模版
	CStack<std::string> stackStr;

	//類模版特化(int)
	CStack<int> stackInt;

    return 0;
}

五.類模版局部特化

#include "stdafx.h"
#include <string>
#include <iostream>

//類模版
template <typename T1, typename T2>
class MyClass
{
	//....
};

//類模版局部特化(兩個模版參數具有相同的類型)
template <typename T>
class MyClass<T, T>
{
	//...
};

//類模版局部特化(第2個參數爲整形)
template <typename T>
class MyClass<T, int>
{
	//...
};

//類模版局部特化(兩個模版參數都是指針類型)
template <typename T1, typename T2>
class MyClass<T1*, T2*>
{
	//...
};

六. 缺省模版實參

    顧名思義,缺省模版實參的意思就是有默認參數。

#include <vector>
#include <deque>

//缺省模版實參(以下缺省爲實參爲:CONT=std::vector<T>)
template<typename T, typename CONT=std::vector<T>>
class MyClass1
{
	//...
};

int main()
{
	//使用缺省參數std::vector
	MyClass1<int> myClass1;

	//使用std::deque
	MyClass1<int, std::deque<int>> MyClass2;
	return 0;
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章