C ++ Macro Arithmetic

Possible duplicate:
C ++ var-arg macro, NOT template

I have to do with macros (these are macros that invoke macros, so templates are out of the question).

Here is what I want:

foo(3, a, b1, c1) --> foo1(a, b1, c1); foo(5, a, b1, c1, b2, c2) -> foo2(a, b1, c1, b2, c2); foo(7, a, b1, c1, b2, c2, b3, c3) -> foo3(a, b1, c1, b2, c2, b3, c3); 

So basically, I want to be able to execute the β€œfunction” n β†’ (n-1) / 2 with increasing macro times. Is it possible?

Thanks!

[PS if you do not like my questions; I support your right to downvote; so far my worst question is only -17, so maybe we can break this record; however, please let me know why my question is not technically valid.]

thanks

EDIT:

Foo accepts a variable of # arguments of the form:

 foo(N, a1, b1, a2, b2, ... a_N, b_N) -> foo##N(a1, b1, a2, b2, ... a_N, b_N); 

EDIT:

For all closers. This is a completely different issue. The first is "how to count the number of arguments in a macro." (to which a good response was received on the mailing list).

This question is a question: given that I have counted # arguments, how do I send it?

+4
source share
2 answers

I have not tested this, but should work:

 #define SUBSTFOO3( a, b1, c1 ) foo1(a, b1, c1) #define SUBSTFOO5( a, b1, c1, b2, c2 ) foo2(a, b1, c1, b2, c2) /* ad nauseam */ #define foo( N, ... ) SUBSTFOO ## N ( __VA_ARGS__ ) 

This may also work:

 #define SUBSTFOO3 foo1 /* no arguments needed */ #define SUBSTFOO5 foo2 /* "( __VA_ARGS__)" already the correct substitution */ #define foo( N, ... ) SUBSTFOO ## N ( __VA_ARGS__ ) 
+3
source

I'm not sure I understand your question, but it reminds me of this hype I saw in the GCC source. You may notice something applicable.

 #if GCC_VERSION >= 3000 || __STDC_VERSION__ >= 199901L /* Use preprocessor trickery to map "build" to "buildN" where N is the expected number of arguments. This is used for both efficiency (no varargs), and checking (verifying number of passed arguments). */ #define build(code, ...) \ _buildN1(build, _buildC1(__VA_ARGS__))(code, __VA_ARGS__) #define _buildN1(BASE, X) _buildN2(BASE, X) #define _buildN2(BASE, X) BASE##X #define _buildC1(...) _buildC2(__VA_ARGS__,9,8,7,6,5,4,3,2,1,0,0) #define _buildC2(x,a1,a2,a3,a4,a5,a6,a7,a8,a9,c,...) c #endif 
+1
source

Source: https://habr.com/ru/post/1304512/


All Articles