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

error: expected expression in variadic macros

I want to write a macros to print pairs of {variable name, variable value}. The variables are supposed to be arguments of variadic macros. I made a simple code, that produces compilation error: "expected expression" in cases when number of variables…
Karim
  • 1
0
votes
0 answers

Propagate __VA_ARGS__ macro types inside template methods

I was wondering if there was a clean method to extract types from __VA_ARGS__ and use them to fill template method or structure definitions? Thanks in advance I have the following problem: my macro definition: #define MY_MACRO(topic, callback_name,…
0
votes
0 answers

C, MSVC, __VA_ARGS__, Error calculating the number of variable parameters when 0 parameters

#include #define VARGS_CNT(...) VARGS_CNT_LOCAL(, ##__VA_ARGS__,\ 64, 63, 62, 61, 60, \ 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \ 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \ 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \ 29, 28, 27, 26,…
0
votes
1 answer

TypeScript macro expansion

I need to reproduce the functionality of this C code but in typescript. The purpose of this code is mainly to simplify error checking as stated in JPL's The Power of Ten but I couldn't find a way to do it in TS. #define ecall(retVal, l_call,…
Pablo Ariel
  • 251
  • 3
  • 16
0
votes
1 answer

Access the contents of __VA_ARGS__ in a macro (not a function)

One can access the contents of ... inside a function using stdarg.h: void fn(int nargs, ...){ va_list args; va_start(args,nargs); i64 arg0 = va_arg(args,i64); va_end(args); } The only way I know of using __VA_ARGS__ is to pass it to another…
étale-cohomology
  • 2,098
  • 2
  • 28
  • 33
0
votes
1 answer

Default arguments to C macros

Suppose I have function bshow() with signature void bshow(int arg0, int arg1, int arg2); but for arbitrary reasons I want to implement it as a macro. Furthermore, I want the function have default arguments int arg0=0x10; int arg1=0x11; int…
étale-cohomology
  • 2,098
  • 2
  • 28
  • 33
0
votes
1 answer

Variadic macros in C

In GNU documentation on variadic macros, an example is #define debug(format, ...) fprintf (stderr, format, __VA_ARGS__) ... In standard C, you are not allowed to leave the variable argument out entirely; but you are allowed to pass an empty…
lzhh
  • 852
  • 7
  • 16
0
votes
1 answer

Using __VA_ARGS__ in a nested macro, but the args are truncated

I'm doing something like below: #define AA(mac, a, ...) mac(a, __VA_ARGS__) #define MAC1(a, b, c) a##b##c AA(MAC1, 0, 1, 2) what I really want is to translate "AA(MAC1, 0, 1, 2)" to "012", but I get "01,2", which is reasonalbe though, but not I…
Alexis
  • 145
  • 8
0
votes
1 answer

C11 variadic macro : put elements into brackets

I'm looking at a macro, or more likely a combination of macros, that would achieve the following effect : BRACKET(a) => { a } BRACKET(a, b) => { a }, { b } BRACKET(a, b, c) => { a }, { b }, { c } Sounds pretty simple ? Alas, I couldn't find…
Cyan
  • 13,248
  • 8
  • 43
  • 78
0
votes
1 answer

SPDLOG_LOGGER_CALL and __VA_ARGS__ in

I'm trying to understand why my variadic arguments don't work in spdlog. I understand that there is an SPDLOG_LOGGER_INFO macro to do what I do, but at the moment I need to understand how SPDLOG_LOGGER_CALL works. So here is the code: #include…
flashburn
  • 4,180
  • 7
  • 54
  • 109
0
votes
1 answer

Macro or c++ template to change const variable value in struct or class

I’m implementing a protocol in C++, and it’s an ongoing project therefore the protocol changes frequently, and I have to change the code accordingly. Let’s say I define a message as follows: struct Message { const int MSG_LENGTH = 3; // sizeof(a)…
Qiao
  • 452
  • 5
  • 15
0
votes
1 answer

C++20: generate type from given variadic types

Lets say, I have a variadic type list which can grow e.g. #define MY_TYPES void, float, int, vector, ..... I am looking for way to generate these type definition at compile time in c++17/20 in generic way, e.g. #define to_tuple(MY_TYPES)…
0
votes
1 answer

Issue with nested #define for defining a function with variadic arguments

How can I define a nested #define into a macro with variadic arguments #ifndef MY_PRINTF #define MY_PRINTF(f_, ...) { \ #ifdef USE_WRITE_DEBUG_INFO \ char buff[200]; \ sprintf(buff, (f_), __VA_ARGS__); \ WriteDebugInfo(buff); \ #else \ …
afp_2008
  • 1,940
  • 1
  • 19
  • 46
0
votes
1 answer

trick or new method to expend PPNARG macro

I asked a question: c++ macro expansion(__VA_ARGS__ item name and value) #include #include #include #include #define PP_NARG(...) PP_NARG_(__VA_ARGS__,PP_RSEQ_N()) #define PP_NARG_(...) …
breaker00
  • 167
  • 7
0
votes
1 answer

How to keep original function return value after using Variadic Macros?

It is a VS2010 C++ project. I have a list of APIs: int a = API1("para1", "para2", ...); double a = API2("para1", "para2", ...); float a = API3("para1", "para2", ...); Now, I need to add a new API, APINEW(). As long as above APIs are run, APINEW…
Hun Su
  • 13
  • 4