Use Boost Preprocessor to Parse sequence of elements

时光总嘲笑我的痴心妄想 提交于 2019-12-01 22:20:34

问题


I have a macro defined which is

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

How to expand this macro using Boost Preprocessor something like this?

int height = 10;
int width = 20;

at most i am able to get is height,int,10 and width,int,20 as string but can't parse individual element.


回答1:


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



来源:https://stackoverflow.com/questions/39766077/use-boost-preprocessor-to-parse-sequence-of-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!