MQL5 compiler issue

 

Today I ran into a problem with MQL5 compiler. Errors appear when compiling my old code.

I have written simple example to demonstrate it.

class DataContainer
{
public:
        int m_int;
};

class TestClass
{
public:
        DataContainer* get()
        {
                return &m_data;
        }
        
        const DataContainer* get() const
        {
                return &m_data;
        }
        
private:
        DataContainer m_data;
};

void OnStart()
{
        TestClass cc;
        cc.get().m_int = 10;
}

Here the compiler is using the constant method get instead of non constant and a error appears

'm_int' - constant cannot be modified


It is reproduced on builds 3446 and 3470.

 
Ramil.Bagizov: Today I ran into a problem with MQL5 compiler. Errors appear when compiling my old code. I have written simple example to demonstrate it. Here the compiler is using the constant method get instead of non constant and a error appears It is reproduced on builds 3446 and 3470.

Read the following thread on the Russian forum. I remember reading several posts there about problems with "const" but I don't know if it's related ...

Новая версия платформы MetaTrader 5 build 3440: Новый отчет по торговому счету - В новой версии MetaTrader 5 выпущена обновленная версия платформы MetaTrader 5. Улучшена работа функции CArrayList LastIndexOf.

 
Ramil.Bagizov:
        const DataContainer* get() const

You return a pointer to a constant DataContainer, so of course your attempt to modify it (m_int=10) fails.

 
William Roeder #:

You return a pointer to a constant DataContainer, so of course your attempt to modify it (m_int=10) fails.

Please read the code carefully. TestClass has two get() methods, const and non-const for const and non-const TestClass instances, respectively.

In my example non-const method must be used. And so it was until recently.

class DataContainer
{
public:
        int m_int;
};

class TestClass
{
public:
        DataContainer* get()
        {
                return &m_data;
        }
        
        const DataContainer* get() const
        {
                return &m_data;
        }
        
private:
        DataContainer m_data;
};

void OnStart()
{
        TestClass tc;
        tc.get().m_int = 10; // this is correct, "DataContainer* get()" method must be used

        const TestClass ctc;
        ctc.get().m_int = 10; // compilation error appears, "const DataContainer* get() const" method must be used
}
 

I continue investigation and found out an interesting thing.

I have an archive with the old terminal: build 2650 09 Oct 2020.

And prepared a simplier code example

class TestClass
{
public:
   void method() { Print("NON-CONST METHOD is called"); }
   void method() const { Print("CONST METHOD is called"); }
};

void OnStart()
{
   TestClass nc;
   nc.method();
   
   const TestClass c;
   c.method();
}

Output in build 2650

2022.10.18 23:37:48.892 const-test (EURUSD,H1)  NON-CONST METHOD is called
2022.10.18 23:37:48.892 const-test (EURUSD,H1)  CONST METHOD is called

Output in build 3470

2022.10.18 23:47:48.918 const-test (EURUSD,H1)  CONST METHOD is called
2022.10.18 23:47:48.918 const-test (EURUSD,H1)  CONST METHOD is called


If I change the order in which the methods are declared, the behavior of the new build changes.

class TestClass
{
public:
   void method() const { Print("CONST METHOD is called"); }
   void method() { Print("NON-CONST METHOD is called"); }
};

void OnStart()
{
   TestClass nc;
   nc.method();
   
   const TestClass c;
   c.method();
}

Output in build 3470

2022.10.18 23:51:56.972 const-test (EURUSD,H1)  NON-CONST METHOD is called
2022.10.18 23:51:56.972 const-test (EURUSD,H1)  CONST METHOD is called


So it looks like I have a workaround for my problem. But I'm pretty sure it's a compiler bug.

 
Ramil.Bagizov #:

I continue investigation and found out an interesting thing.

I have an archive with the old terminal: build 2650 09 Oct 2020.

And prepared a simplier code example

Output in build 2650

Output in build 3470


If I change the order in which the methods are declared, the behavior of the new build changes.

Output in build 3470


So it looks like I have a workaround for my problem. But I'm pretty sure it's a compiler bug.

Thank you for detailed report
Fixed
Please wait for updates