Errors, bugs, questions - page 2419

 
Alexey Navoykov:

How would you feel about adding the ability to pass an argument as an r-value to the language? This would immediately solve all issues and allow to create universal containers for any type.

how? ) Universal containers need links and arrows, not this stuff.

And the average user isn't the one to do r-value.

 
Alexey Navoykov:

What do you think of adding the ability to pass an argument as an r-value to the language? This would immediately solve all the issues and allow to create universal containers for any type. In particular, the above method would be overloaded for r-value:

This is exactly how it is implemented in all STL containers.

And the second plus: it will allow to define move constructors. Now this is also very missing, in particular for the implementation of smart pointers unique_ptr and other classes, designed to monopoly store some unique resource inside, ie usual copy constructors are unacceptable for them.

What then is the point of passing a parameter by reference?

 
Slava:

What then is the point of passing the parameter by reference?

you are asking very strange questions. r-value links are only for moving semantics. normal links are for everything else.
 
Slava:

What's the point of passing the parameter by reference then?

I don't really understand what kind of reference you're talking about either. Originally, we were saying that l-value references (available in MQL) do not cover all needs, which was demonstrated by impossibility of passing a constant or expression value to such function. For this purpose, r-value reference is needed, which will accept all other types. Thus, combination of two overloaded functions for r-value and l-value will ensure that all types of arguments will be accepted, regardless of their origin.

What you said, that a constant is not stored anywhere, but created on the fly, it means that it should be passed as r-value, not l-value (unlike C++). It does not make a difference in principle in what form it is interpreted, the main thing is that it can be accepted in a function.

 
Alexey Navoykov:

What you said, that the constant is not stored anywhere, but is created on the fly, it means that it must be passed in the form of r-value, not l-value (unlike C++). It does not make any difference in principle in what form it is interpreted, as long as you can accept it in the function.

The r-value reference actually implies an object for which the move will be done. i.e. a temporary object must still be created.

 
TheXpert:

Well actually r-value reference implies that there is an object for which the move will be done. i.e. a temporary object has to be created anyway.

Obviously, the object always exists somewhere. I wrote that this object is created on the fly, i.e. temporary.

But I think I understood what Slava was asking. He meant why we need to accept a temporary object by reference when we can accept it by value.

Well, the point is that it is impossible to overload a function simultaneously for reference and value in one copy:

template<typename T>
 void f(T) { }
template<typename T>
 void f(T const&) { }
 
class A { };

void OnStart()
{
  A a;
  f(a);
  const int b=0;
  f(b);  // 'f' - ambiguous call to overloaded function with the same parameters
}

This makes it problematic to write universal solutions. And by replacing the value with r-value, we get a working variant:

template<typename T>
 void f(T &&) { }
template<typename T>
 void f(T const&) { }
 
Alexey Navoykov:

He meant why take a temporary object by reference when you can take it by value.

because

void f(int) {}
void f(const int&) {}

void OnStart()
{
   const int x = 0;
   f(x); // 'f' - ambiguous call to overloaded function with the same parameters
}

and because it is usually more convenient to pass something by reference than by value.

And if the method is templated (which is where it all started), then the current behaviour just doesn't allow you to write properly.

 
TheXpert:

And if the method is template-based (which is where it all started), then the current behaviour simply does not allow you to write properly.

Yes, I replaced my example with a template one to make it clearer, because the ints passed by reference really don't look very convincing)
 
Alexey Navoykov:

And by replacing the value with the r-value, we get a working variant:

not really )

move semantics implies telling the object to be moved that it doesn't need to remove its internals. if the object is constant, you would need a mutable class member, mql doesn't support that

 
TheXpert:

not really )

move semantics implies to tell the object to be moved that it doesn't need to remove its internals. if the object is a constant, you need a mutable class member, mql doesn't support this

Yes, you're right, const for r-value mustn't be set (it doesn't compile that way in pluses). I will correct it now )