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
2
votes
3 answers

Variadic macro wrapper that expands to format string with characters corresponding to number of arguments

Question I am looking for a variadic C preprocessor macro that passes its argument and a corresponding format string to a function, repeating a character depending on the number of arguments. For example, I would like a macro FOO which expands as…
Wrzlprmft
  • 4,234
  • 1
  • 28
  • 54
2
votes
1 answer

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 ,it would add two commas ",NULL,NULL". Then it would…
I.Omar
  • 501
  • 7
  • 19
2
votes
2 answers

Case variadic macro in C

I have 2 wrapper macros for asserting function input parameters: /** * @brief An assert wrapper with no value return in case assert fails. * @param x_: value to test for being non zero. */ #define UTIL_ASSERT_VOID(x_) …
Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74
2
votes
2 answers

Argument counting in macro

I'm trying to understand the argument counting in C preprocessing macro and the idea in this answer. We have the following macro (I changed the number of arguments for simplicity): #define HAS_ARGS(...) HAS_ARGS_(__VA_ARGS__, 1, 1, 0,) #define…
Some Name
  • 8,555
  • 5
  • 27
  • 77
2
votes
3 answers

Cpp : How to understand and/or debug complex macros?

I am trying to learn preprocessor tricks that I found not so easy (Can we have recursive macros?, Is there a way to use C++ preprocessor stringification on variadic macro arguments?, C++ preprocessor __VA_ARGS__ number of arguments, Variadic macro…
Stef1611
  • 1,978
  • 2
  • 11
  • 30
2
votes
3 answers

Macro to replace nested for loops

I found this macro #define TIMES(x) for(int i1=0;i1
Stef1611
  • 1,978
  • 2
  • 11
  • 30
2
votes
3 answers

Incorrect Substitution of OpenMP Pragma in Macro Expansion

When an OpenMP pragma is used as part of an argument for a macro, it gets incorrectly substituted. In this code: #define make_body( ... ) { __VA_ARGS__ } extern foo( int ); int main(){ make_body( #pragma omp parallel for for( int i = 0; i…
2
votes
1 answer

Macro for printing variadic arguments, with the option of no arguments

I want to implement the following macro: ASSERT(condition, ...) Which is defined like this: 1. If it gets only one parameter - if the condition is false we just print "condition is false". 2. If it gets two parameters or more…
John
  • 861
  • 1
  • 7
  • 19
2
votes
4 answers

print multiples debug lines with variadic macro

I'm using qt, gcc and c++11. I'd like to have a debug function which works in this way: int a = 1; float b = 2.2; QString c = "hello"; usage: myFunc(a); // print a 1 myFunc(a, b); // print a 1 b 2.2 myFunc(a, b, c); // print a 1 b 2.2 c…
Moia
  • 2,216
  • 1
  • 12
  • 34
2
votes
2 answers

Why won't my variadic macro accept no arguments correctly?

Overloading Macro on Number of Arguments https://codecraft.co/2014/11/25/variadic-macros-tricks/ I've been looking at the two links above, trying to get the following code to work: #define _GET_NUMBER(_0, _1, _2, _3, _4, _5, NAME, ...) NAME #define…
Brian
  • 3,264
  • 4
  • 30
  • 43
2
votes
2 answers

Variadic macro argument count not working as expected

So, basically I'm trying to implement a macro to count the number of arguments in VA_ARGS. For the sake of simplicity it only works up to 3 parameters. The problem is that when the macro is used with less than 3 parameters, it doesn't work, and…
2
votes
2 answers

Unroll loop at compile time

I want to write a load of lines into a C++ file of the form foo(i) for i = 0,1, ... , n, is there a way of doing this at compile time? I want to do this because I've got a templated class: template class MyClass{ ... } and I want to test it…
R.Mckemey
  • 335
  • 1
  • 3
  • 14
2
votes
2 answers

C/C++ preprocessor: extract every second variadic parameter

Is there a way to extract every second parameter from a list of variadic parameters in C/C++ preprocessor? I would like to write a macro to generate boilerplate code for interface methods in the following way: #define INTERFACE_FN(_NAME, _X, _Y,…
Brain
  • 311
  • 2
  • 12
2
votes
2 answers

Convert __VA_ARGS__ to string

I'm trying to redefine a Variadic Macro to use cout instead of printf. Here's the original code: #define LOGE(...) PRINT_LEVEL(1, __VA_ARGS__); #define PRINT_LEVEL(level,...) do { \ if (debug_components.DEBUG_COMPONENT >= level) \ …
user1765354
  • 347
  • 1
  • 5
  • 21
2
votes
1 answer

Variadic template wrapping function call

I need a macro/templated function that will wrap function call of some method on specific object, i.e a.Destroy() where a can be of any type as well as Destroy and Destroy may or may not take 0 to n parameters. Inside this wrapper I need to do some…
mezo
  • 423
  • 1
  • 7
  • 19