开发者

Passing template into boost function

开发者 https://www.devze.com 2023-01-03 05:44 出处:网络
template <class EventType> class IEvent; class IEventable; typedef boost::function<void (IEventable&, IEvent&)> behaviorRef;
template <class EventType>
class IEvent;

class IEventable;

typedef boost::function<void (IEventable&, IEvent&)> behaviorRef;

What is the right way for passing template class IEvent into boost function? With this code I get: error: 开发者_开发问答functional cast expression list treated as compound expression error: template argument 1 is invalid error: invalid type in declaration before ‘;’ token


boost::function needs a type, so you cannot pass it a template's name, it has to be a template instance. So either use a specific instance

typedef boost::function<void (IEventable&, IEvent<SomeEventType>&)> behaviorRef;

or put the whole thing itself into a template:

template< typename EventType >
struct foo {
  typedef boost::function<void (IEventable&, IEvent<EventType >&)> behaviorRef;
};


A class template is just a template for a class, it's not a real class yet. You need to specify the template parameters to get a class out of it, for example IEvent<int>. So you need to decide for which kind of events you want that typedef to be, for example for int:

typedef boost::function<void (IEventable&, IEvent<int>&)> behaviorRef;

Otherwise, if you want a typedef for all possible template instantiations, you need to put the typedef itself into another templated class. See sbi's answer for an example of that.

0

精彩评论

暂无评论...
验证码 换一张
取 消