Search code examples
c++c-preprocessorvariadic-macrosboost-preprocessor

How can i generate variadic macro for concatenate string


I got stuck here...

#define CONCAT(a,b) BOOST_PP_STRINGIZE(BOOST_PP_CAT(a,b))
#define CONCAT1(a,b,c) CONCAT(CONCAT(a,b),c) and so on.

How i can to generate the CONCAT macro even if 20 arguments? May be i can to use BOOST_PP_SEQ_FOR_EACH but i don't understand how to do it?


Solution

  • It depends on you use-case.

    This

    #include <boost/preprocessor/cat.hpp>
    #include <boost/preprocessor/stringize.hpp>
    #include <boost/preprocessor/seq/for_each.hpp>
    
    #define SEQ (a)(b)(c)
    
    BOOST_PP_STRINGIZE(BOOST_PP_SEQ_CAT(SEQ)) // "abc"
    

    will concatenate the sequence and then stringize it. It is also possible to simply stringize each argument as "a" "b" "c" is equivalent to "abc".

    #define MY_STRINGIZE(r, data, elem) BOOST_PP_STRINGIZE(elem)
    BOOST_PP_SEQ_FOR_EACH(MY_STRINGIZE, _, SEQ)