C宏:有条件地连接符号(C macro: concatenate symbols conditionally)

系统教程 行业动态 更新时间:2024-06-14 16:57:17
C宏:有条件地连接符号(C macro: concatenate symbols conditionally)

我有

#define A_T 1 #define B_T 2 int x_a = 1, x_b =2;

如何定义一个宏,它可以将后缀_a和_b连接到var名称?

for example, something like this #define A_T_SUF _a #define B_t_SUF _b #define SUFFIX(t) t ## _SUF #define VAR_SUF(var, t) var ## SUFFIX(t) ..... VAR_SUF(x, A_T) ---> be replaced to x_a

这可能吗?

I have

#define A_T 1 #define B_T 2 int x_a = 1, x_b =2;

How can I define a macro, which can concatenate the suffix _a and _b to the var name?

for example, something like this #define A_T_SUF _a #define B_t_SUF _b #define SUFFIX(t) t ## _SUF #define VAR_SUF(var, t) var ## SUFFIX(t) ..... VAR_SUF(x, A_T) ---> be replaced to x_a

Is this possible?

最满意答案

您需要VAR_SUF宏中的额外间接,以强制它在连接令牌之前评估被调用的宏,而不是先连接:

#define A_T_SUF _a #define B_t_SUF _b #define SUFFIX(t) t ## _SUF #define CAT(a, b) a ## b #define XCAT(a, b) CAT(a, b) #define VAR_SUF(var, t) XCAT(var, SUFFIX(t)) ..... VAR_SUF(x, A_T) ---> be replaced to x_a

没有额外的间接, VAR_SUF(x, A_T)将扩展为xSUFFIX(A_T) (首先连接,然后寻找更多的宏)。 使用额外的CAT / XCAT间接,它将首先扩展SUFFIX(A_T)然后连接。

XCAT是EXPAND_AND_CONCATENATE ,而CAT只是CONCATENATE (没有扩展。)

编辑

如果A_T也是一个宏(例如,# #define A_T 1 ),那么它将首先被替换。 您可以通过删除SUFFIX宏中的##的间接来避免这种情况:

#define A_T_SUF _a #define B_t_SUF _b #define CAT(a, b) a ## b #define XCAT(a, b) CAT(a, b) #define VAR_SUF(var, t) XCAT(var, t##_SUF) ..... VAR_SUF(x, A_T) ---> be replaced to x_a

这将导致首先发生连接,然后扩展宏,然后将发生其他连接

如果x也是一个宏,那么你有一个问题,因为在连接它们之前没有好的方法来扩展一个令牌而不是另一个令牌。

You need an extra indirection in the VAR_SUF macro to force it to evaluate the called macros before concatenating the tokens instead of concatenating first:

#define A_T_SUF _a #define B_t_SUF _b #define SUFFIX(t) t ## _SUF #define CAT(a, b) a ## b #define XCAT(a, b) CAT(a, b) #define VAR_SUF(var, t) XCAT(var, SUFFIX(t)) ..... VAR_SUF(x, A_T) ---> be replaced to x_a

without the extra indirect, VAR_SUF(x, A_T) would expand to xSUFFIX(A_T) (concatenate first, then look for more macros). With the extra CAT/XCAT indirection, it will expand SUFFIX(A_T) first and then concatenate.

XCAT is short for EXPAND_AND_CONCATENATE, while CAT is just CONCATENATE (without expansion.)

edit

If A_T is also a macro (eg, #define A_T 1) then it will be replaced first. You can avoid that by removing the indirection of the ## in the SUFFIX macro:

#define A_T_SUF _a #define B_t_SUF _b #define CAT(a, b) a ## b #define XCAT(a, b) CAT(a, b) #define VAR_SUF(var, t) XCAT(var, t##_SUF) ..... VAR_SUF(x, A_T) ---> be replaced to x_a

This will cause that concatenation to happen first, then macros will be expanded, then the other concatenation will happen

If x is also a macro, then you have a problem, as there's no good way to expand one token and not the other before you concatenate them.

更多推荐

本文发布于:2023-04-12 19:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/4e512150b6f54fce47f08e1f41207ddd.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有条件   符号   macro   conditionally   symbols

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!