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

How to Correctly use Variadic Macros to Call Other Macros

I know this isn't a fix my build error site, but I've exhausted all my options. None of my coworkers can see anything wrong. I have a set of macros in the format EventWriteQuic* and take variable number of arguments. For example: #define…
Nick Banks
  • 4,298
  • 5
  • 39
  • 65
1
vote
1 answer

How can have an argument to a function be a type? (Similar to va_arg's second argument)

In the function va_arg for variadic functions, the second argument is just 'type'. When using this function, examples pass something like 'int'. How can I pass and use types in functions of my own? For example if I wanted to malloc a block of memory…
nevets
  • 11
  • 3
1
vote
2 answers

Three dots operator "..." for initializing an array

Consider the following example which initialize an array with a default value: static unsigned int array[10] = { [ 0 ... 9 ] = 5 }; What exactly this operator does? It is related to variadic macro __VA_ARGS__?
bogdan tudose
  • 1,064
  • 1
  • 9
  • 21
1
vote
1 answer

How to expand multiple macros based on variadic macro

I have many macros that end up generating code. For example: #define CODE_GEN_IDENT1_HDR(PARAM1, PARAM2, PARAM3) \ // code generated #define CODE_GEN_IDENT2_HDR(PARAM1, PARAM2, PARAM3) \ // code generated #define CODE_GEN_IDENT1_SRC(PARAM1,…
LeoVen
  • 632
  • 8
  • 18
1
vote
1 answer

How to create macro with different behavior depending on parameters number?

I want to create one macro that will work differently in case when it HAS parameters or NOT. For example: there are two different implementation of printing error: // 1. Print message = check code #define PRINT_IF(wasError) \ do { if (wasError)…
1
vote
1 answer

C logging with variadic macros

I'm trying to write an overhead-free logging macro in C. My first idea is: #define debug_print(...) \ { \ printf(_LOG_FMT, _LOG_ARGS); \ printf(__VA_ARGS__); \ printf("\n"); \ } But this has the problem that I have to call printf…
James Pinkerton
  • 161
  • 2
  • 14
1
vote
2 answers

Is this __VA_ARGS__ expansion valid c99?

I am trying to write a function which takes variadic parameters. It has the following prototype: void foo(const char *name, const char *file, uint32_t line, const char *fmt, ...); and I call it with the following macro: #define FOO(name, ...) \ …
Chris Frank
  • 4,124
  • 4
  • 30
  • 42
1
vote
1 answer

Adding separators in iteration over __VA_ARGS__ in C/C++ macro

background I'm trying to make automatic generator of Lua-C interface using C macros. The biggest problem was to make it general for varying number of arguments, which I resolved by using __VA_ARGS__ with help of this answer: Is it possible to…
Prokop Hapala
  • 2,424
  • 2
  • 30
  • 59
1
vote
1 answer

too few arguments for class template "std::pair" : Passing std pair as arguments in varidiac function

I have to pass std::pair of std::string to a variadic function. std::pair shows error too few arguments for class template "std::pair" when trying to access std::pair using va_arg macro. #include #include #include using…
Akhil V Suku
  • 870
  • 2
  • 13
  • 34
1
vote
2 answers

macro that defines entire derived class from one function and certain type specifiers?

I have a class called system. A system takes some object managers and changes all objects in them in some way. For example there might be a system that draws all images in a imageManager. Every derived class works somewhat like this (pseudo…
MoustacheSpy
  • 743
  • 1
  • 6
  • 27
1
vote
1 answer

Feeding boost::format with variadic parameters

I am attempting to write a logging function which takes a variadic argument list and prints in a safe manor. vprintf seems like the obvious answer, but I cannot seem to find a safe way to handle when the format string expects more arguments than…
1
vote
2 answers

C preprocessor : call function with same argument as parent

I would like to define lots of functions dispatchers. Based on a flag, I will call one or the other. Flag checking is always the same, and namings also. This is an example code: int myfunction(int a,int b) { if (flag) return…
Charles B
  • 160
  • 1
  • 12
1
vote
1 answer

count __VA_ARGS__ MSVC giving unexpected results

I am trying to count the number of parameters to call the correct macro. The concatenation and number of arguments appear to be giving expected results but for some reason on MSVC the number of arguments are not working. I have tried known fixes…
Matthew
  • 800
  • 3
  • 12
  • 35
1
vote
1 answer

How I can evaluate arithmetic expression of an macro function to pass to another macro function in C Preprocessor?

how can I make this example code work?(In C or C++) The cout is just for example.I want evaluate the correct decremented number #define PRINT_1 std::cout<<"One : " <<1; #define PRINT_2 std::cout<<"Two : " <<2; #define DEC_AND_PRINT(number)…
1
vote
2 answers

Trouble with __VA_ARGS__

C++ preprocessor __VA_ARGS__ number of arguments The accepted answer there doesn't work for me. I've tried with MSVC++ 10 and g++ 3.4.5. I also crunched the example down into something smaller and started trying to get some information printed out…
Edward Strange
  • 40,307
  • 7
  • 73
  • 125