I am attempting to map the filter functor with one of the member methods of my class based on the value of an input string.
#include <iostream>
#include <map>
#include <boost/function.hpp>
#include <boost/cstdint.hpp>
#include <boost/assign.hpp>
#include <boost/bind.hpp>
typedef boost::function < bool(std::map<std::string, std::string>, std::string) > MyFilterFunctor;
class MyClass
{
public:
bool FilterFunction1(std::map<std::string, std::string> myMap, std::string filterValue)
{
//do something
return true;
}
};
int main() {
MyFilterFunctor myFilter = boost::bind(&MyClass::FilterFunction1, _1, _2, _3);
return 0;
}
And my error:
/usr/include/boost/bind/bind.hpp:375:
error: ambiguous overload for ‘operator[]’ in ‘a[boost::_bi::storage3<A1, A2, boost::arg<I> >::a3_ [with A1 = boost::arg<1>, A2 = boost::arg<2>, int I = 3]]’
Edit: I simplified my example slightly at the suggestion of a proposed answer to my question. It has been suggested that I need to pass MyClass() as an argument to boost::bind, and this does solve the compilation error in the code segment posted. However, it is not possible for me to do that given my code structure. I would like to know why what I have done is different from this example in the boost::bind documentation:
struct X
{
int f(int);
}
int main()
{
boost::bind(&X::f, 1); // error, X::f takes two arguments
boost::bind(&X::f, _1, 1); // OK
}
Shouldn't the _1 parameter take care of the implicit 'this' which is being suggested I supply explicitly with MyClass()?
This has nothing to do with boost::assign::map_list_of
or std::map
, the same error can be reproduced simply by this:
MyFilterFunctor mff;
auto bb = boost::bind(&MyClass::FilterFunction1, _1, _2, _3);
mff = bb;
bb
requires 3 parameters: a MyClass
, a map<string,string>
and a string
. mff
requires 2 parameters, a map<string,string>
and a string
. The two are clearly incompatible.
Try boost::bind(&MyClass::FilterFunction1, MyClass(), _1, _2))
instead.