我有一个 boost::function 对象的列表,并且正在尝试找到一个特定的对象,以便可以将其从列表中删除。实际上注册了一个函数(推送到一个向量上),我希望能够取消注册它(搜索向量并删除匹配的函数指针)。这是代码:
#include <string>
#include <vector>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
class DummyClass
{
public:
std::string Data;
};
typedef boost::shared_ptr<DummyClass> DummyClassPtrType;
class UpdaterClass
{
public:
void handle(DummyClassPtrType Dummy);
};
class ManagerClass
{
public:
typedef boost::function<void (DummyClassPtrType Dummy)> HandlerFunctionType;
typedef std::vector<HandlerFunctionType> HandlerFunctionListType;
//
HandlerFunctionListType HandlerFunctionList;
void registerHandler(HandlerFunctionType Handler)
{
HandlerFunctionList.push_back(Handler);
}
void unRegister(HandlerFunctionType Handler)
{
// find the function pointer in the list and delete it from the list if found
HandlerFunctionListType::iterator HandlerIter = HandlerFunctionList.begin();
while (HandlerIter != HandlerFunctionList.end())
{
if (*HandlerIter == Handler) // error C2666: 'boost::operator ==' : 4 overloads have similar conversions
{
HandlerIter = HandlerFunctionList.erase(HandlerIter);
break;
}
else
{
++HandlerIter;
}
}
}
};
int main()
{
ManagerClass Manager;
UpdaterClass Updater;
Manager.registerHandler(boost::bind(&UpdaterClass::handle, &Updater, _1));
Manager.unRegister(boost::bind(&UpdaterClass::handle, &Updater, _1));
return 0;
}
编译器(VS2008 SP1)不喜欢这行:
if (*HandlerIter == Handler)
我不知道如何实现这一目标。