"x" 在典型的 xstr 宏中是什么意思?
What does "x" mean in the typical xstr macro?
常见的做法是定义字符串化宏,例如:
#define str(token) #token
#define xstr(token) str(token)
为什么通常使用 x
前缀? "x"有什么意义吗?
(我猜它可能代表 "expanded"(如果有的话),但我没有证据支持这一点。)
当我在宏或函数调用名称前使用 x
时,我用它来表示 扩展 用法,我想我并不孤单。
对于您的示例,使用 str
将 token
字符串化,但应该使用 xstr
进行扩展使用,即扩展宏参数并将结果字符串化。
为了比较,当大多数时间只使用一个宏而另一个用于内部实现时,我会做以下事情:
#define _str(token) #token
#define str(token) _str(token)
正如其他人所建议的,x
似乎用于 extended 或者我也看到 expanded 使用以及。 GNU 在 Stringizing 上的一篇文章中提到了这种用法。
他们将其表述为:
"If you want to stringize the result [...],
you have to use two levels of macros."
#define xstr(s) str(s)
#define str(s) #s
#define foo 4
str (foo)
→ "foo"
xstr (foo)
→ xstr (4)
→ str (4)
→ "4"
他们继续声明:
"s is stringized when it is used in str, so it is not macro-expanded
first. But s is an ordinary argument to xstr, so it is completely
macro-expanded before xstr itself is expanded (see Argument Prescan).
Therefore, by the time str gets to its argument, it has already been
macro-expanded."
虽然 none 是具体的,但我认为您可以安全地假设 x 是 expanded/extended 用法。
常见的做法是定义字符串化宏,例如:
#define str(token) #token
#define xstr(token) str(token)
为什么通常使用 x
前缀? "x"有什么意义吗?
(我猜它可能代表 "expanded"(如果有的话),但我没有证据支持这一点。)
当我在宏或函数调用名称前使用 x
时,我用它来表示 扩展 用法,我想我并不孤单。
对于您的示例,使用 str
将 token
字符串化,但应该使用 xstr
进行扩展使用,即扩展宏参数并将结果字符串化。
为了比较,当大多数时间只使用一个宏而另一个用于内部实现时,我会做以下事情:
#define _str(token) #token
#define str(token) _str(token)
正如其他人所建议的,x
似乎用于 extended 或者我也看到 expanded 使用以及。 GNU 在 Stringizing 上的一篇文章中提到了这种用法。
他们将其表述为:
"If you want to stringize the result [...], you have to use two levels of macros."
#define xstr(s) str(s)
#define str(s) #s
#define foo 4
str (foo)
→ "foo"
xstr (foo)
→ xstr (4)
→ str (4)
→ "4"
他们继续声明:
"s is stringized when it is used in str, so it is not macro-expanded first. But s is an ordinary argument to xstr, so it is completely macro-expanded before xstr itself is expanded (see Argument Prescan). Therefore, by the time str gets to its argument, it has already been macro-expanded."
虽然 none 是具体的,但我认为您可以安全地假设 x 是 expanded/extended 用法。