I want to write a macro in C that accepts any number of parameters, not a specific number
我想在C中写一个宏,它接受任意数量的参数,而不是一个特定的数字。
example:
例子:
#define macro( X ) something_complicated( whatever( X ) )
where X
is any number of parameters
X是多少个参数
I need this because whatever
is overloaded and can be called with 2 or 4 parameters.
我需要这个,因为任何重载的东西都可以用2或4个参数调用。
I tried defining the macro twice, but the second definition overwrote the first one!
我尝试了两次定义宏,但是第二个定义覆盖了第一个定义!
The compiler I'm working with is g++ (more specifically, mingw)
我所使用的编译器是g++(更确切地说,是mingw)
5 个解决方案
#1
249
C99 way, also supported by VC++ compiler.
C99方式,也支持vc++编译器。
#define FOO(fmt, ...) printf(fmt, ##__VA_ARGS__)
#2
28
__VA_ARGS__
is the standard way to do it. Don't use compiler-specific hacks if you don't have to.
__VA_ARGS__是标准的方法。如果没有必要,不要使用特定于编译器的技巧。
I'm really annoyed that I can't comment on the original post. In any case, C++ is not a superset of C. It is really silly to compile your C code with a C++ compiler. Don't do what Donny Don't does.
我真的很生气,因为我不能对原帖发表评论。无论如何,c++不是C的超集,使用c++编译器编译C代码是非常愚蠢的。不要做唐尼不做的事。
#3
22
I don't think that's possible, you could fake it with double parens ... just as long you don't need the arguments individually.
我认为这是不可能的,你可以用双引号来伪装……只要您不需要单独的参数。
#define macro(ARGS) some_complicated (whatever ARGS)
// ...
macro((a,b,c))
macro((d,e))
#4
9
#define DEBUG
#ifdef DEBUG
#define PRINT print
#else
#define PRINT(...) ((void)0) //strip out PRINT instructions from code
#endif
void print(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vsprintf(str, fmt, args);
va_end(args);
printf("%s\n", str);
}
int main() {
PRINT("[%s %d, %d] Hello World", "March", 26, 2009);
return 0;
}
If the compiler does not understand variadic macros, you can also strip out PRINT with either of the following:
如果编译器不理解可变宏,您也可以用以下任何一种方法去除打印:
#define PRINT //
or
或
#define PRINT if(0)print
The first comments out the PRINT instructions, the second prevents PRINT instruction because of a NULL if condition. If optimization is set, the compiler should strip out never executed instructions like: if(0) print("hello world"); or ((void)0);
第一个注释打印指令,第二个禁止打印指令,因为一个空if条件。如果设置了优化,编译器应该去掉从未执行过的指令,如:If(0)打印(“hello world”);或(0)(空白);
#5
4
explained for g++ here, though it is part of C99 so should work for everyone
这里解释为g++,虽然它是C99的一部分,所以应该对每个人都适用
http://www.delorie.com/gnu/docs/gcc/gcc_44.html
http://www.delorie.com/gnu/docs/gcc/gcc_44.html
quick example:
简单的例子:
#define debug(format, args...) fprintf (stderr, format, args)
#1
249
C99 way, also supported by VC++ compiler.
C99方式,也支持vc++编译器。
#define FOO(fmt, ...) printf(fmt, ##__VA_ARGS__)
#2
28
__VA_ARGS__
is the standard way to do it. Don't use compiler-specific hacks if you don't have to.
__VA_ARGS__是标准的方法。如果没有必要,不要使用特定于编译器的技巧。
I'm really annoyed that I can't comment on the original post. In any case, C++ is not a superset of C. It is really silly to compile your C code with a C++ compiler. Don't do what Donny Don't does.
我真的很生气,因为我不能对原帖发表评论。无论如何,c++不是C的超集,使用c++编译器编译C代码是非常愚蠢的。不要做唐尼不做的事。
#3
22
I don't think that's possible, you could fake it with double parens ... just as long you don't need the arguments individually.
我认为这是不可能的,你可以用双引号来伪装……只要您不需要单独的参数。
#define macro(ARGS) some_complicated (whatever ARGS)
// ...
macro((a,b,c))
macro((d,e))
#4
9
#define DEBUG
#ifdef DEBUG
#define PRINT print
#else
#define PRINT(...) ((void)0) //strip out PRINT instructions from code
#endif
void print(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vsprintf(str, fmt, args);
va_end(args);
printf("%s\n", str);
}
int main() {
PRINT("[%s %d, %d] Hello World", "March", 26, 2009);
return 0;
}
If the compiler does not understand variadic macros, you can also strip out PRINT with either of the following:
如果编译器不理解可变宏,您也可以用以下任何一种方法去除打印:
#define PRINT //
or
或
#define PRINT if(0)print
The first comments out the PRINT instructions, the second prevents PRINT instruction because of a NULL if condition. If optimization is set, the compiler should strip out never executed instructions like: if(0) print("hello world"); or ((void)0);
第一个注释打印指令,第二个禁止打印指令,因为一个空if条件。如果设置了优化,编译器应该去掉从未执行过的指令,如:If(0)打印(“hello world”);或(0)(空白);
#5
4
explained for g++ here, though it is part of C99 so should work for everyone
这里解释为g++,虽然它是C99的一部分,所以应该对每个人都适用
http://www.delorie.com/gnu/docs/gcc/gcc_44.html
http://www.delorie.com/gnu/docs/gcc/gcc_44.html
quick example:
简单的例子:
#define debug(format, args...) fprintf (stderr, format, args)