variadic-macros

C - Variadic macro which expands into set of macro calls on each argument

不问归期 提交于 2019-12-07 09:56:43
问题 I want to have a single macro call which takes in multiple function pointers, and each function pointer is called by a second macro which is a function declaration. I want two macros on the form #define FUNCTION_DEF(func) extern int func(void); #define FUNCTION_DEFS(...) (???) which is called as such FUNCTION_DEFS( myFunc1, myFunc2, otherFunc1, otherFunc2, defaultFunc ) which expands into FUNCTION_DEF(myFunc1) FUNCTION_DEF(myFunc2) FUNCTION_DEF(otherFunc1) FUNCTION_DEF(otherFunc2) FUNCTION

Detect presence or absence of arguments in a C macro

浪子不回头ぞ 提交于 2019-12-06 03:29:36
问题 How can one define a C macro IFARGS(YES, NO, ...) such that invoking IFARGS with no additional arguments produces NO , and invoking IFARGS with one or more arguments produces YES ? I have an answer using GCC (see below), but I'd prefer one for C99 if possible (or a proof of its impossibility). 回答1: In C99 it is possible to detect if a macro argument is empty, but making that robust against all odds that may appear in that argument (arguments that are themselves expanding, contain () and stuff

Token pasting in C

自闭症网瘾萝莉.ら 提交于 2019-12-05 08:17:52
After reading about VA_NARG I tried to implement function overloading depending on number of arguments in C using macros. Now the problem is: void hello1(char *s) { ... } void hello2(char *s, char *t) { ... } // PP_NARG(...) macro returns number of arguments :ref to link above // does not work #define hello(...) hello ## PP_NARG(__VA_ARGS__) int main(void) { hello("hi"); // call hello1("hi"); hello("foo","bar"); // call hello2("foo","bar"); return 0; } I've read this from C-faq. But still could not get it to work... Jens Gustedt This is because of the evaluation rules for macros. You would

Variadic macro with no arguments for its variadic parameter

心不动则不痛 提交于 2019-12-05 05:26:21
Is it legal to invoke a variadic macro M with no arguments for its variadic parameter? The relevant standard quote is [cpp.replace]/4 : If the identifier-list in the macro definition does not end with an ellipsis, the number of arguments (including those arguments consisting of no preprocessing tokens) in an invocation of a function-like macro shall equal the number of parameters in the macro definition. Otherwise, there shall be more arguments in the invocation than there are parameters in the macro definition (excluding the ... ). There shall exist a ) preprocessing token that terminates the

Overloading a macro

亡梦爱人 提交于 2019-12-04 14:14:41
I'm trying to overload a macro by the number of parameter. Of course I can't actually overload the macro. I've tried using variadic macros to choose the right macro (using the fact that if __VA_ARGS__ doesn't exist it's supposed to delete the last coma before it - GCC Reference ): #define TEST1() printf("TEST1"); #define TEST2() printf("TEST2"); #define CHOOSER(x, y,FUNC,...) FUNC() #define MANIMACRO(...) CHOOSER(,__VA_ARGS__,TEST1,TEST2) int main(void) { MANIMACRO(1); MANIMACRO(); } The idea was that if __VA_ARGS__ exists it should pass 4 arguments to CHOOSER , where the third one should have

What does __VA_ARGS__ in a macro mean?

依然范特西╮ 提交于 2019-12-04 08:44:59
问题 /* Debugging */ #ifdef DEBUG_THRU_UART0 # define DEBUG(...) printString (__VA_ARGS__) #else void dummyFunc(void); # define DEBUG(...) dummyFunc() #endif I've seen this notation in different headers of C programming, I basically understood it's passing arguments, but I didn't understand what this "three dots notation" is called? Can someone explain it with example or provide links also about VA Args? 回答1: The dots are called, together with the __VA_ARGS__ , variadic macros When the macro is

Detect presence or absence of arguments in a C macro

本秂侑毒 提交于 2019-12-04 07:21:18
How can one define a C macro IFARGS(YES, NO, ...) such that invoking IFARGS with no additional arguments produces NO , and invoking IFARGS with one or more arguments produces YES ? I have an answer using GCC (see below), but I'd prefer one for C99 if possible (or a proof of its impossibility). In C99 it is possible to detect if a macro argument is empty, but making that robust against all odds that may appear in that argument (arguments that are themselves expanding, contain () and stuff like that) is difficult. My macro package P99 implements such a thing, so you wouldn't have to worry too

__VA_ARGS__ expansion using MSVC

99封情书 提交于 2019-12-04 04:27:24
I found a question showing how to overload macros based on the number of arguments : Overloading Macro on Number of Arguments But as they say, it's not working using MSVC because MSVC expands __VA_ARGS__ into a single token instead of a list of arguments ( arg1, arg2, arg3 ). They point to another question where a work around is given : MSVC doesn't expand __VA_ARGS__ correctly But not explained at all, so I can't adapt it to my own case since I can't understand it. Could you please explain how this workaround works ? The workaround in question is this: #define EXPAND( x ) x #define F(x, ...)

A group of variadic macros

此生再无相见时 提交于 2019-12-04 03:43:49
问题 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, _3, _2, _1, N, ...) N #define M_CONC(A, B) M_CONC_(A, B) #define M_CONC_(A, B) A##B #define M_ID(...) __VA_ARGS__ #define M_LEFT(L, R) L #define M_RIGHT(L, R) R #define M_FOR_EACH(ACTN, ...) M_CONC(M_FOR_EACH_, M_NARGS(__VA_ARGS__)) (ACTN, __VA

Error when defining a stringising macro with __VA_ARGS__

删除回忆录丶 提交于 2019-12-04 02:17:59
I have been trying to implement a function macro in C that prepends "DEBUG: ", to the argument, and passes its arguments to printf: #define DBG(format, ...) printf("DEBUG: " #format "\n", __VA_ARGS__) This gives me this error in gcc: src/include/debug.h:4:70: error: expected expression before ‘)’ token #define DBG(format, ...) printf("DEBUG: " #format "\n", __VA_ARGS__) ^ Supposedly, it should stringise format, and pass its variable arguments to printf, but so far I can't get past this error. EDIT After giving up on stringising arguments, and double-hashing ( ## ) __VA_ARGS__ I now have this