MetaTrader 5 Platform build 5326: Improvements and fixes - page 2

 
MetaQuotes:

On Friday, October 3, 2025, an updated version of the MetaTrader 5 desktop platform will be released.


my systems were pushed to the next version 5327
 

Found a casting bug:


Reference is documentation:

https://www.mql5.com/en/docs/basis/types/casting

Failing code example:

//+------------------------------------------------------------------+
//|                                                  Playground1.mq5 |
//|                                  Copyright 2025, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+


template <typename CUSTOM_TYPE>
const CUSTOM_TYPE test_func(const CUSTOM_TYPE p_in)
{
    CUSTOM_TYPE p_out = p_in;

    return(p_out);
}



void OnStart()
{
    const int test_val = 3;

    printf("Integer: %i", test_func(test_val));

    printf("Cast to Integer: %i", test_func((int)NULL));

    printf("Cast to Integer: %i", test_func(int(NULL)));
}



Compiler log:


Documentation on MQL5: Language Basics / Data Types / Typecasting
Documentation on MQL5: Language Basics / Data Types / Typecasting
  • www.mql5.com
Often a necessity occurs to convert one numeric type into another. Not all numeric types can be converted into another. Here is the scheme of...
 
Dominik Egert #Wrong.

NULL is not numeric. You can't and will never be able to cast NULL to an integer in MQL5.

template<typename T>
T test_func(const T p_in)
{
   return p_in;
}

void OnStart()
{
   const int test_val = 3;

   PrintFormat("Integer: %i", test_func(test_val));
   PrintFormat("Cast to Integer: %i", test_func(0));
}

   

 
Miguel Angel Vico Alba #:
You can't and will never be able to cast NULL to an integer in MQL5.

If this is not casting NULL to int, then what is it called?

 
Vladislav Boyko #:
If this is not casting NULL to int, then what is it called?
void OnStart()
  {
   Print(typename(NULL));      // void
   Print(typename((int)NULL)); // int
  }
 
Vladislav Boyko #:

Exactly. - And, by documentation, the function-like casting is very well supported, but failing my example case.

 
Vladislav Boyko #If this is not casting NULL to int, then what is it called?

Your example is valid, but it is not the same situation.

In your code the target type is explicitly int, so the compiler interprets NULL as zero and assigns it without issue.

In Dominik's template example the type T is deduced from the argument, and since NULL is of type void*, the compiler cannot infer T as int. That is why it fails.

So your case shows implicit conversion once the type is fixed, while Dominik's case fails because of type deduction rules. They are different mechanisms and cannot be grouped together.
 
Dominik Egert #:

Failing code example:

I made your example work by adding a couple of extra parentheses.


 
Vladislav Boyko #:

I made your example work by adding a couple of extra parentheses.


Actually no. The failing expression is the function-like casting notation. Not the "c/c++" standard casting notation. That is working.
 
Dominik Egert #Found a casting bug
False bug report. The error is due to poor programming by the OP. The documentation itself specifies and clarifies this.

It is stated here in the documentation:

"The predefined constant variable NULL is of the void type. It can be assigned to variables of any other fundamental types without conversion. The comparison of fundamental type variables with the NULL value is allowed." (See section: Void Type and NULL Constant, MQL5 Reference)

But that only covers assignment and comparison, not its use inside arithmetic expressions or template type deduction.

In expressions or templated functions, the compiler requires a concrete type. Since NULL is of type void, it cannot be deduced as int, double, or any other numeric type, which is why the compilation error occurs.

Example only to illustrate that NULL is valid syntactically for pointer types, not for numeric use:

template <typename T>
T *test_ptr(T *p) { return p; }

void OnStart()
{
   printf("%p", test_ptr((int*)NULL));
}
The compilation error in this code confirms what is explained: NULL cannot be used with numeric types nor deduced in templates.