I could have sworn I saw in the notes from a recent release that function pointers had been added to the language, and now I can't find it anywhere. I didn't imagine this, did I?
Thanks!
Anyone? I know this is a commonly asked-about feature, and it's never existed - except I really could have sworn I saw it in the release notes for build 1525, and now I can't find any mention of it.
not added to documentation yet, just in list of changes
https://www.mql5.com/en/forum/53/page17
MQL5: Added support for pointers to functions to simplify the arrangement of event models.
To declare a pointer to a function, specify the "pointer to a function" type, for example:
typedef int (*TFunc)(int,int);
Now, TFunc is a type, and it is possible to declare the variable pointer to the function:
TFunc func_ptr;
The func_ptr variable may store the function address to declare it later:
int sub(int x,int y) { return(x-y); } int add(int x,int y) { return(x+y); } int neg(int x) { return(~x); } func_ptr=sub; Print(func_ptr(10,5)); func_ptr=add; Print(func_ptr(10,5)); func_ptr=neg; // error: neg is not of int (int,int) type Print(func_ptr(10)); // error: there should be two parameters
Pointers to functions can be stored and passed as parameters. You cannot get a pointer to a non-static class method.
![List of changes in MetaTrader 5 Client Terminal builds List of changes in MetaTrader 5 Client Terminal builds](/i/community/logo_mql5_small.png)
- www.mql5.com
not added to documentation yet, just in list of changes
https://www.mql5.com/en/forum/53/page17
MQL5: Added support for pointers to functions to simplify the arrangement of event models.
To declare a pointer to a function, specify the "pointer to a function" type, for example:
Now, TFunc is a type, and it is possible to declare the variable pointer to the function:
The func_ptr variable may store the function address to declare it later:
Pointers to functions can be stored and passed as parameters. You cannot get a pointer to a non-static class method.
Great, thanks! I knew I'd seen it somewhere.
Now if we can just get lambda expressions too... :)
not added to documentation yet, just in list of changes
https://www.mql5.com/en/forum/53/page17
MQL5: Added support for pointers to functions to simplify the arrangement of event models.
To declare a pointer to a function, specify the "pointer to a function" type, for example:
Now, TFunc is a type, and it is possible to declare the variable pointer to the function:
The func_ptr variable may store the function address to declare it later:
Pointers to functions can be stored and passed as parameters. You cannot get a pointer to a non-static class method.
Works in MT4 too
![](https://c.mql5.com/3/120/smile.png)
Any way to use function templates with function pointers, to create generic method delegates?
If this can be added, we then can implement the general delegate type equivalents from C#:
void Action<TInput1, TInput2, TInput3...>
TReturn Func<TReturn, TInput1, TInput2, TInput3...>
and this then enables Linq-style operations like Where, SingleOrDefault, OrderBy etc, without having to wrap anything you want to work with first in a CObject-derived type
Hi,
has anyone tried passing a callback as a parameter to an exported dll function? I tried, but it seems MQL5 uses a totally different type. On a 64-bit MT5 installation the size of an MQL function pointer is 4 (bytes), whereas the size of a C++ function pointer is 8. Anyone know how they are stored?
Thanks.
MQL pointers are handles not addresses. C/C++ pointers are addresses.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I could have sworn I saw in the notes from a recent release that function pointers had been added to the language, and now I can't find it anywhere. I didn't imagine this, did I?
Thanks!