Errors, bugs, questions - page 2045

 

But I set the wrong array dimension there, maybe it affected it somehow (although it shouldn't affect it at all).

Try this with dimension = 3

template<typename T>
int F1(const T& array[]) { return ArrayMaximum(array); }

template<typename T>
int F2(const T& array[]) { return F1(array); }

void OnStart()
  {
    int arr[3]= { 1, 2, 3 };
    Print(F2(arr));  // Выдаёт -1 !!!
  }
 

Discovered the cause of the discrepancy. With OPTIMIZE=0 there is an error, but not with OPTIMIZE=1. I usually only use OPTIMIZE=0

 
fxsaber:

If I find a logical (non-contradictory) explanation for the result, I don't see the bug. I don't think it's acceptable to prove a bug if the result doesn't match C++. It's in C++ that someone thought that way and did it. But they might not have thought so and therefore didn't do it. That's why it's better not to refer to something out there, but rely on your own inner concept of what must be. And it is desirable that this "own" should really be one's own. Not the result of imperceptible imposition of stereotypes "the way it should be" as you gain programming experience.

If you do not have your own explanation, it means that there is no error. And if this explanation appears a year later and is rather convincing, will everybody have to redo everything? In C++ they have already thought a hundred times why it's done this way and not that way. And the explanation is needed if something is set up differently in MQL than in C++ and not vice versa.

 
Alexey Viktorov:
A static variable can be initialised with a constant or constant expression corresponding to its type , unlike a simple local variable, which can be initialised with any expression.


But not a function.

Then static class instances should be banned - because they are initialized by a constructor function

 

And here's more on the topic of variable initialisation. If you follow what was stated in the documentation, then you can't reference other global/static variables either. As it is not a constant expression:

int a= 1;
int b= a+1;  // Согласно документации, такая инициализация не возможна

void OnStart()
{
  Print(b);
}

However, for now it works. And I suppose many people have always used such constructs without realizing it. But it turns out that the developers may disable it at any moment. And, as in the case with function initialization, these codes will still compile successfully, but they won't work correctly. All in all, the whole MQL is a time bomb.

 
Alexey Navoykov:

And here is another thing concerning variable initialization. If you follow what was stated in the documentation, then you can't reference other global/static variables either. Because it is not a constant expression:

//+------------------------------------------------------------------+
//|                                                   ExpertMACD.mq5 |
//|                   Copyright 2009-2017, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
CExpert ExtExpert;

is initialised by a constructor function - prohibit!

This is if you logically summarise the last posts

Alexey Navoykov:

Found out the reason for the discrepancy. With OPTIMIZE=0 there is an error, but not with OPTIMIZE=1. I always use only OPTIMIZE=0.

In the 32-bit terminal an error occurs at any OPTIMIZE

int g1( int& t[] ) { return ArraySize( t ); }
int g2( int& t[] ) { return g1( t ); }
void OnStart()
{
        int t[] = { 1, 2, 3, 4, 5 }; //всего 5
        Print( g2(t)); //Результат:          3
}
 
A100:

is initialised by a constructor function - prohibit!

This is if you logically summarise the last posts

Well, the person just expressed it incorrectly there. Of course, we are talking about an initializing value, not an initializing function.


And what the hell with them - bans. They do not cause troubles by themselves. But when this prohibition is not controlled by the compiler in any way but generates an algorithm that bypasses language rules, it does not fit at all. Now you'll have to dig through all the code looking for such things and be constantly on guard. Well, fuck that kind of programming, I'd rather stay on the old build.

 
Alexey Navoykov:
I see, I apologise then, I didn't notice straight away. I'm surprised, of course, how they managed to change it quietly and didn't tell anyone. What I don't understand is why the compiler doesn't react to an invalid operation being executed. That is, the bug is present in any case.
Why do you think it's invalid? Please use at your own discretion. The whole problem is that when a static variable is initialized by a function, the initialization is "paused" and the function is executed. And in the above example, there is still a static variable in that function that hasn't been initialized yet. Hence, a variable initialized by a function takes a different value.
 
Alexey Navoykov:

And here's more on the topic of variable initialisation. If you follow what was stated in the documentation, then you can't reference other global/static variables either. As it is not a constant expression:

However, for now it works. And I suppose many people have always used such constructs without realizing it. But it turns out that the developers may disable it at any moment. And, as in the case with function initialization, these codes will still compile successfully, but they won't work correctly. All in all, the whole MQL code is a mine mine working in slow motion.

Well... there is no discrepancy with the description in the documentation in this example... Take a good look at the quote from the documentation

A static variable can be initialized with a constant or constant-like expression appropriate to its type , unlike a simple local variable that can be initialized with any expression.


or by a constant expression

int a= 1;
int b= a+1;

This initialization is possible. Because the 'a' variable is initialized by a constant and the 'b' variable is initialized by a constant expression.

 
A100:

Then you need to prohibit static instances of classes - because they are initialized by constructor function

Pay attention to

Forum on trading, automated trading systems and trading strategies testing

Bugs, bugs, questions

Alexey Viktorov, 2017.10.18 09:19

Why do you think you have changed, why is it unacceptable? Please use at your own discretion. The whole problem is that when a static variable is initialized by a function, the initialization is "paused" and that function is executed. And in the above example, there is still a static variable in that function that hasn't been initialized yet. Hence, a variable initialized by a function takes a different value.
Unfortunately, I don't know if it is possible to declare and initialize static variables in the constructor, but I hope you will tell me about it. And, as I understand it, the initialization sequence is very important.