C++的模版有时候很可能无法处理某些类型。 例如:
#include
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
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
修改后的例子:
#include
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
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++还可以显式实例化。 语法为,声明所需的种类–用<>符号指示类型,并在声明之前加上关键字template: templata void mSwap
- 本文作者: royalchen
- 本文链接: http://www.royalchen.com/2016/02/24/c学习笔记36模版的显式具体化(templatespecialization)和显式实例/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!