从C 宏中删除额外的括号

Removing extra parentheses from C++ macro

本文关键字:删除      更新时间:2023-10-16

我有一堆源代码,该源代码对一堆宏调用使用了双 - 源代码,其中第二个arg和向前是打印语句的varargs。

   DEBUG((1,"here is a debug"))
   DEBUG((1,"here is %d and %d",42,43))

等。

我想编写一个可以打印args 2 -...的宏,但是我不知道如何删除额外的括号,以便我可以访问args。

即,这显然不起作用:

#define DEBUG(ignore,...) fprintf(stderr,__VA_ARGS__)

和以下偷偷摸摸的尝试也失败了('debug2未定义'(:

#define DEBUG2(ignore,...) fprintf(stderr,__VA_ARGS__)
#define DEBUG(...) DEBUG2

不编辑所有删除Parens的宏观调用,我该如何定义将执行此操作的宏?

您可以做:

#define PRINTF(unused, ...) fprintf(stderr, __VA_ARGS__)
#define DEBUG(arg) PRINTF arg