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
7
votes
1 answer

__VA_ARGS__ expansion using MSVC

I found a question showing how to overload macros based on the number of arguments : Overloading Macro on Number of Arguments But as they say, it's not working using MSVC because MSVC expands __VA_ARGS__ into a single token instead of a list of…
Virus721
  • 8,061
  • 12
  • 67
  • 123
7
votes
2 answers

Detect presence or absence of arguments in a C macro

How can one define a C macro IFARGS(YES, NO, ...) such that invoking IFARGS with no additional arguments produces NO, and invoking IFARGS with one or more arguments produces YES? I have an answer using GCC (see below), but I'd prefer one for C99 if…
augurar
  • 12,081
  • 6
  • 50
  • 65
6
votes
1 answer

Is it possible to prevent the removal of the comma with empty __VA_ARGS__ in Visual C++?

On Visual Studio 2005 I have a macro that looks like this (examplified!!): #define MY_CALL(FUN, ...) \ if(prepare(x, y)) { \ FUN(__VA_ARGS__); \ } /**/ As long as the function takes at least one argument, I'm fine. When the function…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
6
votes
1 answer

Variadic arguments and x64

How do the va_arg \ va_start \ va_list \ va_end macros work under the hood in x64? The calling convention in i386 passes parameters on the stack, hence the macro just increments some pointer that points to the stack base and forwards it. However, in…
DrPrItay
  • 793
  • 6
  • 20
6
votes
3 answers

Token pasting in C

After reading about VA_NARG I tried to implement function overloading depending on number of arguments in C using macros. Now the problem is: void hello1(char *s) { ... } void hello2(char *s, char *t) { ... } // PP_NARG(...) macro returns…
Nyan
  • 2,360
  • 3
  • 25
  • 38
6
votes
2 answers

What is #__VA_ARGS__ supposed to generate when there are no arguments passed?

Example code: #define FOO(...) You passed: #__VA_ARGS__ FOO(1,2,3) FOO() Preprocess with Visual C++ (version 14 CTP), get: You passed: "1,2,3" You passed: In the last line, #__VA_ARGS__ is turned into nothingness. I would prefer it turned into…
Bryan
  • 11,398
  • 3
  • 53
  • 78
6
votes
2 answers

How to stringify a string which contains a comma?

I want to pass a version string in the compile command: $ g++ -Wall -D VERSION="2013-12-03 02:15:21, commit cb060df" -o main main.cpp Inside my code, I have the following: #define TOSTR_(x) #x #define STRINGIFY(x) TOSTR_(x) #define VERSION_STR…
fonini
  • 2,989
  • 3
  • 21
  • 39
6
votes
2 answers

Preprocessor variadic FOR_EACH macro compatible with MSVC++10

I've seen a few questions asking for a variation on a variadic FOR_EACH macro. However unfortunately the answers provided are incompatible with VC++10 due to it expanding __VA_ARGS __ as one argument when passed to another macro. Please could…
Dylan
  • 1,692
  • 1
  • 13
  • 24
6
votes
1 answer

Macro count params

In order to make compiler happy I have to count params passed to A(), otherwise gcc raises "warning: ISO C99 requires rest arguments to be used" when pedantic flag is on and only one param is passed #include /* Count params */ #define…
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
6
votes
3 answers

How to do variadic macros with $(call ...) in GNU Make

I created a macro for use in makefiles along the lines of: TODO_MSG = $(warning TODO: $(1)) $(call TODO_MSG, This part of the msg displays fine, but this part does not) I can get around it with something like the following: BLAH := $(shell perl -e…
Brian Vandenberg
  • 4,011
  • 2
  • 37
  • 53
5
votes
2 answers

Is it possible to handle each element in a variadic macro at compile time in C?

Is it possible to archieve the following somehow? I am using gcc. #define foo(argCount, ...)\ FOR_EACH_IN_VA_ARGS_(argCount, element, __VA_ARGS__)\ {\ printf("%u", sizeof(element));\ } Thanks for your answers.
5
votes
3 answers

Variadic macro to create struct

How can I write a macro (for gcc) that would be used like this: CREATE_STRUCT(my_struct1,foo); CREATE_STRUCT(my_struct2,foo,bar); and expands to struct my_struct1 { std::string foo; }; struct my_struct2 { std::string foo; …
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
5
votes
1 answer

C-preprocessor: iteratively expand macro to comma-separated list

Using Paul Fultz II's solution in the post C-preprocessor recursive macro, I'd like to expand an unlimited number of parenthesized macro arguments, e.g. #define MY_CHAIN (alpha) (beta) (gamma) into a comma-separated list which can be passed to a…
Taylor Nichols
  • 588
  • 2
  • 13
5
votes
2 answers

C - Variadic macro which expands into set of macro calls on each argument

I want to have a single macro call which takes in multiple function pointers, and each function pointer is called by a second macro which is a function declaration. I want two macros on the form #define FUNCTION_DEF(func) extern int…
edvardsp
  • 133
  • 8
5
votes
2 answers

Is variadic macro subsitution for every argument possible?

I read quite a few questions on SO about variadic macros now, but it doesn't seem like anyone answered the most simple question: #define IDENTITY(x) x #define IDENTITY_FOR_ALL(...) ??? Is there a way to make IDENTITY_FOR_ALL expand to IDENTITY(X)…
iFreilicht
  • 13,271
  • 9
  • 43
  • 74
1 2
3
19 20