This is the mail archive of the
mailing list for the GCC project.
cpp(gcc) and recursive macros
- To: help-gcc at gnu dot org
- Subject: cpp(gcc) and recursive macros
- From: Varga Gábor <gvarga at xperts dot hu>
- Date: Tue, 19 Oct 1999 12:06:08 +0200
- Newsgroups: gnu.gcc.help,comp.lang.c,comp.lang.c++
- Organization: Xperts Software
- Xref: wodc7nx0 gnu.gcc.help:1452 comp.lang.c:19018 comp.lang.c++:25089
I have a problem with understanding how gcc (cpp) handles recursive
macros. The old-style handling is not to care about them (if recursive,
there will be endless loops); the new style is once a macro is expanded,
its recursive expansion is suppressed in the resulting text. Both is
clear. gcc works in the later style by default; all this so far is
documented nicely in info pages. But sometimes it chooses to fall back
to old-style mode, expanding recursively. Can you help me what
conditions must be fulfilled to this happen? I peeked into gcc sources,
but I failed to find the *reason*.
Here is an example; I am concerned about why name2 is expanded
recursively in the second example.
// Example 1
#define cat(a,b) a##b
#define CAT(a,b) cat(a,b)
// Recursive expansion of CAT is not done.
// Result is "CAT(x,y)", as expected.
// Example 2
#define name2(a, b) _rwname2(a,b)
#define _rwname2(a, b) a##b
#define RWGQueue(type) name2(type,RWGQueue)
#define declare(Class, type) name2(Class,declare)(type)
#define RWGQueuedeclare(type) class RWGQueue(type);
// name2 is recursive, but expanded - why?
// Result is "class RWCollectableRWGQueue;"
I'd expect the following to happen:
// From this point, I enclose the token sequence that is a
// result of expanding name2 betwen stars.
*class name2(RWCollectable, RWGQueue);*
// Now why name2 isn't suppressed?
(I tried to make a less complicated example, but everything I tried
was working as expected, except this magic one).
I tried to figure out who wrote cpp, to ask him; but I did not succeeded
with that. If you know a competent preson in this topic, who is willing
to give his e-mail, please let me know.