I wonder is it possible to pass function to method as argument in case that method is template.
Valid C++ example of how I want to do:
template<typename TValueType>
class ValueWrapper
{
public:
TValueType* Value;
ValueWrapper(TValueType* initialValue){ Value = initialValue;}
template<typename TNewType>
ValueWrapper<TNewType>* Convert (TNewType* (*convertFunction)(TValueType*)){ // Argument is a function which input contains template type
TNewType* newSuccess = convertFunction(Value);
ValueWrapper<TNewType>* newResult = new ValueWrapper<TNewType>(newSuccess);
deletethis;
return newResult;
}
}
char* ToByte (int* i){
returnnewchar((*i) % 256);
}
int Main(){
ValueWrapper<int> *p = new ValueWrapper<int>(newint(5));
ValueWrapper<char> *pConverted = p->Convert<char>(ToByte); // I give a function how to do conversion between int and char types
delete pConverted;
return0;
}
According to this documentation it looks like MQL allows to pass function as argument only if you define all function input/output parameter types statically. So, most likely MQL does not allow such flexible definitions like C++ does, but I just want to make sure. Does anybody know any solution for that? :)
Example of the problem in MQL:
class baseNumber{
public:
int Value;
baseNumber(int val){Value = val;}
string ToString(){returnIntegerToString(Value);}
};
class number : public baseNumber
{
public:
number() : baseNumber(3){}
};
typedef baseNumber* (*abstractFunc)(baseNumber*); // I need to define function for one specific use case of this templatetemplate<typename TValueType>
class ValueWrapper
{
public:
TValueType* Value;
ValueWrapper(TValueType* value){Value = value;}
template<typename TNewType>
ValueWrapper<TNewType>* Convert (abstractFunc convertFunction){
TNewType* newSuccess = convertFunction (Value);
ValueWrapper<TNewType>* newResult = new ValueWrapper<TNewType>(newSuccess);
deleteGetPointer(this);
return newResult;
}
};
baseNumber* ConvertFunction(baseNumber* model){
return model;
}
intOnInit()
{
ValueWrapper<number>* p = new ValueWrapper<number>(new number());
ValueWrapper<number>* pConverted = p.Convert<number>(ConvertFunction);
delete pConverted;
return(INIT_SUCCEEDED);
}
then my template won't work. The compiler throws "'ConvertFunction2' - cannot resolve function address" and "'Convert<number>' - object pointer expected". Looks like function can't be defined with arguments from template types.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
I wonder is it possible to pass function to method as argument in case that method is template.
Valid C++ example of how I want to do:
I have read documentation from this link: https://docs.mql4.com/basis/types/typedef
According to this documentation it looks like MQL allows to pass function as argument only if you define all function input/output parameter types statically. So, most likely MQL does not allow such flexible definitions like C++ does, but I just want to make sure. Does anybody know any solution for that? :)
Example of the problem in MQL:
The problem is if I will want to have function
number* ConvertFunction2(number* model){ return model; }then my template won't work. The compiler throws "'ConvertFunction2' - cannot resolve function address" and "'Convert<number>' - object pointer expected". Looks like function can't be defined with arguments from template types.