Variadic macro in the C preprocessorA 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] Support for variadic macros with no arguments was added in C++20 and will be added in C23.[2][3] Declaration syntaxThe declaration syntax is similar to that of variadic functions: a sequence of three full stops "..." is used to indicate that one 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. Additionally, regular macro arguments may be listed before the 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.[5] Both the C99 and C++11 standards require at least one argument, but since C++20 and C23 this limitation has been lifted through the __VA_OPT__ functional macro. The __VA_OPT__ macro is replaced by its argument when arguments are present, and omitted otherwise. Common compilers also permit passing zero arguments before this addition, however.[4][6] The C preprocessor rules prevent macro names in the argument of __VA_OPT__ from expanding recursively. It is possible to work around this limitation up to an arbitrary fixed number of recursive expansions, however.[7] SupportSeveral compilers support variable-argument macros when compiling C and C++ code: the GNU Compiler Collection 3.0,[4] Clang (all versions),[8] Visual Studio 2005,[6] C++Builder 2006, and Oracle Solaris Studio (formerly Sun Studio) Forte Developer 6 update 2 (C++ version 5.3).[9] GCC also supports such macros when compiling Objective-C. Support for the __VA_OPT__ macro to support zero arguments has been added in GNU Compiler Collection 8,[10] Clang 6,[11] and Visual Studio 2019.[12] ExampleIf a
dbgprintf ("Hello, world");
which expands to realdbgprintf (__FILE__, __LINE__, "Hello, world");
Another example is 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 Trailing commaThere is a portability issue with generating a trailing comma with empty args for variadic macros in C99. Some compilers (e.g., Visual Studio when not using the new standard-conformant preprocessor[6]) will silently eliminate the trailing comma. Other compilers (e.g.: GCC[4]) support putting # define MYLOG(FormatLiteral, ...) fprintf (stderr, "%s(%u): " 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 %u" "\n", __FILE__, __LINE__, 42);
which is equivalent to fprintf (stderr, "%s(%u): Too many balloons %u\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 with GCC. GCC supports the following (non-portable) extension: # define MYLOG(FormatLiteral, ...) fprintf (stderr, "%s(%u): " FormatLiteral "\n", __FILE__, __LINE__, ##__VA_ARGS__)
which removes the trailing comma when C23 solves this problem by introducing AlternativesBefore the existence of variable-arguments in C99, it was quite common to use doubly nested parentheses to exploit the variable number of arguments that could be supplied to the #define dbgprintf(x) realdbgprintf x
dbgprintf (("Hello, world %d", 27));
which expands to: realdbgprintf ("Hello, world %d", 27);
References
See also |
Portal di Ensiklopedia Dunia