3

我正在创建一堆函数,它们都有效地做同样的事情:

long Foo::check(long retValue, unsigned toCheck, const std::set<unsigned>& s)
{
    auto it = s.find(toCheck);
    return (it == s.end()) ? -retValue : retValue;
}

其中 Foo 是一个类。到目前为止,一切都相当简单。现在,我实际上想在此创建很多变体,但绑定到不同的集合。然后我想将这些存储在 std::map 中。因此,使用 boost::bind 和 boost::function,执行以下操作:

void Foo::addToMap(unsigned option, const std::set<unsigned>& currentSet)
{
    someMap[option] = boost::bind(&Foo::check, this, _1, _2, currentSet);
}

我遇到的问题是试图定义地图的类型。我以为会是:

std::map<unsigned, boost::function<long (long, unsigned)> > someMap;

但是用 MSVC 9.0 编译它会给出:error C2582: 'operator =' function is unavailable in 'boost::function<Signature>'.

map 的第二个模板参数到底应该是什么?

4

2 回答 2

0

使用 boost 1.49 和 g++ 4.4.4,我能够做一些非常相似的事情。这是一个代码片段。

typedef boost::function< void (SomeType) > CallbackType;

std::pair<std::string, CallbackType> NameCallbackPair;

然后,我可以使用以下内容对其进行分配:

NameCallbackPair somePair(someString, boost::bind(&SomeClass::someMethod, this, _1));

也许这与 MSVC9 有关。

于 2012-03-29T01:37:25.810 回答
0

啊我解决了。我包含了错误的头文件;代替:

#include <boost/function.hpp>

我包含了 boost/function 文件夹中的内容,例如:

#include <boost/function/function_fwd.hpp>

于 2012-03-29T01:58:52.837 回答