variadic-macros

Preprocessor variadic FOR_EACH macro compatible with MSVC++10

风格不统一 提交于 2019-11-28 05:28:52
问题 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

Using variadic macros or templates to implement a set of functions

放肆的年华 提交于 2019-11-28 01:35:23
问题 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

C Preprocessor, Macro “Overloading”

天涯浪子 提交于 2019-11-27 14:53:04
I'm trying to do some kind of Macro "Overloading", so that MACRO(something), gets expanded differently than MACRO(something, else). Using a snippet I got from here (I'm not sure if it's 100% portable) and some functions from the Boost PP Library, I was able to make it work :D //THESE TWO COUNT THE NUMBER OF ARGUMENTS #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1) //THIS ONE RETURNS THE PARAMETER AT POSITION _i FROM A LIST OF __VA_ARGS__ #define VA_ARG(_i, ...) BOOST_PP_ARRAY_ELEM(_i, (VA_NARGS(__VA_ARGS__), (__VA_ARGS__))) /

Variadic macros with zero arguments

▼魔方 西西 提交于 2019-11-27 14:36:50
问题 I am working on a call macro, #define CALL(f,...) FN(f)->call((ref(new LinkedList()), __VA_ARGS__)) which when called, CALL(print,2,3,4,5); adds 2 3 4 5 to the linked list (, is overloaded to do so) and calls print which expects a linked list which works as expected how ever there are some calls which do not require arguments, CALL(HeapSize); It still takes a linked list but an empty one, above does not work, I am trying to come up with a macro that woud work with either style? EDIT: Digging

Appending to __VA_ARGS__

一世执手 提交于 2019-11-27 12:27:45
问题 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

Are Variadic macros nonstandard?

余生长醉 提交于 2019-11-27 09:23:55
For debugbuilds, I usually use Clang, as it formats warnings and errors better, and makes it a little easier to track them down, and fix them. But recently after adding a Macro with variadic arguments, Clang told me the following (from a dummy project): main.cpp:5:20: warning: named variadic macros are a GNU extension [-Wvariadic-macros] #define stuff3(args...) stuff_i(args) I know that macroname(args...) compiles fine in a wide range of compilers, including Visualstudio, Sunstudio, and of course GCC. But just to make sure that clang is right, I tried two other ways of expanding the variadic

Generating function declaration using a macro iteration

被刻印的时光 ゝ 提交于 2019-11-27 06:22:00
问题 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

Can macros be overloaded by number of arguments?

China☆狼群 提交于 2019-11-27 06:16:27
How does this work? How can a C99/C++11 variadic macro be implemented to expand to different things on the sole basis of how many arguments are given to it? (Edit: See the end for a ready-made solution.) To get an overloaded macro, first we need a macro which selects between several implementations. This part doesn't use a variadic macro. Then a variadic macro which generically counts its arguments produces a selector. Plugging the argument count into a dispatcher produces an overloaded macro. Caveat: This system cannot tell the difference between zero and one arguments because there is no

Variadic macro trick

青春壹個敷衍的年華 提交于 2019-11-26 20:48:43
What's the trick to create a variadic macro FOO(a1, a2, a3,..., an) such that it expands to FOOn(a1, a2, a3,..., an) for values of n in whatever preselected bounded range you choose? That is, FOO(a) should expand to FOO1(a) , FOO(a, b, c) to FOO3(a, b, c) , etc. I know there's a standard trick but I can't seem to find it. Please feel free to mark this question as a duplicate and close it if there's another question with the answer. I suspect there is but I couldn't find it. This post Variadic macro to count number of arguments has what you're looking for I believe. Look at the first and second

C Preprocessor, Macro “Overloading”

纵然是瞬间 提交于 2019-11-26 18:28:19
问题 I'm trying to do some kind of Macro "Overloading", so that MACRO(something), gets expanded differently than MACRO(something, else). Using a snippet I got from here (I'm not sure if it's 100% portable) and some functions from the Boost PP Library, I was able to make it work :D //THESE TWO COUNT THE NUMBER OF ARGUMENTS #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1) //THIS ONE RETURNS THE PARAMETER AT POSITION _i FROM A LIST OF