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