So, imagine that we had 2 functions (void : ( void ) ) and (std::string : (int, std::string)) , and we could have 10 more. All (or some of them) accept different types of arguments and can return different types. We want to save them in std::map , so we get the API as follows:
//Having a functions like: int hello_world(std::string name, const int & number ) { name += "!"; std::cout << "Hello, " << name << std::endl; return number; } //and void i_do_shadowed_stuff() { return; } //We want to be capable to create a map (or some type with similar API) that would hold our functional objects. like so: myMap.insert(std::pair<std::string, fun_object>("my_method_hello", hello_world) ) myMap.insert(std::pair<std::string, fun_object>("my_void_method", i_do_shadowed_stuff) ) //And we could call tham with params if needed: int a = myMap["my_method_hello"]("Tim", 25); myMap["my_void_method"];
I wonder how to put many different functions in the same container. In particular, how to do this in C ++ 03 using Boost.
The API should be independent of the actual types of functions (with int a = myMap["my_method_hello"]("Tim", 25); not int a = myMap<int, (std::string, int)>["my_method_hello"]("Tim", 25); ).
source share