将模板传递给boost函数

Max*_*rai 3 c++ templates boost function

template <class EventType>
class IEvent;

class IEventable;

typedef boost::function<void (IEventable&, IEvent&)> behaviorRef;
Run Code Online (Sandbox Code Playgroud)

将模板类IEvent传递给boost函数的正确方法是什么?使用此代码,我得到: error: functional cast expression list treated as compound expression error: template argument 1 is invalid error: invalid type in declaration before ‘;’ token

sbi*_*sbi 5

boost::function需要一个类型,所以你不能传递一个模板的名称,它必须是一个模板实例.所以要么使用特定的实例

typedef boost::function<void (IEventable&, IEvent<SomeEventType>&)> behaviorRef;
Run Code Online (Sandbox Code Playgroud)

或者将整个事物本身放入模板中:

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