Thomas Swanson
Thomas Swanson

Reputation: 37

Boost function map to string

I am trying to map a string to a function. The function should get a const char* passed in. I am wondering why I keep getting the error that

*no match for call to ‘(boost::_bi::bind_t<boost::_bi::unspecified, void (*)(const char*), boost::_bi::list0>) (const char*)’*

My code is below

#include <map>
#include <string>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>



typedef boost::function<void(const char*)> fun_t;
typedef std::map<std::string, fun_t> funs_t;



void $A(const char *msg)
{
    std::cout<<"hello $A";
}

int main(int argc, char **argv)
{
   std::string p = "hello";
   funs_t f;
   f["$A"] = boost::bind($A);
   f["$A"](p.c_str());
   return 0;
}

Upvotes: 1

Views: 428

Answers (1)

In your example, using boost::bind is completely superfluous. You can just assign the function itself (it will converted to a pointer to a function, and be type erased by boost::function just fine).

Since you do bind, it's not enough to just pass the function. You need to give boost::bind the argument when binding, or specify a placeholder if you want to have the bound object forward something to your function. You can see it in the error message, that's what boost::_bi::list0 is there for.

So to resolve it:

f["$A"] = boost::bind($A, _1);

Or the simpler

f["$A"] = $A;

Also, as I noted to you in the comment, I suggest you avoid identifiers which are not standard. A $ isn't a valid token in an identifier according to the C++ standard. Some implementations may support it, but not all are required to.

Upvotes: 2

Related Questions