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

 
Alexey Navoykov:
By the way, I came to the conclusion that in the general case, if the return value type of the function is absolutely any type, the problem cannot be solved by MQL means. Requires decltype, which is not present here.

The pointer will return, but the structure - I didn't think so. It should be by reference, after all.

 
fxsaber:

It's the same.

Now 2 arguments are passed to the function instead of 3 :

MacrosFunc(SelectHandle(Memory(A, false)), SelectHandle(Memory(A)) ? B : 0)
 
Alexey Navoykov:

Now generally 2 arguments are passed to the function instead of 3 :

Two is absolutely the original problem. And three is its generalization.

Forum on trading, automated trading systems and trading strategy testing

Errors, bugs, questions

fxsaber, 2018.12.21 22:15

Let me clarify that NewHandle - can be a constant. I.e. valid call
time = MACROS(0, TimeCurrent()); // TimeCurrent из 0-хендла.
time = MACROS(1, TimeCurrent()); // TimeCurrent из 1-хендла.

MACROS(0, SymbolInfoTick(_Symbol, Tick)); // SymbolInfoTick из 0-хендла.

#define Bid SymbolInfoDouble(_Symbol, SYMBOL_BID)
Price = MACROS(3, Bid); // Bid из 3-хендла.
From the examples, it seems clear what is required from the macro.
 

The C++ standard does not define this order and leaves it up to the compiler. The MQL developer must make sure that this order can be changed or if there are any plans to change it in the future.

Therefore, this is the best way to go about it:

template <typename T1, typename T2>
T2 MacrosFunc( const T1 handle, const T2 Value )
{
  SelectHandle(Memory(handle, false));
  return(Value);
}

#define  MACROS(A, B) MacrosFunc(A,  SelectHandle(Memory(A)) ? (B) : NULL)
 
Alexey Navoykov:

The C++ standard does not define this order and leaves it up to the compiler. The MQL developer must make sure that this order can be changed or if there are any plans to change it in the future.

So this way is better:

Yes, it is more reliable.

 

I'm f*cked, dear editorial staff.

Giving code to an interpreter without understanding how he interprets that code... The high road.

 

Forum on trading, automated trading systems and trading strategy testing

Bugs, bugs, questions

fxsaber, 2018.12.21 10:23

I can't figure it out, please ask for a hint. There is such a time acquisition

int GetHandle() { return(0); }

bool SelectHandle( int ) { return(true); }
  
int NewHandle = 0;  
int PrevHandle = GetHandle();    

datetime time = SelectHandle(NewHandle) ? TimeCurrent() : 0;  
SelectHandle(PrevHandle);


How to write a macro that does the same thing

time = MACROS(NewHandle, TimeCurrent()); // Макрос


The problem is that PrevHandle is not created in the macro.

I don't understand why my solution is worse, I'll paste it here too:

//handle_t MACROS_helper_PrevHandle;
int MACROS_helper_PrevHandle;
template <typename T>
T macros_helper_fn(T t)  {SelectHandle(MACROS_helper_PrevHandle); return t;}     
#define  MACROS(NEW_HANDLE_, FN_)  ((MACROS_helper_PrevHandle=GetHandle())*0 == 0 ?     \
                                    SelectHandle(NEW_HANDLE_) ? macros_helper_fn(FN_) : 0 : 0)

Remarkable - implementation of the comma operator through the trend operator (well, nothing brilliant, but it's the first time I've encountered it).

It's not clear - why pull a handle type through a template? Would there be different types of handles, it's not a big deal. And in general the handle type will be specified via typedef/define.

 
pavlick_:

I don't see how my solution is any worse, I'll put it in here too:

Noteworthy - implementation of comma operator through trend operator (well, nothing great, but it's the first time I came across it).

It is not clear - why should we pull a handle type through a template? Would there be different types of handles, it's not a big deal. And in general the handle type will be specified via typedef/define.

It's not good at all. What is handle?

 
Алексей Тарабанов:

It's not good at all. What is handle?

The task condition was: there is some environment that changes via a descriptor (handle), and you need to implement in one macro:

1. Remember the current handle

2. Switch the environment to the new handle

3. Retrieve the required parameter from the environment

4. Switch environment to old handle

5. Return parameter

 
pavlick_:

The task condition was: there is some environment that changes via a descriptor (handle), and you need to implement in one macro:

1. Remember the current handle

2. Switch the environment to the new handle

3. Retrieve the desired parameter from the environment

4. Change environment to the old handle

5. Return parameter

Thank you for your reply.

Reason: