cpp: Self-Referential Macros

1 
1 3.10.5 Self-Referential Macros
1 ------------------------------
1 
1 A "self-referential" macro is one whose name appears in its definition.
1 Recall that all macro definitions are rescanned for more macros to
1 replace.  If the self-reference were considered a use of the macro, it
1 would produce an infinitely large expansion.  To prevent this, the
1 self-reference is not considered a macro call.  It is passed into the
1 preprocessor output unchanged.  Consider an example:
1 
1      #define foo (4 + foo)
1 
1 where 'foo' is also a variable in your program.
1 
1    Following the ordinary rules, each reference to 'foo' will expand
1 into '(4 + foo)'; then this will be rescanned and will expand into '(4 +
1 (4 + foo))'; and so on until the computer runs out of memory.
1 
1    The self-reference rule cuts this process short after one step, at
1 '(4 + foo)'.  Therefore, this macro definition has the possibly useful
1 effect of causing the program to add 4 to the value of 'foo' wherever
1 'foo' is referred to.
1 
1    In most cases, it is a bad idea to take advantage of this feature.  A
1 person reading the program who sees that 'foo' is a variable will not
1 expect that it is a macro as well.  The reader will come across the
1 identifier 'foo' in the program and think its value should be that of
1 the variable 'foo', whereas in fact the value is four greater.
1 
1    One common, useful use of self-reference is to create a macro which
1 expands to itself.  If you write
1 
1      #define EPERM EPERM
1 
1 then the macro 'EPERM' expands to 'EPERM'.  Effectively, it is left
1 alone by the preprocessor whenever it's used in running text.  You can
1 tell that it's a macro with '#ifdef'.  You might do this if you want to
1 define numeric constants with an 'enum', but have '#ifdef' be true for
1 each constant.
1 
1    If a macro 'x' expands to use a macro 'y', and the expansion of 'y'
1 refers to the macro 'x', that is an "indirect self-reference" of 'x'.
1 'x' is not expanded in this case either.  Thus, if we have
1 
1      #define x (4 + y)
1      #define y (2 * x)
1 
1 then 'x' and 'y' expand as follows:
1 
1      x    ==> (4 + y)
1           ==> (4 + (2 * x))
1 
1      y    ==> (2 * x)
1           ==> (2 * (4 + y))
1 
1 Each macro is expanded when it appears in the definition of the other
1 macro, but not when it indirectly appears in its own definition.
1