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 "map" a variadic macro with boost preprocessor?

Say I have a macro F: #define F(x) /*...*/ and a macro G that takes one or more arguments: #define G(...) /*...*/ and I want to write a macro H that takes one or more arguments that expands to G with F applied to each argument: #define H(...) /*…
1
vote
1 answer

printf-style logger that is non-variadic

Our C/C++ project is using a new internal script that liberally wraps every function in SWIG to make available to python scripts. It chokes on our logger function since SWIG cannot wrap variadic functions well. So I decided to hide the variadic…
Shaun Lebron
  • 2,501
  • 28
  • 29
1
vote
1 answer

How to expand a recursive macro via __VA_OPT__ in a nested context

I have read this article, which illustrates how the __VA_OPT__ function macro can be used to recursively expand a macro. I would like to implement something similar, with the difference being that the macro is expanded in a nested context. The…
Salvage
  • 448
  • 1
  • 4
  • 13
1
vote
1 answer

What am I doing wrong with this variadic macro in C?

I'm working on a university project and I made a macro wrapper of printf which can color the text, print the file, function and line of the instruction with a string error based on errno. #define PRINT_WITH_COLOR(color, title, message, errno_code)…
Ivan Lo Greco
  • 315
  • 1
  • 9
1
vote
2 answers

Array of struct initialization using variadic macro

I have the following type definition: typedef struct { int (*function)(int argc, char *argv[]); char *name; } command_t; The member function is a function pointer and the member name is a string which will store the name of the function. To…
A.Lacasse
  • 125
  • 10
1
vote
2 answers

Calling a printf with __VA_ARGS__ not working in a function but works in a macro

The following macro works: #define DEBUG(msg, ...) printf(msg, __VA_ARGS__) But when I add my own function, it says error: '__VA_ARGS__' was not declared in this scope. My code: void Debug(const char* msg, ...) { printf(msg,…
Sajib
  • 404
  • 3
  • 15
1
vote
1 answer

How to test if a macro identifier is defined without using #ifdef?

Does anyone know how to, or if it's even possible to, create a variadic macro that expands to 0 if it's argument is not a defined macro, but expands to 1 if it's argument is a defined macro? (I'm using c99.) #define BOB #define SUE 45 #define…
textral
  • 1,029
  • 8
  • 13
1
vote
2 answers

Making a C macro substitute itself as a function call in the result?

I have a logging macro and a function, and I would like for them to have the same name, so that when I miss the header with the macro, the program would still build (via C implicit function declaration). I've tried: /* Declare function. */ void…
psprint
  • 349
  • 1
  • 10
1
vote
1 answer

Make printf() always print the function name with a wrapper

How can I always print the function name, in which printf() is called, without supplying the information every time?
earthling
  • 620
  • 6
  • 20
1
vote
2 answers

Wrap C++ function with default parameters in C

Say I have a function like this defined in a C++ header: namespace foo { void bar(int a, int b = 1); } and I would like to use this function in C code. One obvious solution would be to define two functions like this: void foo_bar_1(int a) {…
Peter
  • 2,919
  • 1
  • 16
  • 35
1
vote
3 answers

Macro shadowing both function and its address

I am working on a library in C. I have a function which should be available for the user of the library. It should be compiled and executed when a certain value is defined as 1, but it should not be compiled at all when this value is defined as 0…
Dinuirar
  • 25
  • 5
1
vote
2 answers

Specializing macro with arguments

Suppose I have the following macro: #define LOG(L, ...) func(L, __VA_ARGS__); Where L can be one of INFO, WARN, FATAL Now I want to define it for FATAL differently. #define LOG(FATAL, ...) {func(FATAL, __VA_ARGS__); exit(-1);} How to accomplish…
nishantsingh
  • 4,537
  • 5
  • 25
  • 51
1
vote
2 answers

is it safe to use va_copy on windows

I have gone through the link https://devblogs.microsoft.com/oldnewthing/20131114-00/?p=2663 for potential pitsfalls using va_list And below code segment from the same link specifies not to use va_list in manner shown because a va_list is not…
user13003546
1
vote
1 answer

Modern / generic approach to variadic macro with stringizing

I have a library (wrapper around nlohmann/json) that allows me to deserialize from JSON: struct MyStruct { int propertyA; std::string propertyB; std::vector propertyC; } void from_json(const JSON::Node& json, MyStruct& s) { …
kbirk
  • 3,906
  • 7
  • 48
  • 72
1
vote
1 answer

How to implement C/C++ variadic logging macro with __FILE__ and __LINE__ info for multiple platforms?

I want to implement a C/C++ variadic logging macro, which contains __FILE__ and __LINE__ information. This is my simple implementation: #include #define MYLOG(format, ...) printf("%s:%d " format, __VA_ARGS__) The only issue is that, this…
linrongbin
  • 2,967
  • 6
  • 31
  • 59