[英]Concatenation of tokens in variadic macros
In C, is it possible to concatenate each of the variable arguments in aa variadic macro? 在C中,是否可以在可变参数宏中连接每个变量参数?
Example: 例:
MY_MACRO(A, B, C) // will yield HDR_A, HDR_B, HDR_C
MY_MACRO(X, Y) // will yield HDR_X, HDR_Y
The normal ##
operator has special meaning for variadic macros (avoiding the comma for empty argument list). 正常的
##
运算符对于可变参数宏具有特殊含义(避免使用空参数列表的逗号)。 And concatenation when used with __VA_ARGS__
takes place with the first token only. 与
__VA_ARGS__
一起使用时,串联只与第一个令牌一起发生。
Example: 例:
#define MY_MACRO(...) HDR_ ## __VA_ARGS__
MY_MACRO(X, Y) // yields HDR_X, Y
Suggestions? 建议?
First, the comma rule you are mentioning is a gcc extension, standard C doesn't have it and most probably will never have it since the feature can be achieved by different means. 首先,你提到的逗号规则是gcc扩展,标准C没有它,并且很可能永远不会拥有它,因为该功能可以通过不同的方式实现。
What you are looking for is meta programming with macros, which is possible, but you'd need some tricks to achieve that. 您正在寻找的是使用宏进行元编程,这是可能的,但您需要一些技巧来实现这一点。 P99 provides you with tools for that:
P99为您提供了以下工具:
#define MY_PREFIX(NAME, X, I) P99_PASTE2(NAME, X)
#define MY_MACRO(...) P99_FOR(HDR_, P99_NARG(__VA_ARGS__), P00_SEQ, MY_PREFIX, __VA_ARGS__)
MY_PREFIX
describes what has to be done with the individual items. MY_PREFIX
描述了单个项目必须完成的工作。 P00_SEQ
declares how the items should be separated P00_SEQ
声明如何分隔项目 P99_NARGS
just counts the number of arguments P99_NARGS
只计算参数的数量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.