34

For debugbuilds, I usually use Clang, as it formats warnings and errors better, and makes it a little easier to track them down, and fix them.

But recently after adding a Macro with variadic arguments, Clang told me the following (from a dummy project):

main.cpp:5:20: warning: named variadic macros are a GNU extension [-Wvariadic-macros]
#define stuff3(args...)  stuff_i(args)

I know that macroname(args...) compiles fine in a wide range of compilers, including Visualstudio, Sunstudio, and of course GCC. But just to make sure that clang is right, I tried two other ways of expanding the variadic arguments:

Number 1:

#define stuff1(...)  stuff_i(...)

Number 2:

#define stuff2(...)  stuff_i(__VA_ARGS__)

On both I receive this message:

main.cpp:3:16: warning: variadic macros were introduced in C99 [-Wvariadic-macros]

... Which makes me wonder if Variadic macros are actually part of the standard of C++ (and of course I know that the Preprocessor is interpreted independently)?

a3f
  • 8,517
  • 1
  • 41
  • 46
  • 1
    C99 and C++0x officially support version 2. C++03 doesn't support any variadic macros. Version 1 is a GCC-specific extension – sellibitze Jan 24 '11 at 22:58
  • (like your name) when you compile with GCC there is the `-pedantic` option which should report the extensions used. Like Variable Length Array or Variadic Macros, for example. In GCC they are included simply because since they are part of C99, the logic is already implemented, so it must have seemed a good idea to let the C++ folk benefit :) – Matthieu M. Jan 25 '11 at 07:18
  • @Matthieu M: Yes indeed, I also thought that this feature would be part of C++, as it's already part of C99... interestingly, gcc doesn't say that named variadic macros are indeed a GNU extensions, but instead just says that they're not part of the standard: `warning: ISO C does not permit named variadic macros` –  Jan 25 '11 at 07:31

4 Answers4

42

Quote Wikipedia:

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.

So it's standard from C99 and C++11 onwards, but a GNU extension in C++03.

Thomas
  • 174,939
  • 50
  • 355
  • 478
30

As of C++11, variadic macros are now included in standard C++. Section 16.3 of the C++11 standard specifies variadic macros such that they are compatible with variadic macros from C99 (the second form in the question).

Here is an example of a standard-conforming variadic macro definition in C++:

#define foo(x, y, ...)    bar(x, y, __VA_ARGS__)
Dan Moulding
  • 211,373
  • 23
  • 97
  • 98
  • 2
    It's standard conforming, but it also suffers from the comma bug, if Wikipedia is to be believed. – Alexei Averchenko Aug 12 '14 at 04:29
  • @AlexeiAverchenko: I've seen in gcc documentation that the comma will be removed if nothing is placed in the ... however today I port some code to clang and I am getting `expected expression` error and i'm not yet through understanding it. may be related – v.oddou Dec 03 '14 at 07:55
  • @AlexeiAverchenko `__VA_OPT__` is introduced in C++20 to solve this problem. – L. F. Jun 02 '19 at 11:08
4

In the form of your example "Number 2", they are standard in C99, and generally a C++ compiler's preprocessor is the same for C and C++ compilation.

They are also supported Microsoft VC++ despite its otherwise stubborn resistance to C99 compliance. So between that and GCC there are few reasons to avoid using them. Even on most embedded systems compilers I use they are supported.

Avoid the "Number 1" form however, that is firmly GCC specific, and no doubt deprecated.

Clifford
  • 88,407
  • 13
  • 85
  • 165
0

Standard says in 16.3 Macro replacement:

The identifier _ _ VA_ARGS _ _ shall occur only in the replacement-list of a function-like macro that uses the ellipsis notation in the parameters.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130