Passing Function as Argument like Python 3 Map()

 

Hello,


I am looking for a way to call a literal array of functions and found ways in C++ to do this however MQL4 is obviously different. In C++ you can use pointers of the function to call it by, or in the standard library std:function is included which is what I am trying to mimick.

Is there any way to actually accomplish this without having some sort of explicit pointer? Do I need to do some cluster of inherited classes or something convoluted or stupid like that? 


Please let me know if you need clarification or have an idea.

Thank you!

 
This referring to MQL5, I am not exactly sure about the difference to MQL4, but it should be similar.
Function Pointers are specific to their functions signature.
This means, you cannot use an array of type pointers to different types of functions.
To accomplish something similar, you would somehow need a way to distinguish between the different types of function signatures.
So the function you are pointing to would be some sort of router for your calls. Here you can go multiple ways to make "your own type of dynamic function resolving" at runtime.
Although you still would need some way to handle the different types of function parameters.
You could either use a "harmonic" signature that's across all your function pointers the same, or you'd have to deal with some sort of parameter conversion.
This is relevant especially if you have a high frequency on calling these functions.

Whatever your approach is, this is the same for C/C++.

I would use a harmonic function signature.

 
Dominik Egert #:
This referring to MQL5, I am not exactly sure about the difference to MQL4, but it should be similar.
Function Pointers are specific to their functions signature.
This means, you cannot use an array of type pointers to different types of functions.
To accomplish something similar, you would somehow need a way to distinguish between the different types of function signatures.
So the function you are pointing to would be some sort of router for your calls. Here you can go multiple ways to make "your own type of dynamic function resolving" at runtime.
Although you still would need some way to handle the different types of function parameters.
You could either use a "harmonic" signature that's across all your function pointers the same, or you'd have to deal with some sort of parameter conversion.
This is relevant especially if you have a high frequency on calling these functions.

Whatever your approach is, this is the same for C/C++.

I would use a harmonic function signature.

As far as I am aware, MQL4 implemented pseudo-pointers on Classes only, any other type does not "have a pointer" that we as developers can use. I was hoping to be able to generate the function call and "save" it in an array that I could iterate through the list of callables every update (once per candle). As that does not seem possible, I am going with my backup plan of using a template<typename Key, template value> in an array that a router function will have to read and generate the callable based on the function name being passed in. Basically a a digital version of the old phone switchboards.

From what I understand of your advice is that if the function signature is "the same" (not via overloading but by design) by using the same arguments/parameters I would be able to build a routing function but the argument types would have to be harmonious in nature. My question is two-fold now:

Is there something similar to C11's "..." Keyword for passing optional params?

Could I pass the arguments inside an &array to the router so it's more versatile? (error handling required obviously)

if anyone has something more elegant I would be super grateful to hear it.

 
Function pointers are possible:


Else you'll need to use unions and/or char arrays as input transformations.

It would be some work.

Otherwise, and for a more modern approach, go for OOP. Base class pointer, overloaded functions from derived classes....

Edit:
There is no equivalent to C11 ...Youbwould have to imitate this behavior with preassigned function parameters.
 
Dominik Egert #:
Function pointers are possible:


Else you'll need to use unions and/or char arrays as input transformations.

It would be some work.

Otherwise, and for a more modern approach, go for OOP. Base class pointer, overloaded functions from derived classes....

Edit:
There is no equivalent to C11 ...Youbwould have to imitate this behavior with preassigned function parameters.

 "MQL5 allows creating pointers to functions using typedef." is the exact wording on typedef unfortunately. Looks like I'm either better making a DLL in C++ for this or simplifying my plans.


Thank you so much for the help!

 
Where is this from?

The link I posted does not state that!?
And it's MQ4 docs.

I do not code MQ4, so I cannot test it, but to me it seems obvious to be possible.