Is it possible to pass function as method argument in template class

To add comments, please log in or register
Mantas Smicius
17
Mantas Smicius  

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);

         delete this;

         return newResult;

      }

}



char* ToByte (int* i){

    return new char((*i) % 256);

}



int Main(){

    ValueWrapper<int> *p = new ValueWrapper<int>(new int(5));

    ValueWrapper<char> *pConverted = p->Convert<char>(ToByte); // I give a function how to do conversion between int and char types

    delete pConverted;

    return 0;

}

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:

class baseNumber{
   public:
      int Value;
      baseNumber(int val){Value = val;}
      string ToString(){return IntegerToString(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 template

template<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);
         delete GetPointer(this);
         return newResult;
      }
};

baseNumber* ConvertFunction(baseNumber* model){
   return model;
}

int OnInit()
  {
  ValueWrapper<number>* p = new ValueWrapper<number>(new number());
  ValueWrapper<number>* pConverted = p.Convert<number>(ConvertFunction);
  delete pConverted;
  return(INIT_SUCCEEDED);
  }

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.

To add comments, please log in or register