Function Pointers - added in a recent build?

 

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!

 
smimon:

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
  • www.mql5.com
See the "MQL5 Reference / Standard constants, enumerations and structures / Named constants / Other constants " section.
 
Amir Yacoby:

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.

N‌ow if we can just get lambda expressions too... :)

 
smimon:


Great, thanks! I knew I'd seen it somewhere.

N‌ow if we can just get lambda expressions too... :)


Any way to use function templates with function pointers, to create generic method delegates?
 
Amir Yacoby:

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 
 
smimon:

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.

 

Is there a matching C/C++ datatype for handles? Or is it simply not possible to call MQL functions from a dll using callbacks?

Reason: