Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Variadic macro in the C preprocessor

From Wikipedia, the free encyclopedia
Macro taking a varying number of arguments

Avariadic macro is a feature of some computerprogramming languages, especially theC preprocessor, whereby amacro may be declared to accept a varying number ofarguments.

Variable-argument macros were introduced in 1999 in theISO/IEC 9899:1999 (C99) revision of theC language standard, and in 2011 inISO/IEC 14882:2011 (C++11) revision of theC++ language standard.[1] Support for variadic macros with no arguments was added inC++20 andC23.[2][3]

Declaration syntax

[edit]

The declaration syntax is similar to that ofvariadic functions: a sequence of threefull 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...,[4] but regular arguments may not be listed after 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 theC99 andC++11 standards require at least one argument, but sinceC++20 andC23 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]

Support

[edit]

Severalcompilers support variable-argument macros when compiling C and C++ code: theGNU Compiler Collection 3.0,[4]Clang (all versions),[8]Visual Studio 2005,[6]C++Builder 2006, andOracle Solaris Studio (formerly Sun Studio) Forte Developer 6 update 2 (C++ version 5.3).[9] GCC also supports such macros when compilingObjective-C.

Support for the__VA_OPT__ macro to support zero arguments has been added inGNU Compiler Collection 8,[10]Clang 6,[11] andVisual Studio 2019.[12]

Example

[edit]

If aprintf-likefunctiondbgprintf() were desired, which would take the file and line number from which it was called as arguments, the following solution applies.

Our implemented function:

voidrealdbgprintf(constchar*fileName,intline,constchar*fmt,...);

Due to limitations of the variadic macro support in C++11 the following straightforward solution can fail and should thus be avoided:

#define dbgprintf(cformat, ...) realdbgprintf(__FILE__, __LINE__, cformat, __VA_ARGS__)

The reason is that

dbgprintf("Hallo")

gets expanded to

realdbgprintf(__FILE__,__LINE__,"Hallo",)

where the comma before the closing brace will result in a syntax error.

GNU C++ supports a non-portable extension which solves this:

#define dbgprintf(cformat, ...) realdbgprintf(__FILE__, __LINE__, cformat, ##__VA_ARGS__)

C++20 supports the following syntax.

#define dbgprintf(cformat, ...) realdbgprintf(__FILE__, __LINE__, cformat __VA_OPT__(,) __VA_ARGS__)

By using thecformat string as part of the variadic arguments we can circumvent the abovementioned incompatibilities. This is tricky but portable.

#define dbgprintf(...) realdbgprintf(__FILE__, __LINE__, __VA_ARGS__)

dbgprintf() could then be called as

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 toprintf is not directly possible. The standard workaround is to use thestdargs functionality of C/C++, and have the function callvprintf instead.

Trailing comma

[edit]

There is a portability issue with generating a trailing comma with empty args for variadic macros inC99. 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## in front of__VA_ARGS__.

#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__VA_ARGS__ is empty.

C23 solves this problem by introducing__VA_OPT__ like C++.[3]

Alternatives

[edit]

Before 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 theprintf() function:

#define dbgprintf(x) realdbgprintf x

dbgprintf() could then be called as:

dbgprintf(("Hello, world %d",27));

which expands to:

realdbgprintf("Hello, world %d",27);

References

[edit]
  1. ^Nelson, Clark (16 July 2004)."Working draft changes for C99 preprocessor synchronization".www.open-std.org. Retrieved2025-10-30.
  2. ^"Comma omission and comma deletion". June 18, 2017. RetrievedDecember 24, 2022.
  3. ^ab"WG14 - N3033 : Comma omission and comma deletion". 2022-07-20.
  4. ^abcdVariadic Macros – Using the GNU Compiler Collection (GCC)
  5. ^Laurent Deniau (2006-01-16)."__VA_NARG__".Newsgroupcomp.std.c.Usenet: dqgm2f$ije$1@sunnews.cern.ch.
  6. ^abcVariadic Macros (C++)
  7. ^Recursive macros with C++20 __VA_OPT__
  8. ^Clang source code change that mentions __VA_ARGS__ support (2006-07-29), note that Clang was open-sourced in 2007.http://llvm.org/viewvc/llvm-project?view=revision&revision=38770[permanent dead link]
  9. ^Sun Studio feature comparison –http://developers.sun.com/sunstudio/support/CCcompare.html
  10. ^"C++2a Support in GCC". RetrievedJune 14, 2018.
  11. ^"C++ Support in Clang". RetrievedJune 14, 2018.
  12. ^"MSVC new preprocessor overview". September 10, 2020. RetrievedDecember 8, 2020.

See also

[edit]
Features
Standard library
Implementations
Compilers
IDEs
Comparison with
other languages
Descendant
languages
Designer
Features
Standard Library
Ideas
Compilers
IDEs
Superset languages
Dialects
Relative to
other languages
People
Retrieved from "https://en.wikipedia.org/w/index.php?title=Variadic_macro_in_the_C_preprocessor&oldid=1319534072"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp