function pointer container using Boost Bind

This topic is 3326 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I am trying to call the function(consoleSetRenderMode()) once i get a string called "setRenderMode" but i get compilation errors :( #include <boost/any.hpp> #include <boost/function.hpp> #include <boost/bind.hpp> #include <map> #include <string> #include <vector> #include <iostream> using namespace std; typedef std::vector<boost::any> parameters; typedef boost::function<void(const parameters&)> consoleFunc; typedef std::map<std::string, consoleFunc> consoleFuncMap; consoleFuncMap consoleFuncs; void consoleSetRenderMode(const boost::any ); void consoleSetRenderMode(const parameters ¶ms) { assert(params.size() == 1); // should have 1 parameter int renderMode = boost::any_cast<int>(params[0]); cout<<"Val"<<renderMode<<endl; //renderer->setRenderMode(renderMode); } consoleFuncs["setRenderMode"] = boost::bind(&consoleSetRenderMode, _1);

Share on other sites
You don't need to use boost::bind if your function signature already matches the function type. However, since your function is overloaded, you do need to disambiguate the function overload. Ex:
consoleFuncs["setRenderMode"] = static_cast<void (*)(const parameters &)>(consoleSetRenderMode);

Share on other sites
Quote:
 void consoleSetRenderMode(const boost::any );void consoleSetRenderMode(const parameters ¶ms)

Two functions with the same name and different parameters are overloaded functions. Also, you can't assign elements in a map outside of a function like that. One way to deal with that is to move the assignments to inside main.

Share on other sites
No, your typedef for parameters is std::vector<boost::any>, not boost::any. And even if it was the same, the first one is by value and the second by reference.

Share on other sites
What exactly is the logic behind deleting all your replies here?

Share on other sites
to make the post look clear and short

Share on other sites
Quote:
 Original post by himanojhereto make the post look clear and short

Your definition of "clear" is defective.

Share on other sites
can we get back to the problem

Share on other sites
How? You deleted your responses. Kind of hard to comment on something that's not there.

Share on other sites
What was wrong with SiCrane's original response? That seems like the correct answer to me.
Quote:
Original post by SiCrane
Quote:
 void consoleSetRenderMode(const boost::any );void consoleSetRenderMode(const parameters ¶ms)

Two functions with the same name and different parameters are overloaded functions. Also, you can't assign elements in a map outside of a function like that. One way to deal with that is to move the assignments to inside main.

• 16
• 9
• 13
• 41
• 15