Macro replacement list rescanning for replacement
Clash Royale CLAN TAG#URR8PPP
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
c macros preprocessor
add a comment |
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
c macros preprocessor
add a comment |
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
c macros preprocessor
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
c macros preprocessor
c macros preprocessor
asked Jan 15 at 8:01
Some NameSome Name
968314
968314
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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]
add a comment |
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54194759%2fmacro-replacement-list-rescanning-for-replacement%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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]
add a comment |
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]
add a comment |
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]
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]
answered Jan 15 at 8:10
AngewAngew
132k11252343
132k11252343
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 15 at 8:17
P.WP.W
13.2k31245
13.2k31245
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54194759%2fmacro-replacement-list-rescanning-for-replacement%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown