我试图为本文中讨论的不可思议的快速c ++代表设置一个事件管理器http://blog.coldflake.com/posts/C++-delegates-on-steroids/;
template <typename T, typename R, typename... Params>
class delegate<R (T::*)(Params...) const>
{
public:
typedef R (T::*func_type)(Params...) const;
delegate(func_type func, const T& callee)
: callee_(callee) , func_(func)
{}
R operator()(Params... args) const
{ return (callee_.*func_)(args...); }
bool operator==(const delegate& other) const
{ return (&callee_ == &other.callee_) && (func_ == other.func_); }
bool operator!= (const delegate& other) const
{ return !(*this == other); }
private:
const T& callee_;
func_type func_;
};
和我使用的其中一种方法;
using EventListenerDelegate = delegate<IEventDataPtr>;
using EventListenerList = std::list<EventListenerDelegate>;
bool EventManager::addListener(const EventListenerDelegate& eventDelegate, const EventType& type)
{
// this will find or create the entry
EventListenerList& eventListenerList = m_eventListeners[type];
for (auto it = eventListenerList.begin(); it != eventListenerList.end(); ++it)
{
if (eventDelegate == (*it))
{
std::cout <<"Attempting to double-register a delegate" << std::endl;
return false;
}
}
eventListenerList.push_back(eventDelegate);
return true;
}
代码无法使用gcc错误消息进行编译;
error: no match for 'operator==' (operand types are 'const EventListenerDelegate {aka const delegate<std::shared_ptr<IEventData> >}' and 'delegate<std::shared_ptr<IEventData> >')
if (eventDelegate == (*it))
^
我该怎么做才能解决错误? 谢谢。
编辑: 代码中的另外两个专业化模板;
//specialization for free functions
template <typename R, typename... Params>
class delegate<R (*)(Params...)>
和
//specialization for member functions
template <typename T, typename R, typename... Params>
class delegate<R (T::*)(Params...)>