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

C Variadic Macro to simplify calls to fprintf

I have defined a clunky macro to give myself a reduced probability of making mistakes in a project with a large amount of unique logging situations... #define BANG(...) {fprintf(stderr, "in %s(), ", __func__);\ fprintf(stderr,…
user5069935
1
vote
1 answer

gcc failing to expand some macros

I am developing a program using a third-party UI library with functions in the form Vbox(void *first, ...). These serve as layout functions and take an arbitrary number of parameters. The end of the list is defined by the first NULL detected. This…
Wasabi
  • 2,879
  • 3
  • 26
  • 48
1
vote
1 answer

Check if variadic macro has one vs many arguments?

I am trying to write a C variadic macro that checks if it was invoked with one, or many arguments. I found some solutions which use a macro that counts the arguments, but those solutions are either for a fixed/finite amount of arguments, or rely on…
J08nY
  • 333
  • 3
  • 8
1
vote
1 answer

Is it possible to define a c++ wrapper function for a macro with variadic parameters?

I'd like to come up with a c++ wrapper function that fully wraps the TraceLoggingWrite macro. TraceLoggingWrite is a macro with variadic parameters. I attempted the following code snippet, but it would encounter compilation errors because it seems…
lancery
  • 658
  • 1
  • 6
  • 18
1
vote
1 answer

Using variadic macro arguments as a list

I was trying to write a macro that compares the running times of multiple expressions, and ran into a wall. My problem can be condensed to the following code: (defmacro test-m [& exprs] `(map #(.toString %) ~exprs)) If I call this like (test-m 1…
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
1
vote
1 answer

Wrap macro with variable number of arguments

I need to wrap a macro so that it can be accessed with a function. The macro is defined like: #define gDbgLog(fmt,...) dbgLog(g_pdbg,MODULE_NAME,__FUNCTION__,fmt,##__VA_ARGS__) and I've attempted to wrap it up like: void pMonDbgLog(char* fmt,…
stdcerr
  • 13,725
  • 25
  • 71
  • 128
1
vote
1 answer

Portable variadic macro

I'm looking to make a variadic macro that simply calls a certain function for every argument (say, up to 6). So far I've been using the following code in MSVC: #define do_write2(x,y) do{do_write(x);do_write(y);}while(0) #define do_write3(x,y,z)…
riv
  • 6,846
  • 2
  • 34
  • 63
1
vote
3 answers

##__VA_ARGS__ not swallowing comma when zero args under C99

I'd like to use a macro like the following: #define x(...) y(a,##__VA_ARGS__,b) To expand like so: x(); -> y(a,b); x(1); -> y(a,1,b); With -std=gnu99, it works perfectly. With -std=c99 however, it looks like this: x(); -> y(a,,b); x(1);…
Max
  • 2,760
  • 1
  • 28
  • 47
1
vote
1 answer

Variadic macro expected ')' before numeric constant

this is the actual macro: #ifdef DEBUG #define debug(funcname, format, ...) \ printf(BOLD UNDERLINED REVERSE \ …
1
vote
1 answer

Macro not expanded with direct call, but expanded with indirect

I've got the following macros #include #define DB_FIELD(...) BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__) #define DB_TOFIELD(type,name) \ private:\ type name##_;\ public:\ const type& get_##name(){return name##_;}\ void…
Rafał Dembek
  • 108
  • 1
  • 6
1
vote
1 answer

Extending types of a boost::variant dynamically

I have to write a library exporting data to various formats. Every format is a type of a given boost::variant, e.g. typdef boost::variant TFormat with formats csv, xml and hdf5. This approach worked quite good for me. The library…
Aleph0
  • 5,816
  • 4
  • 29
  • 80
1
vote
1 answer

Writing a C macro that takes variadic arguments and also returns a value

I have a use for a macro that displays a message on stderr showing the current filename (from __FILE__) and line number (from __LINE__), but also allows the use of an optional format and variadic argument list, for custom messages. If a custom…
davidA
  • 12,528
  • 9
  • 64
  • 96
1
vote
2 answers

C macro expansion of a function pointer based on for loop incrementor

I have a function that takes a pointer to a function as a parameter. This function get's called within a for loop due to the similar nature of the names of the function pointer I use a macro to expand the name into the function. IT looks something…
Luke Smith
  • 781
  • 3
  • 7
  • 15
1
vote
2 answers

Wrapper macro for a generated static array

I'm working on generating some large static meta data structures right now, and would like to optimize the size of some the generated c++ files. static constexpr MetaData* metaDataArray[] = { &MetaDataObject0, &MetaDataObject1, …
TomasS
  • 21
  • 2
1
vote
2 answers

Converting the list of numbers in hexadecimal string in C preprocessor

How to convert the list of the 16bit numbers to the hexadecimal string like "\x0f\x56\x44\xe0". How do these 16 bit values look? They are the result of the macro expansion too #define make_word(arg1, arg2) arg1 << 16 + arg2. So the full call should…
Anton Kochkov
  • 1,117
  • 1
  • 9
  • 25