jt234 jt234 - 11 months ago 49
C++ Question

map of pointers to functions of different return types and signatures

I am looking for a way to call different functions by a string input.

I have a map that ties each unique string to a function pointer and a lookup function to search the map and return a pointer if found.

Now the trick is, I need a way to store and return pointers to functions with at least different return types, if possible, also with different signatures.

The usage would be:

Get a string input from a network socket ->
find and execute the found function -> shove the result straight back into the socket to be serialized and sent, not caring what actually happened.

Is this doable? If not, how would one approach this task?

Answer Source

That can be done with a bit of boilerplate code in different ways. If the number of signatures is small enough you can hold multiple vectors of function pointers (one per function type) and then a map that maps the function name with a type identifier (used to select the vector) and the position within the vector.

The second option would be to store a boost::variant (again, if the set of signatures is small). You would need to provide a visitor object that evaluates the function (for each function type stored) and yields the result. The type is managed by the boost::variant type so there would be no need for the type tag to be stored in the map.

You can also use full type erasure and store in the map a tag determining the type of function to be called and a boost::any object storing the function pointer. You can use the type information to retrieve the pointer and execute the function, but you will have to manually handle the switch based on function type.

The simplest approach, on the other hand, is to write adapters that have a fixed interface. Then just store the pointers to the adapters in the map.