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

How to mimic variadic macro in VC++6.0?

In VS2010, I wrote the following variadic macros to dump out information to files. #define INDENT(fp, indent) for(size_t __i = 0; __i < (indent); ++__i) fprintf((fp), " ") // IND_FP = indented fprintf. // This macro uses two IMPLICIT parameters. //…
lzl124631x
  • 4,485
  • 2
  • 30
  • 49
3
votes
1 answer

any trick to unpack a variadic macro?

Any trick to unpack a variadic macro? For example, #define READ(...) means read the arguments one by one READ(a, b, c) will be unpacked to read(a); read(b); read(c)
user1899020
  • 13,167
  • 21
  • 79
  • 154
3
votes
5 answers

How to print a variable number of parameters with a macro?

I would like to define a macro with a variable number of parameters which prints the name and value of each given parameter. For instance : MACRO(x) would print x = 123 MACRO(x,y) would print x,y = 123,666 A better macro would be more readable…
captain_flammy
  • 117
  • 3
  • 9
3
votes
1 answer

Visual studio __VA_ARGS__ issue

I run cl /P test.cpp, the file and result is as following. test.cpp #define FiltedLog( ...) \ if (logDetail) \ MP_LOG(LOG_INFO, __VA_ARGS__); #define MP_LOG(level,fmt,...) \ BOOAT::LOG("MP", level, fmt, ##__VA_ARGS__) #define…
3
votes
4 answers

How can I guarantee type safety of variadic arguments?

In C, I'd like to make a function or a macro that looks like this: void Log(char* what, ...) where the ... must be key-value pairs of const char*'s. I would really like code that doesn't follow this to blow up at compile time. I tried looking for…
Mark Pauley
  • 1,445
  • 12
  • 11
3
votes
2 answers

Overloading a macro

I'm trying to overload a macro by the number of parameter. Of course I can't actually overload the macro. I've tried using variadic macros to choose the right macro (using the fact that if __VA_ARGS__ doesn't exist it's supposed to delete the last…
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
2
votes
2 answers

Variadic macros to create multiple constructs with delimiters

EDIT2: Figured it out. See my answer below. Some background: I am using an SQL library that returns query results as tuples. For each DB statement I write: An SQL query that includes a list of field names A tuple to receive the results of each…
Alex O
  • 1,429
  • 2
  • 13
  • 20
2
votes
2 answers

Overload macro as variable and function

First, there are a lot of posts on overloading macros: Can macros be overloaded by number of arguments? C++ Overloading Macros Macro overloading Overloading Macro on Number of Arguments etc. ... However, all of them ask the question about variadic…
gust
  • 878
  • 9
  • 23
2
votes
1 answer

Variadic macro with zero-args for ISO C++

I wrote this variadic macro template<>-maker. #define TNAME0() #define TNAME1(_1) typename _1 #define TNAME2(_1,_2) typename _1, typename _2 #define TNAME3(_1,_2,_3) typename _1, typename _2, typename _3 #define…
codechimp
  • 1,509
  • 1
  • 14
  • 21
2
votes
1 answer

Is it possible to concatenate parameters of variadic macro to form a variable name?

I am trying to achieve something like the following: #define def_name(delim, ...) ??? // how will this variadic macro concatenate its parameters to define a new variable? // Calling `def_name` as follows should define a new variable. def_name("_",…
Meekaa Saangoo
  • 309
  • 1
  • 2
  • 8
2
votes
1 answer

C header declaration for generics (macro)

I am unsure about where to write the declaration and the call of a macro that replaces the code with a function. I do not really know if I should write the macro to the .h or .c file. Before reading some stuff on the best ways to create libraries, I…
std124_lf
  • 134
  • 2
  • 9
2
votes
3 answers

C macro that voids variable length input arguments

Is there a way to define a macro that voids variable list of arguments? #define VOID_ARGS(...) ((void)##__VA_ARGS__) The use case is void arguments to suppress compiler error [-Werror=unused-value] when warnings treated as errors: #define DEBUG…
hashraf
  • 21
  • 2
2
votes
1 answer

template deduction failure in variadic macro

the decltype(__VA_ARGS__) in my macro compiles when only for a single argument and not when multiple arguments are passed. What I'm trying to achieve is to invoke a variadic function based on a runtime condition. The arguments to my printer could be…
2
votes
1 answer

A way to count the number of __VA_ARGS__ arguments, including 0, without compiler specific constructs

There are plenty of questions discussing how to count __VA_ARGS__ and the problem of zero arguments (e.g. [1] and [2]). However, answers to these questions usually are either not portable, since they use the GCC specific ##__VA_ARGS__ to account for…
Luiz Martins
  • 1,644
  • 10
  • 24
2
votes
1 answer

How to use the token pasting operator with a variable number of arguments?

I thought of having a generic version of #define concatenate(a, b, c) a ## b ## c I tried it like this: #include #define concatenate(arg1, ...) arg1 ## __VA_ARGS__ int main() { int dob = 121201; printf("%d", concatenate(d, o,…
Ardent Coder
  • 3,777
  • 9
  • 27
  • 53