Typing question - page 2

 
Ilya Malev:

Well, in other words you have repeated what I wrote. The question was not if it can or can't, but why it can't and how we can elegantly work around this

just write functions for conversions and don't bother with switch inside methods?

double Double(CParameter<double>)

string String(CParameter<double>)

and use :

PrintFormat("%s %s %f",String(param1),String(param2),Double(param1));

What is the difference between using a unary operator or a named function? A function is better - an operator already has a semantic load and you don't want to change it at will.

 
fxsaber:

Type control is lost. Look at the C++ resources for answers to these questions. I think they are asked quite often.

Well, at least it's clear that this is not a limitation of mql, but a feature of C++ in general, and thank you for that :)

 
Is the inability to overload operators without linking the lparam of the overloaded operator to a specific object or structure also a feature of C++?
 
Ilya Malev:

With explicit overloading and arithmetic operations everything is clear. Yes, overloaded = is clearly better in this case, you're right, I was just hastily building an example for a question and didn't really think about it (although there number is not only int, but also char, uchar, short, ushort, uint, bool and color, so not everything is unambiguous )))))))


The issue here is that the type of theoverloaded method's return value depends on the object's content.

For example, we have a multi-type array (I won't cite examples, because there are a lot of books). And of course, any array must have the [] indexing operation which returns the value of a variable with an appropriate index.

But variables have different types. It could be datetime or string or even some user-defined type under this index. And ideally, I would like the return value type to automatically produce the appropriate type when indexing [], without having to explicitly parameterize it. I used to solve this "problem" this way: var[(char)1], var[(short)1], var[(uint)1], etc., but these crutches are useless.

Use an array of structures like mqlparam.

 
Dmitry Fedoseev:

An array of structures like mqlparam.

It can't be equated to double, string or long in the same way.

MqlParam par;

par.double_value = Ask;

double d = par; // '=' - illegal operation use

 
Ilya Malev:

You can't equate it to a double, string or long in the same way.

MqlParam par;

par.double_value = Ask;

double d = par; // '=' - illegal operation use

You see what type it is and assign it to the appropriate field. It seems to be a crooked approach and you are still looking for a perfect solution. Such a task shouldn't arise at all.

 
Ilya Malev:
Is the impossibility of overloading operators without linking the overloaded operator to a specific object or structure also a C++ feature?

This is what the internet says about this:

Overloading of functions is not possible if they differ only in the type of the return value.

C++14 standard 13.1/2:

 

I found this on the net in answers to a similar question:

template<typename T> T f();
template<> int f() { return 2; }
template<> double f() { return 2.7; }

//...
struct F {
    F() {}
    template<typename T> operator T() { return f<T>(); }
};

int x2 = F();
double y2 = F();


As I understand it, the operator operator is overloaded here to type "operator T()" - it means that in C++ it is at least possible. But in mql as I understand it is not yet.

 
Ilya Malev:
a similar question: why when overloading a method (in the overloaded method signature) the return type does not appear, but only the parameter types. i.e. you cannot define two identical methods with different return value types. why is this restriction made? what is the point in this, why cannot overload a method by the return value type with identical parameters

This is probably why:

int f() {
   call_something();
   return 0;
}

double f() {
   call_something_other();
   return 0;
}

void start() {
   f();
}

Which function should be called exactly?

 
pavlick_:

That's probably why:

Which function should be called exactly?

A template mismatch error should probably occur at compile time.


But in the situation when there is an array object

class Array{

public:

Array *operator[] (int i){ id = i; return GetPointer( this ); }

double operator[]( int i){ id = i; return data[i]; }

Array *operator=(double d){ data[id]=d; return GetPointer( this ); }

private:

double data[10];

int id;

};



int OnStart(){

  Array array;

  double d=123.456;

  array[5]=d;

  d=array[5];

}

such error shouldn't occur because in first case call of Array[int] operation is used as the left parameter = and is not variable double, but in second case it is the right one, and the left parameter is variable of double type.

So, all in all it comes down to wish to introduce into mql possibility of overloading typing operation (including implicit), i.e. define context of method call and depending on expected in this context type of returned value, call the required code. And if the context is not defined explicitly, it will generate an error.

Reason: