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

How to append to an array using a variadic macro?

I'm trying to eliminate a bunch of boilerplate code by using macros. Here's what works. I can replace: int do_register_script(struct context *L) { method_type const _instance_methods[] = { {"new", __new}, {"delete", __delete} …
101010
  • 14,866
  • 30
  • 95
  • 172
3
votes
1 answer

C++ Variadic Macros in Boost.Fusion?

So, according to this answer, C++ doesn't support variadic macros, and the C++ standard doesn't mention variadic macros anywhere. I know that C99 introduced variadic macros with __VA_ARGS__, and certain C++ compilers (like GCC) even provide…
Channel72
  • 24,139
  • 32
  • 108
  • 180
3
votes
2 answers

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 (with /E flag) fails at preprocessing the macro's.…
Yunus King
  • 1,141
  • 1
  • 11
  • 23
3
votes
1 answer

c++ macro to import all names of base template class

When deriving a class from a template base class, one is forced to use the syntax this->member, or using Base::member to get access to a member of the base class. The same would happen if the base class is a generic template parameter and one is…
francesco
  • 7,189
  • 7
  • 22
  • 49
3
votes
2 answers

Variadic Macro calling fprintf: how to add arguments to __VA_ARGS__?

I have two macros: #define LogFunction(str) fprintf(stdout, "%s: %s\n",__FUNCTION__,(str)) #define LogPrintf(f_, ...) fprintf(stdout, (f_), ##__VA_ARGS__) So i can use them this way: void MyFunction() { int N=4; …
Parduz
  • 662
  • 5
  • 22
3
votes
1 answer

Find out the type of __VA_ARGS__ in a variadic macro

Supposedly, I have a variadic macro (e.g., MY_MACRO(...)) and I call it the following way: MY_MACRO(std::pair const &p) Now, __VA_ARGS__ in my macro's body would be std::pair const &p. Is there a way to figure out the type of…
101010
  • 41,839
  • 11
  • 94
  • 168
3
votes
4 answers

Variadic Macro: cannot pass objects of non-trivially-copyable type through '...'

I am trying to write a macro for logging mechanism. I wrote a variadic macro but it does not work with std::string. The code looks like the following: #include #include #define LOG_NOTE(m, ...) printf(m, ##__VA_ARGS__) int…
eneski
  • 1,575
  • 17
  • 40
3
votes
1 answer

How to use variadic macro arguments in both a function definition and a function call?

I'm trying to use a macro to define several similar functions based on the macro's parameters. However the number and types of parameters that the resulting function needs to take isn't the same across all of the functions, but I also need to pass…
AJMansfield
  • 4,039
  • 3
  • 29
  • 50
3
votes
2 answers

Testing member function, by peeling pairs off variadic macro/template/function?

(I'm using catch for unit testing, which unfortunately doesn't yet have what it's calling generators to do this sort of thing.) In c++17, is there a way to reduce this: assert("" == String{"" }.removeLeading(' ')); assert("a" == String{"…
user1902689
  • 1,655
  • 2
  • 22
  • 33
3
votes
1 answer

Weird behavior of variadic macro expansion with gcc and clang

I'm writing a variadic dispatcher macro in C++, to call a different macro based on the number of arguments (from none up to 5) provided to the dispatcher. I came up with this solution: #define GETOVERRIDE(_ignored, _1, _2, _3, _4, _5, NAME, ...)…
Nicola Mori
  • 777
  • 1
  • 5
  • 19
3
votes
2 answers

Make variadic macro / method which prints all variables names and values

An existing macro gets variadic number of variables on my application. I want, using this macro, to print these variables by name=value format. Here is a small example : #define EXISTING_MACRO(...) < ??? > int main() int a = 0; int b = 1; int c =…
SagiLow
  • 5,721
  • 9
  • 60
  • 115
3
votes
1 answer

Overloading macros with variadic arguments

I'm trying to build a macro M which will expand to one of two possibilities, depeding on whether it has one, or more than one, arguments: M(x) should expand to f(x) While M(x, "%d%d%d", 1, 2, 3) should expand to g(x, "%d%d%d", 1, 2, 3) Where the…
mic_e
  • 5,594
  • 4
  • 34
  • 49
3
votes
3 answers

Distributing an argument in a variadic macro

I would like to construct a macro that takes a variable number of arguments and distributes the first argument to each of the subsequent in a format similar to the examples shown below: Call: MACRO(F,A) Result: F:A Call: MACRO(F,A,B,C) Result:…
pt3dNyc
  • 369
  • 1
  • 16
3
votes
1 answer

Is this macro argument counting tactic legit?

I've been aware of the VA_NARGS macro as described at C Preprocessor, Macro "Overloading" for a while, but I've always been put off by the large amount of boilerplate that it takes to make it work. I recently had a need for this functionality, and…
danfuzz
  • 4,253
  • 24
  • 34
3
votes
5 answers

How to write a c++ assert macro with a varying number of informational arguments?

I am trying to write a macro dbgassert similar to the standard assert. In addition to what assert does, I want to dbgassert print an arbitrary number of additional parameters (containing debugging information). What I have so far is listed below,…
thor
  • 21,418
  • 31
  • 87
  • 173