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

Stringize __VA_ARGS__ (C++ variadic macros)

Let's have class Item{ public: Item(int id,const char *name,const char *props=NULL); }; And I want to write: ITEM(1,FIRST); ITEM(2,SECOND, WithSomeProps); With a macro #define ITEM(ID,NAME,...) new Item(ID,NAME, #__VA_ARGS__ ) That…
tru7
  • 6,348
  • 5
  • 35
  • 59
5
votes
3 answers

Adding default arguments to variadic macro

Is it possible to add default arguments before variable argument in variadic macro? e.g I have the version of macro something like #define MACRO(arg1, ...) func(arg1, ##__VA_ARGS__) I would like to add 2 more default arguments in the macro before…
vishal
  • 51
  • 1
  • 2
5
votes
4 answers

Can we implement a max or min macro, which can take variable arguments (more than two parameters )

I want to implement a new max/min macro, which can take more than two parameter, for example: #define max( ... ) ... and then, I can use it like this: max( p0, p1, p2, p3 ) max( 2, 4, 100 ) max( 1,2,3,4,5,6,7 ) -> 7 if this macro can help us to…
boo
  • 493
  • 6
  • 17
5
votes
2 answers

Is the `gnu-zero-variadic-macro-arguments` safe to ignore?

Consider the following code (live example): #define TEST_VA(mX, ...) TEST #define STRINGIFY_IMPL(mX) #mX #define STRINGIFY(mX) STRINGIFY_IMPL(mX) #include int main() { std::cout << STRINGIFY(TEST_VA(1)) << std::endl; std::cout…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
5
votes
2 answers

Using variadic macros or templates to implement a set of functions

I have a set of methods used to instanciate and initialize a set of objects. They all look pretty much the same, except for the number of arguments that are passed to the Init function : ObjectType* CreateObjectType(Arg1 a1, Arg2 arg2, ... ArgN…
user2683028
  • 93
  • 1
  • 4
5
votes
1 answer

Casting all parameters passed in MACRO using __VA_ARGS__

I have a macro FOO(...) that receives an unknown number of parameters. I want to cast all those params to uint. Is there a way to achieve it?
Alex
  • 131
  • 1
  • 11
4
votes
1 answer

Stringizing macro arguments in a multi-level macro call

I have a macro like this: #define SHOW_EXPR(x) printf ("%s=%d\n", #x, (x)) It works: #define FOO 123 int BAR = 456; SHOW_EXPR(FOO+BAR); This prints FOO+BAR=579 as expected. Now I'm trying to define a macro that calls SHOW_EXPR: #define…
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
4
votes
1 answer

variadic format strings in C macros?

I'm trying to write a basic macro like this: #define USER_ERROR(fmt, ...) { \ fprintf(stderr, "ERROR %s(): %s\n", __func__, fmt, ##__VA_ARGS__); \ } \ my ideal usage: USER_ERROR("something went wrong %s %s", more_detail,…
kamkow1
  • 467
  • 2
  • 8
4
votes
1 answer

c++ variadic macro: how to retrieve arguments values

In the class there are a lot of methods with similar implementation, only method's name and argument-list different: void function1 (int a, bool b) { mMember->function1(a, b); } void function2 (double a, string b) { mMember->function2(a,…
shtirlic
  • 41
  • 1
4
votes
1 answer

Macro with "placeholder" value

I'm working with a library that includes a set of preprocessor libraries. One of them is a FOR_EACH style macro which iterates over a __VA_ARGS__ and calls a user-provided macro for each argument. The user provided macro is called like:…
Channel72
  • 24,139
  • 32
  • 108
  • 180
4
votes
1 answer

Surprising expansion of variadic GNU C preprocessor macros in the presence of the ## operator

If we define a macro #define M(x, ...) { x, __VA_ARGS__ } and then use it passing itself as an argument M(M(1, 2), M(3, 4), M(5, 6)) then it expands to the expected form: { { 1, 2 }, { 3, 4 }, { 5, 6 } } However, when we use the ## operator (to…
Maciek Godek
  • 172
  • 12
4
votes
2 answers

Counting function arguments at compile time

I'm trying to count the number of arguments to a function at compile time (I'm wrapping sprintf up in some templates for compile time checks and type safety). I need to check that the number of arguments matches the number of formatting placeholders…
Adam
  • 1,122
  • 9
  • 21
4
votes
2 answers

C++ variadic macro for pairs

I am aware of the MAP macro that can be used to apply a macro function to a variadic argument list. But how would you apply a macro function to pairs of variadic arguments? What I want to create is something like this: #define DECL_VAR(type,var)\ …
4
votes
2 answers

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 struct ptype; template struct ptype { typedef P type; }; #define NAMEe #define COMMAe #define COMMA…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
4
votes
3 answers

Problems with variadic macros in C

I'm having a problem with optional arguments in #define statements in C, or more specifically with gcc 4.2: bool func1(bool tmp) { return false; } void func2(bool tmp, bool tmp2) {} #define CALL(func, tmp, ...) func(tmp, ##__VA_ARGS__) int main()…
BonzaiThePenguin
  • 1,413
  • 13
  • 19