Jump to content

Variadic macro in the C preprocessor: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
Reflinks: Converting bare references
Line 5: Line 5:
== Declaration syntax ==
== Declaration syntax ==


The declaration syntax is similar to that of [[variadic function]]s: an ''ellipsis'' "<tt>...</tt>" is used to indicate that one or more arguments must be passed. Common compilers also permit passing zero arguments to such a macro, however.<ref name="gcc">http://gcc.gnu.org/onlinedocs/gcc/Variadic-Macros.html</ref><ref name="msvc">http://msdn.microsoft.com/en-us/library/ms177415(v=vs.80).aspx</ref> During macro expansion each occurrence of the special identifier <tt>__VA_ARGS__</tt> in the macro replacement list is replaced by the passed arguments.
The declaration syntax is similar to that of [[variadic function]]s: an ''ellipsis'' "<tt>...</tt>" is used to indicate that one or more arguments must be passed. Common compilers also permit passing zero arguments to such a macro, however.<ref name="gcc">[http://gcc.gnu.org/onlinedocs/gcc/Variadic-Macros.html Variadic Macros - Using the GNU Compiler Collection (GCC)<!-- Bot generated title -->]</ref><ref name="msvc">[http://msdn.microsoft.com/en-us/library/ms177415(v=vs.80).aspx Variadic Macros (C++)<!-- Bot generated title -->]</ref> During macro expansion each occurrence of the special identifier <tt>__VA_ARGS__</tt> 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. However, macros can be written to count the number of arguments that have been passed.<ref>{{cite newsgroup
No means is provided to access individual arguments in the variable argument list, nor to find out how many were passed. However, macros can be written to count the number of arguments that have been passed.<ref>{{cite newsgroup

Revision as of 08:44, 27 October 2013

A variadic macro is a feature of some computer programming languages, especially the C preprocessor, whereby a macro may be declared to accept a varying number of arguments.

Variable-argument macros were introduced in 1999 in the ISO/IEC 9899:1999 (C99) revision of the C language standard, and in 2011 in ISO/IEC 14882:2011 (C++11) revision of the C++ language standard.[1]

Declaration syntax

The declaration syntax is similar to that of variadic functions: an ellipsis "..." is used to indicate that one or more arguments must be passed. Common compilers also permit passing zero arguments to such a macro, however.[2][3] 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. However, macros can be written to count the number of arguments that have been passed.[4]

Support

Several compilers support variable-argument macros when compiling C and C++ code: the GNU Compiler Collection 3.0,[2] Visual Studio 2005,[3] C++Builder 2006, and Oracle Solaris Studio (formerly Sun Studio) Forte Developer 6 update 2 (C++ version 5.3).[5] GCC also supports such macros when compiling Objective-C.

Example

If a printf-like function dbgprintf() were desired, which would take the file and line number from which it was called as arguments, the following macro might be used:

// Our implemented function
void realdbgprintf (const char *SourceFilename, int SourceLineno, const char *CFormatString, ...);

// Due to a design bug of the variadic macro in C++, the following is more readable, but should be avoided.
// Reason is, that dbgprintf("Hallo") will be expanded to realdbgprintf (__FILE__, __LINE__, "Hallo", )
// The comma before the closing brace will result in a syntax error ...
//#define dbgprintf(cformat, ...) realdbgprintf (__FILE__, __LINE__, cformat, __VA_ARGS__)

// GNU C++ supports the non portable extention:
//#define dbgprintf(cformat, ...) realdbgprintf (__FILE__, __LINE__, cformat, ##__VA_ARGS__)

// By using the cformat string as part of the variadic arguments we can avoid this.
// This is tricky, but portable solution.
#define dbgprintf(...) realdbgprintf (__FILE__, __LINE__, __VA_ARGS__)

dbgprintf() could then be called as:

dbgprintf ("Hello, world");

which expands to:

realdbgprintf (__FILE__, __LINE__, "Hello, world");

or:

dbgprintf("%d + %d = %d", 2, 2, 5);

which expands to:

realdbgprintf(__FILE__, __LINE__, "%d + %d = %d", 2, 2, 5);

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.

The impossible example

Unfortunately it is (still) not possible to write a carefree and secure macro in portable C++:

MYLOG(FormatLiteral, ...)  fprintf (stderr, "%s(%d): " FormatLiteral "\n", __FILE__, __LINE__, __VA_ARGS__)

The following application works

MYLOG("Too many balloons %u", 42);

which expands to

fprintf (stderr, "%s(%u): " "Too many balloons %d" "\n", __FILE__, __LINE__, 42);

which is equivalent to

fprintf (stderr, "%s(%u): Too many balloons %d\n", __FILE__, __LINE__, 42);

But look at this application:

MYLOG("Attention!");

which expands to

fprintf (stderr, "%s(%u): " "Attention!" "\n", __FILE__, __LINE__, );

which generates a syntax error. There's no way to fix this behaviour!

GCC and Visual Studio support the following (non-portable) extension:

MYLOG(FormatLiteral, ...)  fprintf (stderr, "%s(%u): " FormatLiteral "\n", __FILE__, __LINE__, ##__VA_ARGS__)

which removes the trailing comma when __VA_ARGS__ is empty.

References

  1. ^ Working draft changes for C99 preprocessor synchronization - http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1653.htm
  2. ^ a b Variadic Macros - Using the GNU Compiler Collection (GCC)
  3. ^ a b Variadic Macros (C++)
  4. ^ Laurent Deniau (2006-01-16). "__VA_NARG__". Newsgroupcomp.std.c. dqgm2f$ije$1@sunnews.cern.ch.
  5. ^ Sun Studio feature comparison - http://developers.sun.com/sunstudio/support/CCcompare.html

See also