2012-03-29 59 views
3

我创建了一堆的功能,这都有效地做同样的事情:存储的boost ::绑定功能于一身的std ::地图

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 ::绑定和boost ::功能,这样做:

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>'

究竟应该映射的第二个模板参数是什么?

+0

你试图包裹设置参数(绑定)中的boost :: ref或无论它叫什么?引用是不可分配的。 – 2012-03-29 01:20:15

+0

@ Cheersandhth.-Alf试图用boost :: cref(currentSet)包装它,但是它给出了完全相同的错误。如果我通过非const,相同,如果我通过非const值... – Yuushi 2012-03-29 01:36:45

+1

哦,当。那么请尝试在最小程序中重现该问题,并发布(完整代码)。 – 2012-03-29 01:38:14

回答

0

啊我解决了它。我包含错误的头文件;而不是:

#include <boost/function.hpp>

我包括升压/ function文件夹之类的东西:

#include <boost/function/function_fwd.hpp>

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。