A function pointer to a function that has a list of variable arguments

Do we have a function pointer that points to a function using a variable argument list? For example, let's say I need to select a function from among the functions based on some input T 'input'. Can I use an STL card like this?

template<typename T> map<T, T (*)(T, ...)> func_map; 

If possible, you can say that I will do it right to do it from a design point of view.


[Edit] Actually, I have a set of algorithmic functions, and I have a message field. I need to select this algorithmic function based on several bytes in this post. I was thinking of using hash_map with the key value wrt pointer to this algorithmic function. I wanted to be very fast depending on performance, which could be an easy way to implement this.

Also, the reason I don't choose a simple if-else or switch block for this is because the value that tells us about the function to be executed may refer to some other function at a later point.

+6
source share
2 answers

Yes, it is possible, but you probably shouldn't. There are a number of problems with variational functions, including:

  • All passed types must be POD types or undefined behavior
  • There is no type safety. If you pass the wrong type, you will invoke undefined behavior.

I would recommend you find a different approach. If you talk more about why you need it, maybe someone can give you a better option.

+3
source

Personally, I think variable argument lists are a bad idea. This prevents you from using the compiler for type checking and, therefore, makes it easier to find errors before creating any object code.

Therefore, I recommend that you find a way around this.

0
source

Source: https://habr.com/ru/post/896877/


All Articles