bitchy preprocessor ..

 

This does not compile:

#define _arWsz(A)        #A+"[] sz: " + (string(ArraySize(A)))

//  print array with array name
template<typename T>
int ArrayPrt(T &arr[], const string lne) {
   int i = ArraySize(arr);    
   string s = _arWsz(arr); 
   string S = #arr+"[] sz: " + (string(ArraySize(arr))); // line 19
   Print(lne,"  ",s);
   if (i>0) ArrayPrint(arr);
   return(i);
}

'#arr' - invalid preprocessor command    test_PreProc.mq5    19    15

But this does and it works as intended:

#define _arWsz(A)        #A+"[] sz: " + (string(ArraySize(A)))

//  print array with array name
template<typename T>
int ArrayPrt(T &arr[], const string lne) {
   int i = ArraySize(arr);    
   string s = _arWsz(arr); // #define _arWsz(A)        #A+"[] sz: " + (string(ArraySize(A)))
   //string S = #arr+"[] sz: " + (string(ArraySize(arr))) + " \t";
   Print(lne,"  ",s);
   if (i>0) ArrayPrint(arr);
   return(i);
}

'test_PreProc.mq5'    test_PreProc.mq5    1    1
code generated        1    1
0 errors, 0 warnings, 107 msec elapsed        1    1

Why?


Here the script to test:

//+------------------------------------------------------------------+
//|                                                 test_PreProc.mq5 |
//|                                                            Calli |
//|                              https://www.mql5.com/de/users/gooly |
//+------------------------------------------------------------------+
#property copyright "Calli"
#property link      "https://www.mql5.com/de/users/gooly"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
#define _arWsz(A)        #A+"[] sz: " + (string(ArraySize(A)))

//  print array with array name
template<typename T>
int ArrayPrt(T &arr[], const string lne) {
   int i = ArraySize(arr);    
   string s = _arWsz(arr); // #define _arWsz(A)        #A+"[] sz: " + (string(ArraySize(A))) + " \t"
   //string S = #arr+"[] sz: " + (string(ArraySize(arr))) + " \t";
   Print(lne,"  ",s);
   if (i>0) ArrayPrint(arr);
   return(i);
}


void OnStart()
  {
//---
  int    a[]={-5,-4,-3,-2,-1,0,1,2,3,4,5};
  double b[]={1,5,2.4,3.3};
  ArrayPrt(a,(string)__LINE__);
  ArrayPrt(b,(string)__LINE__);
   
  }
//+------------------------------------------------------------------+

This prints:

2023.07.14 09:22:19.188    test_PreProc (EURUSD,H1)    31  arr[] sz: 11
2023.07.14 09:22:19.189    test_PreProc (EURUSD,H1)    -5 -4 -3 -2 -1  0  1  2  3  4  5
2023.07.14 09:22:19.189    test_PreProc (EURUSD,H1)    32  arr[] sz: 4
2023.07.14 09:22:19.189    test_PreProc (EURUSD,H1)    1.00000 5.00000 2.40000 3.30000
 

At least this works:

//+------------------------------------------------------------------+
//|                                                 test_PreProc.mq5 |
//|                                                            Calli |
//|                              https://www.mql5.com/de/users/gooly |
//+------------------------------------------------------------------+
#property copyright "Calli"
#property link      "https://www.mql5.com/de/users/gooly"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
#define _arWsz(A)       __FUNCTION__+"["+(string)__LINE__+"] "+ #A+"[] sz: " + (string(ArraySize(A)))

//  print array with array name
template<typename T>
void ArrayPrt(T &arr[], const string inf) {
   Print(inf);
   ArrayPrint(arr);
}


void OnStart()
  {
//---
  int    a[]={-5,-4,-3,-2,-1,0,1,2,3,4,5};
  double b[]={1,5,2.4,3.3};
  ArrayPrt(a,_arWsz(a));
  ArrayPrt(b,_arWsz(b));
   
  }
//+------------------------------------------------------------------+

=>

2023.07.14 09:33:18.353    test_PreProc (EURUSD,H1)    OnStart[27] a[] sz: 11
2023.07.14 09:33:18.353    test_PreProc (EURUSD,H1)    -5 -4 -3 -2 -1  0  1  2  3  4  5
2023.07.14 09:33:18.353    test_PreProc (EURUSD,H1)    OnStart[28] b[] sz: 4
2023.07.14 09:33:18.353    test_PreProc (EURUSD,H1)    1.00000 5.00000 2.40000 3.30000

 
Carl Schreiber:

This does not compile:

But this does and it works as intended:

Why?


Here the script to test:

This prints:

This is due to macro expansion order. You must have macro extensions on the line of the macro definition, else expansion will not work, as there is no macro to expand in the first place.

Sometimes you need a macro that calls another macro just to get the expansion order correctly.

This is not only mql but all macro preprocessors limitations.

EDIT:

The "#" instruction only works on the macro definition line itself.
 
Oh, I see - stupid me.