新闻  |   论坛  |   博客  |   在线研讨会
Macros with a Variable Number of Arguments
rain00 | 2008-01-27 11:09:48    阅读:1301   发布文章

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 pre-
processor 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,
these arguments are not macro expanded.


附上一段经典代码, 哈哈
#if (LOG_ENABLE_MASK & LOG_ERR_BIT)
#if LOG_ARGS
#define log_err(cat, ...) \
do { \
_applLog (PRI_CAT (LOG_ERR, cat), _LOG_LOCATION, \
__VA_ARGS__); \
} while (FALSE)
#else
#define log_err(cat, ...) \
do { \
_applLog (PRI_CAT (LOG_ERR, cat), _LOG_LOCATION, ""); \
} while (FALSE)
#endif /* LOG_ARGS */
#else
#define log_err(cat, ...) do {} while (FALSE)
#endif


/*
* If logging is enabled, but LOG_ARGS is not defined, then
* logging decoration will be shown, but the specified log string
* and its variable arguments will not.
* This decreases space requirements while still providing
* logging information sufficient to locate code logging errors.
*/

#ifndef LOG_ARGS
#define LOG_ARGS 1
#endif

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
programming
最近文章
手机市场之我见
2008-02-09 18:22:43
queue
2008-02-03 11:10:19
推荐文章
最近访客