66 Macro variables
Macro reassignment
A macro can be redefined (or “reassigned”) making it like a variable that changes value during the execution of the xcpp preprocessor. No warnings are emitted by xcpp when a macro is redefined:
| Before translation | After translation |
|---|---|
| |
Even though @def can be used to redefine a macro, making it like a variable available during the execution of the xcpp processor, it is not generally suitable for this purpose because the macro is always local to the scope in which the @def directive appears. In the following example, there is no redefinition of m, because the body of the @if directive creates a local scope for macro definitions:
| Before translation | After translation |
|---|---|
| |
In any case there would be problems with infinite recursion. The following crashes the xcpp preprocessor with a stack overflow because the redefinition of m is imposed before processing its body (this is intentional because recursion can be very useful – see section 14):
// Crashes the xcpp preprocessor!
@def int m = 1
@def int m = m+1
int f() { return m; }