Inside macro definitions, two special operators can be used:
In the examples in this book, we often used the following macro:
#define PRT(A) Print(#A, "=", (A)) |
It calls the Print function, in which the passed expression is displayed as a string thanks to #A, and after the sign "equal", the actual value of A is printed.
To demonstrate '##', let's consider another macro:
#define COMBINE(A,B,X) A##B(X) |
With it, we can actually generate a call to the SQN macro defined above:
Print(COMBINE(SQ,N,2)); // 4 |
The literals SQ and N are concatenated, after which the macro SQN expands to ((2)*(2)) and produces the result 4.
The following macro allows you to create a variable definition in code by generating its name given the parameters of the macro:
#define VAR(TYPE,N) TYPE var##N = N |
Then the line of code:
VAR(int, 3); |
is equivalent to the following:
int var3 = 3; |
Concatenation of tokens allows the implementation of a loop shorthand over the array elements using a macro.
#define for_each(I, A) for(int I = 0, max_##I = ArraySize(A); I < max_##I; ++I)
|