全部博文(79)
分类: C/C++
2007-07-28 13:30:18
---------------------------------------------------------------------------------------------------------------------
声明:
此文为原创翻译,欢迎转载,转载请保留如下信息
作者:聂飞(afreez)
联系方式:afreez@sina.com (欢迎与作者交流)
初次发布时间:2006-06-03
不经本人同意,不得用语商业或赢利性质目的,否则,作者有权追究相关责任!
---------------------------------------------------------------------------------------------------------------
带有可变参数的宏(Macros with a Variable Number of Arguments)
在1999年版本的ISO C 标准中,宏可以象函数一样,定义时可以带有可变参数。宏的语法和函数的语法类似。下面有个例子:
#define debug(format, ...) fprintf (stderr, format, __VA_ARGS__)
这里,‘…’指可变参数。这类宏在被调用时,它(这里指‘…’)被表示成零个或多个符号,包括里面的逗号,一直到到右括弧结束为止。当被调用时,在宏体(macro body)中,那些符号序列集合将代替里面的__VA_ARGS__标识符。更多的信息可以参考CPP手册。
GCC始终支持复杂的宏,它使用一种不同的语法从而可以使你可以给可变参数一个名字,如同其它参数一样。例如下面的例子:
#define debug(format, args...) fprintf (stderr, format, args)
这和上面举的那个ISO C定义的宏例子是完全一样的,但是这么写可读性更强并且更容易进行描述。
GNU CPP还有两种更复杂的宏扩展,支持上面两种格式的定义格式。
在标准C里,你不能省略可变参数,但是你却可以给它传递一个空的参数。例如,下面的宏调用在ISO C里是非法的,因为字符串后面没有逗号:
debug ("A message")
GNU CPP在这种情况下可以让你完全的忽略可变参数。在上面的例子中,编译器仍然会有问题(complain),因为宏展开后,里面的字符串后面会有个多余的逗号。
为了解决这个问题,CPP使用一个特殊的‘##’操作。书写格式为:
#define debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
这里,如果可变参数被忽略或为空,‘##’操作将使预处理器(preprocessor)去除掉它前面的那个逗号。如果你在宏调用时,确实提供了一些可变参数,GNU CPP也会工作正常,它会把这些可变参数放到逗号的后面。象其它的pasted macro参数一样,这些参数不是宏的扩展。
GCC4.1.1.pdf 236页
5.14 Macros with a Variable Number of Arguments.
In the ISO C standard of 1999, a macro can be declared to accept a variable number of
arguments much as a function can. The syntax for defining the macro is similar to that of
a function. Here is an example:
#define debug(format, ...) fprintf (stderr, format, __VA_ARGS__)
Here ‘...’ is a variable argument. In the invocation of such a macro, it represents the
zero or more tokens until the closing parenthesis that ends the invocation, including any
commas. This set of tokens replaces the identifier __VA_ARGS__ in the macro body wherever
it appears. See the CPP manual for more information.
GCC has long supported variadic macros, and used a different syntax that allowed you
to give a name to the variable arguments just like any other argument. Here is an example:
#define debug(format, args...) fprintf (stderr, format, args)
This is in all ways equivalent to the ISO C example above, but arguably more readable
and descriptive.
GNU CPP has two further variadic macro extensions, and permits them to be used with
either of the above forms of macro definition.
In standard C, you are not allowed to leave the variable argument out entirely; but you
are allowed to pass an empty argument. For example, this invocation is invalid in ISO C,
because there is no comma after the string:
debug ("A message")
GNU CPP permits you to completely omit the variable arguments in this way. In the
above examples, the compiler would complain, though since the expansion of the macro still
has the extra comma after the format string.
To help solve this problem, CPP behaves specially for variable arguments used with the
token paste operator, ‘##’. If instead you write
#define debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
and if the variable arguments are omitted or empty, the ‘##’ operator causes the preprocessor
to remove the comma before it. If you do provide some variable arguments in
your macro invocation, GNU CPP does not complain about the paste operation and instead
places the variable arguments after the comma. Just like any other pasted macro argument,