What is not clear?
Perhaps it can be "cleared" up.
I think you missed the question. If the type is static, then you are right that there is no need for a "void pointer to function" -- you can use a regular function pointer. If the type is dynamic however, then you cannot use a regular function, and you cannot use a template, since both of these require you to know the type compile time. Therefore, another solution is called for.
Note I am using static in this context to refer to what is know at compile time. Dynamic, in contrast, would refer to everything that cannot be determined before the program is run.
Your template solution still deals with static types, since T must be known at compile time. In contrast a void * and its function pointer equivalent (which is what the OP asked about) does not know the type that it points to. In order to dereference it, run time data must be supplied to provide that type, and the pointer must be cast before being applied.
Now of course nobody really wants a void pointer, since a void pointer can point to any type, and in any real world problem there is only a finite set of possible types that is can stand for. What is really wanted is a pointer that can point to a finite set of data. This is why polymorphic classes are the preferred solution to such problems.
Similarly here what is really wanted is not a void function pointer, but a way for a pointer to be able to point to a finite but different set of possible functions. Again the preferred solution is polymorphic "functors" -- classes with overloaded operator().
Last edited by King Mir; 08-05-2008 at 07:26 PM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
the question is rather broad. there are many ways that you could possibly do such a thing, but few that would be genuinely useful.
in the code you posted, you could alter f1 to accept type void *, and then cast the argument to string when outputting to stdout.
And of course, a finite set of types is usually represented by templates. I get what you mean, and I agree with your preferred solution, as an alternative to the void* mess.
It's a good example of how templates and design can be used to side-step the whole void* mess, which, I admit, was what I was trying to demonstrate - that templates could be used instead of the void*.