Did Structs Change with the last MT5 Update?

 

I am developing some Expert Advisors in which I heavily rely on structs and arrays of structs. After the last MT5 Update I was getting compile-time warnings, so I decided to extract the Code-Snippet that caused these warnings: 

struct myStruct{
   string thisString;
};

myStruct structArr[];

void OnInit(){
   ArrayResize(structArr, 2, 0);
}

void OnTick(){
   myStruct localStructVariable = structArr[1];
}

This causes the following warning: "'struct myStruct' initialized from type type 'myStruct' using assignment operator, this behavior is deprecated and will be removed in future" with the attached hint: "consider defining copy constructor 'myStruct(const myStruct&)'".

Is this new to anyone else? I'm pretty sure before the last update it was compiling without any warnings. What would the fix to that look like? I haven't really found anyrthing in the documentation, could maybe someone point me into the right direction or link an article that discusses that issue? Or am I doing something wrong with structs in general?

 

It is telling you that you need to declare an assignment operator method for the structure, to carry out the copy process explicitly and not implicitly ...

operator=
Documentation on MQL5: Language Basics / Functions / Operation Overloading
Documentation on MQL5: Language Basics / Functions / Operation Overloading
Documentation on MQL5: Language Basics / Functions / Operation Overloading
  • www.mql5.com
For ease of code reading and writing, overloading of some operations is allowed. Overloading operator is written using the keyword operator . The...
 

The reason is that before, it was a kind of a backwards compatibility with "C" code where "struct" was more simplistic.

However, C++ and MQL treat "struct" like a "class", and MQL has probably evolved to the point that maintaining the old "C" compatibility may no longer be ideal or efficient for the future the MQL.

 
EntropyM:

No warnings for your code in build 5222

[edit]

Even adding a constant field does not result in a warning:


 
Fernando Carreiro #:

The reason is that before, it was a kind of a backwards compatibility with "C" code where "struct" were simplistic.

However, C++ and MQL treat "struct" more like a "class", and MQL has evolved to the point that maintaining the old "C" compatibility may no longer be ideal or efficient for the future the MQL.

That explains some things, I come from the C side of things, never written a single line of code in C++ in my life. 


One part I still don't understand, what does the "consider defining copy constructor 'myStruct(const myStruct&)'" part mean? Why would I want to have an explicit constructor in my struct and what would a "copy constructor" look like?

 
EntropyM #One part I still don't understand, what does the "consider defining copy constructor 'myStruct(const myStruct&)'" part mean? Why would I want to have an explicit constructor in my struct and what would a "copy constructor" look like?

As stated, the "copy constructor" is just another name for the assignment operator, which will in essence "copy" the content from the source structure to the target structure. Both names, "assignment" and "copy" are used in C++ as well.

EDIT: Technically they are two different things, so you do have a point, but it is common to mix them up, and I too am guilty of that.

 
Vladislav Boyko #:

No warnings for your code in build 5222

Is it possible that you are using a beta version? The I'm on build 5200 and it tells me it's the latest release version.

 

Forgive me if my previous post is a little confusing. I think this explanation for C++, probably best explains the difference between "copy" and "assignment". 

Copy Constructor vs Assignment Operator in C++ - GeeksforGeeks
Copy Constructor vs Assignment Operator in C++ - GeeksforGeeks
  • www.geeksforgeeks.org
Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.
 
EntropyM #:

Is it possible that you are using a beta version? The I'm on build 5200 and it tells me it's the latest release version.

Yes, I'm using the beta version. Apparently (if I'm not mistaken), implicit copy constructor generation has already been added in build 5222. And in builds 5224+ the using operator and implicit 'operator=' will be added.

 
Vladislav Boyko #:
Apparently (if I'm not mistaken), implicit copy constructor generation has already been added in build 5222. And in builds 5224+ the using operator and implicit 'operator=' will be added.

Ilyas (MQ employee) wrote about this on the Russian-language forum. I won't risk trying to translate it, it's best to wait for the next release notes.

 

@Vladislav Boyko well thats interesting since it already worked as intended before build 5200, and it still works on 5200, it just gives the warning on compile that it's deprecated and will be removed, if you say that it won't be but instead will be reverted to implicitly using the operators without deprecating I'm not sure what went wrong in development.


This is what I came up with as a fix, compiling without any errors or warnings now. @Fernando Carreiro I'm not sure if this was what you had in mind with the assignment operator and I still don't understand the exact difference between what you meant with "copy" and "assignment" being the same (but also not really), but is this at least somewhat close to what you had in mind? 

struct myStruct{
   string thisString;
   myStruct(){
      thisString = "";
   }
   myStruct(const myStruct& structIn){
      thisString = structIn.thisString;
   }
};

myStruct structArr[];

void OnInit(){
   ArrayResize(structArr, 2, 0);
   myStruct localStructVariable = structArr[1];
}

void OnTick(){
   
}