Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distributing an argument in a variadic macro

I would like to construct a macro that takes a variable number of arguments and distributes the first argument to each of the subsequent in a format similar to the examples shown below:

Call:   MACRO(F,A)
Result: F:A

Call:   MACRO(F,A,B,C)
Result: F:A F:B F:C

I have seen https://github.com/swansontec/map-macro and the general concept of the recursion workaround through:

#define EVAL0(...) __VA_ARGS__
#define EVAL1(...) EVAL0 (EVAL0 (EVAL0 (__VA_ARGS__)))
#define EVAL2(...) EVAL1 (EVAL1 (EVAL1 (__VA_ARGS__)))
#define EVAL3(...) EVAL2 (EVAL2 (EVAL2 (__VA_ARGS__)))
#define EVAL4(...) EVAL3 (EVAL3 (EVAL3 (__VA_ARGS__)))
#define EVAL(...)  EVAL4 (EVAL4 (EVAL4 (__VA_ARGS__)))

But I cannot wrap my mind around how to apply this to my problem. Can anyone provide an example to achieve the results shown above? Thanks!

like image 230
pt3dNyc Avatar asked Nov 21 '25 07:11

pt3dNyc


1 Answers

With Boost.PP:

#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/variadic/to_seq.hpp>

#define TRANSFORM(r, data, elem) data:elem

#define MACRO(F, ...) \
    BOOST_PP_SEQ_FOR_EACH(TRANSFORM, F, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))

Demo. This will (AFAIR) work for up to 255 arguments.

like image 189
Columbo Avatar answered Nov 22 '25 21:11

Columbo