Use Boost Preprocessor to Parse sequence of elements

后端 未结 1 617
青春惊慌失措
青春惊慌失措 2021-01-20 13:14

I have a macro defined which is

#define TYPES (height,int,10)(width,int,20)

How to expand this macro using Boost Preprocessor something l

相关标签:
1条回答
  • 2021-01-20 13:40

    Using BOOST_PP_VARIADIC_SEQ_TO_SEQ to turn TYPES into ((height,int,10))((width,int,20)) before processing, so that BOOST_PP_SEQ_FOR_EACH doesn't choke on it:

    #define MAKE_ONE_VARIABLE(r, data, elem) \
        BOOST_PP_TUPLE_ELEM(1, elem) BOOST_PP_TUPLE_ELEM(0, elem) = BOOST_PP_TUPLE_ELEM(2, elem);
    
    #define MAKE_VARIABLES(seq) \
        BOOST_PP_SEQ_FOR_EACH(MAKE_ONE_VARIABLE, ~, BOOST_PP_VARIADIC_SEQ_TO_SEQ(seq))
    

    Usage:

    #define TYPES (height,int,10)(width,int,20)
    
    int main() {
        MAKE_VARIABLES(TYPES)
    }
    

    Is preprocessed into:

    int main() {
        int height = 10; int width = 20;
    }
    

    See it live on Coliru

    0 讨论(0)
提交回复
热议问题