2

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
10
  • 6
    Take a step back for a second. What are you trying to accomplish with this here? Commented Feb 15, 2016 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. Commented Feb 15, 2016 at 12:04
  • 2
    Is the program you show the actual program you want to work? It's not a simplification, a 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. Commented Feb 15, 2016 at 12:09
  • 1
    std::cout << (*func)() << std::endl; Commented Feb 15, 2016 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. Commented Feb 15, 2016 at 12:10

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.