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

Why does variadic macro fail with expected primary-expression before ‘)’ token unless preceded by a named argument?

I'm trying to create some trace macros that use variadic arguments. The macros only seem to function correctly when a named argument precedes the variadic ones. A minimal code version of what I'm doing is shown below. Only TraceTest() without…
M. Webb
  • 157
  • 11
0
votes
1 answer

use c++ preprocessor to autogenerate repetitive code

I'm currently dealing with a piece of extremely repetitive code that is implementing and instantiating a series of template constructors. The general pattern is somewhat like this: // preamble: some dummy classes to work with class A{}; class…
carsten
  • 1,315
  • 2
  • 13
  • 27
0
votes
0 answers

Apply a macro to a variable argument list

I have a variadic macro and a regular macro. I use them like this: LIST( ELEM(a), ELEM(b) // usually about 4-8 elements ) I would like LIST to automatically invoke ELEM on each argument so that I can write LIST(a, b) instead of LIST(ELEM(a),…
Indiana Kernick
  • 5,041
  • 2
  • 20
  • 50
0
votes
2 answers

Variadic macro that outputs its arguments separated by `||`

I would like to make a macro F taking a variable number of parameters, that expands to the parameters each separated by a ||. e.g. F(a, b, c) should expand to a || b || c, F(a) should expand to just a, etc. I know C doesn't support recursive macros.…
CoffeeTableEspresso
  • 2,614
  • 1
  • 12
  • 30
0
votes
1 answer

__VA_ARGS__ sees extra empty arg when invoked inside BOOST_PP_SEQ_FOR_EACH

My actual code example is quite complex, but I will try to summarize the behavior I am seeing with a simple illustration. I have a macro that I want to be able to call individually, or multiple times as a part of a larger macro expansion: #define…
Colin Andrews
  • 191
  • 11
0
votes
4 answers

Porting #define from C++ to C#

I have an old MCPP project which was used as a communication layer between C++ code which runs on machines and C# which runs on a desktop computer. Recently we decided to try and kill this "glue" project. This project has in it a few lists of…
Ramzi Khahil
  • 4,932
  • 4
  • 35
  • 69
0
votes
1 answer

Why does va_list var-arg copy not work as expected if the copy is saved and given to another thread?

As in this example, if I call tsub directly from main, output is as expected. If pthread mode is enabled for tsub, this doesn't give expected output. va_list vobj; void *tsub (void *arg) { printf ("vobj: %d, %f\n", va_arg…
c10
  • 13
  • 4
0
votes
1 answer

How to fix variadic macro related issues with "macro overloading" in MSVC++ (Microsoft Visual studio)?

Inspired from this kind of solution, I have written below code, which simulates "overloading of macros". #include #define CONCATE_(X,Y) X##Y #define CONCATE(X,Y) CONCATE_(X,Y) #define UNIQUE(NAME) CONCATE(NAME, __LINE__) #define…
iammilind
  • 68,093
  • 33
  • 169
  • 336
0
votes
1 answer

C++ - Strip types from parameter list in a macro argument

I have a macro which takes a parameter list as an argument, as shown here: #define func(returnType, name, args) \ static inline returnType fn_ ## name ## impl args; \ DLL_EXPORT returnType fn_ ## name args { \ …
0
votes
1 answer

how to properly expand a macro?

I need to be able to expand a macro to build a typedef which I use for my application. The macro builds a simple typedef. The question I have is how do __VA_ARGS__ (i.e. do you lose arguments farther down the calls?) act when passed through to…
Matthew
  • 800
  • 3
  • 12
  • 35
0
votes
1 answer

Variadic templates and variadic functions combination

I'm creating a logging function that on the one hand needs to be able to parse any number of parameters, but on the other hand will always be passed with __func__ and this(of the calling object). If I use only variadic templates like…
DsCpp
  • 2,259
  • 3
  • 18
  • 46
0
votes
1 answer

Passing an arbitrary number of args to a function

So basically, I would like to redefine this part of code using macros, switch(count) { case 0: variadic(); case 1: variadic(array[0]); case 2; variadic(array[0], array[1]); case 3: variadic(array[0], array[1], array[2]); ... } My constraint is…
Whiteclaws
  • 902
  • 10
  • 31
0
votes
1 answer

How to correctly call a variadic template function from a variadic macro?

Alright, first things first, I will be providing the code I am asking my question for. EX_Factory.h #ifndef EX_FACTORY_H #define EX_FACTORY_H #include #include #include "Base.h" struct EX_Factory{ template
0
votes
2 answers

variadic macro - optional initializer expression

I want a macro that declares an int with a given name and optional initializer expression. I tried using this answer on Stack Overflow but with no success. This is what I tried: #define macro(...) int FIRST(__VA_ARGS__)(REST(__VA_ARGS__)) when used…
onqtam
  • 4,356
  • 2
  • 28
  • 50
0
votes
0 answers

C++ macro overloading by number of arguments in Visual Studio

Some strange problem with macro overload in Visual Studio. #define ENUM_INTERNAL(A, ...) A, ENUM_INTERNAL(__VA_ARGS__) #define ENUM_INTERNAL(A) A, COUNT #define ENUM(name, ...) struct name { enum en {ENUM_INTERNAL(__VA_ARGS__)}; }; ENUM(xxxx, A1,…
aaalex88
  • 619
  • 4
  • 13