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

 
PositionSelect resets all PositionGet data to zero if unsuccessful.
 
Classic mistake
void Func( int& ) {}

void OnStart()
{
  int i;
  
  Func(true ? i : i); // '?' - parameter passed as reference, variable expected
  Func(i); // OK
}
 
fxsaber:
A classic mistake.
Wondering if this is the right mistake.
 
fxsaber:
Wondering if it's right to make a mistake.

This is correct from the point of view of correctness.

Apparently, the ternary operator does not return a reference but a variable by value (which is not logical in principle).

It turns out to be a temp variable, which mql functions with reference parameters don't like.

I.e., ternary operator and references don't make friends, in brief.

Common sense, I don't know, it makes sense.
 
Combinator:

You get a ramp variable, which mql functions with reference parameters don't like.

MqlTick Out()
{
  MqlTick Tick = {0};
  
  return(Tick);
}

void In( MqlTick& ) {}

void OnStart()
{
  In(Out());
}
Just "likes." It was because of such examples that doubts arose.
 
fxsaber:
Exactly "likes". It is because of such examples that doubts arose.

From the description of the error itself, it looks like a problem with links and non-links.

Maybe someone with more up-to-date knowledge of the language can explain.

 

fxsaber:
Classic mistake

void Func( int& ) {}

void OnStart()
{
  int i;
  
  Func(true ? i : i); // '?' - parameter passed as reference, variable expected
  Func(i); // OK
}

And if so

void Func( int& ) {}

void OnStart()
{
  int i;
  
  Func((true ? i : i));
  Func(i); // OK
}
 
Alexey Viktorov:

How about this?

It's faster to check than to make a post.

It does not work, of course.

 
fxsaber:

It's faster to check than it is to make a post.

It doesn't work, of course.

That's weird. I always use this option when opening a position with or without a take.

          trade.BuyStop(lot, buyPrice, _Symbol, 0.0, (tacke == 0.0 ? 0 : buyPrice+tacke));
          trade.SellStop(lot, sellPrice, _Symbol, 0.0, (tacke == 0.0 ? 0 : sellPrice-tacke));
 
Alexey Viktorov:

That's strange. I always use this option when opening positions with or without takeovers.

In such cases it will work even without additional brackets.
Reason: