How can one create an MQL4 function that accepts a parameter that is an array whose elements' datatype is unknown?
Hello,
How can one create an MQL4 function that accepts a parameter that is an array whose elements' datatype is unknown, such as MQL4's built-in ArraySize function?
https://docs.mql4.com/array/arraysize
const void& array[] // checked array
);
The ArraySize function is really flexible because one can use it to find the total number of elements in an array of any type, such as double[], int[], string[], etc.
I am in the process of creating many array functions which call all the built-in MQL4 array functions which accept arrays of unknown datatypes, and it will be a bit of a nightmare if I have to create overloaded versions that differ only by the array datatype.
What is the MQL4 syntax for doing this?
Note that copying the syntax of ArraySize shown above doesn't work. For example, the code below...
{
return 1;
}
... causes a compilation error that says "'const' - illegal use of 'void'"
Thank you very much in advance,
Robox
You can't use void.
Use a template.
int MyFunction_That_Accepts_An_Array_Of_Unknown_Element_DataType(const T &array[])
{
return 1;
}
You can't use void.
Use a template.
int MyFunction_That_Accepts_An_Array_Of_Unknown_Element_DataType(const T &array[])
{
return 1;
}
Thanks, Alain! It works perfectly! This is actually so much more useful than if MQL4 supported user-programmed functions with "void& array[]" params. I am going to use templates quite a bit going forward to write less, more maintainable code.
Thanks again!
Robox
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
How can one create an MQL4 function that accepts a parameter that is an array whose elements' datatype is unknown, such as MQL4's built-in ArraySize function?
https://docs.mql4.com/array/arraysize
const void& array[] // checked array
);
The ArraySize function is really flexible because one can use it to find the total number of elements in an array of any type, such as double[], int[], string[], etc.
I am in the process of creating many array functions which call all the built-in MQL4 array functions which accept arrays of unknown datatypes, and it will be a bit of a nightmare if I have to create overloaded versions that differ only by the array datatype.
What is the MQL4 syntax for doing this?
Note that copying the syntax of ArraySize shown above doesn't work. For example, the code below...
{
return 1;
}
... causes a compilation error that says "'const' - illegal use of 'void'"
Thank you very much in advance,
Robox