【发布时间】:2013-05-23 15:24:29
【问题描述】:
我有一个 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)
我不知道如何实现这一点。
【问题讨论】:
-
为什么我不能将 boost::function 对象与 operator== 或 operator!= 进行比较? boost.org/doc/libs/1_50_0/doc/html/function/faq.html#id1565973
标签: c++ boost boost-function