• 全部
...

模板类中有一个成员函数对string类型有特殊处理,如果不用特化是否还有其他办法?

fibbery 2008-09-28 02:55:34
模板类中有一个成员函数对string类型有特殊处理,如果不用特化是否还有其他办法?
template < typename T1,typename T2 >
class CMyClass;

当T2为std::string时,在CMyClass::func();中需要做一个特殊处理,请问,如果不用特化CMyClass类的办法,还有其他什么办法吗?
...全文
给本帖投票
574 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
fibbery 2008-10-06
  • 打赏
  • 举报
回复
最后,我采用了在模板类中增加一个模板函数和一个重载该模板函数的特化函数,这两个函数主要处理func中对于string与其它类型的不同部分!

即:
template < typename T1,typename T2 > 
class CMyClass
{
template<typename _Ty>
void diff(_Ty & t);
void diff(std::string & t);
public:
void func();//调用diff,当T2为string时调用的函数为void diff(std::string & t);
};
yshuise 2008-09-30
  • 打赏
  • 举报
回复
我实现了一个约束,dev测试通过:
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

template<typename T3, typename T4>
struct must_be_same_type
{
enum { ret = 0 };
};
template<>
struct must_be_same_type<string, string>
{
enum { ret = 1 };
};
template < typename T1,typename T2 >
class CMyClass{
public:

void myfun(){
if(must_be_same_type<T2, string>::ret){
cout<<"很黄,很暴力"<<endl;
}else{
cout<<"很傻,很天真"<<endl;
}
}

};


int main(int argc, char *argv[])
{
CMyClass<int, int> a;
a.myfun();
CMyClass<int, string> b;
b.myfun();
system("PAUSE");
return 0;
}
waruqi 2008-09-29
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 weiyijiji 的回复:]
14L的方法不错,可以根据函数优先级来选择函数重载.不过LZ要求的是成员函数,大概是没看到,改成成员函数的重载版本就可以了.
12L的也可以,不过我来觉得总感觉用运行时来判断不大舒服..
[/Quote]


“运行时” 在哪?
weiyijiji 2008-09-29
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 waruqi 的回复:]
引用 16 楼 weiyijiji 的回复:
14L的方法不错,可以根据函数优先级来选择函数重载.不过LZ要求的是成员函数,大概是没看到,改成成员函数的重载版本就可以了.
12L的也可以,不过我来觉得总感觉用运行时来判断不大舒服..



“运行时” 在哪?
[/Quote]
typeid()
richbirdandy 2008-09-29
  • 打赏
  • 举报
回复
用成员函数调用非成员函数的确是一种方法
yshuise 2008-09-29
  • 打赏
  • 举报
回复
对可以用template设计模式完成这个任务:

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

template<typename T>
void fun(){}
template<>void fun<string>(){
cout<<"hello,wrod"<<endl;
}

template < typename T1,typename T2 >
class CMyClass{
public:
void myfun(){
//.....template模式
fun<T2>();
//.....template模式
};


int main(int argc, char *argv[])
{
CMyClass<int, string> aa;
aa.myfun();
system("PAUSE");
return 0;
}
matrixdwy 2008-09-28
  • 打赏
  • 举报
回复
直接重载这个函数就可以了,编译器似乎会自动匹配一个非模板的函数,避免使用模板
#include<iostream>
#include<string>
using namespace std;

template<class T>
void f(T) {
cout << "in template version" << endl;
}

void f(string) {
cout << "in string version" << endl;
}

int main() {
string test;
f(test);
f<string>(test);
cin.get();
}
fibbery 2008-09-28
  • 打赏
  • 举报
回复
三人行必有我师焉!众人的智慧就是不同凡响!
weiyijiji 2008-09-28
  • 打赏
  • 举报
回复
14L的方法不错,可以根据函数优先级来选择函数重载.不过LZ要求的是成员函数,大概是没看到,改成成员函数的重载版本就可以了.
12L的也可以,不过我来觉得总感觉用运行时来判断不大舒服..
scklotz 2008-09-28
  • 打赏
  • 举报
回复
通过引入第三个模板函数来完成对类型的特殊处理
scklotz 2008-09-28
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;

template <typename T>
void foo( const T& v )
{
cout << "logic2 simple: " << v << endl;
}

void foo( const string& v )
{
cout << "logic2 string: " << v << endl;
}

template <typename T1, typename T2>
class bar
{
public:
bar( const T1& v1, const T2& v2 ) : _t1(v1), _t2(v2)
{
func();
}

void func()
{
cout << "logic1: " << _t1 << endl;
foo<T2>( _t2 );
}

T1 _t1;
T2 _t2;
};


void main()
{
bar<int, float> c1(1, 10.0);
bar<float, float> c2(2.0, 20.0);
bar<float, string> c3(3.0, string("hello, world"));
}
帅得不敢出门 2008-09-28
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 yshuise 的回复:]
C/C++ code#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>

using namespace std;
template < typename T1,typename T2 >
class CMyClass{
public:
void myfun(){
if( typeid(std::string) == typeid(T2))
{
cout<<"很黄,很暴力"<<endl;
}else{
cout<<"很傻,很天真"<<endl;
}

}
};



int _tmain(int …
[/Quote]
貌似有点意思
yshuise 2008-09-28
  • 打赏
  • 举报
回复
#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>

using namespace std;
template < typename T1,typename T2 >
class CMyClass{
public:
void myfun(){
if( typeid(std::string) == typeid(T2))
{
cout<<"很黄,很暴力"<<endl;
}else{
cout<<"很傻,很天真"<<endl;
}

}
};



int _tmain(int argc, _TCHAR* argv[])
{
CMyClass<int, string> a;
a.myfun();
CMyClass<int, int> b;
b.myfun();

return 0;
}

以上在vc2003测试正确
richbirdandy 2008-09-28
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 richbirdandy 的回复:]
把那个成员函数也改成模板函数
然后特化撒

这样也不用改其他的成员函数


C/C++ codetemplate <typename T1,typename T2>
class CMyClass
{
template <typename T>
void fun(){}

template <>
void fun<string>(){}
};
[/Quote]
理解错了 当我没说
richbirdandy 2008-09-28
  • 打赏
  • 举报
回复
把那个成员函数也改成模板函数
然后特化撒

这样也不用改其他的成员函数

template <typename T1,typename T2> 
class CMyClass
{
template <typename T>
void fun(){}

template <>
void fun<string>(){}
};
fibbery 2008-09-28
  • 打赏
  • 举报
回复
首先感谢各位!

我不想特化的原因是其他众多的成员函数均可以共用,如果特化这个类,所有的函数都要复制一份,如果将来修改,我觉得有些麻烦,容易出现版本不一致情况,不容易维护!

也不想继承,最好是类的名字一致,这样,对于客户代码没有什么感觉,就像在用同一个模板类!

继续探索探索,呵呵……
星羽 2008-09-28
  • 打赏
  • 举报
回复

函数好象不支持偏特话,所以你还是特化吧
template<typename t1, typename t2>
struct test {
};

template<typename t1>
struct test<t1, string> {

};
jieao111 2008-09-28
  • 打赏
  • 举报
回复
先说说你为什么不用特化吧
星羽 2008-09-28
  • 打赏
  • 举报
回复
就用特化吧
lyghe 2008-09-28
  • 打赏
  • 举报
回复

//无敌子类

template < typename T1,typename T2 >
class CMyClass
{virtual void func();}

template< typename T >
class CMyClass_string : public CMyClass<T, std::string>
{virtual void func();}
加载更多回复(3)

65,179

社区成员

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

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

手机看
关注公众号

关注公众号

客服 返回
顶部