0
votes

Is there a way to make the C++ preprocessor join arguments with a joiner token?

I've learned that 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 get foo_bar_baz.

I have two questions:

  1. Is there a way to do it for without the repeated explicit joiner characters ((_)) and for an argument list of variadic length?
  2. Is it necessary to pass the arguments like so:

    (arg1)(arg2)(arg3)
    

    Can I wrap it in another macro that'll allow me to pass argument normally, i.e.?:

    arg1, arg2, arg3
    
2
What about ## as glue without the parentheses and the joiner characters. - Tobias
I forgot to mention. I really need this to work with variadic arguments. (#define COMPOSE(...)) The BOOST_PP_SEQ_CAT macro works with arbitrarily long lists. I just don't know how to insert the joiner character into the concatenation. - PSkocik

2 Answers

1
votes
#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)
0
votes

What about:

#include <iostream>

#define COMPOSE(prefix,name) prefix##_##name


int main() {
    int COMPOSE(first,par);

    first_par = 1;

    return first_par;
}