Features of the mql5 language, subtleties and tricks - page 215

 

By the way, if you object a static array X x[n]; and X has a k-tor, then all element fields in it will be zeroed first, even private, and only then the k-tor will be called. So there is no violation of the OOP paradigm in zeroing private fields.

But the fact that the constructor did not work is a bug.

 
mktr8591 #:

By the way, if we create an array static X x[n]; and X has a k-tor, then all element fields in it are zeroed first, even private, and only then the k-tor is called. So there is no violation of the OOP paradigm in zeroing private fields.

That's great. So, it turns out that zeroing comes BEFORE the constructor, and therefore everything is correct. Thank you!

 
Igor Makanu #:

I looked at my research in MQL5, it could be worse, I even pump up the history in the indicator by several TFs:

I can write a lot of things in a for statement ))))

I have never encountered a void-function call in for statement. I have seen other variants quite often.

 
mktr8591 #:
I forgot to add, if a class has non-trivial fields (objects), then after zero-init the whole object for such fields their default c-tor will be called.

Read:

The effects of value initialization are:

1)if T is a class type with nodefault constructor or with a user-provided or deleteddefault constructor, the object isdefault-initialized;
2) if T is a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object iszero-initialized and the semantic constraints for default-initialization are checked, and if T has a non-trivial default constructor, the object isdefault-initialized;
3)if T is an array type, each element of the array is value-initialized;
4) otherwise, the object iszero-initialized.


Read about default-initialized:

  • if T is a (possibly cv-qualified)non-POD(until C++11) class type, the constructors are considered and subjected tooverload resolution against the empty argument list. The constructor selected (which is one of thedefault constructors) is called to provide the initial value for the new object;
  • if T is an array type, every element of the array is default-initialized;
  • otherwise, no initialization is performed: the objects with automatic storage duration (and their subobjects) contain indeterminate values.

UB!

The fact that the compiler initializes you with zeros, doesn't mean anything, today it initializes, and tomorrow, after the next update....

 
Vladimir Simakov #:

Read:

The effects of value initialization are:

1)if T is a class type with nodefault constructor or with a user-provided or deleteddefault constructor, the object isdefault-initialized;
2) if T is a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object iszero-initialized and the semantic constraints for default-initialization are checked, and if T has a non-trivial default constructor, the object isdefault-initialized;
3)if T is an array type, each element of the array is value-initialized;
4) otherwise, the object iszero-initialized.


Read about default-initialized:

  • if T is a (possibly cv-qualified)non-POD(until C++11) class type, the constructors are considered and subjected tooverload resolution against the empty argument list. The constructor selected (which is one of thedefault constructors) is called to provide the initial value for the new object;
  • if T is an array type, every element of the array is default-initialized;
  • otherwise, no initialization is performed: the objects with automatic storage duration (and their subobjects) contain indeterminate values.

UB!

The fact that the compiler initializes you with zeros, doesn't mean anything, today it initializes, and tomorrow, after the next update....

My post "Forgot to add...." was written as a supplement to the previous post:

Forum on trading, automated trading systems and testing trading strategies

Peculiarities of mql5 language, tips and tricks

mktr8591, 2021.11.18 18:15

@A100




If you remember that mql was spawned from C++, both these examples (their counterparts) work well there because there are no constructors in these classes (i.e. there is an implicit constructor):

  • Declaring ClassX x[n]={}; results in a value-initialization of each element of the array.
  • If ClassX has no custom constructors (but has a default unset constructor), then the zero-initialization of the class objects is done - regardless of whether there are private fields.
  • But if there are const fields, the default constructor will be implicitly deleted, so compiler error.

Example in C++:

#include <iostream>
using namespace std;

class X
{
    int a;
    public:
    int get(){return a;}

    //X(){}   //так массив x не обнуляется
    //а если нет конструктора - то обнуляется
};

int main()
{
    X x[10]={};
    for (int i=0; i<10;i++)   cout<<x[i].get()<<endl;
}

So if there are no const fields in the structure /class, then the logic is correct.


It considered a certain situation - when a class has no user constructors and there is an implicit constructor that is not deleted. In this case point 1 quoted by you is not appropriate.

Point"2) if T is a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object iszero-initialized and the semantic constraints for default-initialization are checked, and if T has a non-trivial default constructor, the object isdefault-initialized;".


So it all fits together.

 
Vladimir Simakov #:

Read:

The effects of value initialization are:

1)if T is a class type with nodefault constructor or with a user-provided or deleteddefault constructor, the object isdefault-initialized;
2) if T is a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object iszero-initialized and the semantic constraints for default-initialization are checked, and if T has a non-trivial default constructor, the object isdefault-initialized;
3)if T is an array type, each element of the array is value-initialized;
4) otherwise, the object iszero-initialized.


Read about default-initialized:

  • if T is a (possibly cv-qualified)non-POD(until C++11) class type, the constructors are considered and subjected tooverload resolution against the empty argument list. The constructor selected (which is one of thedefault constructors) is called to provide the initial value for the new object;
  • if T is an array type, every element of the array is default-initialized;
  • otherwise, no initialization is performed: the objects with automatic storage duration (and their subobjects) contain indeterminate values.

UB!

The fact that the compiler initializes you with zeros, doesn't mean anything, today it initializes, and tomorrow, after the next update....

Oops. misread it. In this case:

2) if Tis a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object iszero-initializedand the semantic constraints for default-initialization are checked, and if Thas a non-trivial default constructor, the object isdefault-initialized;
 
fxsaber #:

Great. It turns out that zeroing comes BEFORE the constructor, so everything is correct. Thank you!

Just in case - this is only for static (this is all about C++). For local variables, if there is a custom k-tor, there is no zeroing.
 
fxsaber #:

I haven't seen a void function call in a for statement. The other variants are quite common.

You can write anything you like in the 3rd parameter of for statement, in fact, for statement can replace if() statement with {.....}

SZZ: I've also seen macros like

do
{
.....
}while(0)
 
Igor Makanu #:

You can write anything you like into the 3rd parameter of the for statement, in fact, the for statement can replace the if() statement with {.....}

SZZ: I've also seen macros like

Take away the semicolon, otherwise the whole point is lost)))

 
Vladimir Simakov #:

Remove the semicolon, otherwise all meaning is lost))))

yeah, right - this example in macros to put ; when calling a macro, I don't use macros much - no practice