I'm reading the Standard N1570 about macro replacement and misunderstand some wording from 6.10.3.4
.
1 After all parameters in the replacement list have been substituted and # and ## processing has taken place, all placemarker preprocessing tokens are removed. The resulting preprocessing token sequence is then rescanned, along with all subsequent preprocessing tokens of the source file, for more macro names to replace
So after all #
and ##
are resolved we rescan the replacement list. But the section 2 specifies:
2 If the name of the macro being replaced is found during this scan of the replacement list (not including the rest of the source file’s preprocessing tokens), it is not replaced. Furthermore, if any nested replacements encounter the name of the macro being replaced, it is not replaced.
It looks contradictory to me. So what kind of replacement possible in that rescan? I tried the following example:
#define FOOBAR(a, b) printf(#a #b)
#define INVOKE(a, b) a##b(a, b)
int main() {
INVOKE(FOO, BAR); //expands to printf("FOO" "BAR")
}
So INVOKE(FOO, BAR)
expands to FOOBAR(FOO, BAR)
after substitution of ##
. Then the replacement list FOOBAR(FOO, BAR)
is rescanned. But the section 2.
specifies that the name of the macro being replaced (FOOBAR
) is found (yes, defined above) it is not replaced (but actually replaced as can be seen in th demo).
Can you please clarify that wording? What did I miss?
LIVE DEMO
The (original) macro being replaced is not FOOBAR
, it's INVOKE
. When you're expanding INVOKE
and you find FOOBAR
, you expand FOOBAR
normally. However, if INVOKE
had been found when expanding INVOKE
, it would no longer be expanded.
Let's take the following code:
#define FOOBAR(a, b) printf(#a #b)
#define INVOKE(a, b) e1 a##b(a, b)
int main() {
INVOKE(INV, OKE);
}
I added the e1
to the expansion of INVOKE
to be able to visualise how many expansions happen. The result of preprocessing main
is:
e1 INVOKE(INV, OKE);
This proves that INVOKE
was expanded once and then, upon rescanning, not expanded again.
[Live example]
Consider the following simple example:
#include<stdio.h>
const int FOO = 42;
#define FOO (42 + FOO)
int main()
{
printf("%d", FOO);
}
Here the output will be 84.
The printf
will be expanded to:
printf("%d", 42 + 42);
This means that when the macro FOO
is expanded, the expansion will stop when the second FOO
is found. It will not be further expanded. Otherwise, you will end up with endless recursion resulting in: 42 + (42 + (42 + (42 + ....)
Live demo here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With