使用可变参数和后期扩展创建字符串化宏参数列表

标签 c macros stringification variadic-macros boost-preprocessor

我有以下问题 - 给定可变数量的宏参数 argX 以创建字符串化参数列表 #argX

例子:

LIST(A, B) -> "A", "B"
LIST(A, B, C) -> "A", "B", "C"

我使用的是 Boost,所以上面的宏并不太难实现,对每个参数数量使用辅助宏并将 LIST(...) 分派(dispatch)到适当的 LIST_n(arg1, ... argn)。

当 LIST 的输入本身就是宏时,问题就开始了。在那种情况下(如果我使用 ... 和 __VA_ARGS__),宏在被字符串化之前得到扩展,给出:

#define A 10
LIST(A, B) -> "10", "B"

我希望它能与 Windows header 中定义的宏一起使用,并且那里的大多数值都是宏(MB_OK、AF_INET 等),所以我得到的只是一个字符串化数字列表。

当不使用 __VA_ARGS__ 时一切正常:

#define A 10
#define LIST_1(arg0) #arg0
LIST_1(A) -> "A"

我已经尝试了几个将 __VA_ARGS__ 的扩展推迟到稍后时间的宏(例如,直到 LIST_1,它没有可变参数),但没有任何效果。

这甚至可以使用 C 预处理器来实现吗?

最佳答案

很抱歉,现在可以在 msvc 上执行此操作。由于其预处理器中的经典错误(请参阅 herehere ),他们将 __VA_ARGS__ 视为单个参数。要将其分解为单独的参数,需要应用另一次扫描,然后也将扩展宏。在 C99 预处理器上,您可以使用空占位符禁止 __VA_ARGS__ 的扩展:

/* This counts the number of args */
#define NARGS_SEQ(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N
#define NARGS(...) NARGS_SEQ(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1)

/* This will let macros expand before concating them */
#define PRIMITIVE_CAT(x, y) x ## y
#define CAT(x, y) PRIMITIVE_CAT(x, y)

/* p is an empty placeholder used to inhibit the expansion of __VA_ARGS__ */
#define STRINGIZE_ALL(p, ...) FIRST(NARGS(__VA_ARGS__), PRIMITIVE_STRINGIZE_ALL(p ## __VA_ARGS__,~,~,~,~,~,~,~,~))
#define PRIMITIVE_STRINGIZE_ALL(x1, x2, x3, x4, x5, x6, x7, x8, ...)  #x1, #x2, #x3, #x4, #x5, #x6, #x7, #x8

/* Retrieve the first n arguments from __VA_ARGS__ */
#define FIRST(n, ...) CAT(FIRST_, n)(__VA_ARGS__,~,~,~,~,~,~,~,~)
#define FIRST_1(x1, ...) x1
#define FIRST_2(x1, x2, ...) x1, x2
#define FIRST_3(x1, x2, x3, ...) x1, x2, x3
#define FIRST_4(x1, x2, x3, x4, ...) x1, x2, x3, x4
#define FIRST_5(x1, x2, x3, x4, x5, ...) x1, x2, x3, x4, x5
#define FIRST_6(x1, x2, x3, x4, x5, x6, ...) x1, x2, x3, x4, x5, x6
#define FIRST_7(x1, x2, x3, x4, x5, x6, x7, ...) x1, x2, x3, x4, x5, x6, x7
#define FIRST_8(x1, x2, x3, x4, x5, x6, x7, x8, ...) x1, x2, x3, x4, x5, x6, x7, x8

#define A 10
STRINGIZE_ALL(, A, B)

这将适用于 gcc 和 clang 3.4 或更高版本的最多 8 个参数。

关于使用可变参数和后期扩展创建字符串化宏参数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20871556/

相关文章:

python - 从 Cython 将原始指针传递给 C 函数

c - ARM 链接器错误 : Heap was used, 但未定义堆区域

c-preprocessor - 编译器看到这个宏是什么?

macros - Lisp 宏的问题

visual-c++ - Visual-C++ 和 gcc 之间宏 ## 连接运算符的差异

javascript - 在 Javascript 中,如果有一个对象有很多函数属性,你如何将它们转换为字符串数组(函数名称)?

c - 如何将 C 中分配的字符串返回给 Ada 并在 Ada 中释放它?

c - PIC16f877a 微 Controller 是否遵循小端字节排序系统?

c - 设置 MACRO 的范围

java - 为什么Java没有宏?