vector of boost::function not working with _placeholder

Refresh

April 2019

Views

43 time

1

I need your help. I tried a lot to find a solution for the problem but failed so far.

Idea:

Create a vector of "command tuples" like the following:

typedef boost::tuple<std::string, boost::function<void()>> command_tuple;

std::vector<command_tuple> commands {
  boost::make_tuple("command1", boost::bind(&myclass::command1, this))
};

If the string "command1" is used the void command1() function is called. The ID index value is based on find_if search for the string ("command1" is ID=0) on the vector.

This is working fine!

boost::get<1>(commands[ID])();

Problem:

I found no way so far to use a vector of boost::function (pointers) defined with any kind type of function parameters:

typedef boost::tuple<std::string, boost::function<void(const char *)>> command_tuple;

std::vector<command_tuple> commands {
  boost::make_tuple("command1", boost::bind(&myclass::command1, this, std::placeholders::_1))
};

This will fail with a cannot convert std::_Placeholder<1> to type const char * compilation error.

This is therefore not possible:

boost::get<1>(commands[ID])("dynamic string argument");

Pinning down of the problem:

hmmm ... so I found out so far that even a simple vector of boost::function does not seem to work (in the way I am trying to use it):

std::vector<boost::function<void(const char *)>> commands {
  boost::bind(&myclass:command1, this, std::placeholders::_1)
};

Fails with the same compilation error.

It is possible to replace the std::placeholders::_1 with "static string constants" to avoid any compilation errors. But my idea was to use a "dynamic string argument".

Can someone please give me a quick hint of what I am doing wrong or maybe show some simple alternatives. I want to have quite a big list of function pointers that I can call based on their "string name".

I simply want to avoid a big "switch select case" style code block to select the function to be executed based on a dynamic "input string".

Thanks for your time!

1 answers

0

like T.C. replied in the comments. You should never mix std::bind, std::placeholders and boost::bind.