- Variadic macro
A variadic macro is a feature of the
C preprocessor whereby a "macro" may be declared to accept a varying number of arguments.Variable-argument macros were introduced in the ISO/IEC 9899:1999 (
C99 ) revision of theC Programming Language standard in 1999. They are currently "not" part of theC++ programming language, though many popular C++ implementations support variable-argument macros as an extension, and it is expected that variadic macros may be added to C++ at a later date.Declaration syntax
The declaration syntax is similar to that of
variadic function s: an "ellipsis" "..." is used to indicate that zero or more arguments must be passed. During macro expansion each occurrence of the special identifier __VA_ARGS__ in the macro replacement list is replaced by the passed arguments.No means is provided to access individual arguments in the variable argument list, nor to find out how many were passed.
Support
The
GNU Compiler Collection , since 3.0,C++Builder 2006 andVisual Studio 2005 [http://msdn2.microsoft.com/en-US/library/ms177415(VS.80).aspx] support variable-argument macros, both when compiling C andC++ code. In addition, GCC supports variadic macros when compilingObjective C . Sun Studio C and C++ have had support since Forte Developer 6 update 2 (C++ version 5.3) [Sun Studio feature comparison - http://developers.sun.com/sunstudio/support/CCcompare.html ] .Example
If a
printf -like function dprintf() were desired, which would take the file and line number from which it was called as arguments, the following macro might be used:dprintf() could then be called as:which expands to: or:which expands to:Without variadic macros, writing wrappers to printf is not directly possible. The standard workaround is to use the stdargs functionality of C/C++, and have the function call vprintf instead.References
ee also
*
Variadic function
Wikimedia Foundation. 2010.