模板专业化 [英] template specialization

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

问题描述

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

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

子类类型?


示例:


模板< 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

推荐答案

嘿Eric,我一定在这里遗漏了一些东西,希望我自己也不会傻瓜......
Hey Eric, I must be missing something here, hope I don''t make a total
fool of myself ...
template< class E>
void DeliverEvent(E * Event);
模板<>
void DeliverEvent(CircuitEvent * Event);
template <class E>
void DeliverEvent(E *Event);
template <>
void DeliverEvent(CircuitEvent *Event);




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


2003年11月20日15:32:44 -0800, pu ***** @ hotmail.com (Eric)写道:
On 20 Nov 2003 15:32:44 -0800, pu*****@hotmail.com (Eric) wrote:
有谁知道是否你可以专门为
参数指定模板函数,这些参数是指向特定基类的指针而不会丢失子类类型吗?

示例:

模板< class E>
void DeliverEvent(E * Event);

是一个接收Event对象的函数,然后最终将它传递到正确的目的地。为了避免向下转换,任何
目的地都将有一个接收事件。为其感兴趣的每个特定事件类重载的函数。

有一类特定的事件,称之为CallEvents,这需要特殊处理才能进行特殊处理传递它们。所以我想对这个功能进行专门化:

模板<>
void DeliverEvent(CircuitEvent * Event);

但是这样做就这样,Event对象变成了一个基类
指针,我失去了它的特定类型(我想避免向下转换以重新获得它)。

有没有一种专门化模板功能的方法,以便我的模板
参数保持类型?


是的,但它有点复杂(首次下载提升来自
www.boost.org)


基本上你不是专门模板,而是派遣到

基于传递类型的属性的特定实现。在

这种情况​​下,你寻找SpecialEvent和派生自它的类。


#include< iostream>

#include < boost / type_traits.hpp>

使用名称空间提升;


struct Event

{

};


struct SpecialEvent

{

virtual~FitialEvent(){}


void non_virtual()

{

std :: cout<< SpecialEvent :: non_virtual\\\
;

}

};


struct DerivedSpecialEvent:SpecialEvent

{

void non_virtual()

{

std :: cout<< " DerivedSpecialEvent :: non_virtual\\\
";

}

};


模板< bool>

struct DeliverEventImpl

{

template< class Event>

static void do_it(Event * event)

{

std :: cout<< 非专业的\ n;

}

};


模板<>

struct DeliverEventImpl< true>

{

// SpecialEvent版

模板< class Event>

static void do_it(Event * event)

{

event-> non_virtual();

}

};

模板< class E>

void DeliverEvent(E * event)

{

DeliverEventImpl<

is_base_and_derived< SpecialEvent,E> :: value

|| is_same< SpecialEvent,E> :: value :: do_it(event);
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?
Yes, but it''s a bit complicated (first download boost from
www.boost.org).

Basically you don''t specialize the template, but instead dispatch to a
particular implementation based on properties of the passed type. In
this case, your looking for SpecialEvent and classes derived from it.

#include <iostream>
#include <boost/type_traits.hpp>
using namespace boost;

struct Event
{
};

struct SpecialEvent
{
virtual ~SpecialEvent() {}

void non_virtual()
{
std::cout << "SpecialEvent::non_virtual\n";
}
};

struct DerivedSpecialEvent: SpecialEvent
{
void non_virtual()
{
std::cout << "DerivedSpecialEvent::non_virtual\n";
}
};

template <bool>
struct DeliverEventImpl
{
template <class Event>
static void do_it(Event* event)
{
std::cout << "Non specialized\n";
}
};

template<>
struct DeliverEventImpl<true>
{
//SpecialEvent version
template <class Event>
static void do_it(Event* event)
{
event->non_virtual();
}
};
template <class E>
void DeliverEvent(E* event)
{
DeliverEventImpl<
is_base_and_derived<SpecialEvent, E>::value
|| is_same<SpecialEvent, E>::value::do_it(event);



}


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


je ******** @ yahoo.com (杰夫)在留言新闻中写道:< 7b ************************** @ posting.google。 com> ...
je********@yahoo.com (Jeff) wrote in message news:<7b**************************@posting.google. com>...
嘿Eric,我一定在这里遗漏了一些东西,希望我不要自欺欺人......
Hey Eric, I must be missing something here, hope I don''t make a total
fool of myself ...
template< class E>
void DeliverEvent(E * Event);
template<>
void DeliverEvent(CircuitEvent * Event);
Don''你需要在专业化中指定类型(希望我已经做对了......)?
模板<>
void DeliverEvent< CircuitEvent>(CircuitEvent * Event );
template <class E>
void DeliverEvent(E *Event);
template <>
void DeliverEvent(CircuitEvent *Event);
Don''t you need to specify the type in the specialization (hope I''ve
got this right...) ?
template <>
void DeliverEvent<CircuitEvent>(CircuitEvent* Event);




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

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

其次,如果你正在处理类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



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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆