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

C99 compatible nested calls within variadic macros

Need a way to support nested calls in variadic macros with optional argument. same as this but C99 compatible GNU gcc extension for ## operator prevents nested calls from being expanded, see code below. #define send(obj, msg, ...) find_method(obj,…
Viktor Shepel
  • 181
  • 1
  • 8
4
votes
1 answer

C++ variadic templates function in variadic macro

I'm currently working on a project where I need to simplfy an existing system. Wihtout going into the details the problem is that I get some function pointer (from type: void*) and I need to create a function from it (= create a function with…
MainCPP
  • 43
  • 4
4
votes
1 answer

C Preprocessor, Macro "Overloading"

I'm trying to do some kind of Macro "Overloading", so that MACRO(something), gets expanded differently than MACRO(something, else). Using a snippet I got from here (I'm not sure if it's 100% portable) and some functions from the Boost PP Library, I…
Ale Morales
  • 2,728
  • 4
  • 29
  • 42
4
votes
0 answers

How could I replace variadic template expansion with macro in c++

I've got the following code: template constexpr static const inline int sc(Args&&... args) { return scanf(_hidden::fmt< decltype(args+0)... >::result::data, …
Denis Sheremet
  • 2,453
  • 2
  • 18
  • 34
4
votes
2 answers

How to expand macro and delete comma

For example I want to write my own printf() alternative, but I have to perform calculations on the variable arguments: #define log(fmt_string, ...) my_log(fmt_string, pack_args(__VA_ARGS__), __VA_ARGS__) where pack_args(...) - is a macro too. How…
Anton Kochkov
  • 1,117
  • 1
  • 9
  • 25
4
votes
1 answer

Variadic macros: expansion of pasted tokens

I'm wondering if it's possible to "nest" variadic macro invocations. I'm only truly concerned with GCC and Clang. My macro definition looks like this: /** * @brief Invoke an instance method. */ #define $(obj, method, ...) \ ({ \ …
jdolan
  • 580
  • 5
  • 14
4
votes
2 answers

A group of variadic macros

I would like to have a group of variable number of arguments passed into a macro. I have following macros which is incorrect: #define M_NARGS(...) M_NARGS_(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) #define M_NARGS_(_10, _9, _8, _7, _6, _5, _4,…
yuefengz
  • 3,338
  • 1
  • 17
  • 24
4
votes
6 answers

Writing a variadic macro that uses the names of the arguments passed

I want to write a variadic macro that somehow knows the names of the arguments passed. For example: The code: int x = 2; float f = 4.6; char c = 'A'; char* str = "Bla bla"; PRINT("%d %f %c %s", x, f, c, str); // calling the macro shall…
4
votes
1 answer

Creating list of stringized macro arguments with variadics and late expansions

I have the following problem - given variable number of macro arguments argX to create a list of stringized arguments #argX Example: LIST(A, B) -> "A", "B" LIST(A, B, C) -> "A", "B", "C" I'm using Boost, so the above macro is not too difficult to…
4
votes
3 answers

Variadac Macro apply macro to all arguments

I was experimenting with C++11 variadac macros. I was trying to apply another macro to each argument in the list. This is my first try: #define APPLY_CHAIN(first, ...) APPLY_ACT(first) APPLY_CHAIN( __VA_ARGS__ ) Unfortunately this did not work.…
Martin York
  • 257,169
  • 86
  • 333
  • 562
4
votes
1 answer

Avoiding "ISO C99 requires rest arguments to be used"

With gcc 4.6.3 (with -ansi -pedantic), I've got the following code: // Argument counting macro #define NARGS(...) NARGS_(__VA_ARGS__, 5, 4, 3, 2, 1) #define NARGS_(_1, _2, _3, _4, _5, _, ...) _ static inline void fi_init_(size_t nargs, fileinfo_t…
gct
  • 14,100
  • 15
  • 68
  • 107
4
votes
2 answers

How to pass arguments to a variadic macro?

I have a variadic function: LogWrite(FILE * fp, int level, const char * filename, const char * function, ...) It should be called like this: LogWrite(fp, int Level, __FILE__, __FUNCTION__, "Message: %s", message) However, I want to write a…
Sagar
  • 9,456
  • 6
  • 54
  • 96
4
votes
1 answer

Macroized Parameters

I have a system with many parameter sets "macroized" (macros of the form "#define name value,value,value). I would like to pass these to a macro, but when I do I get an error. example: void fn(int a, int b, int c){ return; } #define MACRO_1(a, b,…
tletnes
  • 1,958
  • 10
  • 30
3
votes
4 answers

Variadic macros with zero arguments doesn't compile even with ##__VA_ARGS__

If I try to compile the following code: template void Dummy(const TArgs &...args) { } #define DUMMY(...) Dummy("Hello", ##__VA_ARGS__) int main() { DUMMY(); } I get the following compilation error: g++ -std=c++17 -O3 -Wall…
anton_rh
  • 8,226
  • 7
  • 45
  • 73
3
votes
3 answers

Variadic macros

Is there any way to write a macro like this: #define G(x1, x2, ... , xn) f(x1), f(x2), ... , f(xn) Or do I need to define this for each individual n? C++0x answers are ok. Edit: I'm asking how to create a macro of this form, not a macro that takes…
Clinton
  • 22,361
  • 15
  • 67
  • 163