[英]Preprocessor replacement
These macros are including or excluding text: 这些宏包括或不包括文本:
#include <stdio.h>
#define SKIP_TEXT(text)
#define JOIN_TEXT(text) text
int main(void)
{
#define S(TEXT) "a" TEXT("b") "c" TEXT("d")
printf("%s\n", S(SKIP_TEXT));
printf("%s\n", S(JOIN_TEXT));
return 0;
}
Output: 输出:
ac
abcd
Now I'm trying to do the same without defining S
for each string to evaluate, but I don't know how to replace TEXT
with SKIP_TEXT
or JOIN_TEXT
现在,我试图在不为要评估的每个字符串定义S
情况下执行相同的操作,但是我不知道如何用SKIP_TEXT
或JOIN_TEXT
替换TEXT
#include <stdio.h>
#define SKIP_TEXT(text)
#define JOIN_TEXT(text) text
#define S(s) S_EXEC(s)
#define S_EXEC_SKIP(s) s /* Here I want to skip text */
#define S_EXEC_JOIN(s) s /* Here I want to join text */
#define S_EXEC(s) S_EXEC_##s
int main(void)
{
printf("%s\n", S(SKIP("a" TEXT("b") "c" TEXT("d")));
printf("%s\n", S(JOIN("a" TEXT("b") "c" TEXT("d")));
return 0;
}
Is there any way to evaluate the arguments from S()
? 有什么方法可以评估S()
的参数吗?
What you are asking is probably impossible, as it would require symbol redefinion inside a macro, instead of a simple macro expansion. 您要问的可能是不可能的,因为这将需要在宏内重新定义符号,而不是简单的宏扩展。
Simplest alternative would probably be: 最简单的选择可能是:
#define TEXT SKIP_TEXT
printf("%s\n", "a" TEXT("b") "c" TEXT("d"));
#define TEXT JOIN_TEXT
printf("%s\n", "a" TEXT("b") "c" TEXT("d"));
or without SKIP_TEXT
/ JOIN_TEXT
-macros: 或没有SKIP_TEXT
/ JOIN_TEXT
宏:
#define TEXT(x)
printf("%s\n", "a" TEXT("b") "c" TEXT("d"));
#define TEXT(x) x
printf("%s\n", "a" TEXT("b") "c" TEXT("d"));
#undef TEXT
is probably needed after each printf
. 每个printf
之后可能需要#undef TEXT
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.