Errors, bugs, questions - page 3101

 

In b3095 I run a script:

template <typename T>
void f1(T* const Ptr) {Print(__FUNCSIG__);}

template <typename T>
void f1(T* & Ptr) {Print(__FUNCSIG__);}


class X {};

void OnStart()
  {
//---
   const X* Ptr = new X;
   
   f1(Ptr);                               //void f1<const X>(const X*&)
   f1<const X>(Ptr);                      //void f1<const X>(const X*&)
   
   //дальше непонятно!
   f1<const X>((const X*) Ptr);           //void func_902::f1<const X>(const X*const)
   f1<const X>((const X* const) Ptr);     //void func_902::f1<const X>(const X*const)
   f1<const X>((X*)Ptr);                  //void func_902::f1<const X>(const X*const)
 
   delete Ptr;
  }

If you remove the overload f1(T* & Ptr), all 5 calls return void f1<const X>(const X*const).

Is this some kind of feature?

 

Please explain:

Line *1* gives a compiler error - expected.

But line *2* compiles and works fine. Why?

int f(int & p) {return p;}

class X
  {
public:
   const int         i;
                     X(){}
        /*1*/      //X(X& x) : i(f(x.i)) {f(x.i);}  //'i' - constant variable cannot be passed as reference
        /*2*/        X(X& x) : i(f(x.i)) {}          //OK
  };

void OnStart()  {}
 
mktr8591 #:

Please explain:

Line *1* gives a compiler error - expected.

But line *2* compiles and works fine. Why?

You need to show a clear contradiction (that you changed const value):

int f( int &p ) { return p = !p; }
struct X {
        const int i;
        X( int i ) : i( i )      {}
        X( X& x  ) : i( f(x.i) ) {}
};
void OnStart()
{
        X x1( 1 );
        const int i =  x1.i;
                                { X x2 = x1; }
        Print(    i == x1.i );  //Результат: false - не может быть
}
And you suggest that both experienced Users and Developers should guess
 
A100 #:

value change

Forum on trading, automated trading systems and trading strategies testing

Peculiarities of mql5 language, subtleties and tricks

My idea is to use mql5 as a forex trading system, and I would like to use it in my own practice.

Is it possible to change the fieldsof a const objectof a class or to call its non-const methods? -You can!
template <typename T>
T GetMe( const T Ptr )
{
  return((T)Ptr);
}

class A
{
public:  
  int i;
};

void OnStart()
{
  const A a;

  GetMe(&a).i = 1;
  
  Print(a.i); // 1
}

Don't like this chip myself. Thought I was insured against unauthorised access. Bummer, though! It doesn't work with const structures, of course. So keep this loophole in mind.


 
A100 #:

You need to show a clear contradiction (that you have changed the value of const):

And you suggest that both experienced Users guess and Developers
I didn't think to describe it in such detail...
 
fxsaber #:
Yes. But in your example you have an explicit (via a function) conversion of const T to T - i.e. a "legalised" loophole.
 
mktr8591 #:
Yes. But in your example you have an explicit (via a function) conversion of const T to T - i.e. a "legalised" loophole.
((A*)(&a)).i = 1;    
 
fxsaber #:
similarly - (const A*) convert to A*.
 
mktr8591 #:
similarly - (const A*) convert to A*.
((A)a).i = 1;
 
fxsaber #:

Your example explicitly converts const to non const, and there it is clean