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

Typecast all arguments in a variadic macro

I want to use sscanf to parse a long string .. The data to be parsed will be stored in a struct whose members are all of the type time_t. Unfortunately, there isn't any format string to mark a time_t so I'm just going to typecast all time_t *…
Amr Ayman
  • 1,129
  • 1
  • 8
  • 24
0
votes
2 answers

How to use Variadic macros with fprintf

I am trying to print logs into a file by writing macros. My macro looks like as shown below: #define LOG(fmt,...){\ FILE *F;\ F = fopen("output.txt","a");\ fprintf(F,fmt " %s %d",__VA_ARGS__,__FILE__,__LINE__);} And I plan to call LOG…
user1692342
  • 5,007
  • 11
  • 69
  • 128
0
votes
1 answer

Is there an easier way to do a macro to define a function with variable amount of arguments?

I have a macro that defines a function with a variable amount of arguments, the macro has some logic to decide which real function must be called. My current approach is the following: #define FUNC(ret,args,args_call) \ ret my_func(args) { \ …
André Puel
  • 8,741
  • 9
  • 52
  • 83
0
votes
1 answer

C++ macro with variable arguments

1.#define debug(...) printf( __VA_ARGS__) 2.#define debug(...) std::cout<< __VA_ARGS__ Apparently, 1 is ok, 2 will get error when compiles. Is there any possibility to use "std::cout" with variable arguments? What's the point of this…
zzn
  • 2,376
  • 16
  • 30
0
votes
1 answer

C Variadic Macros __VA_ARGS__ vs ##__VA_ARGS__ in clang

I have the following two macros: #define F1(...) [NSString stringWithFormat:__VA_ARGS__] #define F2(format, ...) [NSString stringWithFormat:(format), ##__VA_ARGS__] When I nest them, F1 works, but F2 fails to compile. This code: F1(@"%@",…
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
0
votes
1 answer

concatenate nested macro result

I have the following set of macros: macro_1(p1) macro_2(p1, p2) macro_3(p1, p2, p3) etc. Now, I want to make another generic macro that will evaluate to the previous ones, and I am trying to do that like so: #define macro_x(...) macro_ ##…
TCS
  • 5,790
  • 5
  • 54
  • 86
0
votes
2 answers

Variadic function in C99 to deallocate several arrays?

Currently, I have a very simple function to deallocate array of doubles in my program: void deallocate(double** array) { free(*array); } I would like this function to be variadic in order to take several arrays, and free them one after another.…
Vincent
  • 57,703
  • 61
  • 205
  • 388
0
votes
1 answer

Macro expansion within a macro

I'm trying to create LOGDEBUG macro: #ifdef DEBUG #define DEBUG_TEST 1 #else #define DEBUG_TEST 0 #endif #define LOGDEBUG(...) do { if (DEBUG_TEST) syslog(LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG), __VA_ARGS__); } while (0) ... size_t haystack_len =…
LetMeSOThat4U
  • 6,470
  • 10
  • 53
  • 93
0
votes
2 answers

Is it possible to use variadic macros to declare multiple inheritance?

I have written a messaging system which relies a lot on compile-time mechanics. To receive messages, you inherit from a class template like so: class WorldRenderer : public fea::MessageReceiver, public…
Tobias
  • 924
  • 9
  • 23
0
votes
1 answer

Passing arguments to variadic macro

#define SEND_VALUE(num, point1, point2, point3...) \ { \ char number[6]; \ char p1[6];\ char p2[6];\ char p3[6];\ if(num == ONE) {sprintf(number, "ONE");}\ if(num == TWO) {sprintf(number, "TWO");}\ if(num == THREE) {sprintf(number,…
aod
  • 77
  • 1
  • 2
  • 7
0
votes
1 answer

How to use macro to construct a class?

For example, i have a class class A : public B { public: template struct Element { // typedef void Type; // will specialize it. }; A(Element<0>::Type v0 = initial0, Element<1>::Type v1 = initial1, …
user1899020
  • 13,167
  • 21
  • 79
  • 154
0
votes
0 answers

Variadic argument count for macros in MSVC

I'm using this simple macro for logging events of my program: #define _ERROR(format, args...) \ { \ time_t t = time(0);\ struct tm * now = localtime( & t );\ fprintf(stderr, "[ERROR %d-%d-%d %d:%d:%d]: ",now->tm_year + 1900,\ …
sorush-r
  • 10,490
  • 17
  • 89
  • 173
0
votes
1 answer

Generate multiple macro calls according to the number of arguments

I'm trying to call this function several times in a repeditive fashion. template void METADATA_METHODS_IMPL(std::string& metadata, const T &value, const std::string &key) { metadata += boost::format("%1%:%2%") % key % value(); } I have…
maverik
  • 5,508
  • 3
  • 35
  • 55
0
votes
1 answer

Ignore empty variadic params

This code works as expected when all params are passed to HTML_A: #include #define HTML_A_fmt_void #define HTML_A_arg_void #define HTML_A_fmt_link(fmt, ...) " href=\""fmt"\"" #define HTML_A_arg_link(fmt, ...) ,__VA_ARGS__ #define…
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
0
votes
2 answers

How to expand variadic arguments in a macro?

I want to essentially have a macro shortener. The macro, FOO(A,B,C) should expand to defined(_FOO_A) || defined(_FOO_B) || defined(_FOO_C). Is that possible in GCC using variadic macro arguments and not actually writing 3 functions (FOO(A),…
Arindam
  • 342
  • 1
  • 12
1 2 3
19
20