C preprocessor macro expansion -
i have difficulty understanding how rewriting rules applied c preprocessor in following context. have following macros:
#define _a(x) "a" _##x #define _b(x) "b" _##x #define x(x) _##x the idea each of these macros uses concatenation create new expression, can macro — if macro, i'd expanded:
now, following expands expect:
x(x) expands _x x(a(x)) expands "a" _x x(a(b(x))) expands "a" "b" _x however, once same macro used more once, expansion stops:
x(a(a(x))) expands "a" _a(x), expected "a" "a" _x x(b(b(x))) expands "b" _b(x), expected "b" "b" _x x(a(b(a(x)))) expands "a" "b" _a(x), expected "a" "b" "a" _x x(a(b(a(b(x))))) expands "a" "b" _a(b(x)), expected "a" "b" "a" "b" _x i guess there sort of "can expand same-named macro once" rule @ play here? there can macros expand way want?
c99 draft says there's no recursion permitted in macro expansion:
6.10.3.4 rescanning , further replacement
- after parameters in replacement list have been substituted ,
#,##processing has taken place, placemarker preprocessing tokens removed. resulting preprocessing token sequence rescanned, along subsequent preprocessing tokens of source file, more macro names replace.- if name of macro being replaced found during scan of replacement list (not including rest of source file’s preprocessing tokens), it not replaced. furthermore, if nested replacements encounter name of macro being replaced, not replaced. these nonreplaced macro name preprocessing tokens no longer available further replacement if later (re)examined in contexts in macro name preprocessing token otherwise have been replaced.
so x(a(a(x))) expands "a" _a(x), expansion not expanded, you've seen.
Comments
Post a Comment