How do I expand a macro containing commas inside a BOOST_PP_IF

后端 未结 2 1461
感动是毒
感动是毒 2021-01-21 07:00

I asked the following question earlier, but the solution doesn\'t seem to work in this particular case.

How do I print out a comma multiple times using Boost Preprocesso

2条回答
  •  再見小時候
    2021-01-21 07:23

    It turns out that you can do this without __VA_ARGS__. In this simple example I used a comma which is in the template argument of function toString() Working demo:

    #include 
    #include 
    #include 
    #include 
    
    #define SEQUENCE (1)(2)(3)(4)(5)(6)(7)(8)(9)(10)
    
    #define IGNORE_ARG(arg)
    #define GET_NAME(data) BOOST_PP_SEQ_ELEM(0, data)
    #define GET_BELOW(data) BOOST_PP_SEQ_ELEM(1, data)
    
    #define PARSE_SEQUENCE(r, data, elem)                  \
        BOOST_PP_IF(                                       \
            BOOST_PP_GREATER_EQUAL(elem, GET_BELOW(data)), \
                GET_NAME(data), IGNORE_ARG)                \
        (elem)
    
    #define SKIP_NUMBERS_BELOW(name, below)                \
        BOOST_PP_SEQ_FOR_EACH(PARSE_SEQUENCE, (name)(below), SEQUENCE)
    
    #define TEST(name) SKIP_NUMBERS_BELOW(name, 4)
    
    #define MACRO_CONTAINING_COMMA(N) toString() <<
    
    template  // whatever, I just need a comma here.
    std::string toString() {
        return boost::lexical_cast(a) + ":" + boost::lexical_cast(b) + " ";
    }
    
    int main() {
        std::cout << TEST(MACRO_CONTAINING_COMMA) "\n";
    }
    

提交回复
热议问题