我正在尝试使用 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
。这是什么原因造成的?