Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
0 votes
1 answer
58 views

Macro with number of arguments AND default parameter

I'm familiar with the following way of creating a macro with variable number of arguments. However, consider: #define MY_MACRO_N(value, format, ...) my_func(value, format, ##__VA_ARGS__) #define ...
galah92's user avatar
  • 3,933
3 votes
2 answers
125 views

Variadic macro expansion's going wrong

Consider the following code #define COMB(F, ...) F(__VA_ARGS__) #define ADD(X, Y) (X + Y) int foo() { return COMB(ADD, 1, 2); } I have done some experiments on Godbolt. Microsoft VS v19.22 (...
Yunus King's user avatar
  • 1,271
2 votes
1 answer
2k views

C preprocessor macro doesn't parse comma separated tokens?

I want to choose one of two functions depending on the number of arguments: nargs = 0 ----> f1 nargs > 0 ----> f2. Macros do the following: get the first argument, then if no argument supplied ,...
I.Omar's user avatar
  • 541
0 votes
0 answers
39 views

Apply a macro to a variable argument list [duplicate]

I have a variadic macro and a regular macro. I use them like this: LIST( ELEM(a), ELEM(b) // usually about 4-8 elements ) I would like LIST to automatically invoke ELEM on each argument so ...
Indiana Kernick's user avatar
4 votes
3 answers
1k views

C99 compatible nested calls within variadic macros

Need a way to support nested calls in variadic macros with optional argument. same as this but C99 compatible GNU gcc extension for ## operator prevents nested calls from being expanded, see code ...
Viktor Shepel's user avatar
0 votes
2 answers
550 views

C99 Macro Expansion for Struct Member Access

Is it possible to do a nullity check and an access in a macro? Eg: #define LOG(mystruct, severity, format, ...) ({ \ severity_t current = ERROR; \ if (mystruct) { \ current = mystruct->...
bge0's user avatar
  • 921
0 votes
2 answers
142 views

Variadic function in C99 to deallocate several arrays?

Currently, I have a very simple function to deallocate array of doubles in my program: void deallocate(double** array) { free(*array); } I would like this function to be variadic in order to ...
Vincent's user avatar
  • 60.2k
1 vote
1 answer
3k views

Checking if an argument is passed in variadic macros in C

For cleaner error handling I use a macro (it uses C99 and GCC extensions); the behavior is like standard assert: #define A(cond, msg, ...) ({ \ if (!(cond)) { \ if (msg) \ ...
user avatar
2 votes
2 answers
2k views

reverse the arguments to a variadic macro

How do I reverse the arguments to a variadic macro? For example, I'd like #define REVERSE(...) ??? REVERSE(A,B,C) // expands to C,B,A My goal is to separate the front and back arguments: #define ...
joshuanapoli's user avatar
  • 2,509
0 votes
2 answers
668 views

How to expand variadic arguments in a macro?

I want to essentially have a macro shortener. The macro, FOO(A,B,C) should expand to defined(_FOO_A) || defined(_FOO_B) || defined(_FOO_C). Is that possible in GCC using variadic macro arguments and ...
Arindam's user avatar
  • 342
5 votes
1 answer
5k views

Avoiding "ISO C99 requires rest arguments to be used"

With gcc 4.6.3 (with -ansi -pedantic), I've got the following code: // Argument counting macro #define NARGS(...) NARGS_(__VA_ARGS__, 5, 4, 3, 2, 1) #define NARGS_(_1, _2, _3, _4, _5, _, ...) _ ...
gct's user avatar
  • 14.5k
13 votes
1 answer
11k views

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 ...
Petriborg's user avatar
  • 2,960
171 votes
12 answers
109k views

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"); ...
jwd's user avatar
  • 11.1k
4 votes
2 answers
7k views

Generating function declaration using a macro iteration

I'm trying to generate a function declaration using a macro /* goal: generate int f(int a, float b) */ template<typename P> struct ptype; template<typename P> struct ptype<void(P)>...
Johannes Schaub - litb's user avatar
7 votes
2 answers
16k views

Can I define variadic C preprocessor macros with __VA_ARGS in the middle instead of the end?

GCC complains if I do this: #define M(obj,met, ..., contents) obj##_##met(const void * self, __VA_ARGS__) { \ contents \ } Giving me these 2 reasons: error: missing ')' in macro parameter ...
salvador p's user avatar
  • 2,995
6 votes
3 answers
2k views

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 *...
Nyan's user avatar
  • 2,451