C++中类模板做全类特化时,类成员函数是不是一定要放在类里面,不能放在类外面?

shawn_fengxiao 2015-07-14 07:21:43
C++中类模板做全类特化时,类成员函数是不是一定要放在类里面,不能放在类外面?

我的想法是这样的,我想将demo类进行一个全类特化。我把
1)如果我把注释的部分去掉,那么就会报类成员函数的特化出现错误。
2)如果我把成员函数特化写到类里面,就没问题了。
我的问题是,做全类特化的时候,成员函数的特化必须写在类里面么?
非常感谢~!

#include<iostream>
using namespace std;

template <typename T>
class demo{
protected:
T x;
public:
demo(T);
~demo();
void show();
};
template <typename T>
demo<T>::demo(T x):x(x){
cout << "demo<T>" << endl;
}
template <typename T>
demo<T>::~demo(){
}
template <typename T>
void demo<T>::show(){

cout << x << endl;
}

/*
template <>//对类进行全类特化,不过,我不想把成员函数写在类里面,于是我以部分特化的方式将成员函数写在外面。
class demo<int>{
protected:
int x;
public:
demo(int x);
~demo();
void show();
};
*/

template <>//将成员函数都特化在这里。
demo<int>::demo(int x):x(x){
cout << "demo<int>" << endl;
}
template <>
demo<int>::~demo(){
}
template <>
void demo<int>::show(){

cout << x+1 << endl;
}
int main(){

demo<double> d1(1.2);
demo<int> d2(2);

d1.show();
d2.show();

return 0;
}
...全文
230 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
碼上道 2015-07-15
  • 打赏
  • 举报
回复
引用 2 楼 totefeng 的回复:
[quote=引用 1 楼 jerry_dqh 的回复:] 可以是可以, template <>//这里的template<>都要去掉 demo<int>::demo(int x):x(x){ cout << "demo<int>" << endl; } template <>//这里的template<>都要去掉 demo<int>::~demo(){ } template <>//这里的template<>都要去掉 void demo<int>::show(){ cout << x+1 << endl; } 加上template <>,表示你要定义,你在前面已经定义过了,后面只要像普通的类一样写就行了。
非常感谢,去掉后就可以了。 我想确认一下,是不是我在全类特化开始的地方已经定义过template<>了?

template <>//是不是这里定义过后,下面就不用再重复定义了?
class demo<int>{
protected:
	int x;
public:
	demo(int x);
	~demo();
	void show();
};

//template <>
demo<int>::demo(int x):x(x){
	cout << "demo<int>" << endl;
}
//template <>
demo<int>::~demo(){
}
//template <>
void demo<int>::show(){

	cout << x+1 << endl;
}
[/quote] 是的
shawn_fengxiao 2015-07-14
  • 打赏
  • 举报
回复
引用 1 楼 jerry_dqh 的回复:
可以是可以, template <>//这里的template<>都要去掉 demo<int>::demo(int x):x(x){ cout << "demo<int>" << endl; } template <>//这里的template<>都要去掉 demo<int>::~demo(){ } template <>//这里的template<>都要去掉 void demo<int>::show(){ cout << x+1 << endl; } 加上template <>,表示你要定义,你在前面已经定义过了,后面只要像普通的类一样写就行了。
非常感谢,去掉后就可以了。 我想确认一下,是不是我在全类特化开始的地方已经定义过template<>了?

template <>//是不是这里定义过后,下面就不用再重复定义了?
class demo<int>{
protected:
	int x;
public:
	demo(int x);
	~demo();
	void show();
};

//template <>
demo<int>::demo(int x):x(x){
	cout << "demo<int>" << endl;
}
//template <>
demo<int>::~demo(){
}
//template <>
void demo<int>::show(){

	cout << x+1 << endl;
}
碼上道 2015-07-14
  • 打赏
  • 举报
回复
可以是可以, template <>//这里的template<>都要去掉 demo<int>::demo(int x):x(x){ cout << "demo<int>" << endl; } template <>//这里的template<>都要去掉 demo<int>::~demo(){ } template <>//这里的template<>都要去掉 void demo<int>::show(){ cout << x+1 << endl; } 加上template <>,表示你要定义,你在前面已经定义过了,后面只要像普通的类一样写就行了。

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧