关于一个template 得到类型的问题

xxstreamy 2008-10-23 11:20:55
我要实现把一些template类指针放在一个容器里面, 然后异步的进行处理。比如:
Entry<int> *a = new Entry<int>
queue.push(a);
Entry<double> *b = new Entry<double>
queue.push(b);

....
....
现在想把它们从容器里面取出来,因为我并不知道顺序。(是先放的int,还是double)。

如何知道类型呢? 比如: c = queue.pop(),这个c如何定义呢? 我想用rtti,但好想不行呀, 如何解决这个问题。

...全文
139 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
bxhzct 2008-10-23
  • 打赏
  • 举报
回复
学习,呵呵
bad_alloc 2008-10-23
  • 打赏
  • 举报
回复
没用过g_async_queue,不知道用一个偏特化行不行

template<typename T>
class get_elem_type {
typedef T elem_type;
};

template<typename T>
class get_elem_type<Entry<T>*> {
typedef T elem_type;
};

functon test()
{
Entry <int> *a = new Entry <int>
queue.push(a);
Entry <double> *b = new Entry <double>
queue.push(b);

c = queue.pop();

Entry<get_elem_type<c> > //这个应该就是你想要的东西了
}
xxstreamy 2008-10-23
  • 打赏
  • 举报
回复
UP
yshuise 2008-10-23
  • 打赏
  • 举报
回复
template <typename T>
struct Entry{
template <typename T>
int fun;
template <>
int fun<int>{ return 0;}; //0 为 int
template <>
int fun<double>{return 1;}//1 为 double

};
xxstreamy 2008-10-23
  • 打赏
  • 举报
回复
那还可以用g_async_queue吗?因为只存放base class的指针。
xxstreamy 2008-10-23
  • 打赏
  • 举报
回复
能给个例子吗?
taodm 2008-10-23
  • 打赏
  • 举报
回复
存基类指针,然后dynamic_cast。
xxstreamy 2008-10-23
  • 打赏
  • 举报
回复
可以,如何实现呢?
xxstreamy 2008-10-23
  • 打赏
  • 举报
回复
那问题可以换下,我就是想让一个没有type的容器(或者一个固定type的容器)里面存放不同type的template class指针,然后取出指针的时候,得到它们的type。如何实现
taodm 2008-10-23
  • 打赏
  • 举报
回复
你可以不用g_async_queue么。
xxstreamy 2008-10-23
  • 打赏
  • 举报
回复
您是用,rtti来实现,我考虑过,但是那个c 如何定义,才能得到这个gettype呢? 容器是类型无关的,只存放指针,但我知道,g_async_queue_pop的时候是需要cast的。
xxstreamy 2008-10-23
  • 打赏
  • 举报
回复
类型识别能给个连接吗?
yshuise 2008-10-23
  • 打赏
  • 举报
回复
template <typename T>
struct Entry{

T gettype(){return typeid(T).name;}

};
xxstreamy 2008-10-23
  • 打赏
  • 举报
回复
补充下,我用的是glib 的 g_async_queue 来存储这个指针。这个肯定是可以的,不需要type.
xxstreamy 2008-10-23
  • 打赏
  • 举报
回复
前面的是pseudo code. 其实很简单,就是把类指针放在queue里面呀。但取出来的时候如何得到type呢?
yshuise 2008-10-23
  • 打赏
  • 举报
回复
楼主你可以在entry中封装一个类型识别即可。
taodm 2008-10-23
  • 打赏
  • 举报
回复
你前面这部分代码编译通过了么?
yshuise 2008-10-23
  • 打赏
  • 举报
回复
queue
===========这是int 类型还是 double类型,这样不统一。
moolleychean 2008-10-23
  • 打赏
  • 举报
回复
17楼在用运行时实例化模板么?高手
xxstreamy 2008-10-23
  • 打赏
  • 举报
回复
I solved this problem, taodm gave me a hint. I post my code as following:

class Base
{
public:
virtual const char* work()=0;
};

template<typename T>
class A : public Base
{
public:
const char* work()
{
return typeid(T).name();
}
A():a(100){}
int a;
};

template<typename T>
class B : public Base
{
public:
const char* work()
{
return typeid(T).name();
}
B():a(99){}
int a;
};

int main()
{
g_thread_init(NULL);
GAsyncQueue * test_q;
test_q = g_async_queue_new();

Base *t2 = new B<Entry<int64_t> >;
cout<<t2->work()<<endl;;
g_async_queue_push(test_q,t2);

Base *t1 = new A<Entry<int64_t> >;
cout<<t1->work()<<endl;;
g_async_queue_push(test_q,t1);

Base *t3 = (Base*)g_async_queue_pop(test_q);
cout<<t3->work()<<endl;
}


So just store the base class pointer without type. use virtual function to get type. Anyway, Thanks you guys.

65,211

社区成员

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

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