模板专业化

扫码查看
本文介绍了模板专业化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道你是否可以专门为

参数指定模板函数,这些参数是指向特定基类的指针而不会丢失

子类类型?


示例:


模板< class E>

void DeliverEvent(E * Event);


是一个接收Event对象的函数,然后最终

将它传递到正确的目的地。为了避免向下转换,任何

目的地将有一个ReceiveEvent为它感兴趣的每个特定事件类重载的函数。


有一类特定的事件,称之为CallEvents,其中

需要特殊处理才能传递它们。所以我想

专门化这个功能:


模板<>

void DeliverEvent(CircuitEvent * Event) ;


但是这样做,Event对象变成了基类

指针,我失去了它的特定类型(我想避免

downcasting重新获得它。)


有没有办法专门化模板功能,以便我的模板

参数保持它类型?


谢谢,

Eric Simon

Does anyone know if you can specialize a template function for
arguments that are pointers to a particular base class and not lose
the subclass type?

example:

template <class E>
void DeliverEvent(E *Event);

is a function that will receive an Event object, and then eventually
pass it to the proper destination. To avoid downcasting, any
destination will have a "ReceiveEvent" function that is overloaded for
each specific Event class it is interested in.

There is a particular class of Events, call them "CallEvents", which
require special processing before passing them along. So I want to
specialize the function as such:

template <>
void DeliverEvent(CircuitEvent *Event);

But in doing it this way, the Event object becomes a base class
pointer, and I lose its specific type (and I would like to avoid
downcasting to regain it).

Is there a way to specialize the template function so that my template
argument keeps it type?

Thanks,
Eric Simon

推荐答案




Don''你需要在专业化中指定类型(希望我已经

这样做了......)?

templ ate<>

void DeliverEvent< CircuitEvent>(CircuitEvent * Event);

------------


其次,如果你正在处理类heirarchy,为什么你需要

在这里使用模板?难道你不能超负荷这个功能吗?

(原谅这个愚蠢的问题)

void DeliverEvent(Event * e);

void DeliverEvent (CircuitEvent * e);


Jeff



Don''t you need to specify the type in the specialization (hope I''ve
got this right...) ?
template <>
void DeliverEvent<CircuitEvent>(CircuitEvent* Event);
------------

Second, if you''re dealing with a class heirarchy, why do you need to
use templates here at all? Can''t you just overload the function?
(pardon the silly question)
void DeliverEvent(Event* e);
void DeliverEvent(CircuitEvent* e);

Jeff




}


int main()

{

活动e;

SpecialEvent se;

DerivedSpecialEvent dse;

DeliverEvent(& e) );

DeliverEvent(& se);

DeliverEvent(& dse);

}


Tom


}

int main()
{
Event e;
SpecialEvent se;
DerivedSpecialEvent dse;
DeliverEvent(&e);
DeliverEvent(&se);
DeliverEvent(&dse);
}

Tom





是的,你是对的。我的错误。

------------

其次,如果你正在处理类heirarchy,为什么你需要
在这里使用模板?你不能只重载这个功能吗?
(原谅这个愚蠢的问题)
void DeliverEvent(Event * e);
void DeliverEvent(CircuitEvent * e);


问题是事件和电路事件是基类。

是继承自Event的几个具体类。电路事件

也继承自Event,而且还有几个具体的类继承自它的
。我使用模板是因为我不想要向后转换
来重新获得子类类型。我猜这是一个非常弱的

层次结构 - 基类的功能非常少。大多数

有趣的东西都是特定于每个子类的。


谢谢,

Eric

杰夫



Yes, you are right. My mistake.
------------

Second, if you''re dealing with a class heirarchy, why do you need to
use templates here at all? Can''t you just overload the function?
(pardon the silly question)
void DeliverEvent(Event* e);
void DeliverEvent(CircuitEvent* e);
The problem is that Event and Circuit Event are base classes. There
are several concrete classes that inherit from Event. Circuit Event
also inherits from Event, and in turn, has several concrete classes
inheriting from it. I use templates because I don''t want to have to
downcast to regain the subclass type. I guess it''s a very weak
hierarchy - the base class has very little functionality. Most of the
interesting stuff is specific to each subclass.

Thanks,
Eric

Jeff



这篇关于模板专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 15:33
查看更多