variadic-macros

Variadic macro and trailing comma

旧巷老猫 提交于 2019-11-30 19:52:18
I am trying to do object-orientation in C and want to have a syntactic sugar macro for the notation object->vtable->method(object, arg1, arg2) into send(object, method, arg1, arg2) Unfortunately when a method takes no argument, the trailing comma problem arises send(object, method) gives object->vtable->method(object, ) Is there any portable (no ##__VA_ARGS__ or Visual Studio) way of doing this? I figured out one but I need to swap the object and the method #define FIRST_ARG_(N, ...) N #define FIRST_ARG(args) FIRST_ARG_(args) #define send(msg, ...) \ FIRST_ARG(__VA_ARGS__)->vtable->msg(__VA

Casting all parameters passed in MACRO using __VA_ARGS__

百般思念 提交于 2019-11-30 08:46:59
问题 I have a macro FOO(...) that receives an unknown number of parameters. I want to cast all those params to uint. Is there a way to achieve it? 回答1: Yes, using these utility macros you can apply a macro to each argument(up to 8 for these but they could be expanded for more): /* This counts the number of args */ #define NARGS_SEQ(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N #define NARGS(...) NARGS_SEQ(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1) /* This will let macros expand before concating them */ #define

Variadic macro and trailing comma

寵の児 提交于 2019-11-30 03:45:50
问题 I am trying to do object-orientation in C and want to have a syntactic sugar macro for the notation object->vtable->method(object, arg1, arg2) into send(object, method, arg1, arg2) Unfortunately when a method takes no argument, the trailing comma problem arises send(object, method) gives object->vtable->method(object, ) Is there any portable (no ##__VA_ARGS__ or Visual Studio) way of doing this? I figured out one but I need to swap the object and the method #define FIRST_ARG_(N, ...) N

Generate multiple macro calls according to the number of arguments

时光毁灭记忆、已成空白 提交于 2019-11-29 18:37:14
I'm trying to call this function several times in a repeditive fashion. template<class T> void METADATA_METHODS_IMPL(std::string& metadata, const T &value, const std::string &key) { metadata += boost::format("%1%:%2%") % key % value(); } I have written the following macro: #define METADATA_METHODS_IMPL_1(md, v1)\ METADATA_METHODS_IMPL(md, v1, #v1); #define METADATA_METHODS_IMPL_2(md, v1, v2)\ METADATA_METHODS_IMPL_1(md, v1)\ METADATA_METHODS_IMPL_1(md, v2) #define METADATA_METHODS_IMPL_3(md, v1, v2, v3)\ METADATA_METHODS_IMPL_2(md, v1, v2)\ METADATA_METHODS_IMPL_1(md, v3) and so on ... I need

Preprocessor variadic FOR_EACH macro compatible with MSVC++10

醉酒当歌 提交于 2019-11-29 11:37:41
I've seen a few questions asking for a variation on a variadic FOR_EACH macro. However unfortunately the answers provided are incompatible with VC++10 due to it expanding __VA_ARGS __ as one argument when passed to another macro. Please could someone provide a C++11 compliant (thus forward-compatible) version that still works with VC++10. Perhaps using the "workaround" that is often mentioned, #define EXPAND(x) x , however I don't know where to put this in order to get, for example, the latter generalised part of this answer to work in VC++10. To clarify, the intended behaviour is for FOR_EACH

Casting all parameters passed in MACRO using __VA_ARGS__

流过昼夜 提交于 2019-11-29 07:52:13
I have a macro FOO(...) that receives an unknown number of parameters. I want to cast all those params to uint. Is there a way to achieve it? Yes, using these utility macros you can apply a macro to each argument(up to 8 for these but they could be expanded for more): /* This counts the number of args */ #define NARGS_SEQ(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N #define NARGS(...) NARGS_SEQ(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1) /* This will let macros expand before concating them */ #define PRIMITIVE_CAT(x, y) x ## y #define CAT(x, y) PRIMITIVE_CAT(x, y) /* This will call a macro on each argument

Using variadic macros or templates to implement a set of functions

随声附和 提交于 2019-11-29 07:28:14
I have a set of methods used to instanciate and initialize a set of objects. They all look pretty much the same, except for the number of arguments that are passed to the Init function : ObjectType* CreateObjectType(Arg1 a1, Arg2 arg2, ... ArgN aN) { ObjectType* object = new ObjectType(); [...] object->Init(this, a1, a2, ..., aN); [...] return object; } Note that the arguments are not to be used anywhere except to be passed to the Init function. I would like to find a way to implement all of those without having to duplicate the code for each object type. I tried using variadic macros, with

Appending to __VA_ARGS__

爷,独闯天下 提交于 2019-11-28 19:40:26
I know I can do this: #define MACRO(api, ...) \ bool ret = api(123, ##__VA_ARGS__); This is just an example, it's part of a more complicated solution. The point is that I need to append the variable number of arguments to the first 123. The ## makes the compiler strip out the comma after the 123 argument if no arguments were passed into MACRO. But now I want to append arguments to api, like so: #define MACRO(api, ...) \ bool ret = api(__VA_ARGS__##, 456); Nocando. One solution is to have two macros, MACRO and MACRO_V, say, and make the _V version not process any arguments. But is there a way

Generating function declaration using a macro iteration

笑着哭i 提交于 2019-11-28 11:35:18
I'm trying to generate a function declaration using a macro /* goal: generate int f(int a, float b) */ template<typename P> struct ptype; template<typename P> struct ptype<void(P)> { typedef P type; }; #define NAMEe #define COMMAe #define COMMA , #define NAME(N) N PARAMS #define PARAMS(P, ...) COMMA ## __VA_ARGS__ P NAME ## __VA_ARGS__ #define PARAM_ITER(P) P NAME #define PROTO(R, N, P) \ ptype<void R>::type N (PARAM_ITER P (,e)) PROTO((int), f, (int)(a)(float)(b)); It will iteratively process the next (name) or (type) by NAME or PARAMS respectively, with the ... having an empty macro argument

A #define in C with three dots

亡梦爱人 提交于 2019-11-28 10:05:51
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__)) #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__)) This is definition for these 2 macros; later in the code LOGI and LOGW are used this way LOGI("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z); and this way LOGW("Unable to eglMakeCurrent"); Since I try to avoid complex macros and #define in general, I can't get what this macro actually means. What is the role for the 3 dots notation here? What does this