【发布时间】:2010-02-09 04:12:11
【问题描述】:
我正在尝试使用 TR1 中的功能创建类似于 C# 的多播委托和事件。或 Boost,因为 boost::function (大部分)与 std::tr1::function 相同。作为概念证明,我尝试了这个:
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) 行没有。当我编译它时,我得到以下错误:
error C2451: conditional expression of type 'void' is illegal
这是由 list 标头的第 1194 行引起的,该标头位于 remove 函数中。这是什么原因造成的?
【问题讨论】:
标签: c++ stl boost delegates tr1