C++学习笔记36 (模板的细节明确template specialization)和显式实例(template instantiation)

C++有时模板很可能无法处理某些类型的。

例如:

#include <iostream>
using namespace std;
class man{
private:
	string name;
	int data;
public:
	man(string s,int i):name(s),data(i){
	}
	void show()const{
		cout<<"this name is "<<name<<" ,data="<<data<<endl;
	}
};
template <class T>
void mSwap(T t1,T t2){
	T temp=t1;
	t1=t2;
	t2=temp;
	cout<<"Here are template version "<<t1<<" and "<<t2<<" swap successed!"<<endl;

};
int main()
{
	mSwap(88,66);
	man m1("guang",99);
	man m2("jing",10);
	mSwap(m1,m2);		
}



编译结果:


能够看到会出现非常多错误。(尽管这不是重点。

如果我希望mSwap仅仅是交换两个man的data,而name还保持与原来一样。

该怎么做呢?

这个时候,就须要我们为特定的类型提供详细化的模版定义了。

详细化包含显式详细化以及隐式实例化和显式实例化。


1.显式详细化的原型和定义应以template<>开头。并通过名称来指出类型

template<>
void mSwap(man &m1,man &m2)
或者

template<>
void mSwap<man>(man &m1,man &m2)

改动后的样例:

#include <iostream>
using namespace std;
class man{
private:
	string name;
	int data;
public:
	man(string s,int i):name(s),data(i){
	}
	void show()const{
		cout<<"this name is "<<name<<" ,data="<<data<<endl;
	}
	void setData(int d){
		data=d;	
	}
	int getData()const{
		return data;
	}
};
template <class T>
void mSwap(T &t1,T &t2){
	T temp=t1;
	t1=t2;
	t2=temp;
	cout<<"Here are template version "<<t1<<" and "<<t2<<" swap successed!"<<endl;

};
template<>
void mSwap(man &m1,man &m2){
	int temp=m1.getData();
	m1.setData(m2.getData());
	m2.setData(temp);
	cout<<"Here are the man version,successed!"<<endl;
}
int main()
{
	int i=88,j=66;
	mSwap(i,j);
	man m1("guang",99);
	man m2("jing",10);
	mSwap(m1,m2);
	m1.show();
	m2.show();		
}



执行截图:


这就是模版显式详细化的作用了。

须要注意的是,详细化优先于常规模版,而非模版函数优先于详细化和常规模版。


2.实例化

要进一步了解模版,必须理解术语实例化和详细化。

记住,在代码中包括函数模版本号身并不会生成函数定义,它仅仅是一个用于生成函数定义的方案。编译器使用模版为特定类型生成函数定义时。得到的是模版实例。比如,上面的mSwap(i,j),函数调用mSwap(i,j)导致编译器生成mSwap()的一个实例,该实例使用int类型。

模版并不是是函数定义,但使用int的模版实例是函数定义。这样的实例化方式被称为隐式实例化

C++还能够显式实例化。

语法为,声明所需的种类--用<>符号指示类型,并在声明之前加上keywordtemplate:

templata void mSwap<man>(man &,man &)

该声明的意思是“使用mSwap()模版生成man类型的函数定义”。




版权声明:本文博客原创文章。博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/bhlsheji/p/4658531.html