Questions tagged [variadic-macros]

Variadic macros are a feature of the C-preprocessor that permit a variable number of arguments to a macro. They were added in the 1999 revision of the C standard.

Variadic macros are a feature of the C-preprocessor that permit a variable number of arguments to a macro. They were added in the 1999 revision of the C standard.

More details can be obtained from:

295 questions
170
votes
12 answers

Standard alternative to GCC's ##__VA_ARGS__ trick?

There is a well-known problem with empty args for variadic macros in C99. example: #define FOO(...) printf(__VA_ARGS__) #define BAR(fmt, ...) printf(fmt, __VA_ARGS__) FOO("this works fine"); BAR("this breaks!"); The use of BAR() above is…
jwd
  • 10,837
  • 3
  • 43
  • 67
130
votes
12 answers

C++ preprocessor __VA_ARGS__ number of arguments

Simple question for which I could not find answer on the net. In variadic argument macros, how to find the number of arguments? I am okay with boost preprocessor, if it has the solution. If it makes a difference, I am trying to convert variable…
Anycorn
  • 50,217
  • 42
  • 167
  • 261
69
votes
3 answers

MSVC doesn't expand __VA_ARGS__ correctly

Consider this code: #define F(x, ...) X = x and VA_ARGS = __VA_ARGS__ #define G(...) F(__VA_ARGS__) F(1, 2, 3) G(1, 2, 3) The expected output is X = 1 and VA_ARGS = 2, 3 for both macros, and that's what I'm getting with GCC, however, MSVC expands…
uj2
  • 2,255
  • 2
  • 21
  • 32
58
votes
3 answers

What does __VA_ARGS__ in a macro mean?

/* Debugging */ #ifdef DEBUG_THRU_UART0 # define DEBUG(...) printString (__VA_ARGS__) #else void dummyFunc(void); # define DEBUG(...) dummyFunc() #endif I've seen this notation in different headers of C programming, I basically understood…
ganeshredcobra
  • 1,881
  • 3
  • 28
  • 44
45
votes
5 answers

Can macros be overloaded by number of arguments?

How does this work? How can a C99/C++11 variadic macro be implemented to expand to different things on the sole basis of how many arguments are given to it?
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
37
votes
7 answers

Variadic macros with zero arguments

I am working on a call macro, #define CALL(f,...) FN(f)->call((ref(new LinkedList()), __VA_ARGS__)) which when called, CALL(print,2,3,4,5); adds 2 3 4 5 to the linked list (, is overloaded to do so) and calls print which expects a linked list…
Hamza Yerlikaya
  • 49,047
  • 44
  • 147
  • 241
34
votes
4 answers

Are Variadic macros nonstandard?

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…
user350814
33
votes
2 answers

A better LOG() macro using template metaprogramming

A typical LOG() macro-based logging solution may look something like this: #define LOG(msg) \ std::cout << __FILE__ << "(" << __LINE__ << "): " << msg << std::endl This allows programmers to create data rich messages using convenient and…
Marc Eaddy
  • 1,762
  • 3
  • 16
  • 23
28
votes
3 answers

MSVC++ variadic macro expansion

So I've got a macro that works nicely in GCC, but not in Microsoft's C++ Compiler. I'm hoping somebody might know of a workaround, or perhaps can explain to me why it behaves this way. I'm sure this macro isn't exactly "standard", but it would…
Robert Kelly
  • 996
  • 1
  • 10
  • 19
21
votes
2 answers

What does ##__VA_ARGS__ mean?

I would like to know what ## does in this macro definition: #define debug(M, ...) fprintf(stderr,M "\n",##__VA_ARGS __) I googled for an answer and I came up with the following. The ## will remove the comma if no variable arguments are given to the…
Mohammed Micro
  • 313
  • 1
  • 2
  • 5
19
votes
3 answers

Appending to __VA_ARGS__

I know I can do this: #define MACRO(api, ...) \ bool ret = api(123, ##__VA_ARGS__); This is just an example, it's part of a more complicated solution. The point is that I need to append the variable number of arguments to the first 123. The ##…
Mike
  • 2,393
  • 3
  • 25
  • 37
16
votes
9 answers

is there a way to write macros with a variable argument list in visual C++?

As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...) printf(fmt); Is there a way to do that in VC++?
Domo
  • 163
  • 1
  • 2
  • 6
15
votes
2 answers

Is this a valid way of checking if a variadic macro argument list is empty?

I've been looking for a way to check if a variadic macro argument list is empty. All solutions I find seem to be either quite complex or using non-standard extensions. I think I've found an easy solution that is both compact and standard: #define…
Lundin
  • 195,001
  • 40
  • 254
  • 396
14
votes
1 answer

Variadic macro with no arguments for its variadic parameter

Is it legal to invoke a variadic macro M with no arguments for its variadic parameter? The relevant standard quote is [cpp.replace]/4: If the identifier-list in the macro definition does not end with an ellipsis, the number of arguments (including…
K-ballo
  • 80,396
  • 20
  • 159
  • 169
13
votes
1 answer

Cleaning up C/C++ code reveals problems with variadic macros

We're doing some code cleanup, fixing signed/unsigned comparisons, running static analysis, etc, on the code base of C, C++, and Java. One of the warnings we're getting is warning: ISO C does not permit named variadic macros And its companion…
Petriborg
  • 2,940
  • 3
  • 28
  • 49
1
2 3
19 20