更新时间:2023-01-23 19:52:40
您需要另一个宏,因为宏扩展不会在附近进行#
或 ##
:
You need another macro because macro expansion does not take place near #
or ##
:
#define NUM_ARGS() 2
#define MYMACRO0(...) "no args"
#define MYMACRO1(...) "one arg"
#define MYMACRO2(...) "two args"
#define MYMACRO_AUX(num,...) MYMACRO##num(__VA_ARGS__)
#define MYMACRO(num,...) MYMACRO_AUX(num, __VA_ARGS__)
#define GENERATE(...) MYMACRO(NUM_ARGS(),__VA_ARGS__)
#include <stdio.h>
int main(void)
{
puts(GENERATE(0, 1));
return 0;
}
如果这是您要尝试执行的操作,但复杂的预处理器技巧是就像别人已经说过的那样,不是很安全,除非确实需要,否则不要这样做。
If this is what you're trying to do, but complicated preprocessor tricks are not really safe, as others already said, don't do it unless you really have to.