1

The problem replace my if and else statements with a map that contains a string as a key and a function pointer as the value. However each function pointer can point to functions that have a different return type and different parameters without using boost. Basically what I'm wondering is how you create a map with generic function pointer as its value.

Below is simplified version of the problem I'm trying to solve. The desired output.

#include<iostream>

int addtwoNumber(int a, int b){
    return a+b;
}
bool isEqual(std::string str, int number){
    return std::stoi(str)==number;
}

int main(){
    // create a map that contains funtion pointers
    template<typename ReturnType, typename... Args>
    std::map<std::string, ReturnType (*)(Args...)> actionMap; // create a map<string, function pointer>


    actionMap.insert(std::make_pair("one", &addtwoNumber)); // add functions to the map
    actionMap.insert(std::make_pair("two", &isEqual));

    std::cout << "type commands and arguments: " << std::endl;
    std::string command;
    std::cin >> command;
    auto func = actionMap.find(command[0]);
    std::cout << *func() << std::endl; // how do I pass the arguments to the function
}

Desired Output:

./test.out              
one 2 5                  /user input
7                        /Output of the program
./test.out
two 5 5
true
user3697625
  • 167
  • 4
  • 17
  • 6
    Take a step back for a second. What are you trying to accomplish with this here? – StoryTeller - Unslander Monica Feb 15 '16 at 12:02
  • 5
    What is the *actual* problem you try to solve with the solution you want help with? Please [read about the XY problem](http://xyproblem.info/). – Some programmer dude Feb 15 '16 at 12:04
  • I want to get the code to compile – user3697625 Feb 15 '16 at 12:05
  • 2
    Is the program you show the actual program you want to work? It's not a simplification, a [MCVE](http://stackoverflow.com/help/mcve) of another program? You don't have another program with some problem that you want to solve by using maps of function pointers? If you *do* have another program, and the program you show is not the actual program, please tell us the actual problem from the actual program, because then you ask us to help you with a solution without telling us what problem you are actually trying to solve. Please read the link in my previous comment. – Some programmer dude Feb 15 '16 at 12:09
  • 1
    std::cout << (*func)() << std::endl; – oklas Feb 15 '16 at 12:10
  • 3
    Also, it is always helpful to know the actual errors you get, so please edit your question to also include the actual (complete, in full and unedited) errors you get, including any informational notes and messages. – Some programmer dude Feb 15 '16 at 12:10
  • passing argument to the function: std::cout << (*func)(a,b) << std::endl; Are you write command line processor? – oklas Feb 15 '16 at 12:18
  • but next step of your task will be make dispatching to different code blocks anyway. – oklas Feb 15 '16 at 12:22
  • you need to provide the same signature for functions in the map. for the return value you may choose boost::variant. and for params : "...". e.g. typedef boost::variant my_return_type; my_return_type func1(...) { ...; return 1;} my_return_type func2(...) { ...; return false;} p.s. but it is better to review your design – Alexander Feb 15 '16 at 12:38
  • This is a simplification of the problem I'm trying to solve using only the C++11 (without boost). The main problem is that i have a text based game that takes the commands from the terminal. In my code I have a very many if and else case. What i want to do is replace the if-else case with a map that maps commands to functions. – user3697625 Feb 15 '16 at 19:23

2 Answers2

1
struct do_nothing_map{
  void insert(...){}
  int(*)() find(...){return []{return 0;};}
};
int main(){
  do_nothing_map actionMap;

  actionMap.insert(std::make_pair("one", &addtwoNumber));
  actionMap.insert(std::make_pair("two", &isEqual));

  std::cout << "type commands and arguments: " << std::endl;
  std::string command;
  std::cin >> command;
  auto func = actionMap.find(command[0]);
  std::cout << *func() << std::endl;
}

You refused to describe your problem more broadly, and instead said you "just wanted the above code to compile". I have made it compile using as little a change as I can. It does nothing useful, but it compiles, and is nearly unchanged.

You are welcome, in advance.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
1

This is a similar question, the answer should be useful: answer showing heterogeneous function map

The answer shows how to call functions in the map.

Community
  • 1
  • 1
Erik Alapää
  • 2,585
  • 1
  • 14
  • 25