Over the past few days, I’ve been trying to find a way to store different PMF (pointer to member functions) in a container as delegates. And I wanted to type erase the PMF so I can store it in a container along with other typed PMF delegates. The only thing common tying the delegates together would be its call signature.
Straight to the use case:
You can see here, I’m able to store two different PMF’s in the same container. The idea here is to capture the function pointer of each callable type as void * and invoke it when asked. Implementation below:
So the magic here is how we reinterpreted cast both the PMF and object pointer to void *, and then stored the call structure in a typedef. This Delegate model can also be extended for functors, lambdas (capturing and non-capturing), and ofc, standalone function pointers. Just some tinkering will be needed. Happy coding.