i was wondering if there was a way to do this in C++?
void func1(const std::string& s) { std::cout << s << std::endl; } void func2(int me) { std::cout << me << std::endl; } int main() { std::map<std::string, boost::function< ??? > > a_map; a_map['func1'] = &func1; a_map['func1']('HELLO'); }
Is there any way to do what i have above using boost function and a map?
There are ways to store the functions, the problem is, in order to be able to call the function with the desired argument you’d have to know the calling signature of the function anyways, and if you have that information, you might as well use separate maps, or use a more complicated object than boost::function.
If you’re willing to do a bit of work and have a finite number of signatures, you could just do something like this:
This outputs:
Unfortunately, you can’t make templated virtual functions or you easily generalize this all.