Errors, bugs, questions - page 2653

 
Vladimir Simakov:

Well, STL is not a one-size-fits-all solution. You have to pay much attention to specifics here.

You're confusing soft with warm.
It's not a question of STL. I'll figure it out there myself... (if someone doesn't have it, it doesn't mean that it can't be done in principle)

The easiest way to implement all the possible functionality with abstract methods in a base class or interface, and in the descendants - either implementation or =delte.
In this case, you need to pass pointers or references of the same type to the methods of the base class.
Although there is an unavoidable evil in the form of a virtual table, but it's better to arrange the architecture in such a way that there is no expensive branching via dynamic_cast anywhere.

The method you suggested was implemented earlier and also hinges on the same bug:https://www.mql5.com/ru/forum/1111/page2648#comment_15015191


The bug is in the priorities of calling reloaded functions when an implicit type conversion is performed for a pointer/class parameter.
In C++ all is OK, but in MQL the compilation error"ambiguous call to overloaded function"
One of the workaround variants is suggested above, but it is large and not convenient, and I have no desire to use it for a dozen of similar functions.
Perhaps there is something simpler?

 
Macros do their job, managed to reduce the canvas to form (full version in the attachment file):
//+------------------------------------------------------------------+
//|  MQL realization                                                 |
//+------------------------------------------------------------------+
#ifdef __MQL5__
   template <typename _InputIter>
   CREATE_MISSED_ITERATOR_FUNCTION_FW_P2(int, distance, _InputIter&, _InputIter&);
   
   template <typename _InputIter>
   CREATE_MISSED_ITERATOR_FUNCTION_RA_P2(int, distance, _InputIter&, _InputIter&);

   template <typename _InputIter>
   CREATE_MAIN_ITERATOR_FUNCTION_P2(int, distance, _InputIter&, _InputIter&, _InputIter);
   
   void OnStart(){
      MyIterator it1(1);
      MyIterator it2(5);
      printf("result:%d", distance(it1, it2));            
   }
#endif 

Developers, please advise, are there any plans to fixthe bug or is this architecturally impossible?
Files:
 
Bug MT5(build 2323),
Inside a template function, a passed pointer within an explicit type conversion behaves like a class, causing a compile-time error accordingly:
#define  PRINT(x) ; Print(#x, ":", string(x))

template<typename T>
class ClassWrapper{
public:
   T data;
};

class ClassA{};
class ClassB : public ClassA{};


template<typename T, typename TT>
void func(const T &_wrapper, const TT &value){
   T wrapper = _wrapper;   
   
   PRINT(typename(wrapper.data));         // Ok: ClassA*
   PRINT(typename(value));                // Ok: ClassA*
   PRINT(typename(TT));                   // Ok: ClassA*
   
   //wrapper.data = (TT)(value);          // Compiler Error: 'value' - object required  
   //wrapper.data = (TT)(wrapper.data);   // Compiler Error: 'data' - object required   
   
   ClassB b;
   PRINT(typename((TT)(b)));              // Result: C lassA. During explicit type conversion, TT is "ClassA" instead of "ClassA*"
};


void OnStart(){
   ClassWrapper<ClassA*> a_ptr_wrapper;
   
   ClassA a;
   const ClassA* a_ptr = &a;
   func(a_ptr_wrapper, a_ptr);
};
 

Dumb, can't solve a simple problem. I need to understand that trading is forbidden during the following EA startup settings.


This check does not work.

int OnInit()
{
  Alert(AccountInfoInteger(ACCOUNT_TRADE_EXPERT) &&
        AccountInfoInteger(ACCOUNT_TRADE_ALLOWED) &&
        TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)); // true
        
  return(INIT_FAILED);
}


OrderCheck is a red herring. Please advise.

 
bool CheckTradingPermission()
  {
//--- Для режима реального времени
   if(IsRealtime())
     {
      //--- Проверка соединения с сервером
      if(!TerminalInfoInteger(TERMINAL_CONNECTED))
         return(false);
      //--- Разрешение на торговлю на уровне данной запущенной программы
      if(!MQLInfoInteger(MQL_TRADE_ALLOWED))
         return(false);
      //--- Разрешение на торговлю на уровне терминала
      if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
         return(false);
      //--- Разрешение на торговлю для текущего счета
      if(!AccountInfoInteger(ACCOUNT_TRADE_ALLOWED))
         return(false);
      //--- Разрешение на автоматическую торговлю для текущего счета
      if(!AccountInfoInteger(ACCOUNT_TRADE_EXPERT))
         return(false);
     }
//---
   return(true);
  }
ANATOLI KAZHARSKI code

Anatoli Kazharski
Anatoli Kazharski
  • www.mql5.com
Опубликовал статью ZigZag всему голова (Часть II): Примеры получения, обработки и отображения данных В первой части был описан модифицированный индикатор ZigZag и класс для получения данных индикаторов такого типа. Теперь мы покажем как создать индикаторы на основе этих инструментов, а также напишем эксперта для тестов, который будет заключать...
 
Fast235:
ANATOLI KAZHARSKI code

Thanks, didn't notice the MQL_TRADE_ALLOWED.

 
fxsaber:

Dumb, can't solve a simple problem. I need to understand that trading is forbidden during the following EA startup settings.


This check does not work.


OrderCheck is a red herring. Please advise.

I'm not sure from my mobile, but here it is:MQL_TRADE_ALLOWED
 
Bug MT5(build 2323), the same template object B<int> can be created after object of class B<void*>, but if done before, a compilation error occurs.
Probably the reason is the work of template class generator cache.

template<typename T>
class B{
   T data;
};

void OnStart (){ 
   //B<int> b_int_0;    // Compiler Error  'void' - unexpected token, probably type is missing?
   B<void*> b;          // OK
   B<int> b_int_1;      // OK
}
 

Bug MT5(build 2340) uses the same code twice: first pass - successful compilation, second pass - compilation error.
Apparently the problems are related to the template function generator cache.

The error disappears if for the template function "
void run(const T &ff)" the transfer by reference is replaced by the transfer by value"void run(const T ff)".

void func(){
}

template<typename T>
void run(const T &ff){
   ff();
}

void OnStart (){ 
   {
      typedef void(*f_ptr)();
      f_ptr ff = func;
      run(ff);             // OK
   }
   {
      typedef void(*f_ptr)();
      f_ptr ff = func;
      run(ff);            // Compile Error: 'ff' - parameter conversion not allowed     
   }
}
 

Bug MT5(build 2340) Compilation error when attempting to access the internal class for a template parameter of a template function.

class B{
public:
   class C{};
};


// Bypass Compile Error
template<typename T>
class GetClassType{
public:
   class type : public T{};
};


template<typename T>
void func(const T &b){
   T::C* c_ptr = new T::C;          // Compile Error: 'T' - struct undefined    
   
   // Bypass Compile Error
   GetClassType<T>::type::C* c_ptr_bps = new GetClassType<T>::type::C();
};

void OnStart (){ 
   B b;
   func(b);
}