如何使一个可变参数宏(可变的参数个数) [英] How to make a variadic macro (variable number of arguments)
本文介绍了如何使一个可变参数宏(可变的参数个数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用C编写的宏接受任意数量的参数,而不是一个具体的数字。
I want to write a macro in C that accepts any number of parameters, not a specific number
例如:
#define macro( X ) something_complicated( whatever( X ) )
其中, X
为任意数量的参数
我需要这个,因为任何
超载,可以用2个或4个参数来调用。
I need this because whatever
is overloaded and can be called with 2 or 4 parameters.
我试图限定两次宏,但第二个定义覆盖了第一个!
I tried defining the macro twice, but the second definition overwrote the first one!
我正在使用的编译器是G ++(更具体地说,MinGW的)
The compiler I'm working with is g++ (more specifically, mingw)
推荐答案
C99的方式,也被VC ++编译器的支持。
C99 way, also supported by VC++ compiler.
#define FOO(fmt, ...) printf(fmt, ##__VA_ARGS__)
这篇关于如何使一个可变参数宏(可变的参数个数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文