我試圖使用TR1的功能創建類似C#的多播委託和事件。或Boost,因爲boost :: function(大部分)與std :: tr1 :: function相同。作爲一個概念證明,我嘗試了這個:使用std :: tr1 :: function(或boost :: 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
頭,這是在remove
功能的1194線引起的。這是什麼造成的?
啊,謝謝。應該知道Boost已經提供了這個。 – 2010-02-09 06:07:11