创建具有的std :: tr1 ::功能的多播事件(或升压::功能) [英] Creating multicast events with std::tr1::function (or boost::function)

查看:177
本文介绍了创建具有的std :: tr1 ::功能的多播事件(或升压::功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建C#般的组播代表和利用TR1功能的事件。或升压,升压以来::功能(大部分)相同的std :: TR1 ::功能。作为概念证明我尝试这样做:

I'm attempting to create C#-like multicast delegates and events using features from TR1. Or Boost, since boost::function is (mostly) the same as std::tr1::function. As a proof of concept I tried this:

template<typename T1>
class Event
{
private:
 typedef std::tr1::function<void (T1)> action;
 std::list<action> callbacks;

public:

 inline void operator += (action func)
 {
  callbacks.push_back(func);
 }

 inline void operator -= (action func)
 {
  callbacks.remove(func);
 }

 void operator ()(T1 arg1)
 {
  for(std::list<action>::iterator iter = callbacks.begin();
   iter != callbacks.end(); iter++)
  {
   (*iter)(arg1);
  }
 }
};

其中一期工程之类的。行 callbacks.remove(FUNC)没有。当我编译它,我收到以下错误:

Which works, sort of. The line callbacks.remove(func) does not. When I compile it, I get the following error:

error C2451: conditional expression of type 'void' is illegal

这是由列表头,这是在删除函数的1194行造成的。是什么原因造成的?

Which is caused by line 1194 of the list header, which is in the remove function. What is causing this?

推荐答案

我觉得这是完全一样的问题:<一href=\"http://stackoverflow.com/questions/89488/comparing-stdtr1function-objects\">comparing-stdtr1function-objects

I think this is exactly same problem: comparing-stdtr1function-objects

(基本上你不能比较函子,这就是为什么擦除或使用任何运营商==都不行)

(basically you can't compare functors, that's why erase or anything using operator== won't work)

这篇关于创建具有的std :: tr1 ::功能的多播事件(或升压::功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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