Errors, bugs, questions - page 1856

 
Andrey Dik:

How can I see the encoding?
I know that it has changed by changing the appearance of text in a third-party editor, but how to tell which encoding I do not know.
Andrey Khatimlianskii:
Just created an EA in ME 1580: Win-1251 (ANSI)

Yes, with Akepad I see that encoding is Win-1251 (ANSI), but older files have UTF-16LE 1200.

So it does change the default encoding for new files?

 

Forum on trading, automated trading systems and trading strategies testing

Features of mql5 language, subtleties and tricks

fxsaber, 2017.04.14 11:40

Yep, crooked brokers. They'd also put negative numbers in there.

It would be nice if the developers had a limit on the range of possible values of each parameter when brokers set up the symbols.

 

I've noticed that when I update products from the marketplace - all settings for that product on the charts are reset to defaults.

This is not right, how can this be avoided?

 

ArrayMaximum() and ArrayMinimum() - the order in which the parameters are passed does not match the help:


 
fxsaber:
A little know-how. Bypassing the assignment operator

Result

It's not a tip, it's a perversion.

I didn't expect that from you.

 
Koldun Zloy:

It's not a smart trick, it's a perversion.

I did not expect it from you.

So this is for you

Forum on trading, automated trading systems and strategy testing

Libraries: TypeToBytes

fxsaber, 2017.04.13 13:34

An example of how this feature can be useful for identifying potential errors.

Writing and running the script.

#include <TypeToBytes.mqh>

struct STRUCT
{
  int i;
  
  STRUCT() : i(0) {}
  
  template <typename T>
  void operator =( T& ) {}
};

#define  PRINT(A) ::Print(#A + " = " + (string)(A));

void OnStart()
{
  PRINT(_WRONG_ASSIGN_OPERATOR(STRUCT))
}


Result.

_WRONG_ASSIGN_OPERATOR(STRUCT) = true

This indicates that the assignment operator will not copy the structure into a structure of the same type.


If we add more to the structure,

  void operator =( STRUCT &Value ) { this.i = 0; }

the result will be the same.


It may seem that by changing this operator to

  void operator =( STRUCT &Value ) { this.i = Value.i; }

but the library says otherwise.


This is perhaps the subtlest point of this example.

Correct it by

#include <TypeToBytes.mqh>

struct STRUCT
{
  int i;
  
  STRUCT() : i(0) {}
  
  template <typename T>
  void operator =( T& ) {}

  void operator =( const STRUCT &Value ) { this.i = Value.i; }
};

#define  PRINT(A) ::Print(#A + " = " + (string)(A));

void OnStart()
{
  PRINT(_WRONG_ASSIGN_OPERATOR(STRUCT))
}

and you get the result

_WRONG_ASSIGN_OPERATOR(STRUCT) = false


Now the copy operator is correct!

We may check correctness of assignment/copying operators of any simple structure in the same way.

 

1. When we write like this:

STRUCT StructCopy1 = Struct;
It is not operator= that should be called but the copy constructor.

Although, that's more a question for the Metaquotes.

2. If the copy constructor created by the compiler is not called, we can write it ourselves.

struct STRUCT
{
  int i;
  
  STRUCT(){}
  STRUCT( int _i ) : i(_i){}
  STRUCT( const STRUCT& other )
  {
      i = other.i;
  }
  
  void operator =( const STRUCT& )
  {
    this.i = 5;
  }
};

Now operator= is not called.

3. Since the structure members are available to us, we can just write it that way:

StructCopy1.i = Struct.i;

But if you make them private, then your tiphook won't help either.

4. and most importantly: operator= is used to ensure that all assignments go through it and not bypass it.

Usually, the copy constructor is defined together with it.

Yes. I looked through the sample. I did not see much profit.

 
Koldun Zloy:

1. When we write like this:

It is not operator= that should be called but the copy constructor.

Although, that's more a question for the Metaquotes.

2. If the copy constructor created by the compiler is not called, we can write it ourselves.

Now operator= is not called.

3. Since the structure members are available to us, we can just write it that way:

But if you make them private, then your tiphook won't help either.

4. and most importantly: operator= is used to ensure that all assignments go through it and not bypass it.

Usually, the copy constructor is defined together with it.

Yes. I looked through the sample. I did not see much profit.

Lifehack bypasses the copy constructor as well. And the benefit is here
#include <TypeToBytes.mqh>

struct STRUCT
{
  int i;
  
  STRUCT(){}
  STRUCT( int _i ) : i(_i){}
  STRUCT( const STRUCT& other )
  {
      i = 2;
  }
  
  void operator =( const STRUCT& )
  {
//    Print(__FUNCSIG__);
    
    this.i = 5;
  }
};

void OnStart()
{
  STRUCT Struct(1);  

  ArrayPrint(_R(Struct).Bytes); // 1 0 0 0
  
  STRUCT StructCopy1 = Struct;  // STRUCT( const STRUCT& )

  ArrayPrint(_R(StructCopy1).Bytes); // 2 0 0 0
    
  StructCopy1 = Struct;         // void STRUCT::operator=( const STRUCT& )

  ArrayPrint(_R(StructCopy1).Bytes); // 5 0 0 0
}

_R is a universal thing. That's why we don't need to redefine alien structures with their own operators.

#include <TypeToBytes.mqh>

struct STRUCT
{
  int i;
  
  void operator =( const STRUCT& )
  {
    this.i = 5;
  }
};

void OnStart()
{
  STRUCT Structs[] = {{1}, {2}};
  
  ArrayPrint(_R(Structs).Bytes); // 1 0 0 0 2 0 0 0
}
 
Andrey Dik:

Yes, with Akepad I see that the encoding is Win-1251 (ANSI), but older files have UTF-16LE 1200.

So has the default encoding for the new files changed after all?

No, it was always Win-1251 (ANSI) and UTF was added at some point. But only for those source files that contain non-Ansi characters.
 
Andrey Khatimlianskii:
No, it was always Win-1251 (ANSI) and at some point UTF was added. But only for those sources that contain non-Ansi characters.
I see. Thank you.