C ++ preprocessor - combining arguments
Is there a way for the C ++ preprocessor to concatenate arguments with a joiner token?
I found out what I can do:
#include <boost/preprocessor/seq/cat.hpp>
#define arg1 foo
#define arg2 bar
#define arg3 baz
BOOST_PP_SEQ_CAT((arg1)(_)(arg2)(_)(arg3))
to receive foo_bar_baz
.
I have two questions:
- Is there a way to do this without iterating over the explicit character joiner (
(_)
) and for a variable length argument list? -
Do I need to pass arguments like this:
(arg1)(arg2)(arg3)
Is it possible to wrap it in another macro that will allow me to pass the argument normally, i.e.?:
arg1, arg2, arg3
0
source to share
2 answers
#define BOOST_PP_VARIADICS
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/seq/fold_left.hpp>
#include <boost/preprocessor/variadic/to_seq.hpp>
#define OP(s, state, x) BOOST_PP_CAT(state, BOOST_PP_CAT(_, x))
#define COMPOSE(...) BOOST_PP_SEQ_FOLD_LEFT(OP, BOOST_PP_SEQ_HEAD(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)), BOOST_PP_SEQ_TAIL(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)))
#define arg1 foo
#define arg2 bar
#define arg3 baz
COMPOSE(arg1, arg2, arg3)
+1
source to share