如何制作可变参数宏(可变数量的参数) [英] 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
是任意数量的参数
where X
is any number of parameters
我需要这个,因为 whatever
被重载并且可以用 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屋!
查看全文