OOP, templates and macros in mql5, subtleties and uses - page 17

 
Taras Slobodyanik:

Do macros return values?

Well, here is a code example

#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#define  N    0.0
#define  S    0


  double Try_helper;
#define  Try(EXPR, MES)               \
   Try_helper = EXPR;                \
   if (Try_helper <= 0.0) {          \
     printf("Error: %s ", MES);      \
     return 4;                         \
   }

int OnInit()
  { int qw=S; 

   return(INIT_SUCCEEDED);}
  
 
Seric29:

Here is a code example

Macro substitution means that all that code (which is in the define), will be substituted every time in a specified place in the program. So in fact, instead of making a function and calling it, you copy this function inside the code many times, as a piece of code.

And if you makereturn from a macro, it isreturn from all the code where this macro is inserted.

 
Taras Slobodyanik:

macro substitution means that all the code (which is in the define), will be substituted each time in the specified place of the program. In fact, instead of making a function and calling it, you copy this function inside the code many times, as a piece of code.

And if you makereturn from a macro, this is return from the whole code where this macro is inserted.

It turns out that you cannot write a macro function.

 
Seric29:

It turns out that there is no way to write a macro function.

A macro substitution is replacing one text with another (in the source code), it cannot be a function by definition.

 
Write your opinion on thishttps://www.mql5.com/ru/forum/160683/page945#comment_12958065(number 9448)
 

How to write 2 macros without jumping to a new line it greatly increases the number of lines, which greatly lengthens the program, here is an example

#define  I int #define  B bool//это просто пример ни в коем случае не воспринимайте всерьёз

Macro B does not work anymore.

 

is it possible to convert a variable name into a string?

It doesn't work that way:

#define  p_func(NAME)  Print("This is ##NAME")
//+------------------------------------------------------------------+
void OnStart()
{
   int QWERTY;
   int zxcvbn;
   p_func(QWERTY);	//This is ##NAME
   p_func(zxcvbn);	//This is ##NAME
}
 
Seric29:
Guys, I want to write a macro of makros(a) and it will work like this: if a equals 1, it will return any value of int type, if a equals 2, it will return any value of double type, if a equals 3, it will return any value of bool type. Is it possible to do this?

What for? What for? Where are you going to use it?

 
Igor Makanu:

is it possible to convert a variable name into a string?

#define  p_func(NAME)  Print("This is " + #NAME)
 
Seric29:
Guys this question I want to write a macro makros(a), it will work like this: if a is equal to 1ce then the macro will return any value of type int, if a is equal to 2ke then the macro will return any value of type double, if a is equal to 3ke then the macro will return any value of type bool. Is it possible to do this?
#define  GET_HELPER_1 1
#define  GET_HELPER_2 1.0
#define  GET_HELPER_3 true
#define  GET_EXPAND(I) GET_HELPER_##I
#define  GET(I) GET_EXPAND(I)

void OnStart() {
   Alert(GET(1), "  ", GET(2), "  ", GET(3));
}

In µl, you can do without GET_EXPAND, but it's µl-specific stuff, it's more correct. Well, μl macros are very limited due to lack of comma operator (although through crutches ...).

ZZY: and it's all compile time, of course.

ZZZY: by the way, if there was a ghost operator, you could do this:

class Q{
public:
        operator int();
        operator double();
        operator bool();
};
...
Q q;
int i = q;      // call operator int
double d = q;   // call operator double
bool b = q;     // ...
Reason: