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 translationAfter translation
@def m = "hello world"
const char* f() { return m; }
@def m = 200
int g() { return m; }


const char* f() { return "hello world"; }
int g() { return 200; }

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 translationAfter translation
@def m = 1
@if (m > 0)
{
    @def m = m+1
}
int f() { return m; }






int f() { return 1; }

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; }