Arrays of Templated Function Pointers

This class was a random challenge that a student game me. he was looking for a way to pass an array of function pointers to functions that take templated parameters. This was an interesting idea so I worked though it with him and here it is for anyone else to view if they would like to

There are problems with doing something this obstufocated. First it is going to be hard for many programmers to really understand what you are doing and why. And second this is a very specific implementation and generally when you could use this, you could use something else to do it in an easier to understand method. With that said I still think it is an interesting challenge and can be useful in rare situations.

Let’s start by making some easy templated functions

So this challenge brings up one major flaw. You can have a templated function just fine but you can't have a templated array without it being inside of something else that is templated like a struct or class. So to get this implementation to work we must make a struct to handle the templating for us.

Here was my first thought on this.

Though that works, it requires you to make an instance of the structure to use it. Not the most elegant solution that is for sure. So let’s fix that problem.

Now with this implementation we can call the functions whenever we would like. Let's take a look at how we can use this implementation.

This is still not a perfect implementation though. The function pointers can only be set one time, during initialization and the array is always going to be of 4 function pointers. Not any other number. So far it has not been necessary for me to fix that flaw. In the future if I ever do, or if people would like to expand on this more and email me asking me to do so, I will fix these problems.