Bug: Const method + Copy constructor

 

This code reproduces the bug:

class C {
public:     
   C () {}    
   C (C const &) {}
   C foo() const {
     return C();
   }
};

int OnInit() {
  C c;
  return INIT_SUCCEEDED;
}

There are 2 workarounds for this code, you either remove the const qualifier

from the method foo or create a temporary object before returning it.

// Workaround 1
class C {
public:     
   C () {}    
   C (C const &) {}
   C foo() {
     return C();
   }
};

// Workaround 2
class C {
public:     
   C () {}    
   C (C const &) {}
   C foo() const {
     C c;
     return c;
   }
};
 
The bug is present in the latest build 3003.
 
The code I posted does not compile in any build of metatrader 5 for windows, I did not check other platforms.
 
Use pointers. This behaviour is present at least since 2017, good luck if you want it to be fixed.
 
Alain Verleyen:
Use pointers. This behaviour is present at least since 2017, good luck if you want it to be fixed.

I created this thread to alert the devs and other programmers coming from C++, this behaviour can generate cryptic errors.

I used the workaround I posted since I was implementing C++'s iterators and pointers made no sense in that context,

it worked but took sometime to understand why such simple code wasn't compiling.

 
Alexandre Borela:

This code reproduces the bug:

There are 2 workarounds for this code, you either remove the const qualifier

from the method foo or create a temporary object before returning it.

Not sure if that alters your original intension, is it?

class C {
public:     
   C () {}    
   C (const C &) {}
   C foo() const {
     return new C();
   }
};

int OnInit() {
  C c;
  return INIT_SUCCEEDED;
}
 
Amir Yacoby:

Not sure if that alters your original intension, is it?

The new operator would return a pointer C*, it would not work as I would have to delete it to prevent memory leaks

which could be solved by returning an SharedPointer but it would come back to the same problem, if a method has

the const qualifier, the compiler does not reconize that:

C foo() const {
  return C();
}

// The workaround.
C foo() const {
  C tmp;
  return tmp;
}

Are equivalent but only the second one works.

An use case for returning copies instead of pointers are C++ iterators, I was able to implement them in MQL5 using

the workaround and the usage got pretty close to the C++ ones, but this bug was puzzling so I created this topic

mainly to alert others that might be having compilation issues on bigger projects.

 
Alexandre Borela:

The new operator would return a pointer C*, it would not work as I would have to delete it to prevent memory leaks

How about this(double meaning..)? Didn't check if it returns the pointer, but seems unlikely

class C {
public:     
   C () {}    
   C (const C &) {}
   C foo() const {
     return this;
   }
};

int OnInit() {
  C c;
  return INIT_SUCCEEDED;
}
 
Amir Yacoby:

How about this(double meaning..)? Didn't check if it returns the pointer, but seems unlikely

That works in the context of the example and I indeed used it when overloading the operator+=, but with the operator+ I needed to return a copy to allow

other operations with iterators to work as it would on C++.


The class C example was just the to isolate the bug.

Reason: