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

 
Comments not relevant to this topic have been moved to "OOP, templates, macros in mql5, tips and tricks of the trade".
 

I don't know if it was posted here or not, but it might be useful to someone. When you want to overload a method of a parameterized class only by whether it takes an argument of the same type by value or by reference (structures are passed only by reference, simple types most often by value). It can be done in this way (and the second way exactly apply to variant without &, otherwise basic typing by structures will not compile)

template<typename T>
class A
 {
public:
  A* operator<<(T&p){ Print("<< &",typename(T)); return &this; }
  template<typename F>
  A* operator<<(F p){ Print("<< ",typename(F)); return &this; }
 };

void OnStart()
 {
    double ask=Ask,bid=Bid;
    MqlTick mt;
    A<double> a1;
    a1<<Ask<<Bid<<ask;
    A<MqlTick> a2;
    a2<<mt; 
 }

Discussion here.

 
Ilya Malev:

I don't remember if it was here or not, but it may be useful to someone. When you want to overload a method of a parameterized class by value only, or by reference only (structures are passed by reference only, simple types most often by value). This can be done in this way (moreover, the second template exactly apply to the variant without &, otherwise the basic typing by structures will not compile)

Then it turns out that a class designed for a structure type can accept all sorts of numeric types. And what to do with them?

 
Alexey Navoykov:

Then it turns out that a class designed for a structure type can accept all sorts of numeric types. And what to do with them?

In this case, the class is intended for mixed types - for both structures and simple types, otherwise this construction makes no sense. To avoid taking unnecessary types in F, you can write a check there for typename(F), sizeof(F), etc.

 
Ilya Malev:

In this case class is intended for mixed type - both for structures and simple types, otherwise this construction makes no sense. In order not to accept unnecessary types in F, you can write a check for typename(F), sizeof(F), etc. there.

The typename will not protect from accepting unnecessary types) For this, you need to make an overloaded private method with an appropriate type.

And if the class is intended for any type, why parameterize it with a template parameter, creating confusion? Although there are simpler variants for crutch, without template classes.

 
Alexey Navoykov:

if a class is intended to be of any type, why parameterise it with a template parameter, creating confusion?

So how do you take the right type, which can be any type, without committing the parameterization? You can't declare a parameter or field without specifying a type. For example, it could be a "wrapper" class on an arbitrary data type.

Alexey Navoykov:

typename won't protect against accepting unnecessary types )

If you write typename(F)==typename(T), it will protect.

 
Ilya Malev:

So how will you accept the right type, which can be anything, without passing the parametrization? You cannot declare a parameter or a field without specifying a type. For example, it can be a "wrapper" class for an arbitrary data type.

Well, then it's just class A. Why declare a pattern if its parameter is not in any way related to the class behavior?

Ilya Malev:

If you write typename(F)==typename(T), it will protect it.

This is really a cruel thing to do. Bringing the typename control to the runtime stage... Your codes will have to be debugged for years

 
Can you tell me where I can look up a regular trailing stop for a fiver?
 
Nikita Chernyshov:
Can you tell me where I can look up a regular trailing stop for a fiver?

iSAR iOsMA EA-Trailing

 
Vladimir Karputov:

iSAR iOsMA EA-Trailing

Thank you!