1

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()?

Kurt Koller
  • 334
  • 1
  • 3
  • 14

1 Answers1

1

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.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • Thank you for the insightful response. It was my understanding that the first parameter, _1, would take the 'this' argument which is implicit in a call to a member function. That would avoid my having to pass an explicit call to the constructor for that class as one of the parameters, as my code is not structured in a way which would allow me to do that – Kurt Koller Jul 18 '12 at 21:49
  • @user1535568: Maybe if the type `MyFilterFunctor` also took a `MyClass&` argument? – aschepler Jul 18 '12 at 21:53
  • @user1535568, where should the `this` object come from, if you don't want to call the constructor to create such an object? Perhaps your method should be static, so that it doesn't require a `this` pointer? If there is nothing from the object you need to pass along, then `static` should work fine. – MvG Jul 18 '12 at 22:11
  • @MvG Thanks, that actually just put what I was trying to do in perspective, and you are right. The member function can be static, and I do not need a pointer to this. – Kurt Koller Jul 18 '12 at 22:17