I have an unordered_map of functions that should be called on an object when an XML file is parsed.
I have found that boost::function has a base class named boost::function_base, however as expected I cannot invoke it because I don't have the signuture of the function.
Since all of those functions are setter functions, I can guarantee that they return void and have only one parameter of an unknown type.
Is there any better way to resolve the type other then an if-else-if branch which I am trying to avoid?
Asked
Active
Viewed 193 times
1
2 Answers
2
Use a boost::variant is the best way to go. How could you possibly invoke a function with an unknown parameter type, anyway?

Puppy
- 144,682
- 38
- 256
- 465
-
+1 i second that. A variant knows what type is stored, so he can avoid the if-else-if branch. – Johannes Schaub - litb Sep 12 '10 at 20:17
-
@Johannes Schaub - litb: But then how can I just call the function? I still need to know the type I am resolving. – the_drow Sep 12 '10 at 21:57
-
@the_drow look into visitation. boost.variant manual has information on it. – Johannes Schaub - litb Sep 12 '10 at 22:10
-
@Johannes Schaub - litb: So I still need to specify the signuture of my functions, right? – the_drow Sep 13 '10 at 07:41
0
boost::function
is designed for compile time polymorphism only. Why don't you just use a regular function pointer? I.e.
typedef void (*function_type)(void *);

Billy ONeal
- 104,103
- 58
- 317
- 552